statedb

package
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2025 License: LGPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Copyright 2022 Evmos Foundation This file is part of Evmos' Evmos packages.

The Evmos packages 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 Evmos packages 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 Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE

Copyright 2022 Evmos Foundation This file is part of the Evmos Network packages.

Evmos 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 Evmos packages are 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 Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE

Copyright 2022 Evmos Foundation This file is part of the Evmos Network packages.

Evmos 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 Evmos packages are 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 Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE

Copyright 2022 Evmos Foundation This file is part of the Evmos Network packages.

Evmos 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 Evmos packages are 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 Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEmptyAccount

func NewEmptyAccount() *types.StateAccount

NewEmptyAccount returns an empty account.

Types

type Account

type Account struct {
	Nonce    uint64
	Balance  *big.Int
	CodeHash []byte
}

Account is the Ethereum consensus representation of accounts. These objects are stored in the storage of auth module.

func (Account) IsContract

func (acct Account) IsContract() bool

IsContract returns if the account contains contract code.

type Code added in v1.0.8

type Code []byte

type Database added in v1.0.8

type Database interface {
	// OpenTrie opens the main account trie.
	OpenTrie(root common.Hash) (Trie, error)

	// OpenStorageTrie opens the storage trie of an account.
	OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash) (Trie, error)

	// CopyTrie returns an independent copy of the given trie.
	CopyTrie(Trie) Trie

	// ContractCode retrieves a particular contract's code.
	ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error)

	// ContractCodeSize retrieves a particular contracts code's size.
	ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error)

	// DiskDB returns the underlying key-value disk database.
	DiskDB() ethdb.KeyValueStore

	// TrieDB retrieves the low level trie database used for data storage.
	TrieDB() *trie.Database
}

Database wraps access to tries and contract code.

func NewDatabase added in v1.0.8

func NewDatabase(db ethdb.Database) Database

NewDatabase creates a backing store for state. The returned database is safe for concurrent use, but does not retain any recent trie nodes in memory. To keep some historical state in memory, use the NewDatabaseWithConfig constructor.

func NewDatabaseWithConfig added in v1.0.8

func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database

NewDatabaseWithConfig creates a backing store for state. The returned database is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a large memory cache.

func NewDatabaseWithNodeDB added in v1.0.8

func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database

NewDatabaseWithNodeDB creates a state database with an already initialized node database.

type EVMConfig

type EVMConfig struct {
	Params      types.Params
	ChainConfig *params.ChainConfig
	CoinBase    common.Address
	BaseFee     *big.Int
}

EVMConfig encapsulates common parameters needed to create an EVM to execute a message It's mainly to reduce the number of method parameters

type ExtStateDB

type ExtStateDB interface {
	vm.StateDB
	AppendJournalEntry(JournalEntry)
}

ExtStateDB defines an extension to the interface provided by the go-ethereum codebase to support additional state transition functionalities. In particular it supports appending a new entry to the state journal through AppendJournalEntry so that the state can be reverted after running stateful precompiled contracts.

type JournalEntry

type JournalEntry interface {
	// contains filtered or unexported methods
}

JournalEntry is a modification entry in the state change journal that can be Reverted on demand.

type Keeper

type Keeper interface {
	// Read methods
	GetAccount(ctx sdk.Context, addr common.Address) *types.StateAccount
	GetState(ctx sdk.Context, addr common.Address, key common.Hash) common.Hash
	GetCode(ctx sdk.Context, codeHash common.Hash) []byte
	// the callback returns false to break early
	ForEachStorage(ctx sdk.Context, addr common.Address, cb func(key, value common.Hash) bool)

	// Write methods, only called by `StateDB.Commit()`
	SetAccount(ctx sdk.Context, addr common.Address, account types.StateAccount) error
	SetState(ctx sdk.Context, addr common.Address, key common.Hash, value []byte)
	SetCode(ctx sdk.Context, codeHash []byte, code []byte)
	DeleteAccount(ctx sdk.Context, addr common.Address) error
}

