state

package
v0.6.0-alpha.8 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const BlockHashSize = 20
View Source
const L1CommitmentSize = trie.HashSizeBytes + BlockHashSize

Variables

View Source
var (
	ErrTrieRootNotFound      = errors.New("trie root not found")
	ErrUnknownLatestTrieRoot = errors.New("latest trie root is unknown")
)
View Source
var L1CommitmentNil = &L1Commitment{}

Functions

func BlockFromBytes added in v0.2.0

func BlockFromBytes(blockBytes []byte) (*block, error)

Types

type Block added in v0.1.0

type Block interface {
	Mutations() *buffered.Mutations
	MutationsReader() kv.KVStoreReader
	TrieRoot() trie.Hash
	PreviousL1Commitment() *L1Commitment
	StateIndex() uint32
	// L1Commitment contains the TrieRoot + block Hash
	L1Commitment() *L1Commitment
	// Hash is computed from Mutations + PreviousL1Commitment
	Hash() BlockHash
	Bytes() []byte
}

A Block contains the mutations between the previous and current states, and allows to calculate the L1 commitment. Blocks are immutable.

type BlockHash added in v0.3.0

type BlockHash [BlockHashSize]byte

func BlockHashFromData added in v0.3.0

func BlockHashFromData(data []byte) (ret BlockHash)

func BlockHashFromString added in v1.0.3

func BlockHashFromString(blockHashString string) (BlockHash, error)

func (BlockHash) Equals added in v1.0.3

func (bh BlockHash) Equals(other BlockHash) bool

func (BlockHash) String added in v0.3.0

func (bh BlockHash) String() string

type L1Commitment added in v0.3.0

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

L1Commitment represents the data stored as metadata in the anchor output

func L1CommitmentFromBytes added in v0.3.0

func L1CommitmentFromBytes(data []byte) (*L1Commitment, error)

func L1CommitmentFromMarshalUtil

func L1CommitmentFromMarshalUtil(mu *marshalutil.MarshalUtil) (*L1Commitment, error)

func PseudoRandL1Commitment added in v1.0.3

func PseudoRandL1Commitment() *L1Commitment

PseudoRandL1Commitment is for testing only

func (*L1Commitment) BlockHash added in v0.3.0

func (s *L1Commitment) BlockHash() BlockHash

func (*L1Commitment) Bytes added in v0.3.0

func (s *L1Commitment) Bytes() []byte

func (*L1Commitment) Equals added in v1.0.3

func (s *L1Commitment) Equals(other *L1Commitment) bool

func (*L1Commitment) Read added in v0.3.0

func (s *L1Commitment) Read(r io.Reader) error

func (*L1Commitment) String added in v0.3.0

func (s *L1Commitment) String() string

func (*L1Commitment) TrieRoot added in v1.0.3

func (s *L1Commitment) TrieRoot() trie.Hash

func (*L1Commitment) Write added in v0.3.0

func (s *L1Commitment) Write(w io.Writer) error

type State added in v1.0.3

type State interface {
	kv.KVStoreReader
	TrieRoot() trie.Hash
	GetMerkleProof(key []byte) *trie.MerkleProof
	StateCommonValues
}

State is an immutable view of a specific version of the chain state.

type StateCommonValues added in v1.0.3

type StateCommonValues interface {
	BlockIndex() uint32
	Timestamp() time.Time
	PreviousL1Commitment() *L1Commitment
}

type StateDraft added in v1.0.3

type StateDraft interface {
	kv.KVStore
	BaseL1Commitment() *L1Commitment
	Mutations() *buffered.Mutations
	StateCommonValues
}

StateDraft allows to mutate the chain state based on a specific trie root. All mutations are stored in-memory until committed.

type Store added in v1.0.3

type Store interface {
	// HasTrieRoot returns true if the given trie root exists in the store
	HasTrieRoot(trie.Hash) bool
	// BlockByTrieRoot fetches the Block that corresponds to the given trie root
	BlockByTrieRoot(trie.Hash) (Block, error)
	// StateByTrieRoot returns the chain state corresponding to the given trie root
	StateByTrieRoot(trie.Hash) (State, error)

	// SetLatest sets the given trie root to be considered the latest one in the chain.
	// This affects all `*ByIndex` and `Latest*` functions.
	SetLatest(trieRoot trie.Hash) error
	// BlockByIndex returns the block that corresponds to the given state index (see SetLatest).
	BlockByIndex(uint32) (Block, error)
	// StateByIndex returns the chain state corresponding to the given state index (see SetLatest).
	StateByIndex(uint32) (State, error)
	// LatestBlockIndex returns the index of the latest block, if set (see SetLatest)
	LatestBlockIndex() (uint32, error)
	// LatestBlock returns the latest block of the chain, if set (see SetLatest)
	LatestBlock() (Block, error)
	// LatestState returns the latest chain state, if set (see SetLatest)
	LatestState() (State, error)

	// NewOriginStateDraft starts a new StateDraft for the origin block
	NewOriginStateDraft() StateDraft

	// NewStateDraft starts a new StateDraft.
	// The newly created StateDraft will already contain a few mutations updating the common values:
	// - timestamp
	// - block index
	// - previous L1 commitment
	NewStateDraft(timestamp time.Time, prevL1Commitment *L1Commitment) (StateDraft, error)

	// NewEmptyStateDraft starts a new StateDraft without updating any the common values.
	// It may be used to replay a block given the mutations.
	// Note that calling any of the StateCommonValues methods may return invalid data before
	// applying the mutations.
	NewEmptyStateDraft(prevL1Commitment *L1Commitment) (StateDraft, error)

	// Commit commits the given state, creating a new block and trie root in the DB.
	// SetLatest must be called manually to consider the new state as the latest one.
	Commit(StateDraft) Block

	// ExtractBlock performs a dry-run of Commit, discarding all changes that would be
	// made to the DB.
	ExtractBlock(StateDraft) Block
}

Store manages the storage of a chain's state.

The chain state is a key-value store that is updated every time a batch of requests is executed by the VM.

The purpose of the Store is to store not only the latest version of the chain state, but also past versions (up to a limit).

Each version of the key-value pairs is stored in an immutable trie (provided by the trie.go package). Therefore each *state index* corresponds to a unique *trie root*.

For each trie root, the Store also stores a Block, which contains the mutations between the previous and current states, and allows to calculate the L1 commitment.

func NewStore added in v1.0.3

func NewStore(db kvstore.KVStore) Store

Jump to

Keyboard shortcuts

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