Skip to content

Commit ba63071

Browse files
committed
tapdb: rename table to supply_pre_commits and unique on outpoint
Rename table `mint_anchor_uni_commitments` to `supply_pre_commits` to align with current "supply" terminology. Drop the NOT NULL constraint on `batch_id` so the table can also store pre-commit outputs from peer-issued assets, not just those minted locally. Also, remove the unique index on (batch_id, tx_output_index) and add unique index on outpoint.
1 parent 17afcc0 commit ba63071

12 files changed

+196
-76
lines changed

tapdb/asset_minting_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,8 @@ func TestTapscriptTreeManager(t *testing.T) {
18381838
// storeSupplyPreCommit stores a mint anchor commitment in the DB.
18391839
func storeSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
18401840
batchKey []byte, txOutputIndex int32,
1841-
taprootInternalKey keychain.KeyDescriptor, groupKey []byte) {
1841+
taprootInternalKey keychain.KeyDescriptor, groupKey []byte,
1842+
outpoint wire.OutPoint) {
18421843

18431844
ctx := context.Background()
18441845

@@ -1854,12 +1855,16 @@ func storeSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
18541855
})
18551856
require.NoError(t, err)
18561857

1858+
opBytes, err := encodeOutpoint(outpoint)
1859+
require.NoError(t, err)
1860+
18571861
_, err = q.UpsertMintAnchorUniCommitment(
18581862
ctx, sqlc.UpsertMintAnchorUniCommitmentParams{
18591863
BatchKey: batchKey,
18601864
TxOutputIndex: txOutputIndex,
18611865
TaprootInternalKeyID: internalKeyID,
18621866
GroupKey: groupKey,
1867+
Outpoint: opBytes,
18631868
},
18641869
)
18651870
require.NoError(t, err)
@@ -1948,16 +1953,27 @@ func TestUpsertSupplyPreCommit(t *testing.T) {
19481953
},
19491954
)
19501955

1956+
// Define pre-commit outpoint for the batch mint anchor tx.
1957+
txOutputIndex := int32(2)
1958+
txidStr := mintingBatch.GenesisPacket.FundedPsbt.Pkt.UnsignedTx.TxID()
1959+
1960+
txid, err := chainhash.NewHashFromStr(txidStr)
1961+
require.NoError(t, err)
1962+
1963+
preCommitOutpoint := wire.OutPoint{
1964+
Hash: *txid,
1965+
Index: uint32(txOutputIndex),
1966+
}
1967+
19511968
// Serialize keys into bytes for easier handling.
19521969
preCommitInternalKey, _ := test.RandKeyDesc(t)
19531970

19541971
groupPubKeyBytes := group.GroupPubKey.SerializeCompressed()
19551972

19561973
// Upsert a mint anchor commitment for the batch.
1957-
txOutputIndex := int32(2)
19581974
storeSupplyPreCommit(
1959-
t, *assetStore, batchKey, txOutputIndex,
1960-
preCommitInternalKey, groupPubKeyBytes,
1975+
t, *assetStore, batchKey, txOutputIndex, preCommitInternalKey,
1976+
groupPubKeyBytes, preCommitOutpoint,
19611977
)
19621978

19631979
// Retrieve and inspect the mint anchor commitment we just inserted.
@@ -1966,28 +1982,28 @@ func TestUpsertSupplyPreCommit(t *testing.T) {
19661982
preCommitInternalKey, groupPubKeyBytes,
19671983
)
19681984

1969-
// Upsert-ing a new taproot internal key for the same batch should
1970-
// overwrite the existing one.
1985+
// Upsert-ing a new taproot internal key for the same pre-commit
1986+
// outpoint should overwrite the existing one.
19711987
internalKey2, _ := test.RandKeyDesc(t)
19721988

19731989
storeSupplyPreCommit(
19741990
t, *assetStore, batchKey, txOutputIndex, internalKey2,
1975-
groupPubKeyBytes,
1991+
groupPubKeyBytes, preCommitOutpoint,
19761992
)
19771993

19781994
assertSupplyPreCommit(
19791995
t, *assetStore, batchKey, txOutputIndex, internalKey2,
19801996
groupPubKeyBytes,
19811997
)
19821998

1983-
// Upsert-ing a new group key for the same batch should overwrite the
1984-
// existing one.
1999+
// Upsert-ing a new group key for the same pre-commit outpoint should
2000+
// overwrite the existing one.
19852001
groupPubKey2 := test.RandPubKey(t)
19862002
groupPubKey2Bytes := groupPubKey2.SerializeCompressed()
19872003

