Documentation ¶
Overview ¶
Package state provides a caching layer atop the Ethereum state mtp.
Index ¶
- Variables
- func NewStateSync(root utils.Hash, database mtp.DatabaseReader) *mtp.Sync
- type Account
- type Code
- type Database
- type Dump
- type DumpAccount
- type ManagedState
- func (ms *ManagedState) GetNonce(addr utils.Address) uint64
- func (ms *ManagedState) HasAccount(addr utils.Address) bool
- func (ms *ManagedState) NewNonce(addr utils.Address) uint64
- func (ms *ManagedState) RemoveNonce(addr utils.Address, n uint64)
- func (ms *ManagedState) SetNonce(addr utils.Address, nonce uint64)
- func (ms *ManagedState) SetState(statedb *StateDB)
- type NodeIterator
- type StateDB
- func (s *StateDB) AddBalance(addr utils.Address, amount *big.Int)
- func (s *StateDB) AddLog(tlog *types.Log)
- func (s *StateDB) AddPreimage(hash utils.Hash, preimage []byte)
- func (s *StateDB) AddRefund(gas uint64)
- func (s *StateDB) Commit(deleteEmptyObjects bool) (root utils.Hash, err error)
- func (s *StateDB) Copy() *StateDB
- func (s *StateDB) CreateAccount(addr utils.Address)
- func (s *StateDB) Database() Database
- func (s *StateDB) Dump() []byte
- func (s *StateDB) Empty(addr utils.Address) bool
- func (s *StateDB) Error() error
- func (s *StateDB) Exist(addr utils.Address) bool
- func (s *StateDB) Finalise(deleteEmptyObjects bool)
- func (s *StateDB) ForEachStorage(addr utils.Address, cb func(key, value utils.Hash) bool)
- func (s *StateDB) GetBalance(addr utils.Address) *big.Int
- func (s *StateDB) GetCode(addr utils.Address) []byte
- func (s *StateDB) GetCodeHash(addr utils.Address) utils.Hash
- func (s *StateDB) GetCodeSize(addr utils.Address) int
- func (s *StateDB) GetDelegateTimestamp(addr utils.Address) *big.Int
- func (s *StateDB) GetLockedBalance(addr utils.Address) *big.Int
- func (s *StateDB) GetLogs(hash utils.Hash) []*types.Log
- func (s *StateDB) GetNonce(addr utils.Address) uint64
- func (s *StateDB) GetOrNewStateObject(addr utils.Address) *stateObject
- func (s *StateDB) GetRefund() uint64
- func (s *StateDB) GetState(addr utils.Address, bhash utils.Hash) utils.Hash
- func (s *StateDB) GetUnDelegateTimestamp(addr utils.Address) *big.Int
- func (s *StateDB) GetUnLockedBalance(addr utils.Address) *big.Int
- func (s *StateDB) HasSuicided(addr utils.Address) bool
- func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) utils.Hash
- func (s *StateDB) Logs() []*types.Log
- func (s *StateDB) Preimages() map[utils.Hash][]byte
- func (s *StateDB) Prepare(thash, bhash utils.Hash, ti int)
- func (s *StateDB) RawDump() Dump
- func (s *StateDB) Reset(root utils.Hash) error
- func (s *StateDB) RevertToSnapshot(revid int)
- func (s *StateDB) SetBalance(addr utils.Address, amount *big.Int)
- func (s *StateDB) SetCode(addr utils.Address, code []byte)
- func (s *StateDB) SetDelegateTimestamp(addr utils.Address, timestamp *big.Int)
- func (s *StateDB) SetLockedBalance(addr utils.Address, amount *big.Int)
- func (s *StateDB) SetNonce(addr utils.Address, nonce uint64)
- func (s *StateDB) SetState(addr utils.Address, key, value utils.Hash)
- func (s *StateDB) SetUnDelegateTimestamp(addr utils.Address, timestamp *big.Int)
- func (s *StateDB) SetUnLockedBalance(addr utils.Address, amount *big.Int)
- func (s *StateDB) Snapshot() int
- func (s *StateDB) StorageTrie(addr utils.Address) Trie
- func (s *StateDB) SubBalance(addr utils.Address, amount *big.Int)
- func (s *StateDB) Suicide(addr utils.Address) bool
- type Storage
- type Trie
Constants ¶
This section is empty.
Variables ¶
var MaxTrieCacheGen = uint16(120)
MaxTrieCacheGen Trie cache generation limit after which to evict trie nodes from memory.
Functions ¶
func NewStateSync ¶
NewStateSync create a new state trie download scheduler.
Types ¶
type Account ¶
type Account struct { // dpos UnLockedBalance *big.Int UnDelegateTimestamp *big.Int LockedBalance *big.Int DelegateTimestamp *big.Int Nonce uint64 Balance *big.Int Root utils.Hash // merkle root of the storage trie CodeHash []byte }
Account is the Ethereum consensus representation of accounts. These objects are stored in the main account trie.
type Database ¶
type Database interface { // OpenTrie opens the main account mtp. OpenTrie(root utils.Hash) (Trie, error) // OpenStorageTrie opens the storage trie of an account. OpenStorageTrie(addrHash, root utils.Hash) (Trie, error) // CopyTrie returns an independent copy of the given mtp. CopyTrie(Trie) Trie // ContractCode retrieves a particular contract's code. ContractCode(addrHash, codeHash utils.Hash) ([]byte, error) // ContractCodeSize retrieves a particular contracts code's size. ContractCodeSize(addrHash, codeHash utils.Hash) (int, error) // TrieDB retrieves the low level trie database used for data storage. TrieDB() *mtp.Database }
Database wraps access to tries and contract code.
func NewDatabase ¶
NewDatabase creates a backing store for state. The returned database is safe for concurrent use and retains cached trie nodes in memory. The pool is an optional intermediate trie-node memory pool between the low level storage layer and the high level trie abstraction.
type Dump ¶
type Dump struct { Root string `json:"root"` Accounts map[string]DumpAccount `json:"accounts"` }
type DumpAccount ¶
type DumpAccount struct { Balance string `json:"balance"` LockedBalance string `json:"lockedBalance"` DelegateTimestamp string `json:"delegateTimestamp"` Nonce uint64 `json:"nonce"` Root string `json:"root"` CodeHash string `json:"codeHash"` Code string `json:"code"` Storage map[string]string `json:"storage"` }
type ManagedState ¶
type ManagedState struct { *StateDB // contains filtered or unexported fields }
func ManageState ¶
func ManageState(statedb *StateDB) *ManagedState
ManagedState returns a new managed state with the statedb as it's backing layer
func (*ManagedState) GetNonce ¶
func (ms *ManagedState) GetNonce(addr utils.Address) uint64
GetNonce returns the canonical nonce for the managed or unmanaged account.
Because GetNonce mutates the DB, we must take a write lock.
func (*ManagedState) HasAccount ¶
func (ms *ManagedState) HasAccount(addr utils.Address) bool
HasAccount returns whether the given address is managed or not
func (*ManagedState) NewNonce ¶
func (ms *ManagedState) NewNonce(addr utils.Address) uint64
NewNonce returns the new canonical nonce for the managed account
func (*ManagedState) RemoveNonce ¶
func (ms *ManagedState) RemoveNonce(addr utils.Address, n uint64)
RemoveNonce removed the nonce from the managed state and all future pending nonces
func (*ManagedState) SetNonce ¶
func (ms *ManagedState) SetNonce(addr utils.Address, nonce uint64)
SetNonce sets the new canonical nonce for the managed state
func (*ManagedState) SetState ¶
func (ms *ManagedState) SetState(statedb *StateDB)
SetState sets the backing layer of the managed state
type NodeIterator ¶
type NodeIterator struct { Hash utils.Hash // Hash of the current entry being iterated (nil if not standalone) Parent utils.Hash // Hash of the first full ancestor node (nil if current is the root) Error error // Failure set in case of an internal error in the iterator // contains filtered or unexported fields }
NodeIterator is an iterator to traverse the entire state trie post-order, including all of the contract code and contract state tries.
func NewNodeIterator ¶
func NewNodeIterator(state *StateDB) *NodeIterator
NewNodeIterator creates an post-order state node iterator.
func (*NodeIterator) Next ¶
func (it *NodeIterator) Next() bool
Next moves the iterator to the next node, returning whether there are any further nodes. In case of an internal error this method returns false and sets the Error field to the encountered failure.
type StateDB ¶
type StateDB struct {
// contains filtered or unexported fields
}
StateDB within the ethereum protocol are used to store anything within the merkle mtp. StateDBs take care of caching and storing nested states. It's the general query interface to retrieve: * Contracts * Accounts
func (*StateDB) AddBalance ¶
AddBalance adds amount to the account associated with addr.
func (*StateDB) AddPreimage ¶
AddPreimage records a SHA3 preimage seen by the VM.
func (*StateDB) Copy ¶
Copy creates a deep, independent copy of the state. Snapshots of the copied state cannot be applied to the copy.
func (*StateDB) CreateAccount ¶
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:
- sends funds to sha(account ++ (nonce + 1))
- 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) Database ¶
Database retrieves the low level database supporting the lower level trie ops.
func (*StateDB) Empty ¶
Empty returns whether the state object is either non-existent or empty according to the EIP161 specification (balance = nonce = code = 0)
func (*StateDB) Exist ¶
Exist reports whether the given account address exists in the state. Notably this also returns true for suicided accounts.
func (*StateDB) Finalise ¶
Finalise finalises the state by removing the self destructed objects and clears the journal as well as the refunds.
func (*StateDB) ForEachStorage ¶
func (*StateDB) GetDelegateTimestamp ¶
func (*StateDB) GetOrNewStateObject ¶
GetOrNewStateObject Retrieve a state object or create a new state object if nil.
func (*StateDB) GetUnDelegateTimestamp ¶
func (*StateDB) GetUnLockedBalance ¶
func (*StateDB) IntermediateRoot ¶
IntermediateRoot computes the current root hash of the state mtp. It is called in between transactions to get the root hash that goes into transaction receipts.
func (*StateDB) Prepare ¶
Prepare sets the current transaction hash and index and block hash which is used when the EVM emits new state logs.
func (*StateDB) Reset ¶
Reset clears out all ephemeral state objects from the state db, but keeps the underlying state trie to avoid reloading data for the next operations.
func (*StateDB) RevertToSnapshot ¶
RevertToSnapshot reverts all state changes made since the given revision.
func (*StateDB) SetDelegateTimestamp ¶
func (*StateDB) SetLockedBalance ¶
func (*StateDB) SetUnDelegateTimestamp ¶
func (*StateDB) SetUnLockedBalance ¶
func (*StateDB) StorageTrie ¶
StorageTrie returns the storage trie of an account. The return value is a copy and is nil for non-existent accounts.
func (*StateDB) SubBalance ¶
SubBalance subtracts amount from the account associated with addr.
type Trie ¶
type Trie interface { TryGet(key []byte) ([]byte, error) TryUpdate(key, value []byte) error TryDelete(key []byte) error Commit(onleaf mtp.LeafCallback) (utils.Hash, error) Hash() utils.Hash NodeIterator(startKey []byte) mtp.NodeIterator GetKey([]byte) []byte // TODO(fjl): remove this when SecureTrie is removed Prove(key []byte, fromLevel uint, proofDb ldb.Writer) error }
Trie is a Ethereum Merkle mtp.