Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,7 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu
func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
b.mu.Lock()
defer b.mu.Unlock()

return b.pendingState.GetOrNewStateObject(account).Nonce(), nil
return b.pendingState.GetNonce(account), nil
}

// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
Expand Down Expand Up @@ -617,8 +616,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
call.Value = new(big.Int)
}
// Set infinite balance to the fake caller account.
from := stateDB.GetOrNewStateObject(call.From)
from.SetBalance(math.MaxBig256)
stateDB.SetBalance(call.From, math.MaxBig256)
// Execute the call.
msg := callMsg{call}

Expand Down
141 changes: 141 additions & 0 deletions core/state/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package state

import (
"encoding/json"
substate "github.com/Fantom-foundation/Substate"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types"
"math/big"
"time"
)

type StateDbInterface interface {
StartPrefetcher(namespace string)
StopPrefetcher()
Error() error
AddLog(log *types.Log)
GetLogs(hash common.Hash, blockHash common.Hash) []*types.Log
Logs() []*types.Log
AddPreimage(hash common.Hash, preimage []byte)
Preimages() map[common.Hash][]byte
AddRefund(gas uint64)
SubRefund(gas uint64)
Exist(addr common.Address) bool
Empty(addr common.Address) bool
GetBalance(addr common.Address) *big.Int
GetNonce(addr common.Address) uint64
TxIndex() int
GetCode(addr common.Address) []byte
GetCodeSize(addr common.Address) int
GetCodeHash(addr common.Address) common.Hash
GetState(addr common.Address, hash common.Hash) common.Hash
GetProof(addr common.Address) ([][]byte, error)
GetProofByHash(addrHash common.Hash) ([][]byte, error)
GetStorageProof(a common.Address, key common.Hash) ([][]byte, error)
GetCommittedState(addr common.Address, hash common.Hash) common.Hash
Database() Database
StorageTrie(addr common.Address) Trie
HasSuicided(addr common.Address) bool
AddBalance(addr common.Address, amount *big.Int)
SubBalance(addr common.Address, amount *big.Int)
SetBalance(addr common.Address, amount *big.Int)
SetNonce(addr common.Address, nonce uint64)
SetCode(addr common.Address, code []byte)
SetState(addr common.Address, key, value common.Hash)
SetStorage(addr common.Address, storage map[common.Hash]common.Hash)
Suicide(addr common.Address) bool
CreateAccount(addr common.Address)
ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error
Copy() StateDbInterface
Snapshot() int
RevertToSnapshot(revid int)
GetRefund() uint64
Finalise(deleteEmptyObjects bool)
IntermediateRoot(deleteEmptyObjects bool) common.Hash
Prepare(thash common.Hash, ti int)
Commit(deleteEmptyObjects bool) (common.Hash, error)
PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList)
AddAddressToAccessList(addr common.Address)
AddSlotToAccessList(addr common.Address, slot common.Hash)
AddressInAccessList(addr common.Address) bool
SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool)

RawDump(opts *DumpConfig) Dump
IteratorDump(opts *DumpConfig) IteratorDump
IterativeDump(opts *DumpConfig, output *json.Encoder)
Dump(opts *DumpConfig) []byte
DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte)

GetAccountReads() time.Duration
GetAccountHashes() time.Duration
GetAccountUpdates() time.Duration
GetAccountCommits() time.Duration
GetStorageReads() time.Duration
GetStorageHashes() time.Duration
GetStorageUpdates() time.Duration
GetStorageCommits() time.Duration
GetSnapshotAccountReads() time.Duration
GetSnapshotStorageReads() time.Duration
GetSnapshotCommits() time.Duration

SetPrehashedCode(addr common.Address, hash common.Hash, code []byte)
GetSubstatePostAlloc() substate.SubstateAlloc
BeginBlock(number uint64)
EndBlock(number uint64)
Release()
}

type StateDB struct {
StateDbInterface

// Measurements gathered during execution for debugging purposes
AccountReads time.Duration
AccountHashes time.Duration
AccountUpdates time.Duration
AccountCommits time.Duration
StorageReads time.Duration
StorageHashes time.Duration
StorageUpdates time.Duration
StorageCommits time.Duration
SnapshotAccountReads time.Duration
SnapshotStorageReads time.Duration
SnapshotCommits time.Duration
}