Keeper provide underlying storage of StateDB

type StateDB

type StateDB struct {
	// contains filtered or unexported fields
}

StateDB structs within the ethereum protocol are used to store anything within the merkle trie. StateDBs take care of caching and storing nested states. It's the general query interface to retrieve: * Contracts * Accounts

func New

func New(ctx sdk.Context, keeper Keeper, txConfig TxConfig) *StateDB

New creates a new state from a given trie.

func (*StateDB) AddAddressToAccessList

func (s *StateDB) AddAddressToAccessList(addr common.Address)

AddAddressToAccessList adds the given address to the access list

func (*StateDB) AddBalance

func (s *StateDB) AddBalance(addr common.Address, amount *big.Int)

AddBalance adds amount to the account associated with addr.

func (*StateDB) AddLog

func (s *StateDB) AddLog(log *ethtypes.Log)

func (*StateDB) AddPreimage

func (s *StateDB) AddPreimage(_ common.Hash, _ []byte)

AddPreimage records a SHA3 preimage seen by the VM. AddPreimage performs a no-op since the EnablePreimageRecording flag is disabled on the vm.Config during state transitions. No store trie preimages are written to the database.

func (*StateDB) AddRefund

func (s *StateDB) AddRefund(gas uint64)

AddRefund adds gas to the refund counter

func (*StateDB) AddSlotToAccessList

func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash)

AddSlotToAccessList adds the given (address, slot)-tuple to the access list

func (*StateDB) AddressInAccessList

func (s *StateDB) AddressInAccessList(addr common.Address) bool

AddressInAccessList returns true if the given address is in the access list.

func (*StateDB) Commit

func (s *StateDB) Commit() error

Commit writes the dirty states to keeper the StateDB object should be discarded after committed.

func (*StateDB) CreateAccount

func (s *StateDB) CreateAccount(addr common.Address)

CreateAccount explicitly creates a state object. If a state object with the address already exists the balance is carried over to the new account.

CreateAccount is called during the EVM CREATE operation. The situation might arise that a contract does the following:

1. sends funds to sha(account ++ (nonce + 1)) 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)

Carrying over the balance ensures that Ether doesn't disappear.

func (*StateDB) Empty

func (s *StateDB) Empty(addr common.Address) bool

Empty returns whether the state object is either non-existent or empty according to the EIP161 specification (balance = nonce = code = 0)

func (*StateDB) Exist

func (s *StateDB) Exist(addr common.Address) bool

Exist reports whether the given account address exists in the state. Notably this also returns true for suicided accounts.

func (*StateDB) ForEachStorage

