core, internal: address comments

This commit is contained in:
rjl493456442 2019-08-08 19:42:38 +08:00
parent 130e561554
commit 505bcda3b6
3 changed files with 35 additions and 41 deletions

View file

@ -230,18 +230,13 @@ func (s *stateObject) SetState(db Database, key, value common.Hash) {
s.setState(key, value) 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 // After this function is called, all original state will be ignored and state
// lookup only happens in the fake state storage. // lookup only happens in the fake state storage.
// //
// Note this function should only be used for debugging purpose. // Note this function should only be used for debugging purpose.
func (s *stateObject) SetStatesForDebug(storage map[common.Hash]common.Hash) { func (s *stateObject) SetStorage(storage map[common.Hash]common.Hash) {
// Clean the fake storage if debugging is done.
if storage == nil {
s.fakeStorage = nil
return
}
// Allocate fake storage if it's nil. // Allocate fake storage if it's nil.
if s.fakeStorage == nil { if s.fakeStorage == nil {
s.fakeStorage = make(Storage) s.fakeStorage = make(Storage)

View file

@ -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) stateObject := self.GetOrNewStateObject(addr)
if stateObject != nil { if stateObject != nil {
stateObject.SetStatesForDebug(storage) stateObject.SetStorage(storage)
} }
} }

View file

@ -755,13 +755,13 @@ type CallArgs struct {
Data *hexutil.Bytes `json:"data"` 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. // a message call.
// Note, state and stateDiff can't be specified at the same time. If state is // 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 // 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 // if statDiff is set, all diff will be applied first and then execute the call
// message. // message.
type Account struct { type account struct {
Nonce *hexutil.Uint64 `json:"nonce"` Nonce *hexutil.Uint64 `json:"nonce"`
Code *hexutil.Bytes `json:"code"` Code *hexutil.Bytes `json:"code"`
Balance **hexutil.Big `json:"balance"` Balance **hexutil.Big `json:"balance"`
@ -769,7 +769,7 @@ type Account struct {
StateDiff *map[common.Hash]common.Hash `json:"stateDiff"` 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()) defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
state, header, err := b.StateAndHeaderByNumber(ctx, blockNr) state, header, err := b.StateAndHeaderByNumber(ctx, blockNr)
@ -788,8 +788,7 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb
addr = *args.From addr = *args.From
} }
// Override the fields of specified contracts before execution. // Override the fields of specified contracts before execution.
if accounts != nil { for addr, account := range overrides {
for addr, account := range accounts {
// Override account nonce. // Override account nonce.
if account.Nonce != nil { if account.Nonce != nil {
state.SetNonce(addr, uint64(*account.Nonce)) state.SetNonce(addr, uint64(*account.Nonce))
@ -803,12 +802,11 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb
state.SetBalance(addr, (*big.Int)(*account.Balance)) state.SetBalance(addr, (*big.Int)(*account.Balance))
} }
if account.State != nil && account.StateDiff != nil { if account.State != nil && account.StateDiff != nil {
return nil, 0, false, errors.New("can't use state and stateDiff at the same time") return nil, 0, false, fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
} }
// Replace entire state if caller requires. // Replace entire state if caller requires.
if account.State != nil { if account.State != nil {
state.SetStateForDebug(addr, *account.State) state.SetStorage(addr, *account.State)
defer state.SetStateForDebug(addr, nil) // Clean up the "fake" storage
} }
// Apply state diff into specified accounts. // Apply state diff into specified accounts.
if account.StateDiff != nil { if account.StateDiff != nil {
@ -817,7 +815,6 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb
} }
} }
} }
}
// Set default gas & gas price if none were set // Set default gas & gas price if none were set
gas := uint64(math.MaxUint64 / 2) gas := uint64(math.MaxUint64 / 2)
if args.Gas != nil { if args.Gas != nil {
@ -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 // Note, this function doesn't make and changes in the state/blockchain and is
// useful to execute and retrieve values. // 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) { 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 var accounts map[common.Address]account
if overrides != nil { if overrides != nil {
accounts = *overrides accounts = *overrides
} }