Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/release-notes-0.7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
- https://github.com/lightninglabs/taproot-assets/pull/1716
- https://github.com/lightninglabs/taproot-assets/pull/1675
- https://github.com/lightninglabs/taproot-assets/pull/1674
- https://github.com/lightninglabs/taproot-assets/pull/1784

- A new [address version 2 was introduced that supports grouped assets and
custom (sender-defined)
Expand Down
44 changes: 22 additions & 22 deletions tapdb/asset_minting.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ type (
// database ID.
ProofUpdateByID = sqlc.UpsertAssetProofByIDParams

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

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

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

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

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

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

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

_, err = q.UpsertMintAnchorUniCommitment(
ctx, MintAnchorUniCommitParams{
_, err = q.UpsertMintSupplyPreCommit(
ctx, UpsertBatchPreCommitParams{
BatchKey: rawBatchKey,
TxOutputIndex: int32(preCommitOut.OutIdx),
TaprootInternalKeyID: internalKeyID,
Expand Down Expand Up @@ -1347,8 +1347,8 @@ func marshalMintingBatch(ctx context.Context, q PendingAssetStore,
// the pre-commitment output index from the database.
var preCommitOut fn.Option[tapgarden.PreCommitmentOutput]
if dbBatch.UniverseCommitments {
fetchRes, err := q.FetchMintAnchorUniCommitment(
ctx, FetchPreCommitParams{
fetchRes, err := q.FetchMintSupplyPreCommits(
ctx, FetchMintPreCommitsParams{
BatchKey: dbBatch.RawKey,
},
)
Expand Down Expand Up @@ -1545,8 +1545,8 @@ func (a *AssetMintingStore) FetchDelegationKey(ctx context.Context,

readOpts := NewAssetStoreReadTx()
dbErr := a.db.ExecTx(ctx, &readOpts, func(q PendingAssetStore) error {
fetchRow, err := q.FetchMintAnchorUniCommitment(
ctx, FetchPreCommitParams{
fetchRow, err := q.FetchMintSupplyPreCommits(
ctx, FetchMintPreCommitsParams{
GroupKey: groupKeyBytes,
},
)
Expand Down Expand Up @@ -1644,8 +1644,8 @@ func upsertPreCommit(ctx context.Context, q PendingAssetStore,
return fmt.Errorf("unable to encode outpoint: %w", err)
}

_, err = q.UpsertMintAnchorUniCommitment(
ctx, MintAnchorUniCommitParams{
_, err = q.UpsertMintSupplyPreCommit(
ctx, UpsertBatchPreCommitParams{
BatchKey: batchKey,
TxOutputIndex: int32(preCommit.OutIdx),
TaprootInternalKeyID: internalKeyID,
Expand Down
89 changes: 56 additions & 33 deletions tapdb/asset_minting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1835,10 +1835,12 @@ func TestTapscriptTreeManager(t *testing.T) {
loadTapscriptTreeChecked(t, ctx, assetStore, tree5, tree5Hash)
}

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

ctx := context.Background()

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

_, err = q.UpsertMintAnchorUniCommitment(
ctx, sqlc.UpsertMintAnchorUniCommitmentParams{
opBytes, err := encodeOutpoint(outpoint)
require.NoError(t, err)

_, err = q.UpsertMintSupplyPreCommit(
ctx, UpsertBatchPreCommitParams{
BatchKey: batchKey,
TxOutputIndex: txOutputIndex,
TaprootInternalKeyID: internalKeyID,
GroupKey: groupKey,
Outpoint: opBytes,
},
)
require.NoError(t, err)
Expand All @@ -1869,19 +1875,21 @@ func storeMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
_ = assetStore.db.ExecTx(ctx, &writeTxOpts, upsertMintAnchorPreCommit)
}

// assertMintAnchorUniCommitment is a helper function that reads a mint anchor
// commitment from the DB and asserts that it matches the expected values.
func assertMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
// assertMintSupplyPreCommit is a helper function that reads a mint anchor
// supply pre-commitment from the DB and asserts that it matches the expected
// values.
func assertMintSupplyPreCommit(t *testing.T, assetStore AssetMintingStore,
batchKey []byte, txOutputIndex int32,
preCommitInternalKey keychain.KeyDescriptor, groupPubKeyBytes []byte) {
preCommitInternalKey keychain.KeyDescriptor, groupPubKeyBytes []byte,
outpoint wire.OutPoint) {

ctx := context.Background()
readOpts := NewAssetStoreReadTx()

var preCommit *sqlc.FetchMintAnchorUniCommitmentRow
var preCommit *sqlc.FetchMintSupplyPreCommitsRow
readMintAnchorCommitment := func(q PendingAssetStore) error {
fetchRes, err := q.FetchMintAnchorUniCommitment(
ctx, FetchPreCommitParams{
fetchRes, err := q.FetchMintSupplyPreCommits(
ctx, FetchMintPreCommitsParams{
BatchKey: batchKey,
},
)
Expand Down Expand Up @@ -1912,12 +1920,16 @@ func assertMintAnchorUniCommitment(t *testing.T, assetStore AssetMintingStore,
preCommit.InternalKey.KeyFamily,
)
require.Equal(t, groupPubKeyBytes, preCommit.GroupKey)

opBytes, err := encodeOutpoint(outpoint)
require.NoError(t, err)
require.Equal(t, opBytes, preCommit.Outpoint)
}

// TestUpsertMintAnchorUniCommitment tests the UpsertMintAnchorUniCommitment
// FetchMintAnchorUniCommitment and SQL queries. In particular, it tests that
// upsert works correctly.
func TestUpsertMintAnchorUniCommitment(t *testing.T) {
// TestUpsertMintSupplyPreCommit tests the UpsertMintSupplyPreCommit and
// FetchSupplyPreCommits SQL queries. In particular, it tests that upsert works
// correctly.
func TestUpsertMintSupplyPreCommit(t *testing.T) {
t.Parallel()

ctx := context.Background()
Expand Down Expand Up @@ -1948,51 +1960,62 @@ func TestUpsertMintAnchorUniCommitment(t *testing.T) {
},
)

// Define pre-commit outpoint for the batch mint anchor tx.
txOutputIndex := int32(2)
txidStr := mintingBatch.GenesisPacket.FundedPsbt.Pkt.UnsignedTx.TxID()

txid, err := chainhash.NewHashFromStr(txidStr)
require.NoError(t, err)

preCommitOutpoint := wire.OutPoint{
Hash: *txid,
Index: uint32(txOutputIndex),
}

// Serialize keys into bytes for easier handling.
preCommitInternalKey, _ := test.RandKeyDesc(t)

groupPubKeyBytes := group.GroupPubKey.SerializeCompressed()

// Upsert a mint anchor commitment for the batch.
txOutputIndex := int32(2)
storeMintAnchorUniCommitment(
storeMintSupplyPreCommit(
t, *assetStore, batchKey, txOutputIndex,
preCommitInternalKey, groupPubKeyBytes,
preCommitInternalKey, groupPubKeyBytes, preCommitOutpoint,
)

// Retrieve and inspect the mint anchor commitment we just inserted.
assertMintAnchorUniCommitment(
assertMintSupplyPreCommit(
t, *assetStore, batchKey, txOutputIndex,
preCommitInternalKey, groupPubKeyBytes,
preCommitInternalKey, groupPubKeyBytes, preCommitOutpoint,
)

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

storeMintAnchorUniCommitment(
storeMintSupplyPreCommit(
t, *assetStore, batchKey, txOutputIndex, internalKey2,
groupPubKeyBytes,
groupPubKeyBytes, preCommitOutpoint,
)

assertMintAnchorUniCommitment(
assertMintSupplyPreCommit(
t, *assetStore, batchKey, txOutputIndex, internalKey2,
groupPubKeyBytes,
groupPubKeyBytes, preCommitOutpoint,
)

// Upsert-ing a new group key for the same batch should overwrite the
// existing one.
// Upsert-ing a new group key for the same pre-commit outpoint should
// overwrite the existing one.
groupPubKey2 := test.RandPubKey(t)
groupPubKey2Bytes := groupPubKey2.SerializeCompressed()

storeMintAnchorUniCommitment(
storeMintSupplyPreCommit(
t, *assetStore, batchKey, txOutputIndex, internalKey2,
groupPubKey2Bytes,
groupPubKey2Bytes, preCommitOutpoint,
)

assertMintAnchorUniCommitment(
assertMintSupplyPreCommit(
t, *assetStore, batchKey, txOutputIndex, internalKey2,
groupPubKey2Bytes,
groupPubKey2Bytes, preCommitOutpoint,
)
}

Expand Down
7 changes: 7 additions & 0 deletions tapdb/assets_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type UpsertAssetStore interface {
// DB.
UpsertChainTx(ctx context.Context, arg ChainTxParams) (int64, error)

// FetchChainTx fetches a chain tx from the DB.
FetchChainTx(ctx context.Context, txid []byte) (ChainTx, error)

// UpsertGenesisAsset inserts a new or updates an existing genesis asset
// (the base asset info) in the DB, and returns the primary key.
//
Expand Down Expand Up @@ -97,6 +100,10 @@ type UpsertAssetStore interface {
// UpsertTapscriptTreeRootHash inserts a new tapscript tree root hash.
UpsertTapscriptTreeRootHash(ctx context.Context,
arg TapscriptTreeRootHash) (int64, error)

// UpsertSupplyPreCommit inserts a new supply pre-commit record.
UpsertSupplyPreCommit(ctx context.Context,
arg sqlc.UpsertSupplyPreCommitParams) (int64, error)
}

var (
Expand Down
11 changes: 9 additions & 2 deletions tapdb/burn_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,16 @@ func insertBurnsInternal(ctx context.Context, db BaseUniverseStore,

// Call the generic upsert function for the burn sub-tree to
// update DB records. MetaReveal is nil for burns.
uniProofType, err :=
supplycommit.BurnTreeType.ToUniverseProofType()
if err != nil {
return nil, fmt.Errorf("unable to map burn supply "+
"tree type to universe proof type: %w", err)
}

_, err = universeUpsertProofLeaf(
ctx, db, subNs, supplycommit.BurnTreeType.String(),
groupKey, leafKey, leaf, nil, blockHeight,
ctx, db, subNs, uniProofType, groupKey, leafKey, leaf,
nil, blockHeight,
)
if err != nil {
return nil, fmt.Errorf("unable to upsert burn "+
Expand Down
2 changes: 1 addition & 1 deletion tapdb/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
// daemon.
//
// NOTE: This MUST be updated when a new migration is added.
LatestMigrationVersion = 45
LatestMigrationVersion = 46
)

// DatabaseBackend is an interface that contains all methods our different
Expand Down
4 changes: 2 additions & 2 deletions tapdb/multiverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ func (b *MultiverseStore) UpsertProofLeaf(ctx context.Context,
}

uniProof, err = universeUpsertProofLeaf(
ctx, dbTx, id.String(), id.ProofType.String(),
ctx, dbTx, id.String(), id.ProofType,
id.GroupKey, key, leaf, metaReveal, blockHeight,
)
if err != nil {
Expand Down Expand Up @@ -883,7 +883,7 @@ func (b *MultiverseStore) UpsertProofLeafBatch(ctx context.Context,
// start with.
uniProof, err := universeUpsertProofLeaf(
ctx, store, item.ID.String(),
item.ID.ProofType.String(),
item.ID.ProofType,
item.ID.GroupKey, item.Key, item.Leaf,
item.MetaReveal, blockHeight,
)
Expand Down
Loading
Loading