state

package
v0.0.0-...-91a82d4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: LGPL-3.0 Imports: 15 Imported by: 0

README

state

use ethereum state model

Documentation

Overview

Package state provides a caching layer atop the Ethereum state mtp.

Index

Constants

This section is empty.

Variables

View Source
var MaxTrieCacheGen = uint16(120)

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

Functions

func NewStateSync

func NewStateSync(root utils.Hash, database mtp.DatabaseReader) *mtp.Sync

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 Code

type Code []byte

func (Code) String

func (s Code) String() string

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

func NewDatabase(db ldb.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"`
}

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 New

func New(root utils.Hash, db Database) (*StateDB, error)

New Create a new state from a given mtp.

func (*StateDB) AddBalance

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

AddBalance adds amount to the account associated with addr.

func (*StateDB) AddLog

func (s *StateDB) AddLog(tlog *types.Log)

func (*StateDB) AddPreimage

func (s *StateDB) AddPreimage(hash utils.Hash, preimage []byte)

AddPreimage records a SHA3 preimage seen by the VM.

func (*StateDB) AddRefund

func (s *StateDB) AddRefund(gas uint64)

func (*StateDB) Commit

func (s *StateDB) Commit(deleteEmptyObjects bool) (root utils.Hash, err error)

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

func (*StateDB) Copy

func (s *StateDB) Copy() *StateDB

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

func (*StateDB) CreateAccount

func (s *StateDB) CreateAccount(addr utils.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) Database

func (s *StateDB) Database() Database

Database retrieves the low level database supporting the lower level trie ops.

func (*StateDB) Dump

func (s *StateDB) Dump() []byte

func (*StateDB) Empty

func (s *StateDB) Empty(addr utils.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) Error

func (s *StateDB) Error() error

func (*StateDB) Exist

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

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

func (*StateDB) Finalise

func (s *StateDB) Finalise(deleteEmptyObjects bool)

Finalise finalises the state by removing the self destructed objects and clears the journal as well as the refunds.

func (*StateDB) ForEachStorage

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

func (*StateDB) GetBalance

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

func (*StateDB) GetCode

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

func (*StateDB) GetCodeHash

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

func (*StateDB) GetCodeSize

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

func (*StateDB) GetDelegateTimestamp

func (s *StateDB) GetDelegateTimestamp(addr utils.Address) *big.Int

func (*StateDB) GetLockedBalance

func (s *StateDB) GetLockedBalance(addr utils.Address) *big.Int

func (*StateDB) GetLogs

func (s *StateDB) GetLogs(hash utils.Hash) []*types.Log

func (*StateDB) GetNonce

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

func (*StateDB) GetOrNewStateObject

func (s *StateDB) GetOrNewStateObject(addr utils.Address) *stateObject

GetOrNewStateObject Retrieve a state object or create a new state object if nil.

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 utils.Address, bhash utils.Hash) utils.Hash

func (*StateDB) GetUnDelegateTimestamp

func (s *StateDB) GetUnDelegateTimestamp(addr utils.Address) *big.Int

func (*StateDB) GetUnLockedBalance

func (s *StateDB) GetUnLockedBalance(addr utils.Address) *big.Int

func (*StateDB) HasSuicided

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

func (*StateDB) IntermediateRoot

func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) utils.Hash

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

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

func (*StateDB) Preimages

func (s *StateDB) Preimages() map[utils.Hash][]byte

Preimages returns a list of SHA3 preimages that have been submitted.

func (*StateDB) Prepare

func (s *StateDB) Prepare(thash, bhash utils.Hash, ti int)

Prepare sets the current transaction hash and index and block hash which is used when the EVM emits new state logs.

func (*StateDB) RawDump

func (s *StateDB) RawDump() Dump

func (*StateDB) Reset

func (s *StateDB) Reset(root utils.Hash) error

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

func (s *StateDB) RevertToSnapshot(revid int)

RevertToSnapshot reverts all state changes made since the given revision.

func (*StateDB) SetBalance

func (s *StateDB) SetBalance(addr utils.Address, amount *big.Int)

func (*StateDB) SetCode

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

func (*StateDB) SetDelegateTimestamp

func (s *StateDB) SetDelegateTimestamp(addr utils.Address, timestamp *big.Int)

func (*StateDB) SetLockedBalance

func (s *StateDB) SetLockedBalance(addr utils.Address, amount *big.Int)

func (*StateDB) SetNonce

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

func (*StateDB) SetState

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

func (*StateDB) SetUnDelegateTimestamp

func (s *StateDB) SetUnDelegateTimestamp(addr utils.Address, timestamp *big.Int)

func (*StateDB) SetUnLockedBalance

func (s *StateDB) SetUnLockedBalance(addr utils.Address, amount *big.Int)

func (*StateDB) Snapshot

func (s *StateDB) Snapshot() int

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

func (*StateDB) StorageTrie

func (s *StateDB) StorageTrie(addr utils.Address) Trie

StorageTrie returns the storage trie of an account. The return value is a copy and is nil for non-existent accounts.

func (*StateDB) SubBalance

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

SubBalance subtracts amount from the account associated with addr.

func (*StateDB) Suicide

func (s *StateDB) Suicide(addr utils.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[utils.Hash]utils.Hash

func (Storage) Copy

func (s Storage) Copy() Storage

func (Storage) String

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

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.

Jump to

Keyboard shortcuts

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