Skip to content

multi: allow supply commit state machine to accept pending updates during state transition #1609

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0d90a02
tapdb: allow danging updates, and add a new frozen bool to state tran…
Roasbeef Jun 18, 2025
33e0407
tapdb: add group_key to supply_update_events
Roasbeef Jun 18, 2025
dba58f4
tapdb: add new queries to handle dangling updates
Roasbeef Jun 18, 2025
77af31c
tapdb: update supply commit queries to account for the new frozen fie…
Roasbeef Jun 18, 2025
60ac4da
tapdb: update the supply update insertion logic to support dangling u…
Roasbeef Jun 18, 2025
3557ab1
tapdb: add method to freeze a state transition
Roasbeef Jun 18, 2025
5802d8b
tapdb: generate sqlc structs
Roasbeef Jun 18, 2025
809477b
tapdb: add method to bind dangling updates to a new state transitions
Roasbeef Jun 18, 2025
f758aa5
tapdb: update tests to factor in new frozen/dangling logic in InsertP…
Roasbeef Jun 18, 2025
5a31596
universe/supplycommit: add new bind+freeze methods to StateMachineStore
Roasbeef Jun 18, 2025
c403151
universe/supplycommit: freeze state transition before tree construction
Roasbeef Jun 18, 2025
a6be47a
universe/supplycommit: allow new supply updates to be inserted in all…
Roasbeef Jun 18, 2025
199638d
universe/supplycommit: add new state transitions to bind any dangling…
Roasbeef Jun 18, 2025
fe5299d
universe/supplycommit: update unit tests due to state machine updates
Roasbeef Jun 18, 2025
ede7e1e
tapdb: add TestBindDanglingUpdatesToTransition test
Roasbeef Jun 18, 2025
0d6476b
universe/supplycommit: add new sub-test for self-transition with dang…
Roasbeef Jun 18, 2025
4dc3a8a
universe/supplycommit: update README.md to detail dangling updates
Roasbeef Jul 23, 2025
ef03639
universe/supplycommit: add unit tests for dangling update transitions
Roasbeef Jul 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion tapdb/sqlc/migrations/000040_asset_commit.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ CREATE TABLE supply_commit_transitions (
-- Can be NULL until the transaction is created and signed.
pending_commit_txn_id BIGINT REFERENCES chain_txns(txn_id),

-- Indicates if this transition is frozen and should not accept new updates.
frozen BOOLEAN NOT NULL DEFAULT FALSE,

-- Indicates if this transition has been successfully completed and committed.
finalized BOOLEAN NOT NULL DEFAULT FALSE,

Expand All @@ -106,8 +109,13 @@ CREATE TABLE supply_commit_transitions (
CREATE TABLE supply_update_events (
event_id INTEGER PRIMARY KEY,

-- The group key of the asset group this event belongs to.
-- This is needed to query for dangling events for a specific group.
group_key BLOB NOT NULL CHECK(length(group_key) = 33),

-- Reference to the state transition this event is part of.
transition_id BIGINT NOT NULL REFERENCES supply_commit_transitions(transition_id) ON DELETE CASCADE,
-- Can be NULL if the event is staged while another transition is active.
transition_id BIGINT REFERENCES supply_commit_transitions(transition_id) ON DELETE CASCADE,

-- The type of update (mint, burn, ignore).
update_type_id INTEGER NOT NULL REFERENCES supply_commit_update_types(id),
Expand Down
4 changes: 3 additions & 1 deletion tapdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions tapdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 28 additions & 12 deletions tapdb/sqlc/queries/supply_commit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ WHERE commit_id = @commit_id;
-- name: InsertSupplyCommitTransition :one
INSERT INTO supply_commit_transitions (
state_machine_group_key, old_commitment_id, new_commitment_id,
pending_commit_txn_id, finalized, creation_time
pending_commit_txn_id, finalized, frozen, creation_time
Copy link
Member

Choose a reason for hiding this comment

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

nit: IMO it's a bit easier to see that nothing is missing when all the changes to the SQL schema and queries (and updating the generated code) are in a single commit.

) VALUES (
$1, $2, $3, $4, $5, $6
$1, $2, $3, $4, $5, $6, $7
) RETURNING transition_id;

-- name: FinalizeSupplyCommitTransition :exec
Expand All @@ -67,9 +67,9 @@ WHERE transition_id = @transition_id;

-- name: InsertSupplyUpdateEvent :exec
INSERT INTO supply_update_events (
transition_id, update_type_id, event_data
group_key, transition_id, update_type_id, event_data
) VALUES (
$1, $2, $3
$1, $2, $3, $4
);

-- name: QuerySupplyCommitStateMachine :one
Expand All @@ -89,21 +89,19 @@ WITH target_machine AS (
FROM supply_commit_state_machines
WHERE group_key = @group_key
)
SELECT
t.transition_id,
t.state_machine_group_key,
t.old_commitment_id,
t.new_commitment_id,
t.pending_commit_txn_id,
t.finalized,
t.creation_time
SELECT sqlc.embed(t)
FROM supply_commit_transitions t
JOIN target_machine tm
ON t.state_machine_group_key = tm.group_key
WHERE t.finalized = FALSE
ORDER BY t.creation_time DESC
LIMIT 1;

-- name: FreezePendingTransition :exec
UPDATE supply_commit_transitions
SET frozen = TRUE
WHERE state_machine_group_key = @group_key AND finalized = FALSE;

-- name: QuerySupplyUpdateEvents :many
SELECT
ue.event_id,
Expand All @@ -117,6 +115,24 @@ JOIN supply_commit_update_types types
WHERE ue.transition_id = @transition_id
ORDER BY ue.event_id ASC;

-- name: QueryDanglingSupplyUpdateEvents :many
SELECT
ue.event_id,
ue.transition_id,
ue.update_type_id,
types.update_type_name,
ue.event_data
FROM supply_update_events ue
JOIN supply_commit_update_types types
ON ue.update_type_id = types.id
WHERE ue.group_key = @group_key AND ue.transition_id IS NULL
ORDER BY ue.event_id ASC;

-- name: LinkDanglingSupplyUpdateEvents :exec
UPDATE supply_update_events
SET transition_id = @transition_id
WHERE group_key = @group_key AND transition_id IS NULL;

-- name: QuerySupplyCommitment :one
SELECT *
FROM supply_commitments
Expand Down
10 changes: 9 additions & 1 deletion tapdb/sqlc/schemas/generated_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ CREATE TABLE supply_commit_transitions (
-- Can be NULL until the transaction is created and signed.
pending_commit_txn_id BIGINT REFERENCES chain_txns(txn_id),

-- Indicates if this transition is frozen and should not accept new updates.
frozen BOOLEAN NOT NULL DEFAULT FALSE,

-- Indicates if this transition has been successfully completed and committed.
finalized BOOLEAN NOT NULL DEFAULT FALSE,

Expand Down Expand Up @@ -847,8 +850,13 @@ CREATE INDEX supply_commitments_group_key_idx ON supply_commitments(group_key);
CREATE TABLE supply_update_events (
event_id INTEGER PRIMARY KEY,

-- The group key of the asset group this event belongs to.
-- This is needed to query for dangling events for a specific group.
group_key BLOB NOT NULL CHECK(length(group_key) = 33),

-- Reference to the state transition this event is part of.
transition_id BIGINT NOT NULL REFERENCES supply_commit_transitions(transition_id) ON DELETE CASCADE,
-- Can be NULL if the event is staged while another transition is active.
transition_id BIGINT REFERENCES supply_commit_transitions(transition_id) ON DELETE CASCADE,

-- The type of update (mint, burn, ignore).
update_type_id INTEGER NOT NULL REFERENCES supply_commit_update_types(id),
Expand Down
Loading
Loading