From 505bcda3b6f2cb3ba2dba0e97c85e87da31175ec Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Thu, 8 Aug 2019 19:42:38 +0800 Subject: [PATCH] core, internal: address comments --- core/state/state_object.go | 9 ++---- core/state/statedb.go | 6 ++-- internal/ethapi/api.go | 61 ++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 41 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 4594f22e24..45ae95a2a9 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -230,18 +230,13 @@ func (s *stateObject) SetState(db Database, key, value common.Hash) { s.setState(key, value) } -// SetStatesForDebug replaces the entire state storage with the given one. +// SetStorage replaces the entire state storage with the given one. // // After this function is called, all original state will be ignored and state // lookup only happens in the fake state storage. // // Note this function should only be used for debugging purpose. -func (s *stateObject) SetStatesForDebug(storage map[common.Hash]common.Hash) { - // Clean the fake storage if debugging is done. - if storage == nil { - s.fakeStorage = nil - return - } +func (s *stateObject) SetStorage(storage map[common.Hash]common.Hash) { // Allocate fake storage if it's nil. if s.fakeStorage == nil { s.fakeStorage = make(Storage) diff --git a/core/state/statedb.go b/core/state/statedb.go index e2ff98bf10..b07f08fd21 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -386,10 +386,12 @@ func (self *StateDB) SetState(addr common.Address, key, value common.Hash) { } } -func (self *StateDB) SetStateForDebug(addr common.Address, storage map[common.Hash]common.Hash) { +// SetStorage replaces the entire storage for the specified account with given +// storage. This function should only be used for debugging. +func (self *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) { stateObject := self.GetOrNewStateObject(addr) if stateObject != nil { - stateObject.SetStatesForDebug(storage) + stateObject.SetStorage(storage) } } diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 40596b65be..aafdbcb2ea 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -755,13 +755,13 @@ type CallArgs struct { Data *hexutil.Bytes `json:"data"` } -// Account indicates the overriding fields of account during the execution of +// account indicates the overriding fields of account during the execution of // a message call. // Note, state and stateDiff can't be specified at the same time. If state is // set, message execution will only use the data in the given state. Otherwise // if statDiff is set, all diff will be applied first and then execute the call // message. -type Account struct { +type account struct { Nonce *hexutil.Uint64 `json:"nonce"` Code *hexutil.Bytes `json:"code"` Balance **hexutil.Big `json:"balance"` @@ -769,7 +769,7 @@ type Account struct { StateDiff *map[common.Hash]common.Hash `json:"stateDiff"` } -func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumber, accounts map[common.Address]Account, vmCfg vm.Config, timeout time.Duration, globalGasCap *big.Int) ([]byte, uint64, bool, error) { +func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumber, overrides map[common.Address]account, vmCfg vm.Config, timeout time.Duration, globalGasCap *big.Int) ([]byte, uint64, bool, error) { defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now()) state, header, err := b.StateAndHeaderByNumber(ctx, blockNr) @@ -788,33 +788,30 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb addr = *args.From } // Override the fields of specified contracts before execution. - if accounts != nil { - for addr, account := range accounts { - // Override account nonce. - if account.Nonce != nil { - state.SetNonce(addr, uint64(*account.Nonce)) - } - // Override account(contract) code. - if account.Code != nil { - state.SetCode(addr, *account.Code) - } - // Override account balance. - if account.Balance != nil { - state.SetBalance(addr, (*big.Int)(*account.Balance)) - } - if account.State != nil && account.StateDiff != nil { - return nil, 0, false, errors.New("can't use state and stateDiff at the same time") - } - // Replace entire state if caller requires. - if account.State != nil { - state.SetStateForDebug(addr, *account.State) - defer state.SetStateForDebug(addr, nil) // Clean up the "fake" storage - } - // Apply state diff into specified accounts. - if account.StateDiff != nil { - for key, value := range *account.StateDiff { - state.SetState(addr, key, value) - } + for addr, account := range overrides { + // Override account nonce. + if account.Nonce != nil { + state.SetNonce(addr, uint64(*account.Nonce)) + } + // Override account(contract) code. + if account.Code != nil { + state.SetCode(addr, *account.Code) + } + // Override account balance. + if account.Balance != nil { + state.SetBalance(addr, (*big.Int)(*account.Balance)) + } + if account.State != nil && account.StateDiff != nil { + return nil, 0, false, fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex()) + } + // Replace entire state if caller requires. + if account.State != nil { + state.SetStorage(addr, *account.State) + } + // Apply state diff into specified accounts. + if account.StateDiff != nil { + for key, value := range *account.StateDiff { + state.SetState(addr, key, value) } } } @@ -889,8 +886,8 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb // // Note, this function doesn't make and changes in the state/blockchain and is // useful to execute and retrieve values. -func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, overrides *map[common.Address]Account) (hexutil.Bytes, error) { - var accounts map[common.Address]Account +func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, overrides *map[common.Address]account) (hexutil.Bytes, error) { + var accounts map[common.Address]account if overrides != nil { accounts = *overrides }