core, core/state, core/vm: review changes

This commit is contained in:
Jeffrey Wilcke 2017-02-20 21:32:43 +01:00
parent 5f4f75cc47
commit 1a02e71376
8 changed files with 33 additions and 54 deletions

View file

@ -214,8 +214,6 @@ func (self *BlockChain) loadLastState() error {
return err return err
} }
self.stateCache = statedb self.stateCache = statedb
// XXX Reviewers: what's this for? I need to know if I need to backport this somehow
//self.stateCache.GetAccount(common.Address{})
// Issue a status log for the user // Issue a status log for the user
headerTd := self.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64()) headerTd := self.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64())

View file

@ -381,19 +381,3 @@ func (self *stateObject) Nonce() uint64 {
func (self *stateObject) Value() *big.Int { func (self *stateObject) Value() *big.Int {
panic("Value on stateObject should never be called") panic("Value on stateObject should never be called")
} }
func (self *stateObject) ForEachStorage(cb func(key, value common.Hash) bool) {
// When iterating over the storage check the cache first
for h, value := range self.cachedStorage {
cb(h, value)
}
it := self.getTrie(self.db.db).Iterator()
for it.Next() {
// ignore cached values
key := common.BytesToHash(self.trie.GetKey(it.Key))
if _, ok := self.cachedStorage[key]; !ok {
cb(key, common.BytesToHash(it.Value))
}
}
}

View file

@ -332,11 +332,10 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr)) checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr))
// Check storage. // Check storage.
if obj := state.getStateObject(addr); obj != nil { if obj := state.getStateObject(addr); obj != nil {
obj.ForEachStorage(func(key, val common.Hash) bool { state.ForEachStorage(addr, func(key, val common.Hash) bool {
return checkeq("GetState("+key.Hex()+")", val, checkstate.GetState(addr, key)) return checkeq("GetState("+key.Hex()+")", val, checkstate.GetState(addr, key))
}) })
checkobj := checkstate.getStateObject(addr) checkstate.ForEachStorage(addr, func(key, checkval common.Hash) bool {
checkobj.ForEachStorage(func(key, checkval common.Hash) bool {
return checkeq("GetState("+key.Hex()+")", state.GetState(addr, key), checkval) return checkeq("GetState("+key.Hex()+")", state.GetState(addr, key), checkval)
}) })
} }

View file

@ -142,7 +142,7 @@ func (self *StateTransition) from() vm.AccountRef {
if !self.state.Exist(f) { if !self.state.Exist(f) {
self.state.CreateAccount(f) self.state.CreateAccount(f)
} }
return vm.Reference(f) return vm.AccountRef(f)
} }
func (self *StateTransition) to() vm.AccountRef { func (self *StateTransition) to() vm.AccountRef {
@ -154,7 +154,7 @@ func (self *StateTransition) to() vm.AccountRef {
return vm.AccountRef{} // contract creation return vm.AccountRef{} // contract creation
} }
reference := vm.Reference(*to) reference := vm.AccountRef(*to)
if !self.state.Exist(*to) { if !self.state.Exist(*to) {
self.state.CreateAccount(*to) self.state.CreateAccount(*to)
} }

View file

@ -39,9 +39,6 @@ type AccountRef common.Address
// Address casts AccountRef to a Address // Address casts AccountRef to a Address
func (ar AccountRef) Address() common.Address { return (common.Address)(ar) } func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
// Reference returns a casted Address to AccountRef
func Reference(addr common.Address) AccountRef { return (AccountRef)(addr) }
// Contract represents an ethereum contract in the state database. It contains // Contract represents an ethereum contract in the state database. It contains
// the the contract code, calling arguments. Contract implements ContractRef // the the contract code, calling arguments. Contract implements ContractRef
type Contract struct { type Contract struct {

View file

@ -116,7 +116,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
} }
var ( var (
to = Reference(addr) to = AccountRef(addr)
snapshot = evm.StateDB.Snapshot() snapshot = evm.StateDB.Snapshot()
) )
if !evm.StateDB.Exist(addr) { if !evm.StateDB.Exist(addr) {
@ -167,7 +167,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
var ( var (
snapshot = evm.StateDB.Snapshot() snapshot = evm.StateDB.Snapshot()
to = Reference(caller.Address()) to = AccountRef(caller.Address())
) )
// initialise a new contract and set the code that is to be used by the // initialise a new contract and set the code that is to be used by the
// E The contract is a scoped evmironment for this execution context // E The contract is a scoped evmironment for this execution context
@ -203,7 +203,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
var ( var (
snapshot = evm.StateDB.Snapshot() snapshot = evm.StateDB.Snapshot()
to = Reference(caller.Address()) to = AccountRef(caller.Address())
) )
// Iinitialise a new contract and make initialise the delegate values // Iinitialise a new contract and make initialise the delegate values
@ -250,7 +250,7 @@ func (evm *EVM) Create(caller ContractRef, code []byte, gas uint64, value *big.I
// initialise a new contract and set the code that is to be used by the // initialise a new contract and set the code that is to be used by the
// E The contract is a scoped evmironment for this execution context // E The contract is a scoped evmironment for this execution context
// only. // only.
contract := NewContract(caller, Reference(contractAddr), value, gas) contract := NewContract(caller, AccountRef(contractAddr), value, gas)
contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code) contract.SetCallCode(&contractAddr, crypto.Keccak256Hash(code), code)
ret, err = evm.interpreter.Run(contract, nil) ret, err = evm.interpreter.Run(contract, nil)

View file

@ -45,25 +45,26 @@ func (NoopEVMCallContext) DelegateCall(me ContractRef, addr common.Address, data
type NoopStateDB struct{} type NoopStateDB struct{}
func (NoopStateDB) CreateAccount(common.Address) {} func (NoopStateDB) CreateAccount(common.Address) {}
func (NoopStateDB) SubBalance(common.Address, *big.Int) {} func (NoopStateDB) SubBalance(common.Address, *big.Int) {}
func (NoopStateDB) AddBalance(common.Address, *big.Int) {} func (NoopStateDB) AddBalance(common.Address, *big.Int) {}
func (NoopStateDB) GetBalance(common.Address) *big.Int { return nil } func (NoopStateDB) GetBalance(common.Address) *big.Int { return nil }
func (NoopStateDB) GetNonce(common.Address) uint64 { return 0 } func (NoopStateDB) GetNonce(common.Address) uint64 { return 0 }
func (NoopStateDB) SetNonce(common.Address, uint64) {} func (NoopStateDB) SetNonce(common.Address, uint64) {}
func (NoopStateDB) GetCodeHash(common.Address) common.Hash { return common.Hash{} } func (NoopStateDB) GetCodeHash(common.Address) common.Hash { return common.Hash{} }
func (NoopStateDB) GetCode(common.Address) []byte { return nil } func (NoopStateDB) GetCode(common.Address) []byte { return nil }
func (NoopStateDB) SetCode(common.Address, []byte) {} func (NoopStateDB) SetCode(common.Address, []byte) {}
func (NoopStateDB) GetCodeSize(common.Address) int { return 0 } func (NoopStateDB) GetCodeSize(common.Address) int { return 0 }
func (NoopStateDB) AddRefund(*big.Int) {} func (NoopStateDB) AddRefund(*big.Int) {}
func (NoopStateDB) GetRefund() *big.Int { return nil } func (NoopStateDB) GetRefund() *big.Int { return nil }
func (NoopStateDB) GetState(common.Address, common.Hash) common.Hash { return common.Hash{} } func (NoopStateDB) GetState(common.Address, common.Hash) common.Hash { return common.Hash{} }
func (NoopStateDB) SetState(common.Address, common.Hash, common.Hash) {} func (NoopStateDB) SetState(common.Address, common.Hash, common.Hash) {}
func (NoopStateDB) Suicide(common.Address) bool { return false } func (NoopStateDB) Suicide(common.Address) bool { return false }
func (NoopStateDB) HasSuicided(common.Address) bool { return false } func (NoopStateDB) HasSuicided(common.Address) bool { return false }
func (NoopStateDB) Exist(common.Address) bool { return false } func (NoopStateDB) Exist(common.Address) bool { return false }
func (NoopStateDB) Empty(common.Address) bool { return false } func (NoopStateDB) Empty(common.Address) bool { return false }
func (NoopStateDB) RevertToSnapshot(int) {} func (NoopStateDB) RevertToSnapshot(int) {}
func (NoopStateDB) Snapshot() int { return 0 } func (NoopStateDB) Snapshot() int { return 0 }
func (NoopStateDB) AddLog(*types.Log) {} func (NoopStateDB) AddLog(*types.Log) {}
func (NoopStateDB) AddPreimage(common.Hash, []byte) {} func (NoopStateDB) AddPreimage(common.Hash, []byte) {}
func (NoopStateDB) ForEachStorage(common.Address, func(common.Hash, common.Hash) bool) {}

View file

@ -107,7 +107,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
var ( var (
address = common.StringToAddress("contract") address = common.StringToAddress("contract")
vmenv = NewEnv(cfg, cfg.State) vmenv = NewEnv(cfg, cfg.State)
sender = vm.Reference(cfg.Origin) sender = vm.AccountRef(cfg.Origin)
) )
cfg.State.CreateAccount(address) cfg.State.CreateAccount(address)
// set the receiver's (the executing contract) code for execution. // set the receiver's (the executing contract) code for execution.
@ -137,7 +137,7 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, error) {
} }
var ( var (
vmenv = NewEnv(cfg, cfg.State) vmenv = NewEnv(cfg, cfg.State)
sender = vm.Reference(cfg.Origin) sender = vm.AccountRef(cfg.Origin)
) )
// Call the code with the given configuration. // Call the code with the given configuration.