Skip to content

Commit 184e9ae

Browse files
committed
core, tests: reduced state copy by N calls
Reduced the amount of state copied that are required by N calls by doing a balance check prior to any state modifications.
1 parent 846f34f commit 184e9ae

9 files changed

Lines changed: 39 additions & 18 deletions

File tree

‎cmd/evm/main.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ func (self *VMEnv) StructLogs() []vm.StructLog {
206206
func (self *VMEnv) AddLog(log *state.Log) {
207207
self.state.AddLog(log)
208208
}
209+
func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
210+
return from.Balance().Cmp(balance) >= 0
211+
}
209212
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
210213
return vm.Transfer(from, to, amount)
211214
}

‎core/execution.go‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/params"
2727
)
2828

29+
// Execution is the execution environment for the given call or create action.
2930
type Execution struct {
3031
env vm.Environment
3132
address *common.Address
@@ -35,19 +36,25 @@ type Execution struct {
3536
Gas, price, value *big.Int
3637
}
3738

39+
// NewExecution returns a new execution environment that handles all calling
40+
// and creation logic defined by the YP.
3841
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
3942
exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
4043
exe.evm = vm.NewVm(env)
4144
return exe
4245
}
4346

47+
// Call executes within the given context
4448
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
4549
// Retrieve the executing code
4650
code := self.env.State().GetCode(codeAddr)
4751

4852
return self.exec(&codeAddr, code, caller)
4953
}
5054

55+
// Create creates a new contract and runs the initialisation procedure of the
56+
// contract. This returns the returned code for the contract and is stored
57+
// elsewhere.
5158
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
5259
// Input must be nil for create
5360
code := self.input
@@ -63,16 +70,24 @@ func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, acco
6370
return
6471
}
6572

73+
// exec executes the given code and executes within the contextAddr context.
6674
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
6775
env := self.env
6876
evm := self.evm
77+
// Depth check execution. Fail if we're trying to execute above the
78+
// limit.
6979
if env.Depth() > int(params.CallCreateDepth.Int64()) {
7080
caller.ReturnGas(self.Gas, self.price)
7181

7282
return nil, vm.DepthError
7383
}
7484

75-
vsnapshot := env.State().Copy()
85+
if !env.CanTransfer(env.State().GetStateObject(caller.Address()), self.value) {
86+
caller.ReturnGas(self.Gas, self.price)
87+
88+
return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, env.State().GetBalance(caller.Address()))
89+
}
90+
7691
var createAccount bool
7792
if self.address == nil {
7893
// Generate a new address
@@ -95,15 +110,7 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.
95110
} else {
96111
to = env.State().GetOrNewStateObject(*self.address)
97112
}
98-
99-
err = env.Transfer(from, to, self.value)
100-
if err != nil {
101-
env.State().Set(vsnapshot)
102-
103-
caller.ReturnGas(self.Gas, self.price)
104-
105-
return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance())
106-
}
113+
vm.Transfer(from, to, self.value)
107114

108115
context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
109116
context.SetCallCode(contextAddr, code)

‎core/vm/environment.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Environment interface {
3636
Time() uint64
3737
Difficulty() *big.Int
3838
GasLimit() *big.Int
39+
CanTransfer(from Account, balance *big.Int) bool
3940
Transfer(from, to Account, amount *big.Int) error
4041
AddLog(*state.Log)
4142
AddStructLog(StructLog)

‎core/vm/instructions.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
1617
package vm
1718

1819
import (

‎core/vm/jit.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
1617
package vm
1718

1819
import (
@@ -48,7 +49,7 @@ func SetJITCacheSize(size int) {
4849
programs, _ = lru.New(size)
4950
}
5051

51-
// GetProgram returns the program by id or nil when non-existant
52+
// GetProgram returns the program by id or nil when non-existent
5253
func GetProgram(id common.Hash) *Program {
5354
if p, ok := programs.Get(id); ok {
5455
return p.(*Program)

‎core/vm/jit_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func (self *Env) AddLog(log *state.Log) {
105105
}
106106
func (self *Env) Depth() int { return self.depth }
107107
func (self *Env) SetDepth(i int) { self.depth = i }
108+
func (self *Env) CanTransfer(from Account, balance *big.Int) bool {
109+
return from.Balance().Cmp(balance) >= 0
110+
}
108111
func (self *Env) Transfer(from, to Account, amount *big.Int) error {
109112
return nil
110113
}

‎core/vm/settings.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
1617
package vm
1718

1819
var (

‎core/vm_env.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (self *VMEnv) GetHash(n uint64) common.Hash {
6969
func (self *VMEnv) AddLog(log *state.Log) {
7070
self.state.AddLog(log)
7171
}
72+
func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
73+
return from.Balance().Cmp(balance) >= 0
74+
}
75+
7276
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
7377
return vm.Transfer(from, to, amount)
7478
}

‎tests/util.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package tests
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"fmt"
2322
"math/big"
2423

@@ -192,18 +191,19 @@ func (self *Env) AddLog(log *state.Log) {
192191
}
193192
func (self *Env) Depth() int { return self.depth }
194193
func (self *Env) SetDepth(i int) { self.depth = i }
195-
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
194+
func (self *Env) CanTransfer(from vm.Account, balance *big.Int) bool {
196195
if self.skipTransfer {
197-
// ugly hack
198196
if self.initial {
199197
self.initial = false
200-
return nil
198+
return true
201199
}
200+
}
202201

203-
if from.Balance().Cmp(amount) < 0 {
204-
return errors.New("Insufficient balance in account")
205-
}
202+
return from.Balance().Cmp(balance) >= 0
203+
}
206204

205+
func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
206+
if self.skipTransfer {
207207
return nil
208208
}
209209
return vm.Transfer(from, to, amount)

0 commit comments

Comments
 (0)