19882004
storeSupplyPreCommit(
19892005
t, *assetStore, batchKey, txOutputIndex, internalKey2,
1990-
groupPubKey2Bytes,
2006+
groupPubKey2Bytes, preCommitOutpoint,
19912007
)
19922008

19932009
assertSupplyPreCommit(

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 = 45
27+
LatestMigrationVersion = 46
2828
)
2929

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

tapdb/sqlc/assets.sql.go

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- Revert batch_id back to NOT NULL in supply_pre_commits table and
2+
-- rename back to mint_anchor_uni_commitments. Since SQLite doesn't support
3+
-- ALTER COLUMN, we need to recreate the table.
4+
5+
-- Create a new table with the original structure (batch_id NOT NULL).
6+
CREATE TABLE mint_anchor_uni_commitments (
7+
id INTEGER PRIMARY KEY,
8+
9+
-- The ID of the minting batch this universe commitment relates to.
10+
batch_id INTEGER NOT NULL REFERENCES asset_minting_batches(batch_id),
11+
12+
-- The index of the mint batch anchor transaction pre-commitment output.
13+
tx_output_index INTEGER NOT NULL,
14+
15+
-- The Taproot output internal key for the pre-commitment output.
16+
group_key BLOB,
17+
18+
-- The taproot internal key ID reference.
19+
taproot_internal_key_id BIGINT REFERENCES internal_keys(key_id) NOT NULL,
20+
21+
-- Reference to supply commitments.
22+
spent_by BIGINT REFERENCES supply_commitments(commit_id),
23+
24+
-- The outpoint for this commitment.
25+
outpoint BLOB
26+
);
27+
28+
-- Copy all existing data from the old table to the new table.
29+
-- This will fail if there are any NULL batch_id values, which is expected
30+
-- behavior for a down migration that removes nullable support.
31+
INSERT INTO mint_anchor_uni_commitments (
32+
id, batch_id, tx_output_index, group_key, taproot_internal_key_id, spent_by,
33+
outpoint
34+
)
35+
SELECT
36+
id, batch_id, tx_output_index, group_key, taproot_internal_key_id, spent_by,
37+
outpoint
38+
FROM supply_pre_commits
39+
WHERE batch_id IS NOT NULL;
40+
41+
-- DROP old indexes before dropping the table.
42+
DROP INDEX IF EXISTS supply_pre_commits_unique_outpoint;
43+
44+
-- Drop the old table.
45+
DROP TABLE supply_pre_commits;
46+
47+
-- Recreate the indexes.
48+
CREATE INDEX mint_anchor_uni_commitments_outpoint_idx
49+
ON mint_anchor_uni_commitments(outpoint)
50+
WHERE outpoint IS NOT NULL;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Make batch_id nullable in mint_anchor_uni_commitments table and rename to
2+
-- supply_pre_commits. Since SQLite doesn't support ALTER COLUMN, we need
3+
-- to recreate the table.
4+
5+
-- Create a new table with the desired structure (batch_id nullable).
6+
CREATE TABLE supply_pre_commits (
7+
id INTEGER PRIMARY KEY,
8+
9+
-- The ID of the minting batch this universe commitment relates to.
10+
-- Now nullable to allow universe commitments without a specific batch.
11+
batch_id INTEGER REFERENCES asset_minting_batches(batch_id),
12+
13+
-- The index of the mint batch anchor transaction pre-commitment output.
14+
tx_output_index INTEGER NOT NULL,
15+
16+
-- The Taproot output internal key for the pre-commitment output.
17+
group_key BLOB,
18+
19+
-- The taproot internal key ID reference.
20+
taproot_internal_key_id BIGINT REFERENCES internal_keys(key_id) NOT NULL,
21+
22+
-- Reference to supply commitments.
23+
spent_by BIGINT REFERENCES supply_commitments(commit_id),
24+
25+
-- The outpoint for this commitment.
26+
outpoint BLOB NOT NULL CHECK(length(outpoint) > 0)
27+
);
28+
29+
-- Copy all existing data from the old table to the new table.
30+
INSERT INTO supply_pre_commits (
31+
id, batch_id, tx_output_index, group_key, taproot_internal_key_id, spent_by,
32+
outpoint
33+
)
34+
SELECT
35+
id, batch_id, tx_output_index, group_key, taproot_internal_key_id, spent_by,
36+
outpoint
37+
FROM mint_anchor_uni_commitments;
38+
39+
-- Drop the old index before dropping the table.
40+
DROP INDEX IF EXISTS mint_anchor_uni_commitments_outpoint_idx;
41+
42+
-- Drop the old table.
43+
DROP TABLE mint_anchor_uni_commitments;
44+
45+
-- Create a unique index on outpoint.
46+
CREATE UNIQUE INDEX supply_pre_commits_unique_outpoint
47+
ON supply_pre_commits(outpoint);

tapdb/sqlc/models.go

Lines changed: 10 additions & 10 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: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/assets.sql

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ JOIN genesis_assets
10631063
ORDER BY assets_meta.meta_id;
10641064

10651065
-- name: UpsertMintAnchorUniCommitment :one
1066-
-- Upsert a record into the mint_anchor_uni_commitments table.
1066+
-- Upsert a record into the supply_pre_commits table.
10671067
-- If a record with the same batch ID and tx output index already exists, update
10681068
-- the existing record. Otherwise, insert a new record.
10691069
WITH target_batch AS (
@@ -1073,7 +1073,7 @@ WITH target_batch AS (
10731073
FROM internal_keys keys
10741074
WHERE keys.raw_key = @batch_key
10751075
)
1076-
INSERT INTO mint_anchor_uni_commitments (
1076+
INSERT INTO supply_pre_commits (
10771077
batch_id, tx_output_index, taproot_internal_key_id, group_key, spent_by, outpoint
10781078
)
10791079
VALUES (
@@ -1088,26 +1088,26 @@ ON CONFLICT(batch_id, tx_output_index) DO UPDATE SET
10881088
RETURNING id;
10891089

10901090
-- name: FetchMintAnchorUniCommitment :many
1091-
-- Fetch records from the mint_anchor_uni_commitments table with optional
1091+
-- Fetch records from the supply_pre_commits table with optional
10921092
-- filtering.
10931093
SELECT
1094-
mint_anchor_uni_commitments.id,
1095-
mint_anchor_uni_commitments.batch_id,
1096-
mint_anchor_uni_commitments.tx_output_index,
1097-
mint_anchor_uni_commitments.group_key,
1098-
mint_anchor_uni_commitments.spent_by,
1094+
precommits.id,
1095+
precommits.batch_id,
1096+
precommits.tx_output_index,
1097+
precommits.group_key,
1098+
precommits.spent_by,
10991099
batch_internal_keys.raw_key AS batch_key,
1100-
mint_anchor_uni_commitments.taproot_internal_key_id,
1100+
precommits.taproot_internal_key_id,
11011101
sqlc.embed(taproot_internal_keys)
1102-
FROM mint_anchor_uni_commitments
1102+
FROM supply_pre_commits AS precommits
11031103
JOIN internal_keys taproot_internal_keys
1104-
ON mint_anchor_uni_commitments.taproot_internal_key_id = taproot_internal_keys.key_id
1104+
ON precommits.taproot_internal_key_id = taproot_internal_keys.key_id
11051105
LEFT JOIN asset_minting_batches batches
1106-
ON mint_anchor_uni_commitments.batch_id = batches.batch_id
1106+
ON precommits.batch_id = batches.batch_id
11071107
LEFT JOIN internal_keys batch_internal_keys
11081108
ON batches.batch_id = batch_internal_keys.key_id
11091109
WHERE (
11101110
(batch_internal_keys.raw_key = sqlc.narg('batch_key') OR sqlc.narg('batch_key') IS NULL) AND
1111-
(mint_anchor_uni_commitments.group_key = sqlc.narg('group_key') OR sqlc.narg('group_key') IS NULL) AND
1111+
(precommits.group_key = sqlc.narg('group_key') OR sqlc.narg('group_key') IS NULL) AND
11121112
(taproot_internal_keys.raw_key = sqlc.narg('taproot_internal_key_raw') OR sqlc.narg('taproot_internal_key_raw') IS NULL)
11131113
);

tapdb/sqlc/queries/supply_commit.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ SELECT
212212
mac.group_key,
213213
mint_txn.block_height,
214214
mint_txn.raw_tx
215-
FROM mint_anchor_uni_commitments mac
215+
FROM supply_pre_commits mac
216216
JOIN asset_minting_batches amb ON mac.batch_id = amb.batch_id
217217
JOIN genesis_points gp ON amb.genesis_id = gp.genesis_id
218218
JOIN chain_txns mint_txn ON gp.anchor_tx_id = mint_txn.txn_id
@@ -225,7 +225,7 @@ WHERE
225225

226226
-- name: MarkPreCommitmentSpentByOutpoint :exec
227227
-- Mark a specific pre-commitment output as spent by its outpoint.
228-
UPDATE mint_anchor_uni_commitments
228+
UPDATE supply_pre_commits
229229
SET spent_by = @spent_by_commit_id
230230
WHERE outpoint = @outpoint
231231
AND spent_by IS NULL;

0 commit comments

Comments
 (0)