Skip to content
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): introduce HashDB interface and integrate into TrieDB with added iterators #4315

Open
wants to merge 8 commits into
base: refactor/client-db
Choose a base branch
from

Conversation

timwu20
Copy link
Contributor

@timwu20 timwu20 commented Nov 4, 2024

Changes

hashdb.HashDB

hashdb.HashDB is an interface introduced around a persistent store where the hash of the inserted value is used as the key. This interface is found prominently in substrate, and is used as the underlying storage of triedb.TrieDB. The integration of this interface as the underlying storage is included with the changes.

hashdb.Prefix is introduced to represent prefix partials where there could be an existence of a padded byte. This is useful to represent nibble offset for a partial key.

memorydb.MemoryDB

memorydb.MemoryDB is an implementation of HashDB with generic type parameters for the Hash, Hasher, Key, and KeyFunction. Variations of memorydb.MemoryDB using different key functions where the prefix is actually used vs not used are found in the substrate primitives and are used in PR #4318. One key feature that is of note is that values are actually reference counted, and not actually removed unless an equal amount of removes to the amount of gets.

triedb.TrieDBIterator and triedb.TrieDBKeyIterator

These two iterator types are introduced and used in PR #4318. They are simply exported iterators that will iterate through the pairs (key, values) or just the keys in lexographic order.

triedb.TrieDB.LookupFirstDescendant

This method is required to for TrieBackend work in PR #4318. It will look up the first descendant node from a given key.

Tests

go test -tags integration github.com/ChainSafe/gossamer

Issues

contributes to #3901

@timwu20 timwu20 changed the title refactor(pkg/trie/triedb): Introduce HashDB interface and integrate into TrieDB, with added iterators refactor(pkg/trie/triedb): Introduce HashDB interface and integrate into TrieDB and added iterators Nov 4, 2024
@timwu20 timwu20 changed the base branch from development to refactor/client-db November 4, 2024 22:59
@timwu20 timwu20 changed the title refactor(pkg/trie/triedb): Introduce HashDB interface and integrate into TrieDB and added iterators refactor(pkg/trie/triedb): Introduce HashDB interface and integrate into TrieDB with added iterators Nov 4, 2024
@timwu20 timwu20 force-pushed the tim/triedb-iterators-hashdb branch 3 times, most recently from 183d6d7 to a81ff1b Compare November 5, 2024 14:29
@timwu20 timwu20 marked this pull request as ready for review November 5, 2024 20:09
@timwu20 timwu20 changed the title refactor(pkg/trie/triedb): Introduce HashDB interface and integrate into TrieDB with added iterators refactor(pkg/trie/triedb): introduce HashDB interface and integrate into TrieDB with added iterators Nov 5, 2024
@timwu20 timwu20 changed the base branch from refactor/client-db to tim/refactor-triedb-caching-base November 6, 2024 03:04
@timwu20 timwu20 changed the base branch from tim/refactor-triedb-caching-base to tim/refactor-client-db-base November 6, 2024 03:05
@timwu20 timwu20 changed the base branch from tim/refactor-client-db-base to refactor/client-db November 6, 2024 14:40
internal/hash-db/hash_db.go Outdated Show resolved Hide resolved
internal/hash-db/hash_db.go Outdated Show resolved Hide resolved
// are referenced by the key to look them up in the trie. As multiple different tries can have
// different values under the same key, it is up to the cache implementation to ensure that the
// different values under the same key, it up to the cache implementation to ensure that the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't the "is" here correct?

pkg/trie/triedb/triedb.go Outdated Show resolved Hide resolved
@@ -358,15 +359,16 @@ type inspectResult struct {
changed bool
}

// inspect inspects the given node `stored` and calls the `inspector` function
// inspect inspects the given node stored and calls the inspector function
// then returns the new node and a boolean indicating if the node has changed
func (t *TrieDB[H, Hasher]) inspect(
stored StoredNode,
key *nibbles.Nibbles,
inspector func(Node, *nibbles.Nibbles) (action, error),
) (*inspectResult, error) {
// shallow copy since key will change offset through inspector
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// shallow copy since key will change offset through inspector

assuming key.Clone() does not return a shallow copy

pkg/trie/triedb/triedb.go Outdated Show resolved Hide resolved
@@ -108,9 +109,9 @@ func (vr newValueRef[H]) equal(other nodeValue) bool {
}

func NewValue[H hash.Hash](data []byte, threshold int) nodeValue {
if len(data) >= threshold {
if len(data) > threshold {
Copy link
Contributor

@dimartiro dimartiro Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, do we have a test for this?

Copy link
Contributor

@dimartiro dimartiro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants