Skip to content

Commit b25c053

Browse files
authored
Merge pull request #1784 from lightninglabs/wip/supplyverify/store-nonissuer-supply-pre-commits
supplyverifier: store and verify non-issuer supply pre-commitments
2 parents 592dea0 + e8ea035 commit b25c053

26 files changed

+797
-212
lines changed

docs/release-notes/release-notes-0.7.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
- https://github.com/lightninglabs/taproot-assets/pull/1716
7373
- https://github.com/lightninglabs/taproot-assets/pull/1675
7474
- https://github.com/lightninglabs/taproot-assets/pull/1674
75+
- https://github.com/lightninglabs/taproot-assets/pull/1784
7576

7677
- A new [address version 2 was introduced that supports grouped assets and
7778
custom (sender-defined)

tapdb/asset_minting.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ type (
116116
// database ID.
117117
ProofUpdateByID = sqlc.UpsertAssetProofByIDParams
118118

119-
// FetchPreCommitParams is a type alias for the params used to fetch
120-
// mint anchor pre-commitments.
121-
FetchPreCommitParams = sqlc.FetchMintAnchorUniCommitmentParams
119+
// FetchMintPreCommitsParams is a type alias for the params used to
120+
// fetch mint anchor supply pre-commitments.
121+
FetchMintPreCommitsParams = sqlc.FetchMintSupplyPreCommitsParams
122122

123123
// FetchAssetID is used to fetch the primary key ID of an asset, by
124124
// outpoint and tweaked script key.
@@ -132,9 +132,9 @@ type (
132132
// disk.
133133
NewAssetMeta = sqlc.UpsertAssetMetaParams
134134

135-
// MintAnchorUniCommitParams wraps the params needed to insert a new
136-
// mint anchor uni commitment on disk.
137-
MintAnchorUniCommitParams = sqlc.UpsertMintAnchorUniCommitmentParams
135+
// UpsertBatchPreCommitParams wraps the params needed to insert a new
136+
// mint batch supply pre-commit on disk.
137+
UpsertBatchPreCommitParams = sqlc.UpsertMintSupplyPreCommitParams
138138
)
139139

140140
// PendingAssetStore is a sub-set of the main sqlc.Querier interface that
@@ -248,15 +248,15 @@ type PendingAssetStore interface {
248248
FetchAssetMetaForAsset(ctx context.Context,
249249
assetID []byte) (sqlc.FetchAssetMetaForAssetRow, error)
250250

251-
// FetchMintAnchorUniCommitment fetches mint anchor pre-commitments.
252-
FetchMintAnchorUniCommitment(ctx context.Context,
253-
arg FetchPreCommitParams) (
254-
[]sqlc.FetchMintAnchorUniCommitmentRow, error)
251+
// FetchMintSupplyPreCommits fetches mint anchor pre-commitments.
252+
FetchMintSupplyPreCommits(ctx context.Context,
253+
arg FetchMintPreCommitsParams) (
254+
[]sqlc.FetchMintSupplyPreCommitsRow, error)
255255

256-
// UpsertMintAnchorUniCommitment inserts a new or updates an existing
257-
// mint anchor uni commitment on disk.
258-
UpsertMintAnchorUniCommitment(ctx context.Context,
259-
arg MintAnchorUniCommitParams) (int64, error)
256+
// UpsertMintSupplyPreCommit inserts a new or updates an existing
257+
// mint batch supply commit on disk.
258+
UpsertMintSupplyPreCommit(ctx context.Context,
259+
arg UpsertBatchPreCommitParams) (int64, error)
260260
}
261261

262262
var (
@@ -448,8 +448,8 @@ func insertMintAnchorTx(ctx context.Context, q PendingAssetStore,
448448
return fmt.Errorf("unable to encode outpoint: %w", err)
449449
}
450450

451-
_, err = q.UpsertMintAnchorUniCommitment(
452-
ctx, MintAnchorUniCommitParams{
451+
_, err = q.UpsertMintSupplyPreCommit(
452+
ctx, UpsertBatchPreCommitParams{
453453
BatchKey: rawBatchKey,
454454
TxOutputIndex: int32(preCommitOut.OutIdx),
455455
TaprootInternalKeyID: internalKeyID,
@@ -1347,8 +1347,8 @@ func marshalMintingBatch(ctx context.Context, q PendingAssetStore,
13471347
// the pre-commitment output index from the database.
13481348
var preCommitOut fn.Option[tapgarden.PreCommitmentOutput]
13491349
if dbBatch.UniverseCommitments {
1350-
fetchRes, err := q.FetchMintAnchorUniCommitment(
1351-
ctx, FetchPreCommitParams{
1350+
fetchRes, err := q.FetchMintSupplyPreCommits(
1351+
ctx, FetchMintPreCommitsParams{
13521352
BatchKey: dbBatch.RawKey,
13531353
},
13541354
)
@@ -1545,8 +1545,8 @@ func (a *AssetMintingStore) FetchDelegationKey(ctx context.Context,
15451545

15461546
readOpts := NewAssetStoreReadTx()
15471547
dbErr := a.db.ExecTx(ctx, &readOpts, func(q PendingAssetStore) error {
1548-
fetchRow, err := q.FetchMintAnchorUniCommitment(
1549-
ctx, FetchPreCommitParams{
1548+
fetchRow, err := q.FetchMintSupplyPreCommits(
1549+
ctx, FetchMintPreCommitsParams{
15501550
GroupKey: groupKeyBytes,
15511551
},
15521552
)
@@ -1644,8 +1644,8 @@ func upsertPreCommit(ctx context.Context, q PendingAssetStore,
16441644
return fmt.Errorf("unable to encode outpoint: %w", err)
16451645
}
16461646

1647-
_, err = q.UpsertMintAnchorUniCommitment(
1648-
ctx, MintAnchorUniCommitParams{
1647+
_, err = q.UpsertMintSupplyPreCommit(
1648+
ctx, UpsertBatchPreCommitParams{
16491649
BatchKey: batchKey,
16501650
TxOutputIndex: int32(preCommit.OutIdx),
16511651
TaprootInternalKeyID: internalKeyID,

tapdb/asset_minting_test.go

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,10 +1835,12 @@ func TestTapscriptTreeManager(t *testing.T) {
18351835
loadTapscriptTreeChecked(t, ctx, assetStore, tree5, tree5Hash)
18361836
}
18371837

1838-
// storeMintAnchorUniCommitment stores a mint anchor commitment in the DB.
1839-
func storeMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
1838+
// storeMintSupplyPreCommit stores a mint anchor supply pre-commitment in the
1839+
// DB.
1840+
func storeMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
18401841
batchKey []byte, txOutputIndex int32,
1841-
taprootInternalKey keychain.KeyDescriptor, groupKey []byte) {
1842+
taprootInternalKey keychain.KeyDescriptor, groupKey []byte,
1843+
outpoint wire.OutPoint) {
18421844

18431845
ctx := context.Background()
18441846

@@ -1854,12 +1856,16 @@ func storeMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
18541856
})
18551857
require.NoError(t, err)
18561858

1857-
_, err = q.UpsertMintAnchorUniCommitment(
1858-
ctx, sqlc.UpsertMintAnchorUniCommitmentParams{
1859+
opBytes, err := encodeOutpoint(outpoint)
1860+
require.NoError(t, err)
1861+
1862+
_, err = q.UpsertMintSupplyPreCommit(
1863+
ctx, UpsertBatchPreCommitParams{
18591864
BatchKey: batchKey,
18601865
TxOutputIndex: txOutputIndex,
18611866
TaprootInternalKeyID: internalKeyID,
18621867
GroupKey: groupKey,
1868+
Outpoint: opBytes,
18631869
},
18641870
)
18651871
require.NoError(t, err)
@@ -1869,19 +1875,21 @@ func storeMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
18691875
_ = assetStore.db.ExecTx(ctx, &writeTxOpts, upsertMintAnchorPreCommit)
18701876
}
18711877

1872-
// assertMintAnchorUniCommitment is a helper function that reads a mint anchor
1873-
// commitment from the DB and asserts that it matches the expected values.
1874-
func assertMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
1878+
// assertMintSupplyPreCommit is a helper function that reads a mint anchor
1879+
// supply pre-commitment from the DB and asserts that it matches the expected
1880+
// values.
1881+
func assertMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
18751882
batchKey []byte, txOutputIndex int32,
1876-
preCommitInternalKey keychain.KeyDescriptor, groupPubKeyBytes []byte) {
1883+
preCommitInternalKey keychain.KeyDescriptor, groupPubKeyBytes []byte,
1884+
outpoint wire.OutPoint) {
18771885

18781886
ctx := context.Background()
18791887
readOpts := NewAssetStoreReadTx()
18801888

1881-
var preCommit *sqlc.FetchMintAnchorUniCommitmentRow
1889+
var preCommit *sqlc.FetchMintSupplyPreCommitsRow
18821890
readMintAnchorCommitment := func(q PendingAssetStore) error {
1883-
fetchRes, err := q.FetchMintAnchorUniCommitment(
1884-
ctx, FetchPreCommitParams{
1891+
fetchRes, err := q.FetchMintSupplyPreCommits(
1892+
ctx, FetchMintPreCommitsParams{
18851893
BatchKey: batchKey,
18861894
},
18871895
)
@@ -1912,12 +1920,16 @@ func assertMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
19121920
preCommit.InternalKey.KeyFamily,
19131921
)
19141922
require.Equal(t, groupPubKeyBytes, preCommit.GroupKey)
1923+
1924+
opBytes, err := encodeOutpoint(outpoint)
1925+
require.NoError(t, err)
1926+
require.Equal(t, opBytes, preCommit.Outpoint)
19151927
}
19161928

1917-
// TestUpsertMintAnchorUniCommitment tests the UpsertMintAnchorUniCommitment
1918-
// FetchMintAnchorUniCommitment and SQL queries. In particular, it tests that
1919-
// upsert works correctly.
1920-
func TestUpsertMintAnchorUniCommitment(t *testing.T) {
1929+
// TestUpsertMintSupplyPreCommit tests the UpsertMintSupplyPreCommit and
1930+
// FetchSupplyPreCommits SQL queries. In particular, it tests that upsert works
1931+
// correctly.
1932+
func TestUpsertMintSupplyPreCommit(t *testing.T) {
19211933
t.Parallel()
19221934

19231935
ctx := context.Background()
@@ -1948,51 +1960,62 @@ func TestUpsertMintAnchorUniCommitment(t *testing.T) {
19481960
},
19491961
)
19501962

1963+
// Define pre-commit outpoint for the batch mint anchor tx.
1964+
txOutputIndex := int32(2)
1965+
txidStr := mintingBatch.GenesisPacket.FundedPsbt.Pkt.UnsignedTx.TxID()
1966+
1967+
txid, err := chainhash.NewHashFromStr(txidStr)
1968+
require.NoError(t, err)
1969+
1970+
preCommitOutpoint := wire.OutPoint{
1971+
Hash: *txid,
1972+
Index: uint32(txOutputIndex),
1973+
}
1974+
19511975
// Serialize keys into bytes for easier handling.
19521976
preCommitInternalKey, _ := test.RandKeyDesc(t)
19531977

19541978
groupPubKeyBytes := group.GroupPubKey.SerializeCompressed()
19551979

19561980
// Upsert a mint anchor commitment for the batch.
1957-
txOutputIndex := int32(2)
1958-
storeMintAnchorUniCommitment(
1981+
storeMintSupplyPreCommit(
19591982
t, *assetStore, batchKey, txOutputIndex,
1960-
preCommitInternalKey, groupPubKeyBytes,
1983+
preCommitInternalKey, groupPubKeyBytes, preCommitOutpoint,
19611984
)
19621985

19631986
// Retrieve and inspect the mint anchor commitment we just inserted.
1964-
assertMintAnchorUniCommitment(
1987+
assertMintSupplyPreCommit(
19651988
t, *assetStore, batchKey, txOutputIndex,
1966-
preCommitInternalKey, groupPubKeyBytes,
1989+
preCommitInternalKey, groupPubKeyBytes, preCommitOutpoint,
19671990
)
19681991

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

1973-
storeMintAnchorUniCommitment(
1996+
storeMintSupplyPreCommit(
19741997
t, *assetStore, batchKey, txOutputIndex, internalKey2,
1975-
groupPubKeyBytes,
1998+
groupPubKeyBytes, preCommitOutpoint,
19761999
)
19772000

1978-
assertMintAnchorUniCommitment(
2001+
assertMintSupplyPreCommit(
19792002
t, *assetStore, batchKey, txOutputIndex, internalKey2,
1980-
groupPubKeyBytes,
2003+
groupPubKeyBytes, preCommitOutpoint,
19812004
)
19822005

1983-
// Upsert-ing a new group key for the same batch should overwrite the
1984-
// existing one.
2006+
// Upsert-ing a new group key for the same pre-commit outpoint should
2007+
// overwrite the existing one.
19852008
groupPubKey2 := test.RandPubKey(t)
19862009
groupPubKey2Bytes := groupPubKey2.SerializeCompressed()
19872010

1988-
storeMintAnchorUniCommitment(
2011+
storeMintSupplyPreCommit(
19892012
t, *assetStore, batchKey, txOutputIndex, internalKey2,
1990-
groupPubKey2Bytes,
2013+
groupPubKey2Bytes, preCommitOutpoint,
19912014
)
19922015

1993-
assertMintAnchorUniCommitment(
2016+
assertMintSupplyPreCommit(
19942017
t, *assetStore, batchKey, txOutputIndex, internalKey2,
1995-
groupPubKey2Bytes,
2018+
groupPubKey2Bytes, preCommitOutpoint,
19962019
)
19972020
}
19982021

tapdb/assets_common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type UpsertAssetStore interface {
3737
// DB.
3838
UpsertChainTx(ctx context.Context, arg ChainTxParams) (int64, error)
3939

40+
// FetchChainTx fetches a chain tx from the DB.
41+
FetchChainTx(ctx context.Context, txid []byte) (ChainTx, error)
42+
4043
// UpsertGenesisAsset inserts a new or updates an existing genesis asset
4144
// (the base asset info) in the DB, and returns the primary key.
4245
//
@@ -97,6 +100,10 @@ type UpsertAssetStore interface {
97100
// UpsertTapscriptTreeRootHash inserts a new tapscript tree root hash.
98101
UpsertTapscriptTreeRootHash(ctx context.Context,
99102
arg TapscriptTreeRootHash) (int64, error)
103+
104+
// UpsertSupplyPreCommit inserts a new supply pre-commit record.
105+
UpsertSupplyPreCommit(ctx context.Context,
106+
arg sqlc.UpsertSupplyPreCommitParams) (int64, error)
100107
}
101108

102109
var (

tapdb/burn_tree.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,16 @@ func insertBurnsInternal(ctx context.Context, db BaseUniverseStore,
125125

126126
// Call the generic upsert function for the burn sub-tree to
127127
// update DB records. MetaReveal is nil for burns.
128+
uniProofType, err :=
129+
supplycommit.BurnTreeType.ToUniverseProofType()
130+
if err != nil {
131+
return nil, fmt.Errorf("unable to map burn supply "+
132+
"tree type to universe proof type: %w", err)
133+
}
134+
128135
_, err = universeUpsertProofLeaf(
129-
ctx, db, subNs, supplycommit.BurnTreeType.String(),
130-
groupKey, leafKey, leaf, nil, blockHeight,
136+
ctx, db, subNs, uniProofType, groupKey, leafKey, leaf,
137+
nil, blockHeight,
131138
)
132139
if err != nil {
133140
return nil, fmt.Errorf("unable to upsert burn "+

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/multiverse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ func (b *MultiverseStore) UpsertProofLeaf(ctx context.Context,
802802
}
803803

804804
uniProof, err = universeUpsertProofLeaf(
805-
ctx, dbTx, id.String(), id.ProofType.String(),
805+
ctx, dbTx, id.String(), id.ProofType,
806806
id.GroupKey, key, leaf, metaReveal, blockHeight,
807807
)
808808
if err != nil {
@@ -883,7 +883,7 @@ func (b *MultiverseStore) UpsertProofLeafBatch(ctx context.Context,
883883
// start with.
884884
uniProof, err := universeUpsertProofLeaf(
885885
ctx, store, item.ID.String(),
886-
item.ID.ProofType.String(),
886+
item.ID.ProofType,
887887
item.ID.GroupKey, item.Key, item.Leaf,
888888
item.MetaReveal, blockHeight,
889889
)

0 commit comments

Comments
 (0)