Skip to content

Conversation

Roasbeef
Copy link
Member

@Roasbeef Roasbeef commented May 3, 2025

In this PR, we add a concrete implementation of the two fundamental interfaces that will drive the state machine implemented in #1464.

We start with a state machine on disk for a given group key that also points to the latest supply commitment. When we want to add new elements to the supply tree, we create a new state transition, and then queue pending updates associated with this state transition. Once we've signed and broadcasted the commitment transaction, we then apply the state transition: insert all the updates into the tree, update the state machine to point to that new tree, and finalize the state transition.

See the readme for more details.

@levmi levmi moved this from 🆕 New to 🏗 In progress in Taproot-Assets Project Board May 5, 2025
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from 4778c90 to d718094 Compare May 5, 2025 22:46
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch 2 times, most recently from 522c3ee to d786104 Compare May 5, 2025 23:04
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from d718094 to 1db2104 Compare May 5, 2025 23:08
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from d786104 to af5f8f5 Compare May 5, 2025 23:13
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch 3 times, most recently from e67b58f to 6b99f70 Compare May 5, 2025 23:54
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from af5f8f5 to 64516c9 Compare May 5, 2025 23:54
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from 6b99f70 to 5056daa Compare May 6, 2025 01:19
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 64516c9 to 6e0b6a4 Compare May 6, 2025 01:19
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from 5056daa to 5c20025 Compare May 6, 2025 01:27
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 6e0b6a4 to aa782c8 Compare May 6, 2025 01:27
@levmi levmi added this to the v0.7 milestone May 8, 2025
@guggero guggero self-requested a review May 28, 2025 06:56
@ffranr ffranr self-requested a review May 29, 2025 22:25
Comment on lines 105 to 110
-- Table storing individual update events associated with a pending transition.
CREATE TABLE supply_update_events (
event_id INTEGER PRIMARY KEY,

-- Reference to the state transition this event is part of.
transition_id BIGINT NOT NULL REFERENCES supply_commit_transitions(transition_id) ON DELETE CASCADE,
Copy link
Contributor

Choose a reason for hiding this comment

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

an update event can't be persisted unless it references an existing transition, is that correct?
If so, then if we're waiting for tx conf then new update events can't be persisted?

If true, then I don't think it matters a great deal, but from a UX perspective, we will need to return an error to the user along the lines of: unconfirmed supply tx, can't accept new asset ignore right now.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a great insight. As of right now, if everything is spun up together and a user attempts to add a new update while we're creating a new commitment on-disk, then this'll fail.

I think we can fix this just by removing the NOT NULL here. We'll need some additional follow ups in the state machine though:

  • Any time an update comes through in a state, attempt to insert it to disk w/ a nil transition_id.
  • Once we circle back to the default state, then read all the "dangling" update from disk, and assign them to a state transition (in a single db txn).

Alternatively, we can create some other table, then re-insert them in the default state.

I can either do this in another PR (as it affects this one, and the next), or do the incremental change here. Which is preferred?

Copy link
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.

Great stuff! Did a first pass to load some context, will continue with the other PRs in the saga.

// NULL (e.g., initial commit before ApplyStateTransition ran), use the
// empty root.
var rootNode *mssmt.BranchNode
if commit.SupplyRootHash == nil || !commit.SupplyRootSum.Valid {
Copy link
Contributor

Choose a reason for hiding this comment

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

Check length instead of nil?

// its associated chain confirmation details.
func fetchCommitment(ctx context.Context, db SupplyCommitStore,
commitID sql.NullInt64, groupKeyBytes []byte,
) (lfn.Option[supplycommit.RootCommitment], lfn.Option[commitmentChainInfo], error) { //nolint:lll
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the root commitment here really need to be an option? Doesn't look like it's ever None in a non-error case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have it as an option as in supplycommit.SupplyStateTransition, it's an option. It's an option here as the very first time we go to create a state transition, OldCommitment won't yet exist.

We can return a value of None in the very fist error case: if we query and it doesn't exist (ErrNoRows), then noneRootCommit with a nil error is returned.

}
```

## State Machine Persistence Logic
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice doc!

@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from aa782c8 to 36e3753 Compare June 9, 2025 23:40
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch 2 times, most recently from bf61350 to 16ae70c Compare June 10, 2025 00:31
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 36e3753 to 1c17470 Compare June 10, 2025 00:31
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from 16ae70c to 67728da Compare June 10, 2025 01:12
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 1c17470 to b5f3d5e Compare June 10, 2025 01:12
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 8b06b69 to b8988d3 Compare June 21, 2025 01:22
@lightninglabs-deploy
Copy link

@Roasbeef, remember to re-request review from reviewers when ready

@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from 37bdcfa to b5a6c5e Compare June 25, 2025 00:27
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from b8988d3 to 85109d9 Compare June 25, 2025 00:27
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from b5a6c5e to c59b408 Compare June 25, 2025 02:00
@Roasbeef Roasbeef changed the base branch from universe-supply-trees to main June 25, 2025 02:00
@Roasbeef
Copy link
Member Author

Changed the base to target main.

@ffranr
Copy link
Contributor

ffranr commented Jun 25, 2025

@Roasbeef here's a rebased version of this PR with additional fixups to address the unit test failures and SQL migration conflict:
https://github.com/ffranr/taproot-assets/commits/wip/1534-ignore-tree/supply-commit-machine/

@guggero guggero requested review from guggero and ffranr June 26, 2025 15:45
Copy link
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 pending existing commits and rebase (and DB migration file re-numbering) 🎉

@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from c59b408 to 99529ae Compare June 26, 2025 22:30
@coveralls
Copy link

coveralls commented Jun 26, 2025

Pull Request Test Coverage Report for Build 15915388434

Details

  • 766 of 1065 (71.92%) changed or added relevant lines in 4 files are covered.
  • 25 unchanged lines in 7 files lost coverage.
  • Overall coverage increased (+0.5%) to 39.222%

Changes Missing Coverage Covered Lines Changed/Added Lines %
universe/interface.go 0 19 0.0%
tapdb/sqlc/supply_commit.sql.go 174 198 87.88%
tapdb/supply_commit.go 590 846 69.74%
Files with Coverage Reduction New Missed Lines %
asset/group_key.go 2 62.64%
commitment/tap.go 2 71.14%
internal/test/helpers.go 2 86.95%
rfqmsg/records.go 3 64.11%
asset/asset.go 4 47.09%
internal/test/copy.go 6 82.26%
tapdb/multiverse.go 6 53.28%
Totals Coverage Status
Change from base Build 15899212451: 0.5%
Covered Lines: 30611
Relevant Lines: 78046

💛 - Coveralls

@Roasbeef Roasbeef force-pushed the supply-commit-machine branch 2 times, most recently from 87ba694 to e285f19 Compare June 27, 2025 00:20
Roasbeef added 8 commits June 26, 2025 17:35
… tree state machine

In this commit, we add a new migration for the persistence layer of the
supply tree state machine. We track a state machine that points to the
latest supply commitment. We update the supply commit via a new state
transition, which references a series of updates. Once we're ready to
apply, we'll apply the updates, mark the transition as finalized, and
finally update the relevant pointers.

We also make a change to modify mint_anchor_uni_commitments to point to
a supply commitment. This lets us keep track of the set of pre
commitments that aren't yet spent.
…and supplycommit.StateMachineStore

In this commit, we add concrete implementation of both CommitmentTracker
and StateMachineStore. This implements the persistence layer for the
supply commit state machine. The main method to understand is
ApplyStateTransition, as it implements the atomic update of all the
various components (tree, state transition, etc) on disk.
@Roasbeef Roasbeef force-pushed the supply-commit-machine branch from e285f19 to 277c8f3 Compare June 27, 2025 00:35
@github-project-automation github-project-automation bot moved this from 🏗 In progress to 👀 In review in Taproot-Assets Project Board Jun 27, 2025
@ffranr ffranr added this pull request to the merge queue Jun 27, 2025
Merged via the queue into lightninglabs:main with commit 1b2e06c Jun 27, 2025
18 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Taproot-Assets Project Board Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

6 participants