diff options
Diffstat (limited to 'vm')
-rw-r--r-- | vm/address.go | 18 | ||||
-rw-r--r-- | vm/common.go | 7 | ||||
-rw-r--r-- | vm/vm_debug.go | 26 | ||||
-rw-r--r-- | vm/vm_test.go | 15 |
4 files changed, 39 insertions, 27 deletions
diff --git a/vm/address.go b/vm/address.go index 06bd35f6b..be8921a3b 100644 --- a/vm/address.go +++ b/vm/address.go @@ -12,7 +12,7 @@ type Address interface { } type PrecompiledAddress struct { - Gas *big.Int + Gas func(l int) *big.Int fn func(in []byte) []byte } @@ -21,9 +21,19 @@ func (self PrecompiledAddress) Call(in []byte) []byte { } var Precompiled = map[uint64]*PrecompiledAddress{ - 1: &PrecompiledAddress{big.NewInt(500), ecrecoverFunc}, - 2: &PrecompiledAddress{big.NewInt(100), sha256Func}, - 3: &PrecompiledAddress{big.NewInt(100), ripemd160Func}, + 1: &PrecompiledAddress{func(l int) *big.Int { + return GasEcrecover + }, ecrecoverFunc}, + 2: &PrecompiledAddress{func(l int) *big.Int { + n := big.NewInt(int64(l+31)/32 + 1) + n.Mul(n, GasSha256) + return n + }, sha256Func}, + 3: &PrecompiledAddress{func(l int) *big.Int { + n := big.NewInt(int64(l+31)/32 + 1) + n.Mul(n, GasRipemd) + return n + }, ripemd160Func}, } func sha256Func(in []byte) []byte { diff --git a/vm/common.go b/vm/common.go index 9514ff6d3..5fd512687 100644 --- a/vm/common.go +++ b/vm/common.go @@ -27,10 +27,17 @@ var ( GasBalance = big.NewInt(20) GasCreate = big.NewInt(100) GasCall = big.NewInt(20) + GasCreateByte = big.NewInt(5) + GasSha3Byte = big.NewInt(10) + GasSha256Byte = big.NewInt(50) + GasRipemdByte = big.NewInt(50) GasMemory = big.NewInt(1) GasData = big.NewInt(5) GasTx = big.NewInt(500) GasLog = big.NewInt(32) + GasSha256 = big.NewInt(50) + GasRipemd = big.NewInt(50) + GasEcrecover = big.NewInt(100) Pow256 = ethutil.BigPow(2, 256) diff --git a/vm/vm_debug.go b/vm/vm_debug.go index 0a541a769..c0a2d6d98 100644 --- a/vm/vm_debug.go +++ b/vm/vm_debug.go @@ -166,13 +166,7 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * case EXP: require(2) - exp := new(big.Int).Set(stack.data[stack.Len()-2]) - nbytes := 0 - for exp.Cmp(ethutil.Big0) > 0 { - nbytes += 1 - exp.Rsh(exp, 8) - } - gas.Set(big.NewInt(int64(nbytes + 1))) + gas.Set(big.NewInt(int64(len(stack.data[stack.Len()-2].Bytes()) + 1))) // Gas only case STOP: gas.Set(ethutil.Big0) @@ -260,9 +254,12 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * newMemSize.Mul(newMemSize, u256(32)) switch op { - // Additional gas usage on *CODPY case CALLDATACOPY, CODECOPY, EXTCODECOPY: addStepGasUsage(new(big.Int).Div(newMemSize, u256(32))) + case SHA3: + g := new(big.Int).Div(newMemSize, u256(32)) + g.Mul(g, GasSha3Byte) + addStepGasUsage(g) } if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 { @@ -744,12 +741,12 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * case LOG0, LOG1, LOG2, LOG3, LOG4: n := int(op - LOG0) topics := make([][]byte, n) - mSize, mStart := stack.Pop().Int64(), stack.Pop().Int64() - data := mem.Geti(mStart, mSize) + mSize, mStart := stack.Popn() for i := 0; i < n; i++ { topics[i] = ethutil.LeftPadBytes(stack.Pop().Bytes(), 32) } + data := mem.Geti(mStart.Int64(), mSize.Int64()) log := &Log{closure.Address(), topics, data} self.env.AddLog(log) @@ -839,8 +836,13 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price * self.Printf("CREATE err %v", err) } else { - ref.SetCode(ret) - msg.Output = ret + // gas < len(ret) * CreateDataGas == NO_CODE + dataGas := big.NewInt(int64(len(ret))) + dataGas.Mul(dataGas, GasCreateByte) + if closure.UseGas(dataGas) { + ref.SetCode(ret) + msg.Output = ret + } stack.Push(ethutil.BigD(addr)) } diff --git a/vm/vm_test.go b/vm/vm_test.go index 19aa171a6..84ebf378f 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -13,7 +13,6 @@ import ( "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/trie" checker "gopkg.in/check.v1" - // "github.com/obscuren/mutan" ) type VmSuite struct{} @@ -68,24 +67,18 @@ func setup(level logger.LogLevel, typ Type) (*Closure, VirtualMachine) { } func (s *VmSuite) TestDebugVm(c *checker.C) { - // if mutan.Version < "0.6" { - // t.Skip("skipping for mutan version", mutan.Version, " < 0.6") - // } closure, vm := setup(logger.DebugLevel, DebugVmTy) ret, _, e := closure.Call(vm, nil) c.Assert(e, checker.NotNil) - c.Skip("Depends on mutan") + c.Skip("Depends on mutan. Requires serpent implementation") c.Assert(ret, checker.DeepEquals, big9) } func (s *VmSuite) TestVm(c *checker.C) { - // if mutan.Version < "0.6" { - // t.Skip("skipping for mutan version", mutan.Version, " < 0.6") - // } closure, vm := setup(logger.DebugLevel, StandardVmTy) ret, _, e := closure.Call(vm, nil) c.Assert(e, checker.NotNil) - c.Skip("Depends on mutan") + c.Skip("Depends on mutan. Requires serpent implementation") c.Assert(ret, checker.DeepEquals, big9) } @@ -142,7 +135,7 @@ func (s *VmSuite) TestBuildInSha256(c *checker.C) { `, DebugVmTy) exp := crypto.Sha256(ethutil.LeftPadBytes([]byte{42}, 32)) - c.Skip("Depends on mutan") + c.Skip("Depends on mutan. Requires serpent implementation") c.Assert(ret, checker.DeepEquals, exp) } @@ -157,7 +150,7 @@ func (s *VmSuite) TestBuildInRipemd(c *checker.C) { `, DebugVmTy) exp := ethutil.RightPadBytes(crypto.Ripemd160(ethutil.LeftPadBytes([]byte{42}, 32)), 32) - c.Skip("Depends on mutan") + c.Skip("Depends on mutan. Requires serpent implementation") c.Assert(ret, checker.DeepEquals, exp) } |