func (s *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error

ForEachStorage iterate the contract storage, the iteration order is not defined.

func (*StateDB) GetBalance

func (s *StateDB) GetBalance(addr common.Address) *big.Int

GetBalance retrieves the balance from the given address or 0 if object not found

func (*StateDB) GetCode

func (s *StateDB) GetCode(addr common.Address) []byte

GetCode returns the code of account, nil if not exists.

func (*StateDB) GetCodeHash

func (s *StateDB) GetCodeHash(addr common.Address) common.Hash

GetCodeHash returns the code hash of account.

func (*StateDB) GetCodeSize

func (s *StateDB) GetCodeSize(addr common.Address) int

GetCodeSize returns the code size of account.

func (*StateDB) GetCommittedState

func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash

GetCommittedState retrieves a value from the given account's committed storage trie.

func (*StateDB) GetContext

func (s *StateDB) GetContext() sdk.Context

GetContext returns the transaction Context.

func (*StateDB) GetLogs added in v1.0.8

func (s *StateDB) GetLogs(hash common.Hash, blockHash common.Hash) []*ethtypes.Log

func (*StateDB) GetNonce

func (s *StateDB) GetNonce(addr common.Address) uint64

GetNonce returns the nonce of account, 0 if not exists.

func (*StateDB) GetRefund

func (s *StateDB) GetRefund() uint64

GetRefund returns the current value of the refund counter.

func (*StateDB) GetState

func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash

GetState retrieves a value from the given account's storage trie.

func (*StateDB) GetTransientState added in v1.0.7

func (s *StateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash

GetTransientState gets transient storage for a given account.

func (*StateDB) HasSelfDestructed added in v1.0.8

func (s *StateDB) HasSelfDestructed(addr common.Address) bool

HasSelfDestructed implements vm.StateDB.

func (*StateDB) HasSuicided

func (s *StateDB) HasSuicided(addr common.Address) bool

HasSuicided returns if the contract is suicided in current transaction.

func (*StateDB) Keeper

func (s *StateDB) Keeper() Keeper

Keeper returns the underlying `Keeper`

func (*StateDB) Logs

func (s *StateDB) Logs() []*ethtypes.Log

func (*StateDB) Prepare added in v1.0.8

func (s *StateDB) Prepare(rules params.Rules, sender common.Address, coinbase common.Address, dest *common.Address, precompiles []common.Address, txAccesses ethtypes.AccessList)

Prepare implements vm.StateDB.

func (*StateDB) PrepareAccessList

func (s *StateDB) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list ethtypes.AccessList)

PrepareAccessList handles the preparatory steps for executing a state transition with regards to both EIP-2929 and EIP-2930:

- Add sender to access list (2929) - Add destination to access list (2929) - Add precompiles to access list (2929) - Add the contents of the optional tx access list (2930)

This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number.

func (*StateDB) RevertToSnapshot

func (s *StateDB) RevertToSnapshot(revid int)

RevertToSnapshot reverts all state changes made since the given revision.

func (*StateDB) SelfDestruct added in v1.0.8

func (s *StateDB) SelfDestruct(addr common.Address)

SelfDestruct implements vm.StateDB.

func (*StateDB) Selfdestruct6780 added in v1.0.8

func (s *StateDB) Selfdestruct6780(addr common.Address)

Selfdestruct6780 implements vm.StateDB.

func (*StateDB) SetCode

func (s *StateDB) SetCode(addr common.Address, code []byte)

SetCode sets the code of account.

func (*StateDB) SetNonce

func (s *StateDB) SetNonce(addr common.Address, nonce uint64)

SetNonce sets the nonce of account.

func (*StateDB) SetState

func (s *StateDB) SetState(addr common.Address, key, value common.Hash)

SetState sets the contract state.

func (*StateDB) SetTransientState added in v1.0.7

func (s *StateDB) SetTransientState(addr common.Address, key, value common.Hash)

SetTransientState sets transient storage for a given account. It adds the change to the journal so that it can be rolled back to its previous value if there is a revert.

func (*StateDB) SlotInAccessList

func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool)

SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.

func (*StateDB) Snapshot

func (s *StateDB) Snapshot() int

Snapshot returns an identifier for the current revision of the state.

func (*StateDB) SubBalance

func (s *StateDB) SubBalance(addr common.Address, amount *big.Int)

SubBalance subtracts amount from the account associated with addr.

func (*StateDB) SubRefund

func (s *StateDB) SubRefund(gas uint64)

SubRefund removes gas from the refund counter. This method will panic if the refund counter goes below zero

func (*StateDB) Suicide

func (s *StateDB) Suicide(addr common.Address) bool

Suicide marks the given account as suicided. This clears the account balance.

The account's state object is still available until the state is committed, getStateObject will return a non-nil account after Suicide.

type Storage

type Storage map[common.Hash]common.Hash

Storage represents in-memory cache/buffer of contract storage.

func (Storage) Copy added in v1.0.7

func (s Storage) Copy() Storage

func (Storage) SortedKeys

func (s Storage) SortedKeys() []common.Hash

SortedKeys sort the keys for deterministic iteration

