state

package
v1.0.1-rc.3 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 22 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")
	ErrNoBlocksPruned        = errors.New("no blocks were pruned from the store yet")
)
View Source
var L1CommitmentNil = &L1Commitment{}

Functions

This section is empty.

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
	Read(io.Reader) error
	Write(io.Writer) error
}

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

func BlockFromBytes added in v0.2.0

func BlockFromBytes(data []byte) (Block, error)

func NewBlock added in v0.1.0

func NewBlock() Block

func RandomBlock added in v1.0.3

func RandomBlock() Block

test only function

type BlockHash added in v0.3.0

type BlockHash [BlockHashSize]byte

func BlockHashFromString added in v1.0.3

func BlockHashFromString(hash 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 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 {
	// IsEmpty returns true if no blocks were committed into the database and
	// no snapshot has been loaded.
	IsEmpty() bool

	// 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.
	SetLatest(trieRoot trie.Hash) 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)
	// LatestTrieRoot returns the latest trie root the chain, if set (see SetLatest)
	LatestTrieRoot() (trie.Hash, 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

	// Prune deletes the trie with the given root from the DB
	Prune(trie.Hash) (trie.PruneStats, error)
	// LargestPrunedBlockIndex returns the largest index of block, which was pruned.
	// An error is returned if no blocks were pruned.
	LargestPrunedBlockIndex() (uint32, error)

	// TakeSnapshot takes a snapshot of the block and trie at the given trie root.
	TakeSnapshot(trie.Hash, io.Writer) error

	// RestoreSnapshot restores the block and trie from the given snapshot.
	// It is not required for the previous trie root to be present in the DB.
	RestoreSnapshot(trie.Hash, io.Reader) error
}

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. 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, writeMutex *sync.Mutex) Store

func NewStoreWithMetrics added in v1.0.3

func NewStoreWithMetrics(db kvstore.KVStore, writeMutex *sync.Mutex, metrics *metrics.ChainStateMetrics) Store

func NewStoreWithUniqueWriteMutex added in v1.0.3

func NewStoreWithUniqueWriteMutex(db kvstore.KVStore) Store

Use only for testing -- writes will not be protected from parallel execution

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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