Moved pre-compiled, moved depth check

* Depth check has been moved to the execution
* Pre compiled execution has been moved to the VM
* PrecompiledAddress has been renamed to PrecompiledAccount
This commit is contained in:
obscuren 2014-12-17 23:58:52 +01:00
parent b1c58b76a9
commit 4dbdcaecb1
4 changed files with 35 additions and 23 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/vm" "github.com/ethereum/go-ethereum/vm"
) )
@ -36,6 +35,11 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
env := self.vm.Env() env := self.vm.Env()
chainlogger.Debugf("pre state %x\n", env.State().Root()) chainlogger.Debugf("pre state %x\n", env.State().Root())
if self.vm.Env().Depth() == vm.MaxCallDepth {
// Consume all gas (by not returning it) and return a depth error
return nil, vm.DepthError{}
}
from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address) from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address)
// Skipping transfer is used on testing for the initial call // Skipping transfer is used on testing for the initial call
if !self.SkipTransfer { if !self.SkipTransfer {
@ -50,24 +54,14 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
snapshot := env.State().Copy() snapshot := env.State().Copy()
defer func() { defer func() {
if vm.IsDepthErr(err) || vm.IsOOGErr(err) { if /*vm.IsDepthErr(err) ||*/ vm.IsOOGErr(err) {
env.State().Set(snapshot) env.State().Set(snapshot)
} }
chainlogger.Debugf("post state %x\n", env.State().Root()) chainlogger.Debugf("post state %x\n", env.State().Root())
}() }()
self.object = to self.object = to
// Pre-compiled contracts (address.go) 1, 2 & 3.
naddr := ethutil.BigD(contextAddr).Uint64()
if p := vm.Precompiled[naddr]; p != nil {
if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 {
ret = p.Call(self.input)
self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
self.vm.Endl()
}
} else {
ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input) ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
}
return return
} }

View File

@ -11,25 +11,25 @@ type Address interface {
Call(in []byte) []byte Call(in []byte) []byte
} }
type PrecompiledAddress struct { type PrecompiledAccount struct {
Gas func(l int) *big.Int Gas func(l int) *big.Int
fn func(in []byte) []byte fn func(in []byte) []byte
} }
func (self PrecompiledAddress) Call(in []byte) []byte { func (self PrecompiledAccount) Call(in []byte) []byte {
return self.fn(in) return self.fn(in)
} }
var Precompiled = map[uint64]*PrecompiledAddress{ var Precompiled = map[string]*PrecompiledAccount{
1: &PrecompiledAddress{func(l int) *big.Int { string(ethutil.LeftPadBytes([]byte{1}, 20)): &PrecompiledAccount{func(l int) *big.Int {
return GasEcrecover return GasEcrecover
}, ecrecoverFunc}, }, ecrecoverFunc},
2: &PrecompiledAddress{func(l int) *big.Int { string(ethutil.LeftPadBytes([]byte{2}, 20)): &PrecompiledAccount{func(l int) *big.Int {
n := big.NewInt(int64(l+31)/32 + 1) n := big.NewInt(int64(l+31)/32 + 1)
n.Mul(n, GasSha256) n.Mul(n, GasSha256)
return n return n
}, sha256Func}, }, sha256Func},
3: &PrecompiledAddress{func(l int) *big.Int { string(ethutil.LeftPadBytes([]byte{3}, 20)): &PrecompiledAccount{func(l int) *big.Int {
n := big.NewInt(int64(l+31)/32 + 1) n := big.NewInt(int64(l+31)/32 + 1)
n.Mul(n, GasRipemd) n.Mul(n, GasRipemd)
return n return n

View File

@ -48,7 +48,7 @@ var (
S256 = ethutil.S256 S256 = ethutil.S256
) )
const MaxCallDepth = 1025 const MaxCallDepth = 1024
func calcMemSize(off, l *big.Int) *big.Int { func calcMemSize(off, l *big.Int) *big.Int {
if l.Cmp(ethutil.Big0) == 0 { if l.Cmp(ethutil.Big0) == 0 {

View File

@ -48,9 +48,8 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price *
}) })
closure := NewClosure(msg, caller, me, code, gas, price) closure := NewClosure(msg, caller, me, code, gas, price)
if self.env.Depth() == MaxCallDepth { if p := Precompiled[string(me.Address())]; p != nil {
//closure.UseGas(gas) return self.RunPrecompiled(p, callData, closure)
return closure.Return(nil), DepthError{}
} }
if self.Recoverable { if self.Recoverable {
@ -941,6 +940,25 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price *
} }
} }
func (self *DebugVm) RunPrecompiled(p *PrecompiledAccount, callData []byte, closure *Closure) (ret []byte, err error) {
gas := p.Gas(len(callData))
if closure.UseGas(gas) {
ret = p.Call(callData)
self.Printf("NATIVE_FUNC => %x", ret)
self.Endl()
return closure.Return(ret), nil
} else {
self.Endl()
tmp := new(big.Int).Set(closure.Gas)
closure.UseGas(closure.Gas)
return closure.Return(nil), OOG(gas, tmp)
}
}
func (self *DebugVm) Printf(format string, v ...interface{}) VirtualMachine { func (self *DebugVm) Printf(format string, v ...interface{}) VirtualMachine {
if self.logTy == LogTyPretty { if self.logTy == LogTyPretty {
self.logStr += fmt.Sprintf(format, v...) self.logStr += fmt.Sprintf(format, v...)