Skip to content

Commit

Permalink
cr feedback, remove QueryItem
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Oct 9, 2024
1 parent ba4679e commit 4d95a0a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions pkg/trie/triedb/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
"github.com/ChainSafe/gossamer/pkg/trie/triedb/nibbles"
)

// Description of what kind of query will be made to the trie.
type Query[Item any] func(data []byte) Item

type TrieLookup[H hash.Hash, Hasher hash.Hasher[H], QueryItem any] struct {
type TrieLookup[H hash.Hash, Hasher hash.Hasher[H]] struct {
// db to query from
db db.DBGetter
// hash to start at
Expand All @@ -26,21 +23,21 @@ type TrieLookup[H hash.Hash, Hasher hash.Hasher[H], QueryItem any] struct {
recorder TrieRecorder
}

func NewTrieLookup[H hash.Hash, Hasher hash.Hasher[H], QueryItem any](
func NewTrieLookup[H hash.Hash, Hasher hash.Hasher[H]](
db db.DBGetter,
hash H,
cache Cache,
recorder TrieRecorder,
) TrieLookup[H, Hasher, QueryItem] {
return TrieLookup[H, Hasher, QueryItem]{
) TrieLookup[H, Hasher] {
return TrieLookup[H, Hasher]{
db: db,
hash: hash,
cache: cache,
recorder: recorder,
}
}

func (l *TrieLookup[H, Hasher, QueryItem]) lookupNode(
func (l *TrieLookup[H, Hasher]) lookupNode(
nibbleKey nibbles.Nibbles, fullKey []byte,
) (codec.EncodedNode, error) {
// Start from root node and going downwards
Expand Down Expand Up @@ -132,7 +129,7 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupNode(
}
}

func (l *TrieLookup[H, Hasher, QueryItem]) lookupValue(
func (l *TrieLookup[H, Hasher]) lookupValue(
fullKey []byte, keyNibbles nibbles.Nibbles,
) (value []byte, err error) {
node, err := l.lookupNode(keyNibbles, fullKey)
Expand All @@ -158,7 +155,7 @@ func (l *TrieLookup[H, Hasher, QueryItem]) lookupValue(

// fetchValue gets the value from the node, if it is inlined we can return it
// directly. But if it is hashed (V1) we have to look up for its value in the DB
func (l *TrieLookup[H, Hasher, QueryItem]) fetchValue(
func (l *TrieLookup[H, Hasher]) fetchValue(
prefix nibbles.Prefix, fullKey []byte, value codec.EncodedValue,
) ([]byte, error) {
switch v := value.(type) {
Expand All @@ -181,7 +178,7 @@ func (l *TrieLookup[H, Hasher, QueryItem]) fetchValue(
}
}

func (l *TrieLookup[H, Hasher, QueryItem]) recordAccess(access TrieAccess) {
func (l *TrieLookup[H, Hasher]) recordAccess(access TrieAccess) {
if l.recorder != nil {
l.recorder.Record(access)
}
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 @@ -16,7 +16,7 @@ func TestTrieDB_Lookup(t *testing.T) {
t.Run("root_not_exists_in_db", func(t *testing.T) {
db := newTestDB(t)
empty := runtime.BlakeTwo256{}.Hash([]byte{0})
lookup := NewTrieLookup[hash.H256, runtime.BlakeTwo256, []byte](db, empty, nil, nil)
lookup := NewTrieLookup[hash.H256, runtime.BlakeTwo256](db, empty, nil, nil)

value, err := lookup.lookupValue([]byte("test"), nibbles.NewNibbles([]byte("test")))
assert.Nil(t, value)
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 @@ -140,7 +140,7 @@ func (t *TrieDB[H, Hasher]) lookup(fullKey []byte, partialKey nibbles.Nibbles, h
var partialIdx uint
switch node := handle.(type) {
case persisted[H]:
lookup := NewTrieLookup[H, Hasher, []byte](t.db, node.hash, t.cache, t.recorder)
lookup := NewTrieLookup[H, Hasher](t.db, node.hash, t.cache, t.recorder)
val, err := lookup.lookupValue(fullKey, partialKey)
if err != nil {
return nil, err
Expand Down

0 comments on commit 4d95a0a

Please sign in to comment.