Skip to content

Commit 3cda1fe

Browse files
committed
tapdb+universe: consolidate code, link commitment to previous one
We clean up and consolidate a bunch of code: - Parsing the supply commitment and data was duplicated, is now a single function. - The commitmentChainInfo can be merged directly into the commitment.CommitmentBlock, gets rid of a parameter. We also add a new spent_commitment field to the supply_commitments table that tracks the previous commitment transaction that was spent, to create the full chain. To be able to traverse that chain, we also need to be able to query the commitments either by a commitment's outpoint or by the outpoint a commitment is spending, which allows backward lookup in a loop.
1 parent bf72b0e commit 3cda1fe

13 files changed

+537
-301
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
-- Drop the supply_syncer_log table and its index.
22
DROP INDEX IF EXISTS supply_syncer_log_group_key_idx;
3-
DROP TABLE IF EXISTS supply_syncer_log;
3+
DROP TABLE IF EXISTS supply_syncer_log;
4+
5+
DROP INDEX IF EXISTS supply_commitments_output_index_idx;
6+
DROP INDEX IF EXISTS supply_commitments_spent_commitment_idx;
7+
8+
ALTER TABLE supply_commitments
9+
DROP COLUMN spent_commitment;

tapdb/sqlc/migrations/000044_supply_syncer_log.up.sql

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,21 @@ CREATE TABLE supply_syncer_log (
2020

2121
-- Add index for frequent lookups by group key.
2222
CREATE UNIQUE INDEX supply_syncer_log_group_key_idx
23-
ON supply_syncer_log(group_key);
23+
ON supply_syncer_log(group_key);
24+
25+
-- A nullable column to track the previous supply commitment that was spent to
26+
-- create a new supply commitment. This is only NULL for the very first
27+
-- commitment of an asset group, each subsequent commitment needs to spend a
28+
-- prior commitment to ensure continuity in the supply chain.
29+
ALTER TABLE supply_commitments
30+
ADD COLUMN spent_commitment BIGINT
31+
REFERENCES supply_commitments(commit_id);
32+
33+
-- Add an index to speed up lookups by spent commitment.
34+
CREATE INDEX supply_commitments_spent_commitment_idx
35+
ON supply_commitments(spent_commitment);
36+
37+
-- We also add an index on the output index of the supply commitment to speed
38+
-- up lookups of commitments by outpoint (will need a join over chain_txns).
39+
CREATE INDEX supply_commitments_output_index_idx
40+
ON supply_commitments(output_index);

tapdb/sqlc/models.go

Lines changed: 12 additions & 11 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: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/supply_commit.sql

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ RETURNING current_state_id, latest_commitment_id;
3030
-- name: InsertSupplyCommitment :one
3131
INSERT INTO supply_commitments (
3232
group_key, chain_txn_id,
33-
output_index, internal_key_id, output_key, -- Core fields
33+
output_index, internal_key_id, output_key, spent_commitment, -- Core fields
3434
block_height, block_header, merkle_proof, -- Nullable chain details
3535
supply_root_hash, supply_root_sum -- Nullable root details
3636
) VALUES (
37-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
37+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
3838
) RETURNING commit_id;
3939

4040
-- name: UpdateSupplyCommitmentChainDetails :exec
4141
UPDATE supply_commitments
42-
SET merkle_proof = @merkle_proof,
43-
output_index = @output_index,
44-
block_header = @block_header,
45-
chain_txn_id = @chain_txn_id,
46-
block_height = @block_height
42+
SET merkle_proof = @merkle_proof,
43+
output_index = @output_index,
44+
block_header = @block_header,
45+
chain_txn_id = @chain_txn_id,
46+
block_height = @block_height
4747
WHERE commit_id = @commit_id;
4848

4949
-- name: UpdateSupplyCommitmentRoot :exec
@@ -134,10 +134,37 @@ SET transition_id = @transition_id
134134
WHERE group_key = @group_key AND transition_id IS NULL;
135135

136136
-- name: QuerySupplyCommitment :one
137-
SELECT *
138-
FROM supply_commitments
137+
SELECT sqlc.embed(sc), ct.tx_index
138+
FROM supply_commitments AS sc
139+
JOIN chain_txns AS ct
140+
ON sc.chain_txn_id = ct.txn_id
139141
WHERE commit_id = @commit_id;
140142

143+
-- name: QuerySupplyCommitmentByOutpoint :one
144+
SELECT sqlc.embed(sc), ct.tx_index
145+
FROM supply_commitments AS sc
146+
JOIN chain_txns AS ct
147+
ON sc.chain_txn_id = ct.txn_id
148+
WHERE sc.group_key = @group_key AND
149+
sc.output_index = @output_index AND
150+
ct.txid = @txid;
151+
152+
-- name: QuerySupplyCommitmentBySpentOutpoint :one
153+
WITH spent_commitment AS (
154+
SELECT ssc.commit_id
155+
FROM supply_commitments AS ssc
156+
JOIN chain_txns AS ct
157+
ON ssc.chain_txn_id = ct.txn_id
158+
WHERE ssc.group_key = @group_key AND
159+
ssc.output_index = @output_index AND
160+
ct.txid = @txid
161+
)
162+
SELECT sqlc.embed(sc), ct.tx_index
163+
FROM supply_commitments AS sc
164+
JOIN chain_txns AS ct
165+
ON sc.chain_txn_id = ct.txn_id
166+
WHERE sc.spent_commitment = (SELECT commit_id FROM spent_commitment);
167+
141168
-- name: UpdateSupplyCommitTransitionCommitment :exec
142169
UPDATE supply_commit_transitions
143170
SET new_commitment_id = @new_commitment_id,
@@ -174,17 +201,7 @@ WHERE
174201

175202
-- name: FetchSupplyCommit :one
176203
SELECT
177-
sc.commit_id,
178-
sc.output_index,
179-
sc.output_key,
180-
sqlc.embed(ik),
181-
txn.raw_tx,
182-
txn.block_height,
183-
txn.block_hash,
184-
txn.tx_index,
185-
txn.chain_fees,
186-
sc.supply_root_hash AS root_hash,
187-
sc.supply_root_sum AS root_sum
204+
sqlc.embed(sc), txn.tx_index
188205
FROM supply_commit_state_machines sm
189206
JOIN supply_commitments sc
190207
ON sm.latest_commitment_id = sc.commit_id

tapdb/sqlc/queries/supply_syncer.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- name: UpsertSupplySyncerLog :exec
2-
INSERT INTO supply_syncer_log (group_key, max_fetched_block_height, max_inserted_block_height)
3-
VALUES (@group_key, @max_fetched_block_height, @max_inserted_block_height)
2+
INSERT INTO supply_syncer_log (
3+
group_key, max_fetched_block_height, max_inserted_block_height
4+
) VALUES (
5+
@group_key, @max_fetched_block_height, @max_inserted_block_height
6+
)
47
ON CONFLICT (group_key) DO UPDATE SET
58
max_fetched_block_height = COALESCE(@max_fetched_block_height, supply_syncer_log.max_fetched_block_height),
69
max_inserted_block_height = COALESCE(@max_inserted_block_height, supply_syncer_log.max_inserted_block_height);
@@ -9,3 +12,4 @@ ON CONFLICT (group_key) DO UPDATE SET
912
SELECT group_key, max_fetched_block_height, max_inserted_block_height
1013
FROM supply_syncer_log
1114
WHERE group_key = @group_key;
15+

tapdb/sqlc/schemas/generated_schema.sql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,12 +875,19 @@ CREATE TABLE supply_commitments (
875875

876876
-- The root sum of the supply commitment at this snapshot.
877877
supply_root_sum BIGINT
878-
);
878+
, spent_commitment BIGINT
879+
REFERENCES supply_commitments(commit_id));
879880

880881
CREATE INDEX supply_commitments_chain_txn_id_idx ON supply_commitments(chain_txn_id);
881882

882883
CREATE INDEX supply_commitments_group_key_idx ON supply_commitments(group_key);
883884

885+
CREATE INDEX supply_commitments_output_index_idx
886+
ON supply_commitments(output_index);
887+
888+
CREATE INDEX supply_commitments_spent_commitment_idx
889+
ON supply_commitments(spent_commitment);
890+
884891
CREATE TABLE supply_syncer_log (
885892
id INTEGER PRIMARY KEY,
886893

0 commit comments

Comments
 (0)