mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 20:26:41 +00:00
triedb/pathdb: address comments from guillaume
This commit is contained in:
parent
0fca084df0
commit
0a8ee5238b
6 changed files with 47 additions and 17 deletions
|
|
@ -28,7 +28,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
indexBlockDescSize = 14 // The size of index block descriptor
|
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
|
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 {
|
func (b *blockWriter) estimateFull(ext []uint16) bool {
|
||||||
size := 8 + 2*len(ext)
|
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
|
// last returns the last element in the block. It should only be called when
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ func (f extFilter) contains(bitmap []byte) (bool, error) {
|
||||||
}
|
}
|
||||||
// Check descendants: the presence of any descendant implicitly
|
// Check descendants: the presence of any descendant implicitly
|
||||||
// represents a mutation of its ancestor.
|
// 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:
|
default:
|
||||||
return false, fmt.Errorf("unsupported bitmap size %d", len(bitmap))
|
return false, fmt.Errorf("unsupported bitmap size %d", len(bitmap))
|
||||||
|
|
|
||||||
|
|
@ -208,7 +208,7 @@ func (h *trienodeHistory) encode() ([]byte, []byte, []byte, error) {
|
||||||
restarts = append(restarts, internalValOffset)
|
restarts = append(restarts, internalValOffset)
|
||||||
prefixLen = 0
|
prefixLen = 0
|
||||||
} else {
|
} else {
|
||||||
prefixLen = sharedLen(prevKey, key)
|
prefixLen = commonPrefixLen(prevKey, key)
|
||||||
}
|
}
|
||||||
value := h.nodes[owner][path]
|
value := h.nodes[owner][path]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -580,8 +580,8 @@ func TestTrienodeHistoryReaderIterator(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSharedLen tests the sharedLen helper function
|
// TestCommonPrefixLen tests the commonPrefixLen helper function
|
||||||
func TestSharedLen(t *testing.T) {
|
func TestCommonPrefixLen(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
a, b []byte
|
a, b []byte
|
||||||
expected int
|
expected int
|
||||||
|
|
@ -610,13 +610,13 @@ func TestSharedLen(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
result := sharedLen(test.a, test.b)
|
result := commonPrefixLen(test.a, test.b)
|
||||||
if result != test.expected {
|
if result != test.expected {
|
||||||
t.Errorf("Test %d: sharedLen(%q, %q) = %d, expected %d",
|
t.Errorf("Test %d: sharedLen(%q, %q) = %d, expected %d",
|
||||||
i, test.a, test.b, result, test.expected)
|
i, test.a, test.b, result, test.expected)
|
||||||
}
|
}
|
||||||
// Test commutativity
|
// Test commutativity
|
||||||
resultReverse := sharedLen(test.b, test.a)
|
resultReverse := commonPrefixLen(test.b, test.a)
|
||||||
if result != resultReverse {
|
if result != resultReverse {
|
||||||
t.Errorf("Test %d: sharedLen is not commutative: sharedLen(a,b)=%d, sharedLen(b,a)=%d",
|
t.Errorf("Test %d: sharedLen is not commutative: sharedLen(a,b)=%d, sharedLen(b,a)=%d",
|
||||||
i, result, resultReverse)
|
i, result, resultReverse)
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,8 @@ import (
|
||||||
"slices"
|
"slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// sharedLen returns the length of the common prefix shared by a and b.
|
// commonPrefixLen returns the length of the common prefix shared by a and b.
|
||||||
func sharedLen(a, b []byte) int {
|
func commonPrefixLen(a, b []byte) int {
|
||||||
n := min(len(a), len(b))
|
n := min(len(a), len(b))
|
||||||
for i := range n {
|
for i := range n {
|
||||||
if a[i] != b[i] {
|
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.
|
// isBitSet reports whether the bit at `index` in the byte slice `b` is set.
|
||||||
func isBitSet(b []byte, index int) bool {
|
func isBitSet(b []byte, index int) bool {
|
||||||
return b[index/8]&(1<<(index%8)) != 0
|
return b[index/8]&(1<<(7-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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setBit sets the bit at `index` in the byte slice `b` to 1.
|
// setBit sets the bit at `index` in the byte slice `b` to 1.
|
||||||
func setBit(b []byte, index int) {
|
func setBit(b []byte, index int) {
|
||||||
b[index/8] |= 1 << (index % 8)
|
b[index/8] |= 1 << (7 - index%8)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package pathdb
|
package pathdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue