state

package
v0.0.0-...-e4e12f0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: LGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package state manages the main accounts trie. It follows the flow as bellow:

         o
         |
[ revertable state ]
         |
  [ stacked map ] -> [ journal ] -> [ playback(staging) ] -> [ updated trie ]
         |
   [ trie cache ]
         |
  [ read-only trie ]

It's much simpler than Ethereum's statedb. An important difference with statedb is the logic of account suicide. TODO: explain more

Index

Constants

View Source
const (
	// AccountTrieName is the name of account trie.
	AccountTrieName       = "a"
	StorageTrieNamePrefix = "s"
)

Variables

This section is empty.

Functions

func StorageTrieName

func StorageTrieName(sid []byte) string

StorageTrieName converts the storage id into the name of storage trie. It'll panic when empty sid passed.

Types

type Account

type Account struct {
	Balance     *big.Int
	Energy      *big.Int
	BlockTime   uint64
	Master      []byte // master address
	CodeHash    []byte // hash of code
	StorageRoot []byte // merkle root of the storage trie
}

Account is the Thor consensus representation of an account. RLP encoded objects are stored in main account trie.

func (*Account) CalcEnergy

func (a *Account) CalcEnergy(blockTime uint64) *big.Int

CalcEnergy calculates energy based on current block time.

func (*Account) IsEmpty

func (a *Account) IsEmpty() bool

IsEmpty returns if an account is empty. An empty account has zero balance and zero length code hash.

type AccountMetadata

type AccountMetadata struct {
	StorageID          []byte // the unique id of the storage trie.
	StorageCommitNum   uint32 // the commit number of the last storage update.
	StorageDistinctNum uint32 // the distinct number of the last storage update.
}

AccountMetadata is the account metadata.

type Error

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

Error is the error caused by state access failure.

func (*Error) Error

func (e *Error) Error() string

type Stage

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

Stage abstracts changes on the main accounts trie.

func (*Stage) Commit

func (s *Stage) Commit() (root workshare.Bytes32, err error)

Commit commits all changes into main accounts trie and storage tries.

func (*Stage) Hash

func (s *Stage) Hash() workshare.Bytes32

Hash computes hash of the main accounts trie.

type State

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

State manages the world state.

func New

func New(db *muxdb.MuxDB, root workshare.Bytes32, blockNum, blockConflicts, steadyBlockNum uint32) *State

New create state object.

func (*State) BuildStorageTrie

func (s *State) BuildStorageTrie(addr workshare.Address) (trie *muxdb.Trie, err error)

BuildStorageTrie build up storage trie for given address with cumulative changes.

func (*State) Checkout

func (s *State) Checkout(root workshare.Bytes32, blockNum, blockConflicts, steadyBlockNum uint32) *State

Checkout checkouts to another state.

func (*State) DecodeStorage

func (s *State) DecodeStorage(addr workshare.Address, key workshare.Bytes32, dec func([]byte) error) error

DecodeStorage get and decode storage value. Error returned by dec will be absorbed by State instance.

func (*State) Delete

func (s *State) Delete(addr workshare.Address)

Delete delete an account at the given address. That's set balance, energy and code to zero value.

func (*State) EncodeStorage

func (s *State) EncodeStorage(addr workshare.Address, key workshare.Bytes32, enc func() ([]byte, error)) error

EncodeStorage set storage value encoded by given enc method. Error returned by end will be absorbed by State instance.

func (*State) Exists

func (s *State) Exists(addr workshare.Address) (bool, error)

Exists returns whether an account exists at the given address. See Account.IsEmpty()

func (*State) GetBalance

func (s *State) GetBalance(addr workshare.Address) (*big.Int, error)

GetBalance returns balance for the given address.

func (*State) GetCode

func (s *State) GetCode(addr workshare.Address) ([]byte, error)

GetCode returns code for the given address.

func (*State) GetCodeHash

func (s *State) GetCodeHash(addr workshare.Address) (workshare.Bytes32, error)

GetCodeHash returns code hash for the given address.

func (*State) GetEnergy

func (s *State) GetEnergy(addr workshare.Address, blockTime uint64) (*big.Int, error)

GetEnergy get energy for the given address at block number specified.

func (*State) GetMaster

func (s *State) GetMaster(addr workshare.Address) (workshare.Address, error)

GetMaster get master for the given address. Master can move energy, manage users...

func (*State) GetRawStorage

func (s *State) GetRawStorage(addr workshare.Address, key workshare.Bytes32) (rlp.RawValue, error)

GetRawStorage returns storage value in rlp raw for given address and key.

func (*State) GetStorage

func (s *State) GetStorage(addr workshare.Address, key workshare.Bytes32) (workshare.Bytes32, error)

GetStorage returns storage value for the given address and key.

func (*State) NewCheckpoint

func (s *State) NewCheckpoint() int

NewCheckpoint makes a checkpoint of current state. It returns revision of the checkpoint.

func (*State) RevertTo

func (s *State) RevertTo(revision int)

RevertTo revert to checkpoint specified by revision.

func (*State) SetBalance

func (s *State) SetBalance(addr workshare.Address, balance *big.Int) error

SetBalance set balance for the given address.

func (*State) SetCode

func (s *State) SetCode(addr workshare.Address, code []byte) error

SetCode set code for the given address.

func (*State) SetEnergy

func (s *State) SetEnergy(addr workshare.Address, energy *big.Int, blockTime uint64) error

SetEnergy set energy at block number for the given address.

func (*State) SetMaster

func (s *State) SetMaster(addr workshare.Address, master workshare.Address) error

SetMaster set master for the given address.

func (*State) SetRawStorage

func (s *State) SetRawStorage(addr workshare.Address, key workshare.Bytes32, raw rlp.RawValue)

SetRawStorage set storage value in rlp raw.

func (*State) SetStorage

func (s *State) SetStorage(addr workshare.Address, key, value workshare.Bytes32)

SetStorage set storage value for the given address and key.

func (*State) Stage

func (s *State) Stage(newBlockNum, newBlockConflicts uint32) (*Stage, error)

Stage makes a stage object to compute hash of trie or commit all changes.

type Stater

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

Stater is the state creator.

func NewStater

func NewStater(db *muxdb.MuxDB) *Stater

NewStater create a new stater.

func (*Stater) NewState

func (s *Stater) NewState(root workshare.Bytes32, blockNum, blockConflicts, steadyBlockNum uint32) *State

NewState create a new state object.

Jump to

Keyboard shortcuts

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