Skip to content

tapdb+universe: implement initial version of universe ignore tree#1420

Merged
ffranr merged 4 commits intolightninglabs:mainfrom
Roasbeef:ignore-tree-impl
Apr 16, 2025
Merged

tapdb+universe: implement initial version of universe ignore tree#1420
ffranr merged 4 commits intolightninglabs:mainfrom
Roasbeef:ignore-tree-impl

Conversation

@Roasbeef
Copy link
Copy Markdown
Member

@Roasbeef Roasbeef commented Mar 5, 2025

In this PR, we implement the initial version of the universe Ignore Tree.

Along the way, we need a migration to the universe leaves, as the current unique index structure prevents us from adding two leaves that have the same script key, but distinct namespace (eg: transfer to a script key, then a burn of that).

With that in place, we're able to implement the ignore tree using the existing MS-SMT/Universe routines we already have in the database.

TODO

  • Consistent interfaces.
  • Unit tests for the database layer

@coveralls
Copy link
Copy Markdown

coveralls commented Mar 7, 2025

Pull Request Test Coverage Report for Build 14483915227

Details

  • 316 of 419 (75.42%) changed or added relevant lines in 6 files are covered.
  • 10 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.2%) to 28.6%

Changes Missing Coverage Covered Lines Changed/Added Lines %
cmd/merge-sql-schemas/main.go 0 2 0.0%
asset/asset.go 0 7 0.0%
universe/interface.go 0 8 0.0%
universe/ignore_records.go 81 121 66.94%
tapdb/ignore_tree.go 229 275 83.27%
Files with Coverage Reduction New Missed Lines %
asset/group_key.go 2 57.89%
tapgarden/caretaker.go 2 68.63%
tappsbt/create.go 2 26.74%
commitment/tap.go 4 71.59%
Totals Coverage Status
Change from base Build 14474635473: 0.2%
Covered Lines: 26254
Relevant Lines: 91798

💛 - Coveralls

@Roasbeef Roasbeef marked this pull request as ready for review March 9, 2025 02:20
@Roasbeef Roasbeef requested review from a team, GeorgeTsagk and ffranr and removed request for a team March 9, 2025 02:20
@Roasbeef
Copy link
Copy Markdown
Member Author

Roasbeef commented Mar 9, 2025

Removed from draft.

@Roasbeef Roasbeef removed the request for review from ffranr March 18, 2025 19:02
@Roasbeef
Copy link
Copy Markdown
Member Author

Pushed up a new version (CI was failing as we had two migration 30's). PTAL.

@lightninglabs-deploy
Copy link
Copy Markdown

@GeorgeTsagk: review reminder

@Roasbeef Roasbeef requested a review from guggero April 2, 2025 23:18
@Roasbeef
Copy link
Copy Markdown
Member Author

Roasbeef commented Apr 2, 2025

Will fix the collision issues in the burn tree PR.

This is ready for review (no changes since my last push to fix CI after the rebase).

Copy link
Copy Markdown
Contributor

@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.

Nice!
Most of my comments are around the SQL migration, the rest looks great.
Love all the rapid tests 💯

Comment thread tapdb/sqlc/migrations/000031_ignore_tree.up.sql Outdated
timestamp TIMESTAMP NOT NULL,
attempt_counter BIGINT NOT NULL DEFAULT 0,
sync_direction TEXT NOT NULL CHECK(sync_direction IN ('push', 'pull')),
proof_leaf_id BIGINT NOT NULL, -- FK constraint intentionally omitted for now.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of making yet another copy at the end, we could re-add the foreign key constraint to this table by using ALTER TABLE ADD FOREIGN KEY. That way we'd have a named constraint that we can just disable in the future.
The only downside is that it's not directly apparent on the table definition anymore...
And this table shouldn't be super large, so perhaps easier to just create it a couple of times, so just a thought.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

IIUC with sqlite, we can't do ALTER TABLE ADD FOREIGN KEY. We can only: rename a table/column, add/drop a table/column.

Need to revisit this, but the reason I needed to make a version w/o the constraint was that the migration fails otherwise either due to the rename of one of the tables above, or because we didn't update all the refs to point to the new version.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, yeah, SQLite doesn't support ALTER TABLE ... ADD CONSTRAINT: https://sqlite.org/omitted.html

, event_timestamp BIGINT NOT NULL DEFAULT 0);

