state

package
v0.1.14-profiler Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: MIT Imports: 15 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

func PublicKeyToAccountAddress

func PublicKeyToAccountAddress(pub ed25519.PublicKey) types.Address

PublicKeyToAccountAddress converts ed25519 public key to account address

Types

type Account

type Account struct {
	Nonce   uint64
	Balance *big.Int
}

Account struct represents basic account info: nonce and balance

type AccountState

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

AccountState is the interface defined to query a single account state

type DB added in v0.1.11

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 added in v0.1.11

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

AddBalance adds amount to the account associated with addr.

func (*DB) Commit added in v0.1.11

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

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

func (*DB) Copy added in v0.1.11

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 added in v0.1.11

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 added in v0.1.11

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

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

func (*DB) Empty added in v0.1.11

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 added in v0.1.11

func (state *DB) Error() error

Error returns db error if it occurred

func (*DB) Exist added in v0.1.11

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) GetBalance added in v0.1.11

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 added in v0.1.11

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 added in v0.1.11

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

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

func (*DB) IntermediateRoot added in v0.1.11

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 added in v0.1.11

func (state *DB) RawDump() Dump

RawDump returns a Dump struct for the receivers state

func (*DB) SetBalance added in v0.1.11

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

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

func (*DB) SetNonce added in v0.1.11

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 added in v0.1.11

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

SubBalance subtracts amount from the account associated with addr.

func (*DB) TrieDB added in v0.1.11

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 Dump

type Dump struct {
	Root     string                 `json:"root"`
	Accounts map[string]DumpAccount `json:"accounts"`
}

Dump is a struct used to dump an entire state root into json form

type DumpAccount

type DumpAccount struct {
	Balance string `json:"balance"`
	Nonce   uint64 `json:"nonce"`
}

DumpAccount is a helper struct that helps dumping account balance and nonce in json form

type Object added in v0.1.11

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 added in v0.1.11

func (state *Object) AddBalance(amount *big.Int)

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

func (*Object) Address added in v0.1.11

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

Address returns the address of the contract/account

func (*Object) Balance added in v0.1.11

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

Balance returns the account current balance

func (*Object) EncodeRLP added in v0.1.11

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

EncodeRLP implements rlp.Encoder.

func (*Object) Nonce added in v0.1.11

func (state *Object) Nonce() uint64

Nonce returns the accounts current nonce

func (*Object) ReturnGas added in v0.1.11

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 added in v0.1.11

func (state *Object) SetBalance(amount *big.Int)

SetBalance sets the balance for current account

func (*Object) SetNonce added in v0.1.11

func (state *Object) SetNonce(nonce uint64)

SetNonce sets the nonce to be nonce for this Object

func (*Object) SubBalance added in v0.1.11

func (state *Object) SubBalance(amount *big.Int)

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

func (*Object) Value added in v0.1.11

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 PreImages added in v0.1.11

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

PreImages is a struct that contains a root hash and the transactions that are in store of this root hash

type Projector

type Projector interface {
	GetProjection(addr types.Address, prevNonce, prevBalance uint64) (nonce, balance uint64, err error)
}

Projector interface defines the interface for a struct that can project the state of an account by applying txs from mem pool

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, projector Projector, 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, miners []types.Address, reward *big.Int)

ApplyRewards applies reward reward to miners vector miners in for layer

func (*TransactionProcessor) ApplyTransaction

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

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

func (*TransactionProcessor) ApplyTransactions

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

ApplyTransactions receives a batch of transaction to apply on state. Returns the number of transaction that 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) 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

func (*TransactionProcessor) ValidateNonceAndBalance

func (tp *TransactionProcessor) ValidateNonceAndBalance(tx *types.Transaction) error

ValidateNonceAndBalance validates that the tx origin account has enough balance to apply the tx, also, it checks that nonce in tx is correct, returns error otherwise

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