customrawdb

package
v0.15.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: GPL-3.0, LGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// CodeToFetchPrefix is the prefix for code hashes that need to be fetched.
	// CodeToFetchPrefix + code hash -> empty value tracks the outstanding code hashes we need to fetch.
	CodeToFetchPrefix = []byte("CP")
)

State sync progress keys and prefixes

Functions

func AddCodeToFetch

func AddCodeToFetch(db ethdb.KeyValueWriter, hash common.Hash)

AddCodeToFetch adds a marker that we need to fetch the code for `hash`.

func ClearAllSyncSegments

func ClearAllSyncSegments(db ethdb.KeyValueStore) error

ClearAllSyncSegments removes all segment markers from db

func ClearAllSyncStorageTries

func ClearAllSyncStorageTries(db ethdb.KeyValueStore) error

ClearAllSyncStorageTries removes all storage tries added for syncing from db

func ClearSyncSegments

func ClearSyncSegments(db ethdb.KeyValueStore, root common.Hash) error

ClearSyncSegments removes segment markers for root from db

func ClearSyncStorageTrie

func ClearSyncStorageTrie(db ethdb.KeyValueStore, root common.Hash) error

ClearSyncStorageTrie removes all storage trie accounts (with the given root) from db. Intended for use when the trie with root has completed syncing.

func DeleteCodeToFetch

func DeleteCodeToFetch(db ethdb.KeyValueWriter, hash common.Hash)

DeleteCodeToFetch removes the marker that the code corresponding to `hash` needs to be fetched.

func DeleteOfflinePruning

func DeleteOfflinePruning(db ethdb.KeyValueStore) error

DeleteOfflinePruning deletes any marker of the last attempt to run offline pruning.

func DeletePopulateMissingTries

func DeletePopulateMissingTries(db ethdb.KeyValueStore) error

DeletePopulateMissingTries deletes any marker of the last attempt to re-populate missing trie nodes.

func DeleteSnapshotBlockHash

func DeleteSnapshotBlockHash(db ethdb.KeyValueWriter)

DeleteSnapshotBlockHash deletes the hash of the block whose state is contained in the persisted snapshot. Since snapshots are not immutable, this method can be used during updates, so a crash or failure will mark the entire snapshot invalid.

func GetLatestSyncPerformed

func GetLatestSyncPerformed(db ethdb.Iteratee) uint64

GetLatestSyncPerformed returns the latest block number state synced performed to.

func HasPruningDisabled

func HasPruningDisabled(db ethdb.KeyValueStore) (bool, error)

HasPruningDisabled returns true if there is a marker present indicating that the node has run with pruning disabled at some pooint.

func InspectDatabase

func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error

InspectDatabase traverses the entire database and checks the size of all different categories of data.

Example
package main

import (
	"fmt"

	"github.com/ava-labs/libevm/common"
	"github.com/ava-labs/libevm/core/rawdb"
	"github.com/ava-labs/libevm/ethdb"
)

func main() {
	db := &stubDatabase{
		iterator: &stubIterator{},
	}

	// Extra metadata keys: (17 + 32) + (12 + 32) = 93 bytes
	WriteSnapshotBlockHash(db, common.Hash{})
	rawdb.WriteSnapshotRoot(db, common.Hash{})
	// Trie segments: (77 + 2) + 1 = 80 bytes
	_ = WriteSyncSegment(db, common.Hash{}, common.Hash{})
	// Storage tries to fetch: 76 + 1 = 77 bytes
	_ = WriteSyncStorageTrie(db, common.Hash{}, common.Hash{})
	// Code to fetch: 34 + 0 = 34 bytes
	AddCodeToFetch(db, common.Hash{})
	// Block numbers synced to: 22 + 1 = 23 bytes
	_ = WriteSyncPerformed(db, 0)

	keyPrefix := []byte(nil)
	keyStart := []byte(nil)

	err := InspectDatabase(db, keyPrefix, keyStart)
	if err != nil {
		fmt.Println(err)
	}
}

type stubDatabase struct {
	ethdb.Database
	iterator *stubIterator
}

func (s *stubDatabase) NewIterator(keyPrefix, keyStart []byte) ethdb.Iterator {
	return s.iterator
}

// AncientSize is used in [InspectDatabase] to determine the ancient sizes.
func (s *stubDatabase) AncientSize(kind string) (uint64, error) {
	return 0, nil
}

func (s *stubDatabase) Ancients() (uint64, error) {
	return 0, nil
}

func (s *stubDatabase) Tail() (uint64, error) {
	return 0, nil
}

func (s *stubDatabase) Put(key, value []byte) error {
	s.iterator.kvs = append(s.iterator.kvs, keyValue{key: key, value: value})
	return nil
}

func (s *stubDatabase) Get(key []byte) ([]byte, error) {
	return nil, nil
}

func (s *stubDatabase) ReadAncients(fn func(ethdb.AncientReaderOp) error) error {
	return nil
}

type stubIterator struct {
	ethdb.Iterator
	i   int // see [stubIterator.pos]
	kvs []keyValue
}

type keyValue struct {
	key   []byte
	value []byte
}

// pos returns the true iterator position, which is otherwise off by one because
// Next() is called _before_ usage.
func (s *stubIterator) pos() int {
	return s.i - 1
}

func (s *stubIterator) Next() bool {
	s.i++
	return s.pos() < len(s.kvs)
}

func (s *stubIterator) Release() {}

func (s *stubIterator) Key() []byte {
	return s.kvs[s.pos()].key
}

func (s *stubIterator) Value() []byte {
	return s.kvs[s.pos()].value
}
Output:

+-----------------+-------------------------+----------+-------+
|    DATABASE     |        CATEGORY         |   SIZE   | ITEMS |
+-----------------+-------------------------+----------+-------+
| Key-Value store | Headers                 | 0.00 B   |     0 |
| Key-Value store | Bodies                  | 0.00 B   |     0 |
| Key-Value store | Receipt lists           | 0.00 B   |     0 |
| Key-Value store | Block number->hash      | 0.00 B   |     0 |
| Key-Value store | Block hash->number      | 0.00 B   |     0 |
| Key-Value store | Transaction index       | 0.00 B   |     0 |
| Key-Value store | Bloombit index          | 0.00 B   |     0 |
| Key-Value store | Contract codes          | 0.00 B   |     0 |
| Key-Value store | Hash trie nodes         | 0.00 B   |     0 |
| Key-Value store | Path trie state lookups | 0.00 B   |     0 |
| Key-Value store | Path trie account nodes | 0.00 B   |     0 |
| Key-Value store | Path trie storage nodes | 0.00 B   |     0 |
| Key-Value store | Trie preimages          | 0.00 B   |     0 |
| Key-Value store | Account snapshot        | 0.00 B   |     0 |
| Key-Value store | Storage snapshot        | 0.00 B   |     0 |
| Key-Value store | Clique snapshots        | 0.00 B   |     0 |
| Key-Value store | Singleton metadata      | 93.00 B  |     2 |
| Light client    | CHT trie nodes          | 0.00 B   |     0 |
| Light client    | Bloom trie nodes        | 0.00 B   |     0 |
| State sync      | Trie segments           | 78.00 B  |     1 |
| State sync      | Storage tries to fetch  | 77.00 B  |     1 |
| State sync      | Code to fetch           | 34.00 B  |     1 |
| State sync      | Block numbers synced to | 23.00 B  |     1 |
+-----------------+-------------------------+----------+-------+
|                            TOTAL          | 305.00 B |       |
+-----------------+-------------------------+----------+-------+

func IterateAccountSnapshots

func IterateAccountSnapshots(db ethdb.Iteratee) ethdb.Iterator

IterateAccountSnapshots returns an iterator for walking all of the accounts in the snapshot

func NewCodeToFetchIterator

func NewCodeToFetchIterator(db ethdb.Iteratee) ethdb.Iterator

NewCodeToFetchIterator returns a KeyLength iterator over all code hashes that are pending syncing. It is the caller's responsibility to unpack the key and call Release on the returned iterator.

func NewSyncPerformedIterator

func NewSyncPerformedIterator(db ethdb.Iteratee) ethdb.Iterator

NewSyncPerformedIterator returns an iterator over all block numbers the VM has state synced to.

func NewSyncSegmentsIterator

func NewSyncSegmentsIterator(db ethdb.Iteratee, root common.Hash) ethdb.Iterator

NewSyncSegmentsIterator returns a KeyLength iterator over all trie segments added for root. It is the caller's responsibility to unpack the key and call Release on the returned iterator.

