mirror of https://github.com/ethereum/go-ethereum
parent
623b17ba20
commit
3232927325
@ -0,0 +1,273 @@ |
|||||||
|
// Copyright 2024 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package state |
||||||
|
|
||||||
|
import ( |
||||||
|
"errors" |
||||||
|
"maps" |
||||||
|
"slices" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil" |
||||||
|
"github.com/ethereum/go-ethereum/core/types" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/holiman/uint256" |
||||||
|
) |
||||||
|
|
||||||
|
//go:generate go run github.com/fjl/gencodec -type OverrideAccount -field-override overrideMarshaling -out state_override_json.go
|
||||||
|
|
||||||
|
// OverrideAccount specifies the fields of an account that should be overridden
|
||||||
|
// during the execution of a message call.
|
||||||
|
//
|
||||||
|
// Note: The 'state' and 'stateDiff' fields are mutually exclusive and cannot
|
||||||
|
// be specified simultaneously. If 'state' is set, the message execution will
|
||||||
|
// only use the data provided in the given state. Otherwise, if 'stateDiff'
|
||||||
|
// is set, all the differences will be applied first, and then the call message
|
||||||
|
// will be executed.
|
||||||
|
type OverrideAccount struct { |
||||||
|
Nonce *uint64 `json:"nonce"` |
||||||
|
Code *[]byte `json:"code"` |
||||||
|
Balance *uint256.Int `json:"balance"` |
||||||
|
State map[common.Hash]common.Hash `json:"state"` |
||||||
|
StateDiff map[common.Hash]common.Hash `json:"stateDiff"` |
||||||
|
} |
||||||
|
|
||||||
|
// nolint
|
||||||
|
type overrideMarshaling struct { |
||||||
|
Nonce *hexutil.Uint64 |
||||||
|
Code *hexutil.Bytes |
||||||
|
Balance *hexutil.U256 |
||||||
|
} |
||||||
|
|
||||||
|
// copy returns a deep-copied override object.
|
||||||
|
func (o OverrideAccount) copy() OverrideAccount { |
||||||
|
var obj OverrideAccount |
||||||
|
if o.Nonce != nil { |
||||||
|
nonce := *o.Nonce |
||||||
|
obj.Nonce = &nonce |
||||||
|
} |
||||||
|
if o.Code != nil { |
||||||
|
code := slices.Clone(*o.Code) |
||||||
|
obj.Code = &code |
||||||
|
} |
||||||
|
if o.Balance != nil { |
||||||
|
obj.Balance = new(uint256.Int).Set(o.Balance) |
||||||
|
} |
||||||
|
if o.State != nil { |
||||||
|
obj.State = maps.Clone(o.State) |
||||||
|
} |
||||||
|
if o.StateDiff != nil { |
||||||
|
obj.StateDiff = maps.Clone(o.StateDiff) |
||||||
|
} |
||||||
|
return obj |
||||||
|
} |
||||||
|
|
||||||
|
// overrideReader implements the Reader interface, serving as a wrapper around a
|
||||||
|
// provided state reader, but incorporating with overridden states.
|
||||||
|
type overrideReader struct { |
||||||
|
reader Reader |
||||||
|
overrides map[common.Address]OverrideAccount |
||||||
|
} |
||||||
|
|
||||||
|
// newOverrideReader creates a reader with customized state overrides.
|
||||||
|
func newOverrideReader(overrides map[common.Address]OverrideAccount, reader Reader) *overrideReader { |
||||||
|
return &overrideReader{ |
||||||
|
reader: reader, |
||||||
|
overrides: overrides, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Account implementing Reader interface, retrieving the account associated with
|
||||||
|
// a particular address.
|
||||||
|
//
|
||||||
|
// - Returns a nil account if it does not exist
|
||||||
|
// - Returns an error only if an unexpected issue occurs
|
||||||
|
// - The returned account is safe to modify after the call
|
||||||
|
func (r *overrideReader) Account(addr common.Address) (*types.StateAccount, error) { |
||||||
|
account, err := r.reader.Account(addr) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
// apply the overrides if it's specified
|
||||||
|
override, ok := r.overrides[addr] |
||||||
|
if ok { |
||||||
|
if account == nil { |
||||||
|
account = types.NewEmptyStateAccount() |
||||||
|
} |
||||||
|
if override.Nonce != nil { |
||||||
|
account.Nonce = *override.Nonce |
||||||
|
} |
||||||
|
if override.Balance != nil { |
||||||
|
account.Balance = new(uint256.Int).Set(override.Balance) |
||||||
|
} |
||||||
|
if override.Code != nil { |
||||||
|
account.CodeHash = crypto.Keccak256(*override.Code) |
||||||
|
} |
||||||
|
// TODO what about the storage root then, should we compute the
|
||||||
|
// storage root of overridden state here?
|
||||||
|
} |
||||||
|
return account, nil |
||||||
|
} |
||||||
|
|
||||||
|
// Storage implementing Reader interface, retrieving the storage slot associated
|
||||||
|
// with a particular account address and slot key.
|
||||||
|
//
|
||||||
|
// - Returns an empty slot if it does not exist
|
||||||
|
// - Returns an error only if an unexpected issue occurs
|
||||||
|
// - The returned storage slot is safe to modify after the call
|
||||||
|
func (r *overrideReader) Storage(addr common.Address, slot common.Hash) (common.Hash, error) { |
||||||
|
override, ok := r.overrides[addr] |
||||||
|
if ok { |
||||||
|
if override.State != nil { |
||||||
|
return override.State[slot], nil |
||||||
|
} |
||||||
|
if override.StateDiff != nil { |
||||||
|
if val, ok := override.StateDiff[slot]; ok { |
||||||
|
return val, nil |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return r.reader.Storage(addr, slot) |
||||||
|
} |
||||||
|
|
||||||
|
// Stats returns the statistics of the reader, specifically detailing the time
|
||||||
|
// spent on account reading and storage reading.
|
||||||
|
func (r *overrideReader) Stats() (time.Duration, time.Duration) { return 0, 0 } |
||||||
|
|
||||||
|
// Copy implementing Reader interface, returning a deep-copied state reader.
|
||||||
|
func (r *overrideReader) Copy() Reader { |
||||||
|
overrides := make(map[common.Address]OverrideAccount) |
||||||
|
for addr, override := range r.overrides { |
||||||
|
overrides[addr] = override.copy() |
||||||
|
} |
||||||
|
return &overrideReader{ |
||||||
|
overrides: overrides, |
||||||
|
reader: r.reader.Copy(), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// OverrideDatabase implements the Database interface, serving as a wrapper
|
||||||
|
// around a standard state database, but incorporating overridden states.
|
||||||
|
type OverrideDatabase struct { |
||||||
|
Database |
||||||
|
overrides map[common.Address]OverrideAccount |
||||||
|
} |
||||||
|
|
||||||
|
// NewOverrideDatabase creates a state database with provided state overrides.
|
||||||
|
func NewOverrideDatabase(db Database, overrides map[common.Address]OverrideAccount) *OverrideDatabase { |
||||||
|
// Allocate an empty override set just in case the provided one
|
||||||
|
// is nil. Don't panic for lazy users.
|
||||||
|
if overrides == nil { |
||||||
|
overrides = make(map[common.Address]OverrideAccount) |
||||||
|
} |
||||||
|
return &OverrideDatabase{ |
||||||
|
Database: db, |
||||||
|
overrides: overrides, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Reader returns a state reader associated with the specified state root.
|
||||||
|
func (db *OverrideDatabase) Reader(root common.Hash) (Reader, error) { |
||||||
|
reader, err := db.Database.Reader(root) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return newOverrideReader(db.overrides, reader), nil |
||||||
|
} |
||||||
|
|
||||||
|
// ContractCode retrieves a particular contract's code.
|
||||||
|
func (db *OverrideDatabase) ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error) { |
||||||
|
override, ok := db.overrides[addr] |
||||||
|
if ok && override.Code != nil { |
||||||
|
return *override.Code, nil |
||||||
|
} |
||||||
|
return db.Database.ContractCode(addr, codeHash) |
||||||
|
} |
||||||
|
|
||||||
|
// ContractCodeSize retrieves a particular contracts code's size.
|
||||||
|
func (db *OverrideDatabase) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) { |
||||||
|
override, ok := db.overrides[addr] |
||||||
|
if ok && override.Code != nil { |
||||||
|
return len(*override.Code), nil |
||||||
|
} |
||||||
|
return db.Database.ContractCodeSize(addr, codeHash) |
||||||
|
} |
||||||
|
|
||||||
|
// The stateWrapReader wraps a live state database as the state source.
|
||||||
|
type stateWrapReader struct { |
||||||
|
state *StateDB |
||||||
|
} |
||||||
|
|
||||||
|
// Account implementing Reader interface, retrieving the account associated with
|
||||||
|
// a particular address.
|
||||||
|
//
|
||||||
|
// - Returns a nil account if it does not exist
|
||||||
|
// - Returns an error only if an unexpected issue occurs
|
||||||
|
// - The returned account is safe to modify after the call
|
||||||
|
func (r *stateWrapReader) Account(addr common.Address) (*types.StateAccount, error) { |
||||||
|
obj := r.state.getStateObject(addr) |
||||||
|
if obj == nil { |
||||||
|
return nil, nil |
||||||
|
} |
||||||
|
return obj.data.Copy(), nil |
||||||
|
} |
||||||
|
|
||||||
|
// Storage implementing Reader interface, retrieving the storage slot associated
|
||||||
|
// with a particular account address and slot key.
|
||||||
|
//
|
||||||
|
// - Returns an empty slot if it does not exist
|
||||||
|
// - Returns an error only if an unexpected issue occurs
|
||||||
|
// - The returned storage slot is safe to modify after the call
|
||||||
|
func (r *stateWrapReader) Storage(addr common.Address, slot common.Hash) (common.Hash, error) { |
||||||
|
return r.state.GetState(addr, slot), nil |
||||||
|
} |
||||||
|
|
||||||
|
// Stats returns the statistics of the reader, specifically detailing the time
|
||||||
|
// spent on account reading and storage reading.
|
||||||
|
func (r *stateWrapReader) Stats() (time.Duration, time.Duration) { return 0, 0 } |
||||||
|
|
||||||
|
// Copy implementing Reader interface, returning a deep-copied state reader.
|
||||||
|
func (r *stateWrapReader) Copy() Reader { |
||||||
|
return &stateWrapReader{state: r.state.Copy()} |
||||||
|
} |
||||||
|
|
||||||
|
// stateWrap is an internal struct wrapping a live state instance as the state
|
||||||
|
// data source. This can be useful in scenarios where we need to create the
|
||||||
|
// snapshot of the current state and apply some additional overrides on top
|
||||||
|
// (e.g. state override in RPC call serving).
|
||||||
|
type stateWrap struct { |
||||||
|
Database |
||||||
|
state *StateDB |
||||||
|
} |
||||||
|
|
||||||
|
// Reader returns a state reader associated with the specified state root.
|
||||||
|
func (db *stateWrap) Reader(root common.Hash) (Reader, error) { |
||||||
|
if root != db.state.originalRoot { |
||||||
|
return nil, errors.New("state root is not matched") |
||||||
|
} |
||||||
|
return &stateWrapReader{state: db.state}, nil |
||||||
|
} |
||||||
|
|
||||||
|
// OverrideState applies the state overrides into the provided live state database.
|
||||||
|
func OverrideState(state *StateDB, overrides map[common.Address]OverrideAccount) (*StateDB, error) { |
||||||
|
db := NewOverrideDatabase(&stateWrap{ |
||||||
|
Database: state.db, |
||||||
|
state: state.Copy(), |
||||||
|
}, overrides) |
||||||
|
return New(state.originalRoot, db) |
||||||
|
} |
@ -0,0 +1,62 @@ |
|||||||
|
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||||
|
|
||||||
|
package state |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/common/hexutil" |
||||||
|
"github.com/holiman/uint256" |
||||||
|
) |
||||||
|
|
||||||
|
var _ = (*overrideMarshaling)(nil) |
||||||
|
|
||||||
|
// MarshalJSON marshals as JSON.
|
||||||
|
func (o OverrideAccount) MarshalJSON() ([]byte, error) { |
||||||
|
type OverrideAccount struct { |
||||||
|
Nonce *hexutil.Uint64 `json:"nonce"` |
||||||
|
Code *hexutil.Bytes `json:"code"` |
||||||
|
Balance *hexutil.U256 `json:"balance"` |
||||||
|
State map[common.Hash]common.Hash `json:"state"` |
||||||
|
StateDiff map[common.Hash]common.Hash `json:"stateDiff"` |
||||||
|
} |
||||||
|
var enc OverrideAccount |
||||||
|
enc.Nonce = (*hexutil.Uint64)(o.Nonce) |
||||||
|
enc.Code = (*hexutil.Bytes)(o.Code) |
||||||
|
enc.Balance = (*hexutil.U256)(o.Balance) |
||||||
|
enc.State = o.State |
||||||
|
enc.StateDiff = o.StateDiff |
||||||
|
return json.Marshal(&enc) |
||||||
|
} |
||||||
|
|
||||||
|
// UnmarshalJSON unmarshals from JSON.
|
||||||
|
func (o *OverrideAccount) UnmarshalJSON(input []byte) error { |
||||||
|
type OverrideAccount struct { |
||||||
|
Nonce *hexutil.Uint64 `json:"nonce"` |
||||||
|
Code *hexutil.Bytes `json:"code"` |
||||||
|
Balance *hexutil.U256 `json:"balance"` |
||||||
|
State map[common.Hash]common.Hash `json:"state"` |
||||||
|
StateDiff map[common.Hash]common.Hash `json:"stateDiff"` |
||||||
|
} |
||||||
|
var dec OverrideAccount |
||||||
|
if err := json.Unmarshal(input, &dec); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
if dec.Nonce != nil { |
||||||
|
o.Nonce = (*uint64)(dec.Nonce) |
||||||
|
} |
||||||
|
if dec.Code != nil { |
||||||
|
o.Code = (*[]byte)(dec.Code) |
||||||
|
} |
||||||
|
if dec.Balance != nil { |
||||||
|
o.Balance = (*uint256.Int)(dec.Balance) |
||||||
|
} |
||||||
|
if dec.State != nil { |
||||||
|
o.State = dec.State |
||||||
|
} |
||||||
|
if dec.StateDiff != nil { |
||||||
|
o.StateDiff = dec.StateDiff |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
@ -0,0 +1,287 @@ |
|||||||
|
// Copyright 2024 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package state |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/core/rawdb" |
||||||
|
"github.com/ethereum/go-ethereum/core/tracing" |
||||||
|
"github.com/ethereum/go-ethereum/core/types" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/holiman/uint256" |
||||||
|
) |
||||||
|
|
||||||
|
func initTestState() (common.Hash, *CachingDB) { |
||||||
|
db := NewDatabaseForTesting(rawdb.NewMemoryDatabase()) |
||||||
|
state, _ := New(types.EmptyRootHash, db) |
||||||
|
state.SetNonce(testAllocAddr, 1) |
||||||
|
state.SetBalance(testAllocAddr, uint256.NewInt(100), tracing.BalanceChangeUnspecified) |
||||||
|
state.SetCode(testAllocAddr, []byte{0x1}) |
||||||
|
state.SetState(testAllocAddr, testAllocSlotKey, common.Hash{0x1}) |
||||||
|
root, err := state.Commit(0, false) |
||||||
|
if err != nil { |
||||||
|
panic("failed to commit state") |
||||||
|
} |
||||||
|
return root, db |
||||||
|
} |
||||||
|
|
||||||
|
var ( |
||||||
|
testAllocAddr = common.HexToAddress("0xdeadbeef") |
||||||
|
testAllocAddr2 = common.HexToAddress("0xbabecafe") |
||||||
|
testAllocSlotKey = common.HexToHash("0xdeadbeef") |
||||||
|
testAllocSlotKey2 = common.HexToHash("0xbabecafe") |
||||||
|
) |
||||||
|
|
||||||
|
func overrideNonce(val uint64) *uint64 { |
||||||
|
return &val |
||||||
|
} |
||||||
|
|
||||||
|
func overrideCode(code []byte) *[]byte { |
||||||
|
return &code |
||||||
|
} |
||||||
|
|
||||||
|
func TestStateOverride(t *testing.T) { |
||||||
|
var cases = []struct { |
||||||
|
overrides map[common.Address]OverrideAccount |
||||||
|
expectAccounts map[common.Address]types.StateAccount |
||||||
|
expectStorages map[common.Address]map[common.Hash]common.Hash |
||||||
|
expectCodes map[common.Address][]byte |
||||||
|
}{ |
||||||
|
// override is nil, it should be allowed
|
||||||
|
{ |
||||||
|
nil, |
||||||
|
map[common.Address]types.StateAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: 1, |
||||||
|
Balance: uint256.NewInt(100), |
||||||
|
CodeHash: crypto.Keccak256([]byte{0x1}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]map[common.Hash]common.Hash{ |
||||||
|
testAllocAddr: {testAllocSlotKey: {0x1}}, |
||||||
|
}, |
||||||
|
map[common.Address][]byte{ |
||||||
|
testAllocAddr: {0x1}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
// empty override
|
||||||
|
{ |
||||||
|
map[common.Address]OverrideAccount{}, |
||||||
|
map[common.Address]types.StateAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: 1, |
||||||
|
Balance: uint256.NewInt(100), |
||||||
|
CodeHash: crypto.Keccak256([]byte{0x1}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]map[common.Hash]common.Hash{ |
||||||
|
testAllocAddr: {testAllocSlotKey: {0x1}}, |
||||||
|
}, |
||||||
|
map[common.Address][]byte{ |
||||||
|
testAllocAddr: {0x1}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
// empty override, access the non-existent states
|
||||||
|
{ |
||||||
|
map[common.Address]OverrideAccount{}, |
||||||
|
map[common.Address]types.StateAccount{ |
||||||
|
testAllocAddr2: { |
||||||
|
Nonce: 0, |
||||||
|
Balance: common.U2560, |
||||||
|
CodeHash: common.Hash{}.Bytes(), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]map[common.Hash]common.Hash{ |
||||||
|
testAllocAddr2: {testAllocSlotKey: {}}, |
||||||
|
}, |
||||||
|
map[common.Address][]byte{ |
||||||
|
testAllocAddr2: nil, |
||||||
|
}, |
||||||
|
}, |
||||||
|
// override account metadata
|
||||||
|
{ |
||||||
|
map[common.Address]OverrideAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: overrideNonce(50), |
||||||
|
Code: overrideCode([]byte{0x2}), |
||||||
|
Balance: uint256.NewInt(200), |
||||||
|
}, |
||||||
|
testAllocAddr2: { |
||||||
|
Nonce: overrideNonce(50), |
||||||
|
Code: overrideCode([]byte{0x2}), |
||||||
|
Balance: uint256.NewInt(200), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]types.StateAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: 50, |
||||||
|
Balance: uint256.NewInt(200), |
||||||
|
CodeHash: crypto.Keccak256([]byte{0x2}), |
||||||
|
}, |
||||||
|
testAllocAddr2: { |
||||||
|
Nonce: 50, |
||||||
|
Balance: uint256.NewInt(200), |
||||||
|
CodeHash: crypto.Keccak256([]byte{0x2}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]map[common.Hash]common.Hash{ |
||||||
|
testAllocAddr: {testAllocSlotKey: {0x1}}, |
||||||
|
}, |
||||||
|
map[common.Address][]byte{ |
||||||
|
testAllocAddr: {0x2}, |
||||||
|
testAllocAddr2: {0x2}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
// override storage by diff
|
||||||
|
{ |
||||||
|
map[common.Address]OverrideAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
StateDiff: map[common.Hash]common.Hash{ |
||||||
|
testAllocSlotKey: {0x2}, |
||||||
|
testAllocSlotKey2: {0x2}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]types.StateAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: 1, |
||||||
|
Balance: uint256.NewInt(100), |
||||||
|
CodeHash: crypto.Keccak256([]byte{0x1}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]map[common.Hash]common.Hash{ |
||||||
|
testAllocAddr: { |
||||||
|
testAllocSlotKey: {0x2}, |
||||||
|
testAllocSlotKey2: {0x2}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address][]byte{ |
||||||
|
testAllocAddr: {0x1}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
// override storage by replacing entire storage
|
||||||
|
{ |
||||||
|
map[common.Address]OverrideAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
State: map[common.Hash]common.Hash{ |
||||||
|
testAllocSlotKey2: {0x2}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]types.StateAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: 1, |
||||||
|
Balance: uint256.NewInt(100), |
||||||
|
CodeHash: crypto.Keccak256([]byte{0x1}), |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address]map[common.Hash]common.Hash{ |
||||||
|
testAllocAddr: { |
||||||
|
testAllocSlotKey: {}, |
||||||
|
testAllocSlotKey2: {0x2}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
map[common.Address][]byte{ |
||||||
|
testAllocAddr: {0x1}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
} |
||||||
|
for _, c := range cases { |
||||||
|
root, db := initTestState() |
||||||
|
stateDb, err := New(root, NewOverrideDatabase(db, c.overrides)) |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("Failed to initialize state, %v", err) |
||||||
|
} |
||||||
|
for addr, expect := range c.expectAccounts { |
||||||
|
if got := stateDb.GetBalance(addr); got.Cmp(expect.Balance) != 0 { |
||||||
|
t.Fatalf("Balance is not matched, got %v, want: %v", got, expect.Balance) |
||||||
|
} |
||||||
|
if got := stateDb.GetNonce(addr); got != expect.Nonce { |
||||||
|
t.Fatalf("Nonce is not matched, got %v, want: %v", got, expect.Nonce) |
||||||
|
} |
||||||
|
if got := stateDb.GetCodeHash(addr); !bytes.Equal(got.Bytes(), expect.CodeHash) { |
||||||
|
t.Fatalf("CodeHash is not matched, got %v, want: %v", got.Bytes(), expect.CodeHash) |
||||||
|
} |
||||||
|
} |
||||||
|
for addr, slots := range c.expectStorages { |
||||||
|
for key, val := range slots { |
||||||
|
got := stateDb.GetState(addr, key) |
||||||
|
if got != val { |
||||||
|
t.Fatalf("Storage slot is not matched, got %v, want: %v", got, val) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
for addr, code := range c.expectCodes { |
||||||
|
if got := stateDb.GetCode(addr); !bytes.Equal(got, code) { |
||||||
|
t.Fatalf("Code is not matched, got %v, want: %v", got, code) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestRecursiveOverride(t *testing.T) { |
||||||
|
root, db := initTestState() |
||||||
|
|
||||||
|
overrideA := map[common.Address]OverrideAccount{ |
||||||
|
testAllocAddr: {Nonce: overrideNonce(2)}, |
||||||
|
} |
||||||
|
overrideB := map[common.Address]OverrideAccount{ |
||||||
|
testAllocAddr: {Nonce: overrideNonce(3)}, |
||||||
|
} |
||||||
|
overrideC := map[common.Address]OverrideAccount{ |
||||||
|
testAllocAddr: { |
||||||
|
Nonce: overrideNonce(0), |
||||||
|
State: map[common.Hash]common.Hash{ |
||||||
|
testAllocSlotKey: {}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
} |
||||||
|
odbA := NewOverrideDatabase(db, overrideA) |
||||||
|
stateDb, err := New(root, odbA) |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("Failed to initialize state, %v", err) |
||||||
|
} |
||||||
|
nonce := stateDb.GetNonce(testAllocAddr) |
||||||
|
if nonce != 2 { |
||||||
|
t.Fatalf("Unexpected nonce, want: %d, got: %d", 2, nonce) |
||||||
|
} |
||||||
|
// recursively override
|
||||||
|
stateDb2, err := OverrideState(stateDb, overrideB) |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("Failed to initialize state, %v", err) |
||||||
|
} |
||||||
|
nonce = stateDb2.GetNonce(testAllocAddr) |
||||||
|
if nonce != 3 { |
||||||
|
t.Fatalf("Unexpected nonce, want: %d, got: %d", 3, nonce) |
||||||
|
} |
||||||
|
// recursively override again
|
||||||
|
stateDb3, err := OverrideState(stateDb2, overrideC) |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("Failed to initialize state, %v", err) |
||||||
|
} |
||||||
|
nonce = stateDb3.GetNonce(testAllocAddr) |
||||||
|
if nonce != 0 { |
||||||
|
t.Fatalf("Unexpected nonce, want: %d, got: %d", 0, nonce) |
||||||
|
} |
||||||
|
val := stateDb3.GetState(testAllocAddr, testAllocSlotKey) |
||||||
|
if val != (common.Hash{}) { |
||||||
|
t.Fatalf("Unexpected storage slot, want: %v, got: %v", common.Hash{}, val) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue