Skip to content

Commit 1b2e06c

Browse files
authored
Merge pull request #1508 from Roasbeef/supply-commit-machine
tapdb: add implementation of supplycommit.CommitmentTracker and supplycommit.StateMachineStore
2 parents 4bb4d5a + 277c8f3 commit 1b2e06c

14 files changed

+4394
-11
lines changed

tapdb/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
// daemon.
2525
//
2626
// NOTE: This MUST be updated when a new migration is added.
27-
LatestMigrationVersion = 39
27+
LatestMigrationVersion = 40
2828
)
2929

3030
// DatabaseBackend is an interface that contains all methods our different

tapdb/sqlc/assets.sql.go

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
DROP INDEX IF EXISTS supply_commit_transitions_single_pending_idx;
2+
DROP INDEX IF EXISTS supply_update_events_transition_id_idx;
3+
DROP INDEX IF EXISTS supply_commit_transitions_state_machine_group_key_idx;
4+
DROP INDEX IF EXISTS supply_commitments_chain_txn_id_idx;
5+
DROP INDEX IF EXISTS supply_commitments_group_key_idx;
6+
7+
DROP TABLE IF EXISTS supply_update_events;
8+
DROP TABLE IF EXISTS supply_commit_transitions;
9+
DROP TABLE IF EXISTS supply_commit_state_machines;
10+
DROP TABLE IF EXISTS supply_commitments;
11+
DROP TABLE IF EXISTS supply_commit_update_types;
12+
DROP TABLE IF EXISTS supply_commit_states;
13+
14+
ALTER TABLE mint_anchor_uni_commitments DROP COLUMN spent_by;
15+
16+
ALTER TABLE supply_commitments DROP COLUMN supply_root_hash;
17+
ALTER TABLE supply_commitments DROP COLUMN supply_root_sum;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
-- Enum-like table for state machine states.
2+
CREATE TABLE supply_commit_states (
3+
id INTEGER PRIMARY KEY,
4+
state_name TEXT UNIQUE NOT NULL
5+
);
6+
7+
-- Populate the possible states.
8+
INSERT INTO supply_commit_states (id, state_name) VALUES
9+
(0, 'DefaultState'),
10+
(1, 'UpdatesPendingState'),
11+
(2, 'CommitTreeCreateState'),
12+
(3, 'CommitTxCreateState'),
13+
(4, 'CommitTxSignState'),
14+
(5, 'CommitBroadcastState'),
15+
(6, 'CommitFinalizeState');
16+
17+
-- Enum-like table for supply update event types.
18+
CREATE TABLE supply_commit_update_types (
19+
id INTEGER PRIMARY KEY,
20+
update_type_name TEXT UNIQUE NOT NULL
21+
);
22+
23+
-- Populate the possible update types.
24+
INSERT INTO supply_commit_update_types (id, update_type_name) VALUES
25+
(0, 'mint'),
26+
(1, 'burn'),
27+
(2, 'ignore');
28+
29+
-- Table storing the details of a specific supply commitment (root and sub-trees).
30+
-- This represents a committed state on chain.
31+
CREATE TABLE supply_commitments (
32+
commit_id INTEGER PRIMARY KEY,
33+
34+
-- The tweaked group key identifying the asset group this commitment belongs to.
35+
group_key BLOB NOT NULL CHECK(length(group_key) = 33),
36+
37+
-- The chain transaction that included this commitment.
38+
chain_txn_id BIGINT NOT NULL REFERENCES chain_txns(txn_id),
39+
40+
-- The output index within the chain_txn_id transaction for the commitment.
41+
output_index INTEGER,
42+
43+
-- The internal key used for the commitment output.
44+
internal_key_id BIGINT NOT NULL REFERENCES internal_keys(key_id),
45+
46+
-- The taproot output key used for the commitment output.
47+
output_key BLOB NOT NULL CHECK(length(output_key) = 33),
48+
49+
-- The block header of the block mining the commitment transaction.
50+
block_header BLOB,
51+
52+
-- The block height at which the commitment transaction was confirmed.
53+
-- Can be NULL if the transaction is not yet confirmed.
54+
block_height INTEGER,
55+
56+
-- The merkle proof demonstrating the commitment's inclusion in the block.
57+
merkle_proof BLOB,
58+
59+
-- The root hash of the supply commitment at this snapshot.
60+
supply_root_hash BLOB,
61+
62+
-- The root sum of the supply commitment at this snapshot.
63+
supply_root_sum BIGINT
64+
);
65+
66+
-- Main table tracking the state machine instance per asset group.
67+
CREATE TABLE supply_commit_state_machines (
68+
-- The tweaked group key identifying the asset group's state machine.
69+
group_key BLOB PRIMARY KEY CHECK(length(group_key) = 33),
70+
71+
-- The current state of the state machine.
72+
current_state_id INTEGER NOT NULL REFERENCES supply_commit_states(id),
73+
74+
-- The latest successfully committed supply state on chain.
75+
-- Can be NULL if no commitment has been made yet.
76+
latest_commitment_id BIGINT REFERENCES supply_commitments(commit_id)
77+
);
78+
79+
-- Table tracking a pending state transition for a state machine.
80+
CREATE TABLE supply_commit_transitions (
81+
transition_id INTEGER PRIMARY KEY,
82+
83+
-- Reference back to the state machine this transition belongs to.
84+
state_machine_group_key BLOB NOT NULL REFERENCES supply_commit_state_machines(group_key),
85+
86+
-- The commitment being replaced by this transition.
87+
-- Can be NULL if this is the first commitment.
88+
old_commitment_id BIGINT REFERENCES supply_commitments(commit_id),
89+
90+
-- The new commitment that this transition aims to create.
91+
-- Can be NULL initially, before the commitment details are created.
92+
new_commitment_id BIGINT REFERENCES supply_commitments(commit_id),
93+
94+
-- The chain transaction that, once confirmed, will finalize this transition.
95+
-- Can be NULL until the transaction is created and signed.
96+
pending_commit_txn_id BIGINT REFERENCES chain_txns(txn_id),
97+
98+
-- Indicates if this transition has been successfully completed and committed.
99+
finalized BOOLEAN NOT NULL DEFAULT FALSE,
100+
101+
-- Timestamp when this transition was initiated (unix timestamp in seconds).
102+
creation_time BIGINT NOT NULL
103+
);
104+
105+
-- Table storing individual update events associated with a pending transition.
106+
CREATE TABLE supply_update_events (
107+
event_id INTEGER PRIMARY KEY,
108+
109+
-- Reference to the state transition this event is part of.
110+
transition_id BIGINT NOT NULL REFERENCES supply_commit_transitions(transition_id) ON DELETE CASCADE,
111+
112+
-- The type of update (mint, burn, ignore).
113+
update_type_id INTEGER NOT NULL REFERENCES supply_commit_update_types(id),
114+
115+
-- Opaque blob containing the serialized data for the specific
116+
-- SupplyUpdateEvent (NewMintEvent, NewBurnEvent, NewIgnoreEvent).
117+
event_data BLOB NOT NULL
118+
);
119+
120+
-- In order to be able to easily fetch the set of unspent pre-commitment
121+
-- outputs, we'll add a new spent_by field to mint_anchor_uni_commitments.
122+
ALTER TABLE mint_anchor_uni_commitments
123+
ADD COLUMN spent_by BIGINT REFERENCES supply_commitments(commit_id);
124+
125+
-- Add indexes for frequent lookups.
126+
CREATE INDEX supply_commitments_chain_txn_id_idx ON supply_commitments(chain_txn_id);
127+
CREATE INDEX supply_commit_transitions_state_machine_group_key_idx ON supply_commit_transitions(state_machine_group_key);
128+
CREATE INDEX supply_update_events_transition_id_idx ON supply_update_events(transition_id);
129+
CREATE INDEX supply_commitments_group_key_idx ON supply_commitments(group_key);
130+
131+
-- Ensure only one non-finalized transition exists per state machine group key.
132+
CREATE UNIQUE INDEX supply_commit_transitions_single_pending_idx
133+
ON supply_commit_transitions (state_machine_group_key) WHERE finalized = FALSE;

tapdb/sqlc/models.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/querier.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)