From 94134263b5fc5def2769acff2557391bb02df9c5 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Wed, 28 May 2025 15:16:47 +0800 Subject: [PATCH] trie,triedb: store old node length Signed-off-by: jsvisa --- trie/trienode/node.go | 13 ++++++++++--- triedb/pathdb/difflayer_test.go | 6 +++--- triedb/pathdb/nodes.go | 8 ++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/trie/trienode/node.go b/trie/trienode/node.go index c83dc27cef..94e668311b 100644 --- a/trie/trienode/node.go +++ b/trie/trienode/node.go @@ -31,6 +31,8 @@ import ( type Node struct { Hash common.Hash // Node hash, empty for 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. @@ -43,13 +45,18 @@ func (n *Node) IsDeleted() bool { 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. -func New(hash common.Hash, blob []byte) *Node { - return &Node{Hash: hash, Blob: blob} +func New(hash common.Hash, blob []byte, length int) *Node { + return &Node{Hash: hash, Blob: blob, length: length} } // 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. type NodeWithPrev struct { diff --git a/triedb/pathdb/difflayer_test.go b/triedb/pathdb/difflayer_test.go index 83ed833486..dcd46afd7c 100644 --- a/triedb/pathdb/difflayer_test.go +++ b/triedb/pathdb/difflayer_test.go @@ -68,7 +68,7 @@ func benchmarkSearch(b *testing.B, depth int, total int) { var ( path = testrand.Bytes(32) 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 if npath == nil && depth == index { @@ -114,7 +114,7 @@ func BenchmarkPersist(b *testing.B) { var ( path = testrand.Bytes(32) 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 } @@ -152,7 +152,7 @@ func BenchmarkJournal(b *testing.B) { var ( path = testrand.Bytes(32) 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 } diff --git a/triedb/pathdb/nodes.go b/triedb/pathdb/nodes.go index f90bd0f01c..8c39bc524f 100644 --- a/triedb/pathdb/nodes.go +++ b/triedb/pathdb/nodes.go @@ -251,9 +251,9 @@ func (s *nodeSet) decode(r *rlp.Stream) error { // Account nodes for _, n := range entry.Nodes { 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 { - s.accountNodes[string(n.Path)] = trienode.NewDeleted() + s.accountNodes[string(n.Path)] = trienode.NewDeleted(0) } } } else { @@ -261,9 +261,9 @@ func (s *nodeSet) decode(r *rlp.Stream) error { subset := make(map[string]*trienode.Node) for _, n := range entry.Nodes { 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 { - subset[string(n.Path)] = trienode.NewDeleted() + subset[string(n.Path)] = trienode.NewDeleted(0) } } s.storageNodes[entry.Owner] = subset