-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(pkg/trie/triedb): implement generics, refactor nibbles, revise iterator #4221
base: tim/update-golangci-lint
Are you sure you want to change the base?
Conversation
6759481
to
cc55742
Compare
1f89c69
to
895baf8
Compare
…, remove caching temporarily
895baf8
to
1a55c3e
Compare
3ec8390
to
665a370
Compare
665a370
to
9c82571
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments but overall ok!
Thanks for spending time working on this changes Tim! I tried to keep the TrieDB
as simple as possible but it seems that it wasn't enough 💔
@@ -23,11 +24,11 @@ type ( | |||
// InlineNode contains bytes of the encoded node data | |||
InlineNode []byte | |||
// HashedNode contains a hash used to lookup in db for encoded node data | |||
HashedNode common.Hash | |||
HashedNode[H any] struct{ Hash H } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not?
HashedNode[H any] struct{ Hash H } | |
HashedNode[H hash.Hash] struct{ Hash H } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I lean towards trying to be succinct and generic as possible per type. I think it's understood that hash.Hash
is used exclusively in TrieDB
, but it doesn't necessarily mean that we need it to be for HashedNode
. If you feel strongly the other way I'm open to changing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmm since we are always storing hashes in HashedNode
I would say that it will be right to use the hash.Hash
instead of any
to prevent issues and also being consistent with HashedValue[H hash.Hash]
. What do you think? do you see any issue?
pkg/trie/triedb/lookup.go
Outdated
} | ||
|
||
func NewTrieLookup(db db.DBGetter, hash common.Hash, cache cache.TrieCache, recorder *Recorder) TrieLookup { | ||
return TrieLookup{ | ||
func NewTrieLookup[H hash.Hash, Hasher hash.Hasher[H], QueryItem any]( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where are you using the QueryItem
? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops! 😅 This is part of the caching changes coming next. I'll remove on this branch.
@@ -143,32 +150,38 @@ func inMemoryFetchedValue(value nodeValue, prefix []byte, db db.DBGetter) ([]byt | |||
} | |||
} | |||
|
|||
type NodeTypes[H hash.Hash] interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we using this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No not yet. I think it's good to semantically describe what the Node
types are.
@@ -18,20 +19,22 @@ type nodeHandle interface { | |||
} | |||
|
|||
type ( | |||
nodeHandleHash common.Hash | |||
nodeHandleHash[H any] struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not?
nodeHandleHash[H any] struct { | |
nodeHandleHash[H hash.Hash] struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -63,34 +62,34 @@ type ( | |||
NewStoredNode struct { | |||
node Node | |||
} | |||
CachedStoredNode struct { | |||
CachedStoredNode[H any] struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not hash.Hash
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return n.node | ||
} | ||
|
||
// nodeStorage is a struct that contains all the temporal nodes that are stored | ||
// in the trieDB before being written to the backed db | ||
type nodeStorage struct { | ||
type nodeStorage[H any] struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type nodeStorage[H any] struct { | |
type nodeStorage[H hash.Hash] struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You did all the heavy lifting @dimartiro! The fact that I have something to integrate with and contribute to is a testament to your hard work. 💪 |
Changes
hash.Hash
andhash.Hasher
interface constraints intotriedb
package.runtime.Hash
andruntime.Hasher
already ondevelopment
.hash.H256
for the hash andruntime.BlakeTwo256
as the hasher.triedb
. IntroducesNibbleSlice
,Nibbles
, andLeftNibbles
. Each of these introduced types has their own distinct public interface and are used in different components withintriedb
.Nibbles
is analogous totrie_db::nibble::NibbleSlice
. Primarily used to avoid costly slice manipulation and unnecessary copying of underlying bytes. Able to represent different nibble starting points, but not mutable to be able to reconstruct full keys.NibbleSlice
is analogous totrie_db::nibble::NibbleVec
. Mutable in the sense you can push and pop nibbles as well as truncate. Used to reconstruct full keys.LeftNibbles
is analogous totrie_db::nibble::LeftNibbleSlice
. Used in proof generation and verification. SinceNibbleSlice
andNIbbles
are right aligned it makes it more expensive to truncate.LeftNibbles
left aligns the nibbles which facilitates faster truncation.TrieDBRawIterator
which is imported by the state machine primitives in substrate. This is embedded in other types to be able to iterate and return expected types. TherawItem
introduced that is returned byrawIterator.nextRawItem
includes the partial key asNibbleSlice
, hash of the node, and an in-memory type that fulfills thecodec.EncodedNode
interface (Branch
orLeaf
).Notes
Cache
interface to avoid too many code changes to how we constructTrieDB
s and the options we provide it.triedb
package.Tests
go test -tags integration github.com/ChainSafe/gossamer
Issues
contributes to #4157