@@ -127,6 +127,10 @@ type (
127
127
// NewAssetMeta wraps the params needed to insert a new asset meta on
128
128
// disk.
129
129
NewAssetMeta = sqlc.UpsertAssetMetaParams
130
+
131
+ // MintAnchorUniCommitParams wraps the params needed to insert a new
132
+ // mint anchor uni commitment on disk.
133
+ MintAnchorUniCommitParams = sqlc.UpsertMintAnchorUniCommitmentParams
130
134
)
131
135
132
136
// PendingAssetStore is a sub-set of the main sqlc.Querier interface that
@@ -244,6 +248,11 @@ type PendingAssetStore interface {
244
248
// for a given batch.
245
249
FetchMintAnchorUniCommitment (ctx context.Context ,
246
250
batchID int32 ) (sqlc.MintAnchorUniCommitment , error )
251
+
252
+ // UpsertMintAnchorUniCommitment inserts a new or updates an existing
253
+ // mint anchor uni commitment on disk.
254
+ UpsertMintAnchorUniCommitment (ctx context.Context ,
255
+ arg MintAnchorUniCommitParams ) (int64 , error )
247
256
}
248
257
249
258
var (
@@ -317,6 +326,77 @@ type OptionalSeedlingFields struct {
317
326
GroupAnchorID sql.NullInt64
318
327
}
319
328
329
+ // insertMintAnchorTx inserts a mint anchor transaction into the database.
330
+ func insertMintAnchorTx (ctx context.Context , q PendingAssetStore ,
331
+ anchorPackage tapgarden.FundedMintAnchorPsbt ,
332
+ batchKey btcec.PublicKey , genesisOutpoint wire.OutPoint ) error {
333
+
334
+ // Ensure that the genesis point is in the database.
335
+ genesisPointDbID , err := upsertGenesisPoint (
336
+ ctx , q , genesisOutpoint ,
337
+ )
338
+ if err != nil {
339
+ return fmt .Errorf ("%w: %w" , ErrUpsertGenesisPoint , err )
340
+ }
341
+
342
+ var psbtBuf bytes.Buffer
343
+ err = anchorPackage .Pkt .Serialize (& psbtBuf )
344
+ if err != nil {
345
+ return fmt .Errorf ("%w: %w" , ErrEncodePsbt , err )
346
+ }
347
+
348
+ rawBatchKey := batchKey .SerializeCompressed ()
349
+ enableUniverseCommitments := anchorPackage .PreCommitmentOutput .IsSome ()
350
+
351
+ batchID , err := q .BindMintingBatchWithTx (ctx , BatchChainUpdate {
352
+ RawKey : rawBatchKey ,
353
+ MintingTxPsbt : psbtBuf .Bytes (),
354
+ ChangeOutputIndex : sqlInt32 (anchorPackage .ChangeOutputIndex ),
355
+ AssetsOutputIndex : sqlInt32 (anchorPackage .AssetAnchorOutIdx ),
356
+ GenesisID : sqlInt64 (genesisPointDbID ),
357
+ UniverseCommitments : enableUniverseCommitments ,
358
+ })
359
+ if err != nil {
360
+ return fmt .Errorf ("%w: %w" , ErrBindBatchTx , err )
361
+ }
362
+
363
+ // If universe commitments are not enabled for this batch, we can
364
+ // return early.
365
+ if ! enableUniverseCommitments {
366
+ return nil
367
+ }
368
+
369
+ // At this point, universe commitments are enabled for this batch, so
370
+ // we'll insert the mint anchor uni commitment record.
371
+ preCommitOut , err := anchorPackage .PreCommitmentOutput .UnwrapOrErr (
372
+ fmt .Errorf ("pre-commitment outpoint bundle not set" ),
373
+ )
374
+ if err != nil {
375
+ return err
376
+ }
377
+
378
+ // Serialize internal key.
379
+ internalKey := preCommitOut .InternalKey .SerializeCompressed ()
380
+
381
+ // Serialize group key.
382
+ groupPubKey := preCommitOut .GroupPubKey .SerializeCompressed ()
383
+
384
+ _ , err = q .UpsertMintAnchorUniCommitment (
385
+ ctx , MintAnchorUniCommitParams {
386
+ BatchID : int32 (batchID ),
387
+ TxOutputIndex : int32 (preCommitOut .OutIdx ),
388
+ TaprootInternalKey : internalKey ,
389
+ GroupKey : groupPubKey ,
390
+ },
391
+ )
392
+ if err != nil {
393
+ return fmt .Errorf ("unable to insert mint anchor uni " +
394
+ "commitment: %w" , err )
395
+ }
396
+
397
+ return nil
398
+ }
399
+
320
400
// CommitMintingBatch commits a new minting batch to disk along with any
321
401
// seedlings specified as part of the batch. A new internal key is also
322
402
// created, with the batch referencing that internal key. This internal key
@@ -371,31 +451,16 @@ func (a *AssetMintingStore) CommitMintingBatch(ctx context.Context,
371
451
if newBatch .GenesisPacket != nil {
372
452
genesisPacket := newBatch .GenesisPacket
373
453
genesisTx := genesisPacket .Pkt .UnsignedTx
374
- changeIdx := genesisPacket .ChangeOutputIndex
375
454
genesisOutpoint := genesisTx .TxIn [0 ].PreviousOutPoint
376
455
377
- var psbtBuf bytes.Buffer
378
- err := genesisPacket .Pkt .Serialize (& psbtBuf )
379
- if err != nil {
380
- return fmt .Errorf ("%w: %w" , ErrEncodePsbt , err )
381
- }
382
-
383
- genesisPointID , err := upsertGenesisPoint (
384
- ctx , q , genesisOutpoint ,
456
+ // Insert the batch transaction.
457
+ err = insertMintAnchorTx (
458
+ ctx , q , * genesisPacket ,
459
+ * newBatch .BatchKey .PubKey , genesisOutpoint ,
385
460
)
386
461
if err != nil {
387
- return fmt .Errorf ("%w: %w" ,
388
- ErrUpsertGenesisPoint , err )
389
- }
390
-
391
- _ , err = q .BindMintingBatchWithTx (ctx , BatchChainUpdate {
392
- RawKey : rawBatchKey ,
393
- MintingTxPsbt : psbtBuf .Bytes (),
394
- ChangeOutputIndex : sqlInt32 (changeIdx ),
395
- GenesisID : sqlInt64 (genesisPointID ),
396
- })
397
- if err != nil {
398
- return fmt .Errorf ("%w: %w" , ErrBindBatchTx , err )
462
+ return fmt .Errorf ("unable to insert mint " +
463
+ "anchor tx: %w" , err )
399
464
}
400
465
}
401
466
@@ -1340,31 +1405,19 @@ func (a *AssetMintingStore) CommitBatchTx(ctx context.Context,
1340
1405
genesisPacket tapgarden.FundedMintAnchorPsbt ) error {
1341
1406
1342
1407
genesisOutpoint := genesisPacket .Pkt .UnsignedTx .TxIn [0 ].PreviousOutPoint
1343
- rawBatchKey := batchKey .SerializeCompressed ()
1344
-
1345
- var psbtBuf bytes.Buffer
1346
- if err := genesisPacket .Pkt .Serialize (& psbtBuf ); err != nil {
1347
- return fmt .Errorf ("%w: %w" , ErrEncodePsbt , err )
1348
- }
1349
1408
1350
1409
var writeTxOpts AssetStoreTxOptions
1351
1410
return a .db .ExecTx (ctx , & writeTxOpts , func (q PendingAssetStore ) error {
1352
- genesisPointID , err := upsertGenesisPoint (
1353
- ctx , q , genesisOutpoint ,
1411
+ // Insert the batch transaction.
1412
+ err := insertMintAnchorTx (
1413
+ ctx , q , genesisPacket , * batchKey , genesisOutpoint ,
1354
1414
)
1355
1415
if err != nil {
1356
- return fmt .Errorf ("%w: %w" , ErrUpsertGenesisPoint , err )
1416
+ return fmt .Errorf ("unable to insert mint anchor " +
1417
+ "tx: %w" , err )
1357
1418
}
1358
1419
1359
- _ , err = q .BindMintingBatchWithTx (ctx , BatchChainUpdate {
1360
- RawKey : rawBatchKey ,
1361
- MintingTxPsbt : psbtBuf .Bytes (),
1362
- ChangeOutputIndex : sqlInt32 (
1363
- genesisPacket .ChangeOutputIndex ,
1364
- ),
1365
- GenesisID : sqlInt64 (genesisPointID ),
1366
- })
1367
- return err
1420
+ return nil
1368
1421
})
1369
1422
}
1370
1423
@@ -1501,31 +1554,22 @@ func (a *AssetMintingStore) AddSproutsToBatch(ctx context.Context,
1501
1554
1502
1555
var writeTxOpts AssetStoreTxOptions
1503
1556
return a .db .ExecTx (ctx , & writeTxOpts , func (q PendingAssetStore ) error {
1504
- genesisPointID , _ , err := upsertAssetsWithGenesis (
1557
+ // Upsert the assets with genesis.
1558
+ _ , _ , err := upsertAssetsWithGenesis (
1505
1559
ctx , q , genesisOutpoint , sortedAssets , nil ,
1506
1560
)
1507
1561
if err != nil {
1508
1562
return fmt .Errorf ("error inserting assets with " +
1509
1563
"genesis: %w" , err )
1510
1564
}
1511
1565
1512
- // With all the assets inserted, we'll now update the
1513
- // corresponding batch that references all these assets with
1514
- // the genesis packet, and genesis point information.
1515
- var psbtBuf bytes.Buffer
1516
- if err := genesisPacket .Pkt .Serialize (& psbtBuf ); err != nil {
1517
- return fmt .Errorf ("%w: %w" , ErrEncodePsbt , err )
1518
- }
1519
- _ , err = q .BindMintingBatchWithTx (ctx , BatchChainUpdate {
1520
- RawKey : rawBatchKey ,
1521
- MintingTxPsbt : psbtBuf .Bytes (),
1522
- ChangeOutputIndex : sqlInt32 (
1523
- genesisPacket .ChangeOutputIndex ,
1524
- ),
1525
- GenesisID : sqlInt64 (genesisPointID ),
1526
- })
1566
+ // Insert the batch transaction.
1567
+ err = insertMintAnchorTx (
1568
+ ctx , q , * genesisPacket , * batchKey , genesisOutpoint ,
1569
+ )
1527
1570
if err != nil {
1528
- return fmt .Errorf ("%w: %w" , ErrBindBatchTx , err )
1571
+ return fmt .Errorf ("unable to insert mint anchor " +
1572
+ "tx: %w" , err )
1529
1573
}
1530
1574
1531
1575
// Finally, update the batch state to BatchStateCommitted.
0 commit comments