func NewSyncStorageTriesIterator

func NewSyncStorageTriesIterator(db ethdb.Iteratee, seek []byte) ethdb.Iterator

NewSyncStorageTriesIterator returns a KeyLength iterator over all storage tries added for syncing (beginning at seek). It is the caller's responsibility to unpack the key and call Release on the returned iterator.

func ReadAcceptorTip

func ReadAcceptorTip(db ethdb.KeyValueReader) (common.Hash, error)

ReadAcceptorTip reads the hash of the last accepted block that was fully processed. If there is no value present (the index is being initialized for the first time), then the empty hash is returned.

func ReadOfflinePruning

func ReadOfflinePruning(db ethdb.KeyValueStore) (time.Time, error)

ReadOfflinePruning reads the most recent timestamp of an attempt to run offline pruning if present.

func ReadPopulateMissingTries

func ReadPopulateMissingTries(db ethdb.KeyValueStore) (time.Time, error)

ReadPopulateMissingTries reads the most recent timestamp of an attempt to re-populate missing trie nodes.

func ReadSnapshotBlockHash

func ReadSnapshotBlockHash(db ethdb.KeyValueReader) common.Hash

ReadSnapshotBlockHash retrieves the hash of the block whose state is contained in the persisted snapshot.

func ReadSyncRoot

func ReadSyncRoot(db ethdb.KeyValueReader) (common.Hash, error)

ReadSyncRoot reads the root corresponding to the main trie of an in-progress sync and returns common.Hash{} if no in-progress sync was found.

func UnpackSyncPerformedKey

func UnpackSyncPerformedKey(key []byte) uint64

UnpackSyncPerformedKey returns the block number from keys the iterator returned from NewSyncPerformedIterator.

func UnpackSyncSegmentKey

func UnpackSyncSegmentKey(keyBytes []byte) (common.Hash, []byte)

UnpackSyncSegmentKey returns the root and start position for a trie segment key returned from NewSyncSegmentsIterator.

func UnpackSyncStorageTrieKey

func UnpackSyncStorageTrieKey(keyBytes []byte) (common.Hash, common.Hash)

UnpackSyncStorageTrieKey returns the root and account for a storage trie key returned from NewSyncStorageTriesIterator.

func WriteAcceptorTip

func WriteAcceptorTip(db ethdb.KeyValueWriter, hash common.Hash) error

WriteAcceptorTip writes `hash` as the last accepted block that has been fully processed.

func WriteOfflinePruning

func WriteOfflinePruning(db ethdb.KeyValueStore) error

WriteOfflinePruning writes a time marker of the last attempt to run offline pruning. The marker is written when offline pruning completes and is deleted when the node is started successfully with offline pruning disabled. This ensures users must disable offline pruning and start their node successfully between runs of offline pruning.

func WritePopulateMissingTries

func WritePopulateMissingTries(db ethdb.KeyValueStore) error

WritePopulateMissingTries writes a marker for the current attempt to populate missing tries.

func WritePruningDisabled

func WritePruningDisabled(db ethdb.KeyValueStore) error

WritePruningDisabled writes a marker to track whether the node has ever run with pruning disabled.

func WriteSnapshotBlockHash

func WriteSnapshotBlockHash(db ethdb.KeyValueWriter, blockHash common.Hash)

WriteSnapshotBlockHash stores the root of the block whose state is contained in the persisted snapshot.

func WriteSyncPerformed

func WriteSyncPerformed(db ethdb.KeyValueWriter, blockNumber uint64) error

WriteSyncPerformed logs an entry in `db` indicating the VM state synced to `blockNumber`.

func WriteSyncRoot

func WriteSyncRoot(db ethdb.KeyValueWriter, root common.Hash) error

WriteSyncRoot writes root as the root of the main trie of the in-progress sync.

func WriteSyncSegment

func WriteSyncSegment(db ethdb.KeyValueWriter, root common.Hash, start common.Hash) error

WriteSyncSegment adds a trie segment for root at the given start position.

func WriteSyncStorageTrie

func WriteSyncStorageTrie(db ethdb.KeyValueWriter, root common.Hash, account common.Hash) error

WriteSyncStorageTrie adds a storage trie for account (with the given root) to be synced.

Types

This section is empty.

Jump to

Keyboard shortcuts

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