Skip to content

Commit 6ab502d

Browse files
committed
core, triedb: address comments from marius
1 parent 507c50d commit 6ab502d

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

core/rawdb/schema.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,50 +416,41 @@ func trienodeHistoryIndexKey(addressHash common.Hash, path []byte) []byte {
416416

417417
// accountHistoryIndexBlockKey = StateHistoryAccountBlockPrefix + addressHash + blockID
418418
func accountHistoryIndexBlockKey(addressHash common.Hash, blockID uint32) []byte {
419-
var buf4 [4]byte
420-
binary.BigEndian.PutUint32(buf4[:], blockID)
421-
422419
totalLen := len(StateHistoryAccountBlockPrefix) + common.HashLength + 4
423420
out := make([]byte, totalLen)
424421

425422
off := 0
426423
off += copy(out[off:], StateHistoryAccountBlockPrefix)
427424
off += copy(out[off:], addressHash.Bytes())
428-
copy(out[off:], buf4[:])
425+
binary.BigEndian.PutUint32(out[off:], blockID)
429426

430427
return out
431428
}
432429

433430
// storageHistoryIndexBlockKey = StateHistoryStorageBlockPrefix + addressHash + storageHash + blockID
434431
func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Hash, blockID uint32) []byte {
435-
var buf4 [4]byte
436-
binary.BigEndian.PutUint32(buf4[:], blockID)
437-
438432
totalLen := len(StateHistoryStorageBlockPrefix) + 2*common.HashLength + 4
439433
out := make([]byte, totalLen)
440434

441435
off := 0
442436
off += copy(out[off:], StateHistoryStorageBlockPrefix)
443437
off += copy(out[off:], addressHash.Bytes())
444438
off += copy(out[off:], storageHash.Bytes())
445-
copy(out[off:], buf4[:])
439+
binary.BigEndian.PutUint32(out[off:], blockID)
446440

447441
return out
448442
}
449443

450444
// trienodeHistoryIndexBlockKey = TrienodeHistoryBlockPrefix + addressHash + trienode path + blockID
451445
func trienodeHistoryIndexBlockKey(addressHash common.Hash, path []byte, blockID uint32) []byte {
452-
var buf4 [4]byte
453-
binary.BigEndian.PutUint32(buf4[:], blockID)
454-
455446
totalLen := len(TrienodeHistoryBlockPrefix) + common.HashLength + len(path) + 4
456447
out := make([]byte, totalLen)
457448

458449
off := 0
459450
off += copy(out[off:], TrienodeHistoryBlockPrefix)
460451
off += copy(out[off:], addressHash.Bytes())
461452
off += copy(out[off:], path)
462-
copy(out[off:], buf4[:])
453+
binary.BigEndian.PutUint32(out[off:], blockID)
463454

464455
return out
465456
}

triedb/pathdb/history_trienode.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ func newTrienodeHistory(root common.Hash, parent common.Hash, block uint64, node
160160

161161
// sharedLen returns the length of the common prefix shared by a and b.
162162
func sharedLen(a, b []byte) int {
163-
n := len(a)
164-
if len(b) < n {
165-
n = len(b)
166-
}
163+
n := min(len(a), len(b))
167164
for i := 0; i < n; i++ {
168165
if a[i] != b[i] {
169166
return i
@@ -259,7 +256,8 @@ func (h *trienodeHistory) encode() ([]byte, []byte, []byte, error) {
259256
internalValOffset += uint32(len(value))
260257
}
261258

262-
// Encode trailer
259+
// Encode trailer, the number of restart sections is len(restarts))/2,
260+
// as we track the offsets of both key and value sections.
263261
var trailer []byte
264262
for _, number := range append(restarts, uint32(len(restarts))/2) {
265263
binary.BigEndian.PutUint32(buf[:4], number)
@@ -341,7 +339,7 @@ func decodeSingle(keySection []byte, onValue func([]byte, int, int) error) ([]st
341339

342340
keys []string
343341
)
344-
// Decode restarts
342+
// Decode the number of restart section
345343
if len(keySection) < 4 {
346344
return nil, fmt.Errorf("key section too short, size: %d", len(keySection))
347345
}

triedb/pathdb/history_trienode_test.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ func randomTrienodes(n int) (map[common.Hash]map[string][]byte, common.Hash) {
4343
nodes[owner] = make(map[string][]byte)
4444

4545
for j := 0; j < 10; j++ {
46-
pathLen := rand.Intn(10)
47-
path := testrand.Bytes(pathLen)
46+
path := testrand.Bytes(rand.Intn(10))
4847
for z := 0; z < len(path); z++ {
49-
valLen := rand.Intn(128)
50-
nodes[owner][string(path[:z])] = testrand.Bytes(valLen)
48+
nodes[owner][string(path[:z])] = testrand.Bytes(rand.Intn(128))
5149
}
5250
}
5351
// zero-size trie node, representing it was non-existent before
@@ -65,7 +63,7 @@ func randomTrienodes(n int) (map[common.Hash]map[string][]byte, common.Hash) {
6563
return nodes, root
6664
}
6765

68-
func makeTrinodeHistory() *trienodeHistory {
66+
func makeTrienodeHistory() *trienodeHistory {
6967
nodes, root := randomTrienodes(10)
7068
return newTrienodeHistory(root, common.Hash{}, 1, nodes)
7169
}
@@ -86,7 +84,7 @@ func makeTrienodeHistories(n int) []*trienodeHistory {
8684
func TestEncodeDecodeTrienodeHistory(t *testing.T) {
8785
var (
8886
dec trienodeHistory
89-
obj = makeTrinodeHistory()
87+
obj = makeTrienodeHistory()
9088
)
9189
header, keySection, valueSection, err := obj.encode()
9290
if err != nil {
@@ -108,6 +106,21 @@ func TestEncodeDecodeTrienodeHistory(t *testing.T) {
108106
if !compareMapSet(dec.nodes, obj.nodes) {
109107
t.Fatal("trienode content is mismatched")
110108
}
109+
110+
// Re-encode again, ensuring the encoded blob still match
111+
header2, keySection2, valueSection2, err := dec.encode()
112+
if err != nil {
113+
t.Fatalf("Failed to encode trienode history: %v", err)
114+
}
115+
if !bytes.Equal(header, header2) {
116+
t.Fatal("re-encoded header is mismatched")
117+
}
118+
if !bytes.Equal(keySection, keySection2) {
119+
t.Fatal("re-encoded key section is mismatched")
120+
}
121+
if !bytes.Equal(valueSection, valueSection2) {
122+
t.Fatal("re-encoded value section is mismatched")
123+
}
111124
}
112125

113126
func TestTrienodeHistoryReader(t *testing.T) {
@@ -280,7 +293,7 @@ func TestNilNodeValues(t *testing.T) {
280293

281294
// TestCorruptedHeader tests error handling for corrupted header data
282295
func TestCorruptedHeader(t *testing.T) {
283-
h := makeTrinodeHistory()
296+
h := makeTrienodeHistory()
284297
header, keySection, valueSection, _ := h.encode()
285298

286299
// Test corrupted version
@@ -311,7 +324,7 @@ func TestCorruptedHeader(t *testing.T) {
311324

312325
// TestCorruptedKeySection tests error handling for corrupted key section data
313326
func TestCorruptedKeySection(t *testing.T) {
314-
h := makeTrinodeHistory()
327+
h := makeTrienodeHistory()
315328
header, keySection, valueSection, _ := h.encode()
316329

317330
// Test empty key section when header indicates data
@@ -345,7 +358,7 @@ func TestCorruptedKeySection(t *testing.T) {
345358

346359
// TestCorruptedValueSection tests error handling for corrupted value section data
347360
func TestCorruptedValueSection(t *testing.T) {
348-
h := makeTrinodeHistory()
361+
h := makeTrienodeHistory()
349362
header, keySection, valueSection, _ := h.encode()
350363

351364
// Test truncated value section
@@ -368,7 +381,7 @@ func TestCorruptedValueSection(t *testing.T) {
368381

369382
// TestInvalidOffsets tests error handling for invalid offsets in encoded data
370383
func TestInvalidOffsets(t *testing.T) {
371-
h := makeTrinodeHistory()
384+
h := makeTrienodeHistory()
372385
header, keySection, valueSection, _ := h.encode()
373386

374387
// Corrupt key offset in header (make it larger than key section)
@@ -395,7 +408,7 @@ func TestInvalidOffsets(t *testing.T) {
395408
// TestTrienodeHistoryReaderNonExistentPath tests reading non-existent paths
396409
func TestTrienodeHistoryReaderNonExistentPath(t *testing.T) {
397410
var (
398-
h = makeTrinodeHistory()
411+
h = makeTrienodeHistory()
399412
freezer, _ = rawdb.NewTrienodeFreezer(t.TempDir(), false, false)
400413
)
401414
defer freezer.Close()
@@ -523,7 +536,7 @@ func TestTrienodeHistoryReaderNilKey(t *testing.T) {
523536

524537
// TestTrienodeHistoryReaderIterator tests the iterator functionality
525538
func TestTrienodeHistoryReaderIterator(t *testing.T) {
526-
h := makeTrinodeHistory()
539+
h := makeTrienodeHistory()
527540

528541
// Count expected entries
529542
expectedCount := 0
@@ -614,7 +627,7 @@ func TestSharedLen(t *testing.T) {
614627
// TestDecodeHeaderCorruptedData tests decodeHeader with corrupted data
615628
func TestDecodeHeaderCorruptedData(t *testing.T) {
616629
// Create valid header data first
617-
h := makeTrinodeHistory()
630+
h := makeTrienodeHistory()
618631
header, _, _, _ := h.encode()
619632

620633
// Test with empty header
@@ -661,7 +674,7 @@ func TestDecodeHeaderCorruptedData(t *testing.T) {
661674

662675
// TestDecodeSingleCorruptedData tests decodeSingle with corrupted data
663676
func TestDecodeSingleCorruptedData(t *testing.T) {
664-
h := makeTrinodeHistory()
677+
h := makeTrienodeHistory()
665678
_, keySection, _, _ := h.encode()
666679

667680
// Test with empty key section

0 commit comments

Comments
 (0)