Skip to content

Commit

Permalink
chore(core/rawdb): add ExampleInspectDatabase to check custom behav…
Browse files Browse the repository at this point in the history
…ior (#790)
  • Loading branch information
qdm12 authored Feb 28, 2025
1 parent bd1aabe commit a4abc14
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
8 changes: 4 additions & 4 deletions core/rawdb/accessors_state_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewSyncSegmentsIterator(db ethdb.Iteratee, root common.Hash) ethdb.Iterator
}

// WriteSyncSegment adds a trie segment for root at the given start position.
func WriteSyncSegment(db ethdb.KeyValueWriter, root common.Hash, start []byte) error {
func WriteSyncSegment(db ethdb.KeyValueWriter, root common.Hash, start common.Hash) error {
return db.Put(packSyncSegmentKey(root, start), []byte{0x01})
}

Expand All @@ -104,11 +104,11 @@ func UnpackSyncSegmentKey(keyBytes []byte) (common.Hash, []byte) {
}

// packSyncSegmentKey packs root and account into a key for storage in db.
func packSyncSegmentKey(root common.Hash, start []byte) []byte {
bytes := make([]byte, len(syncSegmentsPrefix)+common.HashLength+len(start))
func packSyncSegmentKey(root common.Hash, start common.Hash) []byte {
bytes := make([]byte, syncSegmentsKeyLength)
copy(bytes, syncSegmentsPrefix)
copy(bytes[len(syncSegmentsPrefix):], root[:])
copy(bytes[len(syncSegmentsPrefix)+common.HashLength:], start)
copy(bytes[len(syncSegmentsPrefix)+common.HashLength:], start.Bytes())
return bytes
}

Expand Down
2 changes: 1 addition & 1 deletion core/rawdb/accessors_state_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestClearPrefix(t *testing.T) {
require := require.New(t)
db := NewMemoryDatabase()
// add a key that should be cleared
require.NoError(WriteSyncSegment(db, common.Hash{1}, common.Hash{}.Bytes()))
require.NoError(WriteSyncSegment(db, common.Hash{1}, common.Hash{}))

// add a key that should not be cleared
key := append(syncSegmentsPrefix, []byte("foo")...)
Expand Down
131 changes: 131 additions & 0 deletions core/rawdb/database_ext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package rawdb

import (
"fmt"

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

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

// Extra metadata keys: (17 + 32) + (12 + 32) = 93 bytes
WriteSnapshotBlockHash(db, common.Hash{})
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)
}
// 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 | |
// +-----------------+-------------------------+----------+-------+
}

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
}
2 changes: 1 addition & 1 deletion sync/statesync/trie_segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (t *trieToSync) createSegments(numSegments int) error {

// create the segments
segment := t.addSegment(startBytes, endBytes)
if err := rawdb.WriteSyncSegment(t.sync.db, t.root, segment.start); err != nil {
if err := rawdb.WriteSyncSegment(t.sync.db, t.root, common.BytesToHash(segment.start)); err != nil {
return err
}
}
Expand Down

0 comments on commit a4abc14

Please sign in to comment.