Skip to content

Commit 1aa5bea

Browse files
committed
Change WriteSyncSegment to take a common.Hash instead of []byte to enforce a length of 32 bytes
1 parent c9f7296 commit 1aa5bea

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
lines changed

core/rawdb/accessors_state_sync.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func NewSyncSegmentsIterator(db ethdb.Iteratee, root common.Hash) ethdb.Iterator
7777
}
7878

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

@@ -104,14 +104,11 @@ func UnpackSyncSegmentKey(keyBytes []byte) (common.Hash, []byte) {
104104
}
105105

106106
// packSyncSegmentKey packs root and account into a key for storage in db.
107-
func packSyncSegmentKey(root common.Hash, start []byte) []byte {
108-
if len(start) != common.HashLength {
109-
panic("start must be 32 bytes")
110-
}
107+
func packSyncSegmentKey(root common.Hash, start common.Hash) []byte {
111108
bytes := make([]byte, syncSegmentsKeyLength)
112109
copy(bytes, syncSegmentsPrefix)
113110
copy(bytes[len(syncSegmentsPrefix):], root[:])
114-
copy(bytes[len(syncSegmentsPrefix)+common.HashLength:], start)
111+
copy(bytes[len(syncSegmentsPrefix)+common.HashLength:], start.Bytes())
115112
return bytes
116113
}
117114

core/rawdb/accessors_state_sync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestClearPrefix(t *testing.T) {
1414
require := require.New(t)
1515
db := NewMemoryDatabase()
1616
// add a key that should be cleared
17-
require.NoError(WriteSyncSegment(db, common.Hash{1}, common.Hash{}.Bytes()))
17+
require.NoError(WriteSyncSegment(db, common.Hash{1}, common.Hash{}))
1818

1919
// add a key that should not be cleared
2020
key := append(syncSegmentsPrefix, []byte("foo")...)

core/rawdb/database_ext_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func ExampleInspectDatabase() {
1616
WriteSnapshotBlockHash(db, common.Hash{})
1717
WriteSnapshotRoot(db, common.Hash{})
1818
// Trie segments: (77 + 2) + 1 = 80 bytes
19-
_ = WriteSyncSegment(db, common.Hash{}, common.Hash{}.Bytes())
19+
_ = WriteSyncSegment(db, common.Hash{}, common.Hash{})
2020
// Storage tries to fetch: 76 + 1 = 77 bytes
2121
_ = WriteSyncStorageTrie(db, common.Hash{}, common.Hash{})
2222
// Code to fetch: 34 + 0 = 34 bytes

sync/statesync/trie_segments.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (t *trieToSync) createSegments(numSegments int) error {
301301

302302
// create the segments
303303
segment := t.addSegment(startBytes, endBytes)
304-
if err := rawdb.WriteSyncSegment(t.sync.db, t.root, segment.start); err != nil {
304+
if err := rawdb.WriteSyncSegment(t.sync.db, t.root, common.BytesToHash(segment.start)); err != nil {
305305
return err
306306
}
307307
}

0 commit comments

Comments
 (0)