diff --git a/triedb/pathdb/history_index_block.go b/triedb/pathdb/history_index_block.go index 0b2f44b7d3..fd43d81b78 100644 --- a/triedb/pathdb/history_index_block.go +++ b/triedb/pathdb/history_index_block.go @@ -28,7 +28,7 @@ import ( const ( indexBlockDescSize = 14 // The size of index block descriptor - indexBlockSizeCap = 4096 // The maximum size of a single index block + indexBlockMaxSize = 4096 // The maximum size of a single index block indexBlockRestartLen = 256 // The restart interval length of index block ) @@ -457,7 +457,7 @@ func (b *blockWriter) empty() bool { func (b *blockWriter) estimateFull(ext []uint16) bool { size := 8 + 2*len(ext) - return len(b.data)+size > indexBlockSizeCap + return len(b.data)+size > indexBlockMaxSize } // last returns the last element in the block. It should only be called when diff --git a/triedb/pathdb/history_index_iterator.go b/triedb/pathdb/history_index_iterator.go index 8731687c5e..076baaa9e5 100644 --- a/triedb/pathdb/history_index_iterator.go +++ b/triedb/pathdb/history_index_iterator.go @@ -126,7 +126,7 @@ func (f extFilter) contains(bitmap []byte) (bool, error) { } // Check descendants: the presence of any descendant implicitly // represents a mutation of its ancestor. - return isByteSet(bitmap, 2+2*n) || isByteSet(bitmap, 3+2*n), nil + return bitmap[2+2*n] != 0 || bitmap[3+2*n] != 0, nil } default: return false, fmt.Errorf("unsupported bitmap size %d", len(bitmap)) diff --git a/triedb/pathdb/history_trienode.go b/triedb/pathdb/history_trienode.go index 360d248aa0..6c0c0fe8cc 100644 --- a/triedb/pathdb/history_trienode.go +++ b/triedb/pathdb/history_trienode.go @@ -208,7 +208,7 @@ func (h *trienodeHistory) encode() ([]byte, []byte, []byte, error) { restarts = append(restarts, internalValOffset) prefixLen = 0 } else { - prefixLen = sharedLen(prevKey, key) + prefixLen = commonPrefixLen(prevKey, key) } value := h.nodes[owner][path] diff --git a/triedb/pathdb/history_trienode_test.go b/triedb/pathdb/history_trienode_test.go index be4740a904..0c0422f00f 100644 --- a/triedb/pathdb/history_trienode_test.go +++ b/triedb/pathdb/history_trienode_test.go @@ -580,8 +580,8 @@ func TestTrienodeHistoryReaderIterator(t *testing.T) { } } -// TestSharedLen tests the sharedLen helper function -func TestSharedLen(t *testing.T) { +// TestCommonPrefixLen tests the commonPrefixLen helper function +func TestCommonPrefixLen(t *testing.T) { tests := []struct { a, b []byte expected int @@ -610,13 +610,13 @@ func TestSharedLen(t *testing.T) { } for i, test := range tests { - result := sharedLen(test.a, test.b) + result := commonPrefixLen(test.a, test.b) if result != test.expected { t.Errorf("Test %d: sharedLen(%q, %q) = %d, expected %d", i, test.a, test.b, result, test.expected) } // Test commutativity - resultReverse := sharedLen(test.b, test.a) + resultReverse := commonPrefixLen(test.b, test.a) if result != resultReverse { t.Errorf("Test %d: sharedLen is not commutative: sharedLen(a,b)=%d, sharedLen(b,a)=%d", i, result, resultReverse) diff --git a/triedb/pathdb/history_trienode_utils.go b/triedb/pathdb/history_trienode_utils.go index b30373cf24..0513343404 100644 --- a/triedb/pathdb/history_trienode_utils.go +++ b/triedb/pathdb/history_trienode_utils.go @@ -22,8 +22,8 @@ import ( "slices" ) -// sharedLen returns the length of the common prefix shared by a and b. -func sharedLen(a, b []byte) int { +// commonPrefixLen returns the length of the common prefix shared by a and b. +func commonPrefixLen(a, b []byte) int { n := min(len(a), len(b)) for i := range n { if a[i] != b[i] { @@ -74,15 +74,10 @@ func isAncestor(x, y uint16) bool { // isBitSet reports whether the bit at `index` in the byte slice `b` is set. func isBitSet(b []byte, index int) bool { - return b[index/8]&(1<<(index%8)) != 0 -} - -// isByteSet reports whether the byte at `index` in the byte slice `b` is set. -func isByteSet(b []byte, index int) bool { - return b[index] != 0 + return b[index/8]&(1<<(7-index%8)) != 0 } // setBit sets the bit at `index` in the byte slice `b` to 1. func setBit(b []byte, index int) { - b[index/8] |= 1 << (index % 8) + b[index/8] |= 1 << (7 - index%8) } diff --git a/triedb/pathdb/history_trienode_utils_test.go b/triedb/pathdb/history_trienode_utils_test.go index 23efb65d14..17eabb2a98 100644 --- a/triedb/pathdb/history_trienode_utils_test.go +++ b/triedb/pathdb/history_trienode_utils_test.go @@ -17,6 +17,7 @@ package pathdb import ( + "bytes" "testing" ) @@ -44,3 +45,37 @@ func TestIsAncestor(t *testing.T) { } } } + +func TestBitmapSet(t *testing.T) { + suites := []struct { + index int + expect []byte + }{ + { + 0, []byte{0b10000000, 0x0}, + }, + { + 1, []byte{0b01000000, 0x0}, + }, + { + 7, []byte{0b00000001, 0x0}, + }, + { + 8, []byte{0b00000000, 0b10000000}, + }, + { + 15, []byte{0b00000000, 0b00000001}, + }, + } + for _, tc := range suites { + var buf [2]byte + setBit(buf[:], tc.index) + + if !bytes.Equal(buf[:], tc.expect) { + t.Fatalf("bitmap = %v, want %v", buf, tc.expect) + } + if !isBitSet(buf[:], tc.index) { + t.Fatal("bit is not set") + } + } +}