Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 11, 2024
1 parent 9e30c89 commit bd9f03b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 49 deletions.
12 changes: 6 additions & 6 deletions pkg/trie/triedb/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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].
//
Expand Down
12 changes: 3 additions & 9 deletions pkg/trie/triedb/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()...)
Expand Down Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
62 changes: 30 additions & 32 deletions pkg/trie/triedb/node_owned.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -191,25 +191,23 @@ 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 {
panic(err)
}
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 {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trie/triedb/triedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit bd9f03b

Please sign in to comment.