diff --git a/pkg/trie/triedb/cache.go b/pkg/trie/triedb/cache.go index 90113c223b..1bfe2a5c40 100644 --- a/pkg/trie/triedb/cache.go +++ b/pkg/trie/triedb/cache.go @@ -22,16 +22,16 @@ func NewCachedValue[H any, CV CachedValues[H]](cv CV) CachedValue[H] { // The value doesn't exist in the trie. type NonExistingCachedValue[H any] struct{} -func (NonExistingCachedValue[H]) data() []byte { return nil } -func (NonExistingCachedValue[H]) hash() *H { return nil } +func (NonExistingCachedValue[H]) data() []byte { return nil } //nolint:unused +func (NonExistingCachedValue[H]) hash() *H { return nil } //nolint:unused // The hash is cached and not the data because it was not accessed. type ExistingHashCachedValue[H any] struct { Hash H } -func (ExistingHashCachedValue[H]) data() []byte { return nil } -func (ehcv ExistingHashCachedValue[H]) hash() *H { return &ehcv.Hash } +func (ExistingHashCachedValue[H]) data() []byte { return nil } //nolint:unused +func (ehcv ExistingHashCachedValue[H]) hash() *H { return &ehcv.Hash } //nolint:unused // The value exists in the trie. type ExistingCachedValue[H any] struct { @@ -41,8 +41,8 @@ type ExistingCachedValue[H any] struct { Data []byte } -func (ecv ExistingCachedValue[H]) data() []byte { return ecv.Data } -func (ecv ExistingCachedValue[H]) hash() *H { return &ecv.Hash } +func (ecv ExistingCachedValue[H]) data() []byte { return ecv.Data } //nolint:unused +func (ecv ExistingCachedValue[H]) hash() *H { return &ecv.Hash } //nolint:unused // A cache that can be used to speed-up certain operations when accessing [TrieDB]. // diff --git a/pkg/trie/triedb/lookup.go b/pkg/trie/triedb/lookup.go index 003d086e52..25923df597 100644 --- a/pkg/trie/triedb/lookup.go +++ b/pkg/trie/triedb/lookup.go @@ -128,7 +128,7 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupWithCache( data, err := loadValueOwned[H]( // If we only have the hash cached, this can only be a value node. // For inline nodes we cache them directly as [ExistingCachedValue]. - ValueOwned[H](ValueOwnedNode[H]{Hash: cachedVal.Hash}), + ValueOwnedNode[H](cachedVal), nibbleKey.OriginalDataPrefix(), fullKey, l.cache, @@ -460,10 +460,7 @@ func loadValueOwned[H hash.Hash]( if recorder != nil { recorder.Record(InlineValueAccess{fullKey}) } - return valueHash[H]{ - Value: v.Value, - Hash: v.Hash, - }, nil + return valueHash[H](v), nil case ValueOwnedNode[H]: node, err := cache.GetOrInsertNode(v.Hash, func() (NodeOwned[H], error) { prefixedKey := append(prefix.JoinedBytes(), v.Hash.Bytes()...) @@ -630,10 +627,7 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupHashWithCache( // the hash. This is done to prevent requiring to re-record this key. recorder.Record(InlineValueAccess{FullKey: fullKey}) } - return valueHash[H]{ - Value: value.Value, - Hash: value.Hash, - }, nil + return valueHash[H](value), nil case ValueOwnedNode[H]: if recorder != nil { recorder.Record(HashAccess{FullKey: fullKey}) diff --git a/pkg/trie/triedb/lookup_test.go b/pkg/trie/triedb/lookup_test.go index 1fae443896..35d1cc873f 100644 --- a/pkg/trie/triedb/lookup_test.go +++ b/pkg/trie/triedb/lookup_test.go @@ -77,7 +77,7 @@ func Test_TrieLookup_lookupValueWithCache(t *testing.T) { bytes, err := lookup.lookupWithCache([]byte(k), nibbles.NewNibbles([]byte(k))) require.NoError(t, err) require.NotNil(t, bytes) - require.Equal(t, []byte(v), *bytes) + require.Equal(t, v, *bytes) } } diff --git a/pkg/trie/triedb/node_owned.go b/pkg/trie/triedb/node_owned.go index 40952b2422..96b0900cc5 100644 --- a/pkg/trie/triedb/node_owned.go +++ b/pkg/trie/triedb/node_owned.go @@ -33,14 +33,12 @@ type ( } ) -func (vo ValueOwnedInline[H]) data() []byte { return vo.Value } -func (vo ValueOwnedNode[H]) data() []byte { return nil } -func (vo ValueOwnedInline[H]) dataHash() *H { return &vo.Hash } -func (vo ValueOwnedNode[H]) dataHash() *H { return &vo.Hash } +func (vo ValueOwnedInline[H]) data() []byte { return vo.Value } //nolint:unused +func (vo ValueOwnedNode[H]) data() []byte { return nil } //nolint:unused +func (vo ValueOwnedInline[H]) dataHash() *H { return &vo.Hash } //nolint:unused +func (vo ValueOwnedNode[H]) dataHash() *H { return &vo.Hash } //nolint:unused func (vo ValueOwnedInline[H]) EncodedValue() codec.EncodedValue { return codec.InlineValue(vo.Value) } -func (vo ValueOwnedNode[H]) EncodedValue() codec.EncodedValue { - return codec.HashedValue[H]{Hash: vo.Hash} -} +func (vo ValueOwnedNode[H]) EncodedValue() codec.EncodedValue { return codec.HashedValue[H](vo) } func newValueOwnedFromEncodedValue[H hash.Hash, Hasher hash.Hasher[H]](encVal codec.EncodedValue) ValueOwned[H] { switch encVal := encVal.(type) { @@ -79,7 +77,7 @@ type ( ) func (nho NodeHandleOwnedHash[H]) ChildReference() ChildReference { - return HashChildReference[H]{Hash: nho.Hash} + return HashChildReference[H](nho) } func (nho NodeHandleOwnedInline[H]) ChildReference() ChildReference { encoded := nho.NodeOwned.encoded() @@ -90,7 +88,9 @@ func (nho NodeHandleOwnedInline[H]) ChildReference() ChildReference { return InlineChildReference(encoded) } -func newNodeHandleOwnedFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]](mv codec.MerkleValue) (NodeHandleOwned, error) { +func newNodeHandleOwnedFromMerkleValue[H hash.Hash, Hasher hash.Hasher[H]]( + mv codec.MerkleValue, +) (NodeHandleOwned, error) { switch mv := mv.(type) { case codec.HashedNode[H]: return NodeHandleOwnedHash[H](mv), nil @@ -156,33 +156,33 @@ type ( } ) -func (NodeOwnedEmpty[H]) data() []byte { return nil } -func (no NodeOwnedLeaf[H]) data() []byte { return no.Value.data() } -func (no NodeOwnedBranch[H]) data() []byte { +func (NodeOwnedEmpty[H]) data() []byte { return nil } //nolint:unused +func (no NodeOwnedLeaf[H]) data() []byte { return no.Value.data() } //nolint:unused +func (no NodeOwnedBranch[H]) data() []byte { //nolint:unused if no.Value != nil { return no.Value.data() } return nil } -func (no NodeOwnedValue[H]) data() []byte { return no.Value } +func (no NodeOwnedValue[H]) data() []byte { return no.Value } //nolint:unused -func (NodeOwnedEmpty[H]) dataHash() *H { return nil } -func (no NodeOwnedLeaf[H]) dataHash() *H { return no.Value.dataHash() } -func (no NodeOwnedBranch[H]) dataHash() *H { +func (NodeOwnedEmpty[H]) dataHash() *H { return nil } //nolint:unused +func (no NodeOwnedLeaf[H]) dataHash() *H { return no.Value.dataHash() } //nolint:unused +func (no NodeOwnedBranch[H]) dataHash() *H { //nolint:unused if no.Value != nil { return no.Value.dataHash() } return nil } -func (no NodeOwnedValue[H]) dataHash() *H { return &no.Hash } +func (no NodeOwnedValue[H]) dataHash() *H { return &no.Hash } //nolint:unused -func (NodeOwnedEmpty[H]) children() []child[H] { return nil } -func (no NodeOwnedLeaf[H]) children() []child[H] { return nil } -func (no NodeOwnedBranch[H]) children() []child[H] { +func (NodeOwnedEmpty[H]) children() []child[H] { return nil } //nolint:unused +func (no NodeOwnedLeaf[H]) children() []child[H] { return nil } //nolint:unused +func (no NodeOwnedBranch[H]) children() []child[H] { //nolint:unused r := []child[H]{} for i, ch := range no.Children { if ch != nil { - nibble := uint8(i) + nibble := uint8(i) //nolint:gosec r = append(r, child[H]{ nibble: &nibble, NodeHandleOwned: ch, @@ -191,17 +191,15 @@ func (no NodeOwnedBranch[H]) children() []child[H] { } return r } -func (no NodeOwnedValue[H]) children() []child[H] { return nil } +func (no NodeOwnedValue[H]) children() []child[H] { return nil } //nolint:unused -func (NodeOwnedEmpty[H]) partialKey() *nibbles.NibbleSlice { return nil } -func (no NodeOwnedLeaf[H]) partialKey() *nibbles.NibbleSlice { return &no.PartialKey } -func (no NodeOwnedBranch[H]) partialKey() *nibbles.NibbleSlice { return &no.PartialKey } -func (no NodeOwnedValue[H]) partialKey() *nibbles.NibbleSlice { return nil } +func (NodeOwnedEmpty[H]) partialKey() *nibbles.NibbleSlice { return nil } //nolint:unused +func (no NodeOwnedLeaf[H]) partialKey() *nibbles.NibbleSlice { return &no.PartialKey } //nolint:unused +func (no NodeOwnedBranch[H]) partialKey() *nibbles.NibbleSlice { return &no.PartialKey } //nolint:unused +func (no NodeOwnedValue[H]) partialKey() *nibbles.NibbleSlice { return nil } //nolint:unused -func (NodeOwnedEmpty[H]) encoded() []byte { - return []byte{EmptyTrieBytes} -} -func (no NodeOwnedLeaf[H]) encoded() []byte { +func (NodeOwnedEmpty[H]) encoded() []byte { return []byte{EmptyTrieBytes} } //nolint:unused +func (no NodeOwnedLeaf[H]) encoded() []byte { //nolint:unused encodingBuffer := bytes.NewBuffer(nil) err := NewEncodedLeaf(no.PartialKey.Right(), no.PartialKey.Len(), no.Value.EncodedValue(), encodingBuffer) if err != nil { @@ -209,7 +207,7 @@ func (no NodeOwnedLeaf[H]) encoded() []byte { } return encodingBuffer.Bytes() } -func (no NodeOwnedBranch[H]) encoded() []byte { +func (no NodeOwnedBranch[H]) encoded() []byte { //nolint:unused encodingBuffer := bytes.NewBuffer(nil) children := [16]ChildReference{} for i, ch := range no.Children { @@ -233,7 +231,7 @@ func (no NodeOwnedBranch[H]) encoded() []byte { } return encodingBuffer.Bytes() } -func (no NodeOwnedValue[H]) encoded() []byte { return no.Value } +func (no NodeOwnedValue[H]) encoded() []byte { return no.Value } //nolint:unused func newNodeOwnedFromNode[H hash.Hash, Hasher hash.Hasher[H]](n codec.EncodedNode) (NodeOwned[H], error) { switch n := n.(type) { diff --git a/pkg/trie/triedb/triedb.go b/pkg/trie/triedb/triedb.go index 3c31c56faf..a76ab494c2 100644 --- a/pkg/trie/triedb/triedb.go +++ b/pkg/trie/triedb/triedb.go @@ -1142,7 +1142,7 @@ func (t *TrieDB[H, Hasher]) cacheValue(fullKey []byte, value []byte, hash H) { }, nil }) if err != nil { - panic("this should never happend") + panic("this should never happen") } if node != nil { val = node.data()