func (s *StateDB) Copy() *StateDB {
return &StateDB{s.StateDbInterface.Copy(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}

func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
hash, err := s.StateDbInterface.Commit(deleteEmptyObjects)
s.AccountReads = s.GetAccountReads()
s.AccountHashes = s.GetAccountHashes()
s.AccountUpdates = s.GetAccountUpdates()
s.AccountCommits = s.GetAccountCommits()
s.StorageReads = s.GetStorageReads()
s.StorageHashes = s.GetStorageHashes()
s.StorageUpdates = s.GetStorageUpdates()
s.StorageCommits = s.GetStorageCommits()
s.SnapshotAccountReads = s.GetSnapshotAccountReads()
s.SnapshotStorageReads = s.GetSnapshotStorageReads()
s.SnapshotCommits = s.GetSnapshotCommits()
return hash, err
}

// New creates a new state from a given trie.
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
return NewWithSnapLayers(root, db, snaps, 128)
}

func NewWithSnapLayers(root common.Hash, db Database, snaps *snapshot.Tree, layers int) (*StateDB, error) {
sdb, err := NewLegacyWithSnapLayers(root, db, snaps, layers)
if err != nil {
return nil, err
}
return NewWrapper(sdb), nil
}

func NewWrapper(inner StateDbInterface) *StateDB {
return &StateDB{inner, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}
10 changes: 5 additions & 5 deletions core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (d iterativeDump) OnRoot(root common.Hash) {

// DumpToCollector iterates the state according to the given options and inserts
// the items into a collector for aggregation or serialization.
func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
func (s *LegacyStateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
// Sanitize the input to allow nil configs
if conf == nil {
conf = new(DumpConfig)
Expand Down Expand Up @@ -201,7 +201,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
}

// RawDump returns the entire state an a single large object
func (s *StateDB) RawDump(opts *DumpConfig) Dump {
func (s *LegacyStateDB) RawDump(opts *DumpConfig) Dump {
dump := &Dump{
Accounts: make(map[common.Address]DumpAccount),
}
Expand All @@ -210,7 +210,7 @@ func (s *StateDB) RawDump(opts *DumpConfig) Dump {
}

// Dump returns a JSON string representing the entire state as a single json-object
func (s *StateDB) Dump(opts *DumpConfig) []byte {
func (s *LegacyStateDB) Dump(opts *DumpConfig) []byte {
dump := s.RawDump(opts)
json, err := json.MarshalIndent(dump, "", " ")
if err != nil {
Expand All @@ -220,12 +220,12 @@ func (s *StateDB) Dump(opts *DumpConfig) []byte {
}

// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
func (s *StateDB) IterativeDump(opts *DumpConfig, output *json.Encoder) {
func (s *LegacyStateDB) IterativeDump(opts *DumpConfig, output *json.Encoder) {
s.DumpToCollector(iterativeDump{output}, opts)
}

// IteratorDump dumps out a batch of accounts starts with the given start key
func (s *StateDB) IteratorDump(opts *DumpConfig) IteratorDump {
func (s *LegacyStateDB) IteratorDump(opts *DumpConfig) IteratorDump {
iterator := &IteratorDump{
Accounts: make(map[common.Address]DumpAccount),
}
Expand Down
4 changes: 2 additions & 2 deletions core/state/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// NodeIterator is an iterator to traverse the entire state trie post-order,
// including all of the contract code and contract state tries.
type NodeIterator struct {
state *StateDB // State being iterated
state *LegacyStateDB // State being iterated

stateIt trie.NodeIterator // Primary iterator for the global state trie
dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract
Expand All @@ -44,7 +44,7 @@ type NodeIterator struct {
}

// NewNodeIterator creates an post-order state node iterator.
func NewNodeIterator(state *StateDB) *NodeIterator {
func NewNodeIterator(state *LegacyStateDB) *NodeIterator {
return &NodeIterator{
state: state,
}
Expand Down
30 changes: 15 additions & 15 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// reverted on demand.
type journalEntry interface {
// revert undoes the changes introduced by this journal entry.
revert(*StateDB)
revert(*LegacyStateDB)

// dirtied returns the Ethereum address modified by this journal entry.
dirtied() *common.Address
Expand Down Expand Up @@ -57,7 +57,7 @@ func (j *journal) append(entry journalEntry) {

// revert undoes a batch of journalled modifications along with any reverted
// dirty handling too.
func (j *journal) revert(statedb *StateDB, snapshot int) {
func (j *journal) revert(statedb *LegacyStateDB, snapshot int) {
for i := len(j.entries) - 1; i >= snapshot; i-- {
// Undo the changes made by the operation
j.entries[i].revert(statedb)
Expand Down Expand Up @@ -140,7 +140,7 @@ type (
}
)

func (ch createObjectChange) revert(s *StateDB) {
func (ch createObjectChange) revert(s *LegacyStateDB) {
delete(s.stateObjects, *ch.account)
delete(s.stateObjectsDirty, *ch.account)
}
Expand All @@ -149,7 +149,7 @@ func (ch createObjectChange) dirtied() *common.Address {
return ch.account
}

func (ch resetObjectChange) revert(s *StateDB) {
func (ch resetObjectChange) revert(s *LegacyStateDB) {
s.setStateObject(ch.prev)
if !ch.prevdestruct && s.snap != nil {
delete(s.snapDestructs, ch.prev.addrHash)
Expand All @@ -162,7 +162,7 @@ func (ch resetObjectChange) dirtied() *common.Address {
return &ch.prev.address
}

func (ch suicideChange) revert(s *StateDB) {
func (ch suicideChange) revert(s *LegacyStateDB) {
obj := s.getStateObject(*ch.account)
if obj != nil {
obj.suicided = ch.prev
Expand All @@ -176,54 +176,54 @@ func (ch suicideChange) dirtied() *common.Address {

var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")

func (ch touchChange) revert(s *StateDB) {
func (ch touchChange) revert(s *LegacyStateDB) {
}

func (ch touchChange) dirtied() *common.Address {
return ch.account
}

func (ch balanceChange) revert(s *StateDB) {
func (ch balanceChange) revert(s *LegacyStateDB) {
s.getStateObject(*ch.account).setBalance(ch.prev)
}

func (ch balanceChange) dirtied() *common.Address {
return ch.account
}

func (ch nonceChange) revert(s *StateDB) {
func (ch nonceChange) revert(s *LegacyStateDB) {
s.getStateObject(*ch.account).setNonce(ch.prev)
}

func (ch nonceChange) dirtied() *common.Address {
return ch.account
}

func (ch codeChange) revert(s *StateDB) {
func (ch codeChange) revert(s *LegacyStateDB) {
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
}

func (ch codeChange) dirtied() *common.Address {
return ch.account
}

func (ch storageChange) revert(s *StateDB) {
func (ch storageChange) revert(s *LegacyStateDB) {
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
}

func (ch storageChange) dirtied() *common.Address {
return ch.account
}

func (ch refundChange) revert(s *StateDB) {
func (ch refundChange) revert(s *LegacyStateDB) {
s.refund = ch.prev
}

func (ch refundChange) dirtied() *common.Address {
return nil
}

func (ch addLogChange) revert(s *StateDB) {
func (ch addLogChange) revert(s *LegacyStateDB) {
logs := s.logs[ch.txhash]
if len(logs) == 1 {
delete(s.logs, ch.txhash)
Expand All @@ -237,15 +237,15 @@ func (ch addLogChange) dirtied() *common.Address {
return nil
}

func (ch addPreimageChange) revert(s *StateDB) {
func (ch addPreimageChange) revert(s *LegacyStateDB) {
delete(s.preimages, ch.hash)
}

func (ch addPreimageChange) dirtied() *common.Address {
return nil
}

func (ch accessListAddAccountChange) revert(s *StateDB) {
func (ch accessListAddAccountChange) revert(s *LegacyStateDB) {
/*
One important invariant here, is that whenever a (addr, slot) is added, if the
addr is not already present, the add causes two journal entries:
Expand All @@ -262,7 +262,7 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
return nil
}

func (ch accessListAddSlotChange) revert(s *StateDB) {
func (ch accessListAddSlotChange) revert(s *LegacyStateDB) {
s.accessList.DeleteSlot(*ch.address, *ch.slot)
}

Expand Down
6 changes: 3 additions & 3 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type stateObject struct {
address common.Address
addrHash common.Hash // hash of ethereum address of the account
data Account
db *StateDB
db *LegacyStateDB

// DB error.
// State objects are used by the consensus core and VM which are
Expand Down Expand Up @@ -111,7 +111,7 @@ type Account struct {
}

// newObject creates a state object.
func newObject(db *StateDB, address common.Address, data Account) *stateObject {
func newObject(db *LegacyStateDB, address common.Address, data Account) *stateObject {
if data.Balance == nil {
data.Balance = new(big.Int)
}
Expand Down Expand Up @@ -466,7 +466,7 @@ func (s *stateObject) setBalance(amount *big.Int) {
s.data.Balance = amount
}

func (s *stateObject) deepCopy(db *StateDB) *stateObject {
func (s *stateObject) deepCopy(db *LegacyStateDB) *stateObject {
stateObject := newObject(db, s.address, s.data)
if s.trie != nil {
stateObject.trie = db.db.CopyTrie(s.trie)
Expand Down
Loading