CREATE TABLE universe_leaves (
CREATE TABLE "universe_leaves" (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hmm, is this up-to-date? I wonder where the double quotes come from...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah not sure why that was added. The way the tool works, it just reads the schema directly from the meta table in sqlite itself...

Perhaps there was some formatting or extra white space in the migration that resulted in this? Will fiddle with it a bit.

Comment thread universe/interface.go Outdated
Comment thread tapdb/ignore_tree.go
Comment thread tapdb/ignore_tree.go Outdated
@levmi levmi requested review from ffranr and removed request for GeorgeTsagk April 7, 2025 15:43
Copy link
Copy Markdown
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

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

I feel as if "tuple" is used as a a bit of a filler word here. We haven't used the word "coin" much, but this might be the right time to use it.

IgnoreCoin, AddCoins(...),

// AddCoin adds a new coin to the ignore tree.

I think the programming rule of thumb is "Name the thing, not the container.", basically encapsulation which makes it cheaper to change the underlying data type if we ever need to.

Comment thread universe/ignore_records.go
Comment thread tapdb/ignore_tree.go
@Roasbeef
Copy link
Copy Markdown
Member Author

I feel as if "tuple" is used as a a bit of a filler word here.

Tuple as in: https://en.wikipedia.org/wiki/Tuple.

It's not really a "coin" in the context we usually use that term within Bitcoin.

@ffranr
Copy link
Copy Markdown
Contributor

ffranr commented Apr 10, 2025

I feel as if "tuple" is used as a a bit of a filler word here.

Tuple as in: https://en.wikipedia.org/wiki/Tuple.

It's not really a "coin" in the context we usually use that term within Bitcoin.

I think a term like “asset coin” could be useful. Or something similar, I'm not sure what the convention is around "coin". Basically a higher level term for an asset leaf in a Taproot Asset MS-SMT—something that conveys spendability.

Comment thread universe/interface.go Outdated
Comment thread universe/interface.go
Comment thread tapdb/ignore_tree.go Outdated
Comment thread tapdb/ignore_tree.go Outdated
Comment thread universe/interface.go
Comment on lines +1212 to +1213
// TupleQueryResp is the response to a query for ignore tuples.
type TupleQueryResp = lfn.Result[lfn.Option[[]AuthenticatedIgnoreTuple]]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why include lfn.Option here? Wouldn't an empty slice signal the same thing as a None?

I think a similar sort of question can be asked for SumQueryResp below. I assume that if there are any leaves in the tree then the sum is always > 0, am I wrong? In which case None and 0 mean the same thing.

Copy link
Copy Markdown
Member Author

@Roasbeef Roasbeef Apr 10, 2025

Choose a reason for hiding this comment

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

I went back and forth a bit here: the None value makes it explicit when the query has no response value, while the empty slice is an implicit indicator.

I assume that if there are any leaves in the tree then the sum is always > 0, am I wrong? In which case None and 0 mean the same thing.

Same thing here re implicit vs explicit. If there's no tree, then a sum value isn't defined. An implicit interpretation is that if there's no tree, then the sum is zero.

I tend to prefer the explicit route.

Comment thread tapdb/sqlc/queries/assets.sql Outdated
Comment thread universe/ignore_records.go Outdated
@Roasbeef Roasbeef force-pushed the ignore-tree-impl branch 2 times, most recently from f04a2a8 to ee41b84 Compare April 16, 2025 03:14
@Roasbeef Roasbeef requested review from ffranr and guggero April 16, 2025 03:14
In this commit, we fix a bug in the existing final schema generation
script to ensure it includes named indexes.

From the sqlite docs:
> The sqlite_schema.type column will be one of the following text
strings: 'table', 'index', 'view', or 'trigger' according to the type of
object defined. The 'table' string is used for both ordinary and virtual
tables.
… table

This commit introduces a new migration (Migration 30) that modifies the
unique index on the `universe_leaves` table. The unique constraint is
updated from two columns (`minting_point`, `script_key_bytes`) to three
columns (`minting_point`, `script_key_bytes`, `leaf_node_namespace`).

Additionally, a new test case, `TestMigration30`, is added to verify the
behavior of the database before and after the migration. The test
ensures that inserting a duplicate row with the same `minting_point` and
`script_key_bytes` but a different `leaf_node_namespace` fails before
the migration and succeeds after the migration is applied.

Furthermore, the `universe_leaves` table schema is updated in the
generated SQL schema file to reflect the new unique constraint. Other
minor changes include the addition of new proof types in the `universe`
package and the introduction of new types related to ignore tuples,
enhancing the overall functionality of the codebase.
Copy link
Copy Markdown
Contributor

@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 🎉

timestamp TIMESTAMP NOT NULL,
attempt_counter BIGINT NOT NULL DEFAULT 0,
sync_direction TEXT NOT NULL CHECK(sync_direction IN ('push', 'pull')),
proof_leaf_id BIGINT NOT NULL, -- FK constraint intentionally omitted for now.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, yeah, SQLite doesn't support ALTER TABLE ... ADD CONSTRAINT: https://sqlite.org/omitted.html

@github-project-automation github-project-automation Bot moved this from 🏗 In progress to 👀 In review in Taproot-Assets Project Board Apr 16, 2025
@ffranr ffranr added this pull request to the merge queue Apr 16, 2025
Merged via the queue into lightninglabs:main with commit 2da076d Apr 16, 2025
18 checks passed
@github-project-automation github-project-automation Bot moved this from 👀 In review to ✅ Done in Taproot-Assets Project Board Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

6 participants