Documentation ¶
Index ¶
- Constants
- Variables
- func BlockFromBytes(blockBytes []byte) (*block, error)
- type Block
- type BlockHash
- type L1Commitment
- func (s *L1Commitment) BlockHash() BlockHash
- func (s *L1Commitment) Bytes() []byte
- func (s *L1Commitment) Equals(other *L1Commitment) bool
- func (s *L1Commitment) Read(r io.Reader) error
- func (s *L1Commitment) String() string
- func (s *L1Commitment) TrieRoot() trie.Hash
- func (s *L1Commitment) Write(w io.Writer) error
- type State
- type StateCommonValues
- type StateDraft
- type Store
Constants ¶
const BlockHashSize = 20
const ( // KeyChainID is the key used to store the chain ID in the state. // It should not collide with any hname prefix (which are 32 bits long). KeyChainID = "\x00" )
const L1CommitmentSize = trie.HashSizeBytes + BlockHashSize
Variables ¶
var ( ErrTrieRootNotFound = errors.New("trie root not found") ErrUnknownLatestTrieRoot = errors.New("latest trie root is unknown") )
var L1CommitmentNil = &L1Commitment{}
Functions ¶
func BlockFromBytes ¶ added in v0.2.0
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 BlockHashFromString ¶ added in v1.0.3
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 OriginL1Commitment ¶ added in v0.3.0
func OriginL1Commitment() *L1Commitment
OriginL1Commitment calculates the L1Commitment for the origin block, which is the same for every chain.
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) 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
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 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 InitChainStore ¶
InitChainStore initializes a new chain store, committing the origin block and state to the DB. The ChainID is not known at this point, so it will contain all zeroes.