Skip to content

Commit 6cace73

Browse files
committed
core/vm/runtime: simplified runtime calling mechanism
Implemented `runtime.Call` which uses - unlike Execute - the given state for the execution and the address of the contract you wish to execute. Unlike `Execute`, `Call` requires a config.
1 parent ecc876c commit 6cace73

3 files changed

Lines changed: 117 additions & 18 deletions

File tree

‎core/vm/runtime/runtime.go‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Config struct {
4141
DisableJit bool // "disable" so it's enabled by default
4242
Debug bool
4343

44+
State *state.StateDB
4445
GetHashFn func(n uint64) common.Hash
4546
}
4647

@@ -94,12 +95,14 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
9495
vm.EnableJit = !cfg.DisableJit
9596
vm.Debug = cfg.Debug
9697

98+
if cfg.State == nil {
99+
db, _ := ethdb.NewMemDatabase()
100+
cfg.State, _ = state.New(common.Hash{}, db)
101+
}
97102
var (
98-
db, _ = ethdb.NewMemDatabase()
99-
statedb, _ = state.New(common.Hash{}, db)
100-
vmenv = NewEnv(cfg, statedb)
101-
sender = statedb.CreateAccount(cfg.Origin)
102-
receiver = statedb.CreateAccount(common.StringToAddress("contract"))
103+
vmenv = NewEnv(cfg, cfg.State)
104+
sender = cfg.State.CreateAccount(cfg.Origin)
105+
receiver = cfg.State.CreateAccount(common.StringToAddress("contract"))
103106
)
104107
// set the receiver's (the executing contract) code for execution.
105108
receiver.SetCode(code)
@@ -117,5 +120,43 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
117120
if cfg.Debug {
118121
vm.StdErrFormat(vmenv.StructLogs())
119122
}
120-
return ret, statedb, err
123+
return ret, cfg.State, err
124+
}
125+
126+
// Call executes the code given by the contract's address. It will return the
127+
// EVM's return value or an error if it failed.
128+
//
129+
// Call, unlike Execute, requires a config and also requires the State field to
130+
// be set.
131+
func Call(address common.Address, input []byte, cfg *Config) ([]byte, error) {
132+
setDefaults(cfg)
133+
134+
// defer the call to setting back the original values
135+
defer func(debug, forceJit, enableJit bool) {
136+
vm.Debug = debug
137+
vm.ForceJit = forceJit
138+
vm.EnableJit = enableJit
139+
}(vm.Debug, vm.ForceJit, vm.EnableJit)
140+
141+
vm.ForceJit = !cfg.DisableJit
142+
vm.EnableJit = !cfg.DisableJit
143+
vm.Debug = cfg.Debug
144+
145+
vmenv := NewEnv(cfg, cfg.State)
146+
147+
sender := cfg.State.GetOrNewStateObject(cfg.Origin)
148+
// Call the code with the given configuration.
149+
ret, err := vmenv.Call(
150+
sender,
151+
address,
152+
input,
153+
cfg.GasLimit,
154+
cfg.GasPrice,
155+
cfg.Value,
156+
)
157+
158+
if cfg.Debug {
159+
vm.StdErrFormat(vmenv.StructLogs())
160+
}
161+
return ret, err
121162
}

‎core/vm/runtime/runtime_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package runtime
1818

1919
import (
20+
"math/big"
2021
"strings"
2122
"testing"
2223

2324
"github.com/ethereum/go-ethereum/accounts/abi"
2425
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/state"
2527
"github.com/ethereum/go-ethereum/core/vm"
28+
"github.com/ethereum/go-ethereum/ethdb"
2629
)
2730

2831
func TestDefaults(t *testing.T) {
@@ -71,6 +74,49 @@ func TestEnvironment(t *testing.T) {
7174
}, nil, nil)
7275
}
7376

