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

mssmt: add transactional tree storage interface and sqlite implementation #90

Merged
merged 9 commits into from
Aug 30, 2022

Conversation

bhandras
Copy link
Member

@bhandras bhandras commented Aug 19, 2022

This PR changes the mssmt.Store interface to allow transactional updates and reads of underlying trees. We also add an sqlite based implementation.

TODO:
- fix transaction contexts, currently all are simply context.TODO()
- make sure failed transactions roll back the tree roots in all cases
- improve test coverage
- more experiments with the schema to allow better performance

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

Great work!

Still in a draft state, so did a light pass, mainly focusing on the SQL/DB aspects. I love how elegant the recursive CTE query is here for fetching a sub-tree. I think I grok it mostly, but need to let it marinate in my mind a bit longer. After looking at the query (namely limit 3), I think I see what you mean here about if the "unrolled" join would be faster or not. The query seems pretty flexible as is (can parameterize 3 to allow it to fetch a larger sub tree), and I think if we wanted to get really crazy, we could use SUBSTR to implement a recursive branch fetching to create merkle proofs.

mssmt/compacted_tree.go Outdated Show resolved Hide resolved
mssmt/node.go Outdated Show resolved Hide resolved
mssmt/store.go Show resolved Hide resolved
tarodb/sqlite/migrations/000004_mssmt.up.sql Show resolved Hide resolved
tarodb/sqlite/queries/mssmt.sql Show resolved Hide resolved
tarodb/sqlite/queries/mssmt.sql Show resolved Hide resolved
tarodb/sqlite/queries/mssmt.sql Show resolved Hide resolved
tarodb/mssmt.go Outdated Show resolved Hide resolved
isLeft := bytes.Equal(row.HashKey, lHashKey)
isRight := bytes.Equal(row.HashKey, rHashKey)

if !isLeft && !isRight {
Copy link
Member

Choose a reason for hiding this comment

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

Will this actually ever happen given the query limits 3 by so should only ever return the immediate left+right child? Or is this related to the compacted leaf?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, so I'll cover this with additional unit test if we approve the current recursive schema. Since we don't store the default nodes in the table (which we may since it's making everything more elegant), we could end up fetching the root, it's child and the child of the child in which case we want to ignore that next level child (and just use the default branch instead).

Copy link
Member Author

Choose a reason for hiding this comment

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

This is due to how recursive cte query works, it won't just anchor the two children to the starter query, but will do so recursively too.

Copy link
Member

Choose a reason for hiding this comment

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

we could end up fetching the root, it's child and the child of the child in which case we want to ignore that next level child (and just use the default branch instead).

Gotcha that makes sense. If this is the case, then I think the idea of explicitly passing in a level accumulator into the CTE would help us have a tighter grasp on the recursion?

mssmt/compacted_tree.go Show resolved Hide resolved
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Pretty amazing stuff 🤩
Did a quick review mostly out of curiosity.

mssmt/compacted_tree.go Outdated Show resolved Hide resolved
sum BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS mssmt_nodes_hash_key_idx ON mssmt_nodes (hash_key);
Copy link
Member

Choose a reason for hiding this comment

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

Since the hash key is a PRIMARY KEY, that already creates an index.

Copy link
Member

Choose a reason for hiding this comment

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

We might want to add indexes for l_hash_key and r_hash_key as they're used in the recursive WHERE clause.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, tried adding the indexes for the left/right keys too, but weirdly the query became slower. Still investigating why.

@bhandras bhandras force-pushed the tree-storage branch 9 times, most recently from 3fadc17 to 3633f71 Compare August 25, 2022 20:01
@bhandras bhandras marked this pull request as ready for review August 25, 2022 20:02
@bhandras
Copy link
Member Author

Dear reviewers, the PR is now ready for review 🚀

Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Great work on this! I love how simple the SQL part is for such a non-trivial data structure. But I guess that's the beauty of recursive structures.

commitment/asset.go Outdated Show resolved Hide resolved
mssmt/bench_test.go Outdated Show resolved Hide resolved
mssmt/store.go Show resolved Hide resolved
mssmt/store.go Show resolved Hide resolved
mssmt/tree.go Show resolved Hide resolved
mssmt/proof.go Outdated Show resolved Hide resolved
tarodb/sqlite/migrations/000004_mssmt.down.sql Outdated Show resolved Hide resolved
tarodb/sqlite/migrations/000004_mssmt.up.sql Show resolved Hide resolved
tarodb/mssmt.go Outdated Show resolved Hide resolved
mssmt/tree_test.go Outdated Show resolved Hide resolved
Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

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

LGTM, great work 🎉

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM🍸

Just needs a rebase!

mssmt/tree_test.go Outdated Show resolved Hide resolved
mssmt/tree_test.go Show resolved Hide resolved
@bhandras bhandras merged commit ca90ff2 into lightninglabs:main Aug 30, 2022
dstadulis pushed a commit that referenced this pull request May 16, 2023
tarocli: fix deleting universes from federation
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.

None yet

3 participants