state

package
v0.2.0-rc6 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const IncomingTxProtocol = "TxGossip"

IncomingTxProtocol is the protocol identifier for tx received by gossip that is used by the p2p

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 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 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 uint64)

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) GetAllAccounts added in v0.1.16

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

GetAllAccounts returns a dump of all accounts in global state

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() types.MultipleAccountsState

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 uint64)

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 uint64)

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 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 uint64)

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() uint64

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 uint64)

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 uint64)

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, txPool *TxMempool, 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 for layer TODO: convert rewards to uint64 (see https://github.com/spacemeshos/go-spacemesh/issues/2069)

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) (int, 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 added in v0.1.15

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) HandleTxData added in v0.1.15

func (tp *TransactionProcessor) HandleTxData(data []byte) error

HandleTxData handles data received on TX gossip channel

func (*TransactionProcessor) HandleTxGossipData

func (tp *TransactionProcessor) HandleTxGossipData(ctx context.Context, data service.GossipMessage, syncer service.Fetcher)

HandleTxGossipData handles data sent from gossip

func (*TransactionProcessor) HandleTxSyncData

func (tp *TransactionProcessor) HandleTxSyncData(data []byte) error

HandleTxSyncData handles data received on TX sync

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) ValidateAndAddTxToPool added in v0.1.15

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

ValidateAndAddTxToPool validates the provided tx nonce and balance with projector and puts it in the transaction pool it returns an error if the provided tx is not valid

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.

type TxMempool added in v0.1.15

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

TxMempool is a struct that holds txs received via gossip network

func NewTxMemPool added in v0.1.15

func NewTxMemPool() *TxMempool

NewTxMemPool returns a new TxMempool struct

func (*TxMempool) Get added in v0.1.15

Get returns transaction by provided id, it returns an error if transaction is not found

func (*TxMempool) GetProjection added in v0.1.15

func (t *TxMempool) GetProjection(addr types.Address, prevNonce, prevBalance uint64) (nonce, balance uint64)

GetProjection returns the estimated nonce and balance for the provided address addr and previous nonce and balance projecting state is done by applying transactions from the pool

func (*TxMempool) GetTxIdsByAddress added in v0.1.15

func (t *TxMempool) GetTxIdsByAddress(addr types.Address) []types.TransactionID

GetTxIdsByAddress returns all transactions from/to a specific address

func (*TxMempool) GetTxsForBlock added in v0.1.15

func (t *TxMempool) GetTxsForBlock(numOfTxs int, getState func(addr types.Address) (nonce, balance uint64, err error)) ([]types.TransactionID, []*types.Transaction, error)

GetTxsForBlock gets a specific number of random txs for a block. This function also receives a state calculation function to allow returning only transactions that will probably be valid

func (*TxMempool) Invalidate added in v0.1.15

func (t *TxMempool) Invalidate(id types.TransactionID)

Invalidate removes transaction from pool

func (*TxMempool) Put added in v0.1.15

func (t *TxMempool) Put(id types.TransactionID, tx *types.Transaction)

Put inserts a transaction into the mem pool. It indexes it by source and dest addresses as well

Jump to

Keyboard shortcuts

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