Skip to content

Commit

Permalink
Change WriteSyncSegment to take a common.Hash instead of []byte to …
Browse files Browse the repository at this point in the history
…enforce a length of 32 bytes
  • Loading branch information
qdm12 committed Feb 14, 2025
1 parent c9f7296 commit 1aa5bea
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
9 changes: 3 additions & 6 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,14 +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 {
if len(start) != common.HashLength {
panic("start must be 32 bytes")
}
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
2 changes: 1 addition & 1 deletion core/rawdb/database_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ExampleInspectDatabase() {
WriteSnapshotBlockHash(db, common.Hash{})
WriteSnapshotRoot(db, common.Hash{})
// Trie segments: (77 + 2) + 1 = 80 bytes
_ = WriteSyncSegment(db, common.Hash{}, common.Hash{}.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
Expand Down
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 1aa5bea

Please sign in to comment.