func (Storage) String added in v1.0.7

func (s Storage) String() (str string)

type Trie added in v1.0.8

type Trie interface {
	// GetKey returns the sha3 preimage of a hashed key that was previously used
	// to store a value.
	//
	// TODO(fjl): remove this when StateTrie is removed
	GetKey([]byte) []byte

	// GetStorage returns the value for key stored in the trie. The value bytes
	// must not be modified by the caller. If a node was not found in the database,
	// a trie.MissingNodeError is returned.
	GetStorage(addr common.Address, key []byte) ([]byte, error)

	// GetAccount abstracts an account read from the trie. It retrieves the
	// account blob from the trie with provided account address and decodes it
	// with associated decoding algorithm. If the specified account is not in
	// the trie, nil will be returned. If the trie is corrupted(e.g. some nodes
	// are missing or the account blob is incorrect for decoding), an error will
	// be returned.
	GetAccount(address common.Address) (*types.StateAccount, error)

	// UpdateStorage associates key with value in the trie. If value has length zero,
	// any existing value is deleted from the trie. The value bytes must not be modified
	// by the caller while they are stored in the trie. If a node was not found in the
	// database, a trie.MissingNodeError is returned.
	UpdateStorage(addr common.Address, key, value []byte) error

	// UpdateAccount abstracts an account write to the trie. It encodes the
	// provided account object with associated algorithm and then updates it
	// in the trie with provided address.
	UpdateAccount(address common.Address, account *types.StateAccount) error

	// UpdateContractCode abstracts code write to the trie. It is expected
	// to be moved to the stateWriter interface when the latter is ready.
	UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error

	// DeleteStorage removes any existing value for key from the trie. If a node
	// was not found in the database, a trie.MissingNodeError is returned.
	DeleteStorage(addr common.Address, key []byte) error

	// DeleteAccount abstracts an account deletion from the trie.
	DeleteAccount(address common.Address) error

	// Hash returns the root hash of the trie. It does not write to the database and
	// can be used even if the trie doesn't have one.
	Hash() common.Hash

	// Commit collects all dirty nodes in the trie and replace them with the
	// corresponding node hash. All collected nodes(including dirty leaves if
	// collectLeaf is true) will be encapsulated into a nodeset for return.
	// The returned nodeset can be nil if the trie is clean(nothing to commit).
	// Once the trie is committed, it's not usable anymore. A new trie must
	// be created with new root and updated trie database for following usage
	Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)

	// NodeIterator returns an iterator that returns nodes of the trie. Iteration
	// starts at the key after the given start key. And error will be returned
	// if fails to create node iterator.
	NodeIterator(startKey []byte) (trie.NodeIterator, error)

	// Prove constructs a Merkle proof for key. The result contains all encoded nodes
	// on the path to the value at key. The value itself is also included in the last
	// node and can be retrieved by verifying the proof.
	//
	// If the trie does not contain a value for key, the returned proof contains all
	// nodes of the longest existing prefix of the key (at least the root), ending
	// with the node that proves the absence of the key.
	Prove(key []byte, proofDb ethdb.KeyValueWriter) error
}

Trie is a Ethereum Merkle Patricia trie.

type TxConfig

type TxConfig struct {
	BlockHash common.Hash // hash of current block
	TxHash    common.Hash // hash of current tx
	TxIndex   uint        // the index of current transaction
	LogIndex  uint        // the index of next log within current block
}

TxConfig encapulates the readonly information of current tx for `StateDB`.

func NewEmptyTxConfig

func NewEmptyTxConfig(bhash common.Hash) TxConfig

NewEmptyTxConfig construct an empty TxConfig, used in context where there's no transaction, e.g. `eth_call`/`eth_estimateGas`.

func NewTxConfig

func NewTxConfig(bhash, thash common.Hash, txIndex, logIndex uint) TxConfig

NewTxConfig returns a TxConfig

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL