trie,triedb: store old node length

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-05-28 15:16:47 +08:00
parent fbac246f39
commit 94134263b5
3 changed files with 17 additions and 10 deletions

View file

@ -31,6 +31,8 @@ import (
type Node struct { type Node struct {
Hash common.Hash // Node hash, empty for deleted node Hash common.Hash // Node hash, empty for deleted node
Blob []byte // Encoded node blob, nil for the deleted node Blob []byte // Encoded node blob, nil for the deleted node
length int // The length of the original value of the node
} }
// Size returns the total memory size used by this node. // Size returns the total memory size used by this node.
@ -43,13 +45,18 @@ func (n *Node) IsDeleted() bool {
return len(n.Blob) == 0 return len(n.Blob) == 0
} }
// OriginLen returns the length of the original value of the node.
func (n *Node) OriginLen() int {
return n.length
}
// New constructs a node with provided node information. // New constructs a node with provided node information.
func New(hash common.Hash, blob []byte) *Node { func New(hash common.Hash, blob []byte, length int) *Node {
return &Node{Hash: hash, Blob: blob} return &Node{Hash: hash, Blob: blob, length: length}
} }
// NewDeleted constructs a node which is deleted. // NewDeleted constructs a node which is deleted.
func NewDeleted() *Node { return New(common.Hash{}, nil) } func NewDeleted(length int) *Node { return New(common.Hash{}, nil, length) }
// NodeWithPrev is a wrapper over Node by tracking the original value of node. // NodeWithPrev is a wrapper over Node by tracking the original value of node.
type NodeWithPrev struct { type NodeWithPrev struct {

View file

@ -68,7 +68,7 @@ func benchmarkSearch(b *testing.B, depth int, total int) {
var ( var (
path = testrand.Bytes(32) path = testrand.Bytes(32)
blob = testrand.Bytes(100) blob = testrand.Bytes(100)
node = trienode.New(crypto.Keccak256Hash(blob), blob) node = trienode.New(crypto.Keccak256Hash(blob), blob, 0)
) )
nodes[common.Hash{}][string(path)] = node nodes[common.Hash{}][string(path)] = node
if npath == nil && depth == index { if npath == nil && depth == index {
@ -114,7 +114,7 @@ func BenchmarkPersist(b *testing.B) {
var ( var (
path = testrand.Bytes(32) path = testrand.Bytes(32)
blob = testrand.Bytes(100) blob = testrand.Bytes(100)
node = trienode.New(crypto.Keccak256Hash(blob), blob) node = trienode.New(crypto.Keccak256Hash(blob), blob, 0)
) )
nodes[common.Hash{}][string(path)] = node nodes[common.Hash{}][string(path)] = node
} }
@ -152,7 +152,7 @@ func BenchmarkJournal(b *testing.B) {
var ( var (
path = testrand.Bytes(32) path = testrand.Bytes(32)
blob = testrand.Bytes(100) blob = testrand.Bytes(100)
node = trienode.New(crypto.Keccak256Hash(blob), blob) node = trienode.New(crypto.Keccak256Hash(blob), blob, 0)
) )
nodes[common.Hash{}][string(path)] = node nodes[common.Hash{}][string(path)] = node
} }

View file

@ -251,9 +251,9 @@ func (s *nodeSet) decode(r *rlp.Stream) error {
// Account nodes // Account nodes
for _, n := range entry.Nodes { for _, n := range entry.Nodes {
if len(n.Blob) > 0 { if len(n.Blob) > 0 {
s.accountNodes[string(n.Path)] = trienode.New(crypto.Keccak256Hash(n.Blob), n.Blob) s.accountNodes[string(n.Path)] = trienode.New(crypto.Keccak256Hash(n.Blob), n.Blob, 0)
} else { } else {
s.accountNodes[string(n.Path)] = trienode.NewDeleted() s.accountNodes[string(n.Path)] = trienode.NewDeleted(0)
} }
} }
} else { } else {
@ -261,9 +261,9 @@ func (s *nodeSet) decode(r *rlp.Stream) error {
subset := make(map[string]*trienode.Node) subset := make(map[string]*trienode.Node)
for _, n := range entry.Nodes { for _, n := range entry.Nodes {
if len(n.Blob) > 0 { if len(n.Blob) > 0 {
subset[string(n.Path)] = trienode.New(crypto.Keccak256Hash(n.Blob), n.Blob) subset[string(n.Path)] = trienode.New(crypto.Keccak256Hash(n.Blob), n.Blob, 0)
} else { } else {
subset[string(n.Path)] = trienode.NewDeleted() subset[string(n.Path)] = trienode.NewDeleted(0)
} }
} }
s.storageNodes[entry.Owner] = subset s.storageNodes[entry.Owner] = subset