firewood

package
v0.8.1-db-metrics-fix Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: GPL-3.0, LGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Defaults = Config{
	CleanCacheSize:       1024 * 1024,
	FreeListCacheEntries: 40_000,
	Revisions:            100,
	ReadCacheStrategy:    ffi.CacheAllReads,
}

Note that `FilePath` is not specified, and must always be set by the user.

Functions

This section is empty.

Types

type AccountTrie

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

AccountTrie implements state.Trie for managing account states. There are a couple caveats to the current implementation:

  1. `Commit` is not used as expected in the state package. The `StorageTrie` doesn't return values, and we thus rely on the `AccountTrie`.
  2. The `Hash` method actually creates the proposal, since Firewood cannot calculate the hash of the trie without committing it. It is immediately dropped, and this can likely be optimized.

Note this is not concurrent safe.

func NewAccountTrie

func NewAccountTrie(root common.Hash, db *Database) (*AccountTrie, error)

func (*AccountTrie) Commit

func (a *AccountTrie) Commit(bool) (common.Hash, *trienode.NodeSet, error)

Commit returns the new root hash of the trie and a NodeSet containing all modified accounts and storage slots. The format of the NodeSet is different than in go-ethereum's trie implementation due to Firewood's design. This boolean is ignored, as it is a relic of the StateTrie implementation.

func (*AccountTrie) Copy

func (a *AccountTrie) Copy() *AccountTrie

func (*AccountTrie) DeleteAccount

func (a *AccountTrie) DeleteAccount(addr common.Address) error

DeleteAccount removes the state account associated with an address.

func (*AccountTrie) DeleteStorage

func (a *AccountTrie) DeleteStorage(addr common.Address, key []byte) error

DeleteStorage removes the value associated with a storage key for a given account address.

func (*AccountTrie) GetAccount

func (a *AccountTrie) GetAccount(addr common.Address) (*types.StateAccount, error)

GetAccount returns the state account associated with an address. - If the account has been updated, the new value is returned. - If the account has been deleted, (nil, nil) is returned. - If the account does not exist, (nil, nil) is returned.

func (*AccountTrie) GetKey

func (*AccountTrie) GetKey([]byte) []byte

GetKey implements state.Trie. This should not be used, since any user should not be accessing by raw key.

func (*AccountTrie) GetStorage

func (a *AccountTrie) GetStorage(addr common.Address, key []byte) ([]byte, error)

GetStorage returns the value associated with a storage key for a given account address. - If the storage slot has been updated, the new value is returned. - If the storage slot has been deleted, (nil, nil) is returned. - If the storage slot does not exist, (nil, nil) is returned.

func (*AccountTrie) Hash

func (a *AccountTrie) Hash() common.Hash

Hash returns the current hash of the state trie. This will create a proposal and drop it, so it is not efficient to call for each transaction. If there are no changes since the last call, the cached root is returned.

func (*AccountTrie) NodeIterator

func (*AccountTrie) NodeIterator([]byte) (trie.NodeIterator, error)

NodeIterator implements state.Trie. Firewood does not support iterating over internal nodes.

func (*AccountTrie) Prove

Prove implements state.Trie. Firewood does not yet support providing key proofs.

func (*AccountTrie) UpdateAccount

func (a *AccountTrie) UpdateAccount(addr common.Address, account *types.StateAccount) error

UpdateAccount replaces or creates the state account associated with an address. This new value will be returned for subsequent `GetAccount` calls.

func (*AccountTrie) UpdateContractCode

func (*AccountTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte) error

UpdateContractCode implements state.Trie. Contract code is controlled by rawdb, so we don't need to do anything here.

func (*AccountTrie) UpdateStorage

func (a *AccountTrie) UpdateStorage(addr common.Address, key []byte, value []byte) error

UpdateStorage replaces or creates the value associated with a storage key for a given account address. This new value will be returned for subsequent `GetStorage` calls.

type Config

type Config struct {
	FilePath             string
	CleanCacheSize       int  // Size of the clean cache in bytes
	FreeListCacheEntries uint // Number of free list entries to cache
	Revisions            uint
	ReadCacheStrategy    ffi.CacheStrategy
}

func (Config) BackendConstructor

func (c Config) BackendConstructor(ethdb.Database) triedb.DBOverride

type Database

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

func New

func New(config Config) (*Database, error)

New creates a new Firewood database with the given disk database and configuration. Any error during creation will cause the program to exit.

func (*Database) Cap

Firewood does not support this.

func (*Database) Close

func (db *Database) Close() error

func (*Database) Commit

func (db *Database) Commit(root common.Hash, report bool) (err error)

Commit persists a proposal as a revision to the database.

Any time this is called, we expect either:

  1. The root is the same as the current root of the database (empty block during bootstrapping)
  2. We have created a valid propsal with that root, and it is of height +1 above the proposal tree root. Additionally, this should be unique.

Afterward, we know that no other proposal at this height can be committed, so we can dereference all children in the the other branches of the proposal tree.

func (*Database) Dereference

func (*Database) Dereference(common.Hash)

Dereference drops a proposal from the database. This function is no-op because unused proposals are dereferenced when no longer valid. We cannot dereference at this call. Consider the following case: Chain 1 has root A and root C Chain 2 has root B and root C We commit root A, and immediately dereference root B and its child. Root C is Rejected, (which is intended to be 2C) but there's now only one record of root C in the proposal map. Thus, we recognize the single root C as the only proposal, and dereference it.

func (*Database) Initialized

func (db *Database) Initialized(common.Hash) bool

Initialized checks whether a non-empty genesis block has been written.

func (*Database) Reader

func (db *Database) Reader(root common.Hash) (database.Reader, error)

Reader retrieves a node reader belonging to the given state root. An error will be returned if the requested state is not available.

func (*Database) Reference

func (*Database) Reference(common.Hash, common.Hash)

This isn't called anywhere in subnet-evm

func (*Database) Scheme

func (*Database) Scheme() string

Scheme returns the scheme of the database. This is only used in some API calls and in StateDB to avoid iterating through deleted storage tries. WARNING: If cherry-picking anything from upstream that uses this, it must be overwritten to use something like: `_, ok := db.(*Database); if !ok { return "" }` to recognize the Firewood database.

func (*Database) Size

Size returns the storage size of diff layer nodes above the persistent disk layer and the dirty nodes buffered within the disk layer Only used for metrics and Commit intervals in APIs. This will be implemented in the firewood database eventually. Currently, Firewood stores all revisions in disk and proposals in memory.

func (*Database) Update

func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint64, nodes *trienode.MergedNodeSet, _ *triestate.Set, opts ...stateconf.TrieDBUpdateOption) error

Update takes a root and a set of keys-values and creates a new proposal. It will not be committed until the Commit method is called. This function should be called even if there are no changes to the state to ensure proper tracking of block hashes.

type ProposalContext

type ProposalContext struct {
	Proposal *ffi.Proposal
	Hashes   map[common.Hash]struct{} // All corresponding block hashes
	Root     common.Hash
	Block    uint64
	Parent   *ProposalContext
	Children []*ProposalContext
}

ProposalContext represents a proposal in the Firewood database. This tracks all outstanding proposals to allow dereferencing upon commit.

type StorageTrie

type StorageTrie struct {
	*AccountTrie
}

func NewStorageTrie

func NewStorageTrie(accountTrie *AccountTrie) (*StorageTrie, error)

`NewStorageTrie` returns a wrapper around an `AccountTrie` since Firewood does not require a separate storage trie. All changes are managed by the account trie.

func (*StorageTrie) Commit

Actual commit is handled by the account trie. Return the old storage root as if there was no change since Firewood will manage the hash calculations without it. All changes are managed by the account trie.

func (*StorageTrie) Copy

func (*StorageTrie) Copy() *StorageTrie

Copy should never be called on a storage trie, as it is just a wrapper around the account trie. Each storage trie should be re-opened with the account trie separately.

func (*StorageTrie) Hash

func (*StorageTrie) Hash() common.Hash

Firewood doesn't require tracking storage roots inside of an account. They will be updated in place when hashing of the proposal takes place.

Jump to

Keyboard shortcuts

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