state

package
v0.2.14-beta.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxTrieCacheGen = uint16(120)

MaxTrieCacheGen is Trie cache generation limit after which to evict trie nodes from memory.

Functions

This section is empty.

Types

type AccountState

type AccountState interface {
	GetBalance() uint64
	GetNonce() uint64
	SetNonce(newNonce uint64)
	AddBalance(amount uint64)
	SubBalance(amount uint64)
	SetBalance(amount uint64)
	GetAddress() types.Address
}

AccountState is the interface defined to query a single account state.

type DB

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

DB is the struct that performs logging of all account states. It consists of a state trie that contains all account data in its leaves. It also stores a dirty object list to dump when state is committed into db.

func New

func New(root types.Hash32, db Database) (*DB, error)

New create a new state from a given trie.

func (*DB) AddBalance

func (state *DB) AddBalance(addr types.Address, amount uint64)

AddBalance adds amount to the account associated with addr.

func (*DB) Commit

func (state *DB) Commit() (root types.Hash32, err error)

Commit writes the state to the underlying in-memory trie database.

func (*DB) Copy

func (state *DB) Copy() *DB

Copy creates a deep, independent copy of the state. Snapshots of the copied state cannot be applied to the copy.

func (*DB) CreateAccount

func (state *DB) CreateAccount(addr types.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 (*DB) Dump

func (state *DB) Dump() []byte

Dump dumps the current state into json form, encoded into bytes.

func (*DB) Empty

func (state *DB) Empty(addr types.Address) bool

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

func (*DB) Error

func (state *DB) Error() error

Error returns db error if it occurred.

func (*DB) Exist

func (state *DB) Exist(addr types.Address) bool

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

func (*DB) GetAllAccounts

func (state *DB) GetAllAccounts() (*types.MultipleAccountsState, error)

GetAllAccounts returns a dump of all accounts in global state.

func (*DB) GetBalance

func (state *DB) GetBalance(addr types.Address) uint64

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

func (*DB) GetNonce

func (state *DB) GetNonce(addr types.Address) uint64

GetNonce gets the current nonce of the given addr, if the address is not found it returns 0.

func (*DB) GetOrNewStateObj

func (state *DB) GetOrNewStateObj(addr types.Address) *Object

GetOrNewStateObj retrieve a state object or create a new state object if nil.

func (*DB) IntermediateRoot

func (state *DB) IntermediateRoot(deleteEmptyObjects bool) types.Hash32

IntermediateRoot computes the current root hash of the state trie. It is called in between transactions to get the root hash that goes into transaction receipts.

func (*DB) RawDump

func (state *DB) RawDump() types.MultipleAccountsState

RawDump returns a Dump struct for the receivers state.

func (*DB) SetBalance

func (state *DB) SetBalance(addr types.Address, amount uint64)

SetBalance sets balance to the specific address, it does not return error if address was not found.

func (*DB) SetNonce

func (state *DB) SetNonce(addr types.Address, nonce uint64)

SetNonce sets nonce to the specific address, it does not return error if address was not found.

func (*DB) SubBalance

func (state *DB) SubBalance(addr types.Address, amount uint64)

SubBalance subtracts amount from the account associated with addr.

func (*DB) TrieDB

func (state *DB) TrieDB() *trie.Database

TrieDB retrieves the low level trie database used for data storage.

type Database

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

	// OpenStorageTrie opens the storage trie of an account.
	OpenStorageTrie(addrHash, root types.Hash32) (Trie, error)

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

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

Database wraps access to tries and contract code.

func NewDatabase

func NewDatabase(db database.Database) Database

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 Object

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

Object is the struct in which account information is stored. It contains account info such as nonce and balance and also the accounts address, address hash and a reference to this structs containing database.

func (*Object) AddBalance

func (state *Object) AddBalance(amount uint64)

AddBalance removes amount from c's balance. It is used to add funds to the destination account of a transfer.

func (*Object) Address

func (state *Object) Address() types.Address

Address returns the address of the contract/account.

func (*Object) Balance

func (state *Object) Balance() uint64

Balance returns the account current balance.

func (*Object) EncodeRLP

func (state *Object) EncodeRLP(w io.Writer) error

EncodeRLP implements rlp.Encoder.

func (*Object) Nonce

func (state *Object) Nonce() uint64

Nonce returns the accounts current nonce.

func (*Object) ReturnGas

func (state *Object) ReturnGas(gas *big.Int)

ReturnGas Return the gas back to the origin. Used by the Virtual machine or Closures.

func (*Object) SetBalance

func (state *Object) SetBalance(amount uint64)

SetBalance sets the balance for current account.

func (*Object) SetNonce

func (state *Object) SetNonce(nonce uint64)

SetNonce sets the nonce to be nonce for this Object.

func (*Object) SubBalance

func (state *Object) SubBalance(amount uint64)

SubBalance removes amount from c's balance. It is used to remove funds from the origin account of a transfer.

func (*Object) Value

func (state *Object) Value() *big.Int

Value Never called, but must be present to allow Object to be used as a vm.Account interface that also satisfies the vm.ContractRef interface. Interfaces are awesome.

type TransactionProcessor

type TransactionProcessor struct {
	log.Log
	*DB
	// contains filtered or unexported fields
}

TransactionProcessor is the struct containing state db and is responsible for applying transactions into it.

func NewTransactionProcessor

func NewTransactionProcessor(allStates, processorDb database.Database, logger log.Log) *TransactionProcessor

NewTransactionProcessor returns a new state processor.

func (*TransactionProcessor) AddressExists

func (tp *TransactionProcessor) AddressExists(addr types.Address) bool

AddressExists checks if an account address exists in this node's global state.

func (*TransactionProcessor) ApplyRewards

func (tp *TransactionProcessor) ApplyRewards(layer types.LayerID, rewards map[types.Address]uint64)

ApplyRewards applies reward reward to miners vector for layer.

func (*TransactionProcessor) ApplyTransaction

func (tp *TransactionProcessor) ApplyTransaction(tx *types.Transaction, layerID types.LayerID) error

ApplyTransaction applies provided transaction to the current state, but does not commit it to persistent storage. It returns an error if the transaction is invalid, i.e., if there is not enough balance in the source account to perform the transaction and pay the fee or if the nonce is incorrect.

func (*TransactionProcessor) ApplyTransactions

func (tp *TransactionProcessor) ApplyTransactions(layer types.LayerID, txs []*types.Transaction) ([]*types.Transaction, error)

ApplyTransactions receives a batch of transactions to apply to state. Returns the number of transactions that we failed to apply.

func (*TransactionProcessor) GetLayerApplied

func (tp *TransactionProcessor) GetLayerApplied(txID types.TransactionID) *types.LayerID

GetLayerApplied gets the layer id at which this tx was applied.

func (*TransactionProcessor) GetLayerStateRoot

func (tp *TransactionProcessor) GetLayerStateRoot(layer types.LayerID) (types.Hash32, error)

GetLayerStateRoot returns the state root at a given layer.

func (*TransactionProcessor) GetStateRoot

func (tp *TransactionProcessor) GetStateRoot() types.Hash32

GetStateRoot gets the current state root hash.

func (*TransactionProcessor) LoadState

func (tp *TransactionProcessor) LoadState(layer types.LayerID) error

LoadState loads the last state from persistent storage.

func (*TransactionProcessor) Process

func (tp *TransactionProcessor) Process(txs []*types.Transaction, layerID types.LayerID) (remaining []*types.Transaction)

Process applies transaction vector to current state, it returns the remaining transactions that failed.

type Trie

type Trie interface {
	TryGet(key []byte) ([]byte, error)
	TryUpdate(key, value []byte) error
	TryDelete(key []byte) error
	Commit(onleaf trie.LeafCallback) (types.Hash32, error)
	Hash() types.Hash32
	NodeIterator(startKey []byte) trie.NodeIterator
	GetKey([]byte) []byte // TODO(fjl): remove this when SecureTrie is removed
	Prove(key []byte, fromLevel uint, proofDb database.Putter) error
}

Trie is a Ethereum Merkle Trie.

Jump to

Keyboard shortcuts

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