77+
func TestExecute(t *testing.T) {
78+
ret, _, err := Execute([]byte{
79+
byte(vm.PUSH1), 10,
80+
byte(vm.PUSH1), 0,
81+
byte(vm.MSTORE),
82+
byte(vm.PUSH1), 32,
83+
byte(vm.PUSH1), 0,
84+
byte(vm.RETURN),
85+
}, nil, nil)
86+
if err != nil {
87+
t.Fatal("didn't expect error", err)
88+
}
89+
90+
num := common.BytesToBig(ret)
91+
if num.Cmp(big.NewInt(10)) != 0 {
92+
t.Error("Expected 10, got", num)
93+
}
94+
}
95+
96+
func TestCall(t *testing.T) {
97+
db, _ := ethdb.NewMemDatabase()
98+
state, _ := state.New(common.Hash{}, db)
99+
address := common.HexToAddress("0x0a")
100+
state.SetCode(address, []byte{
101+
byte(vm.PUSH1), 10,
102+
byte(vm.PUSH1), 0,
103+
byte(vm.MSTORE),
104+
byte(vm.PUSH1), 32,
105+
byte(vm.PUSH1), 0,
106+
byte(vm.RETURN),
107+
})
108+
109+
ret, err := Call(address, nil, &Config{State: state})
110+
if err != nil {
111+
t.Fatal("didn't expect error", err)
112+
}
113+
114+
num := common.BytesToBig(ret)
115+
if num.Cmp(big.NewInt(10)) != 0 {
116+
t.Error("Expected 10, got", num)
117+
}
118+
}
119+
74120
func TestRestoreDefaults(t *testing.T) {
75121
Execute(nil, nil, &Config{Debug: true})
76122
if vm.ForceJit {

‎core/vm_env.go‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,42 @@ import (
2525
"github.com/ethereum/go-ethereum/core/vm"
2626
)
2727

28+
// GetHashFn returns a function for which the VM env can query block hashes thru
29+
// up to the limit defined by the Yellow Paper and uses the given block chain
30+
// to query for information.
31+
func GetHashFn(ref common.Hash, chain *BlockChain) func(n uint64) common.Hash {
32+
return func(n uint64) common.Hash {
33+
for block := chain.GetBlock(ref); block != nil; block = chain.GetBlock(block.ParentHash()) {
34+
if block.NumberU64() == n {
35+
return block.Hash()
36+
}
37+
}
38+
39+
return common.Hash{}
40+
}
41+
}
42+
2843
type VMEnv struct {
2944
state *state.StateDB
3045
header *types.Header
3146
msg Message
3247
depth int
3348
chain *BlockChain
3449
typ vm.Type
50+
51+
getHashFn func(uint64) common.Hash
3552
// structured logging
3653
logs []vm.StructLog
3754
}
3855

3956
func NewEnv(state *state.StateDB, chain *BlockChain, msg Message, header *types.Header) *VMEnv {
4057
return &VMEnv{
41-
chain: chain,
42-
state: state,
43-
header: header,
44-
msg: msg,
45-
typ: vm.StdVmTy,
58+
chain: chain,
59+
state: state,
60+
header: header,
61+
msg: msg,
62+
typ: vm.StdVmTy,
63+
getHashFn: GetHashFn(header.ParentHash, chain),
4664
}
4765
}
4866

@@ -59,13 +77,7 @@ func (self *VMEnv) SetDepth(i int) { self.depth = i }
5977
func (self *VMEnv) VmType() vm.Type { return self.typ }
6078
func (self *VMEnv) SetVmType(t vm.Type) { self.typ = t }
6179
func (self *VMEnv) GetHash(n uint64) common.Hash {
62-
for block := self.chain.GetBlock(self.header.ParentHash); block != nil; block = self.chain.GetBlock(block.ParentHash()) {
63-
if block.NumberU64() == n {
64-
return block.Hash()
65-
}
66-
}
67-
68-
return common.Hash{}
80+
return self.getHashFn(n)
6981
}
7082

7183
func (self *VMEnv) AddLog(log *vm.Log) {

0 commit comments

Comments
 (0)