Skip to content

Commit 4dbdcae

Browse files
committed
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
1 parent b1c58b7 commit 4dbdcae

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

core/execution.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"math/big"
66

7-
"github.com/ethereum/go-ethereum/ethutil"
87
"github.com/ethereum/go-ethereum/state"
98
"github.com/ethereum/go-ethereum/vm"
109
)
@@ -36,6 +35,11 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
3635
env := self.vm.Env()
3736
chainlogger.Debugf("pre state %x\n", env.State().Root())
3837

38+
if self.vm.Env().Depth() == vm.MaxCallDepth {
39+
// Consume all gas (by not returning it) and return a depth error
40+
return nil, vm.DepthError{}
41+
}
42+
3943
from, to := env.State().GetStateObject(caller.Address()), env.State().GetOrNewStateObject(self.address)
4044
// Skipping transfer is used on testing for the initial call
4145
if !self.SkipTransfer {
@@ -50,24 +54,14 @@ func (self *Execution) exec(code, contextAddr []byte, caller vm.ClosureRef) (ret
5054

5155
snapshot := env.State().Copy()
5256
defer func() {
53-
if vm.IsDepthErr(err) || vm.IsOOGErr(err) {
57+
if /*vm.IsDepthErr(err) ||*/ vm.IsOOGErr(err) {
5458
env.State().Set(snapshot)
5559
}
5660
chainlogger.Debugf("post state %x\n", env.State().Root())
5761
}()
5862

5963
self.object = to
60-
// Pre-compiled contracts (address.go) 1, 2 & 3.
61-
naddr := ethutil.BigD(contextAddr).Uint64()
62-
if p := vm.Precompiled[naddr]; p != nil {
63-
if self.Gas.Cmp(p.Gas(len(self.input))) >= 0 {
64-
ret = p.Call(self.input)
65-
self.vm.Printf("NATIVE_FUNC(%x) => %x", naddr, ret)
66-
self.vm.Endl()
67-
}
68-
} else {
69-
ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
70-
}
64+
ret, err = self.vm.Run(to, caller, code, self.value, self.Gas, self.price, self.input)
7165

7266
return
7367
}

vm/address.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ type Address interface {
1111
Call(in []byte) []byte
1212
}
1313

14-
type PrecompiledAddress struct {
14+
type PrecompiledAccount struct {
1515
Gas func(l int) *big.Int
1616
fn func(in []byte) []byte
1717
}
1818

19-
func (self PrecompiledAddress) Call(in []byte) []byte {
19+
func (self PrecompiledAccount) Call(in []byte) []byte {
2020
return self.fn(in)
2121
}
2222

23-
var Precompiled = map[uint64]*PrecompiledAddress{
24-
1: &PrecompiledAddress{func(l int) *big.Int {
23+
var Precompiled = map[string]*PrecompiledAccount{
24+
string(ethutil.LeftPadBytes([]byte{1}, 20)): &PrecompiledAccount{func(l int) *big.Int {
2525
return GasEcrecover
2626
}, ecrecoverFunc},
27-
2: &PrecompiledAddress{func(l int) *big.Int {
27+
string(ethutil.LeftPadBytes([]byte{2}, 20)): &PrecompiledAccount{func(l int) *big.Int {
2828
n := big.NewInt(int64(l+31)/32 + 1)
2929
n.Mul(n, GasSha256)
3030
return n
3131
}, sha256Func},
32-
3: &PrecompiledAddress{func(l int) *big.Int {
32+
string(ethutil.LeftPadBytes([]byte{3}, 20)): &PrecompiledAccount{func(l int) *big.Int {
3333
n := big.NewInt(int64(l+31)/32 + 1)
3434
n.Mul(n, GasRipemd)
3535
return n

vm/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var (
4848
S256 = ethutil.S256
4949
)
5050

51-
const MaxCallDepth = 1025
51+
const MaxCallDepth = 1024
5252

5353
func calcMemSize(off, l *big.Int) *big.Int {
5454
if l.Cmp(ethutil.Big0) == 0 {

vm/vm_debug.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price *
4848
})
4949
closure := NewClosure(msg, caller, me, code, gas, price)
5050

51-
if self.env.Depth() == MaxCallDepth {
52-
//closure.UseGas(gas)
53-
return closure.Return(nil), DepthError{}
51+
if p := Precompiled[string(me.Address())]; p != nil {
52+
return self.RunPrecompiled(p, callData, closure)
5453
}
5554

5655
if self.Recoverable {
@@ -941,6 +940,25 @@ func (self *DebugVm) Run(me, caller ClosureRef, code []byte, value, gas, price *
941940
}
942941
}
943942

943+
func (self *DebugVm) RunPrecompiled(p *PrecompiledAccount, callData []byte, closure *Closure) (ret []byte, err error) {
944+
gas := p.Gas(len(callData))
945+
if closure.UseGas(gas) {
946+
ret = p.Call(callData)
947+
self.Printf("NATIVE_FUNC => %x", ret)
948+
self.Endl()
949+
950+
return closure.Return(ret), nil
951+
} else {
952+
self.Endl()
953+
954+
tmp := new(big.Int).Set(closure.Gas)
955+
956+
closure.UseGas(closure.Gas)
957+
958+
return closure.Return(nil), OOG(gas, tmp)
959+
}
960+
}
961+
944962
func (self *DebugVm) Printf(format string, v ...interface{}) VirtualMachine {
945963
if self.logTy == LogTyPretty {
946964
self.logStr += fmt.Sprintf(format, v...)

0 commit comments

Comments
 (0)