Skip to content

Commit 46f9bb8

Browse files
committed
tapdb: add managed utxo for random genesis asset
1 parent b51eeeb commit 46f9bb8

File tree

1 file changed

+97
-5
lines changed

1 file changed

+97
-5
lines changed

tapdb/asset_minting_test.go

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,13 @@ func storeGroupGenesis(t *testing.T, ctx context.Context, initGen asset.Genesis,
150150
_, err = maybeUpsertAssetMeta(ctx, q, &assetGen, nil)
151151
require.NoError(t, err)
152152

153-
_, _, err := upsertAssetsWithGenesis(
153+
// Insert a random managed UTXO.
154+
utxoID := addRandomManagedUTXO(t, ctx, q, initialAsset)
155+
156+
_, _, err = upsertAssetsWithGenesis(
154157
ctx, q, assetGen.FirstPrevOut,
155-
[]*asset.Asset{initialAsset}, nil,
158+
[]*asset.Asset{initialAsset},
159+
[]sql.NullInt64{sqlInt64(utxoID)},
156160
)
157161
require.NoError(t, err)
158162
return nil
@@ -168,6 +172,85 @@ func storeGroupGenesis(t *testing.T, ctx context.Context, initGen asset.Genesis,
168172
}
169173
}
170174

175+
// addRandomManagedUTXO is a helper function that will create a random managed
176+
// UTXO for a given asset.
177+
func addRandomManagedUTXO(t *testing.T, ctx context.Context,
178+
db PendingAssetStore, asset *asset.Asset) int64 {
179+
180+
// Create the taproot asset root for the given asset.
181+
assetRoot, err := commitment.NewAssetCommitment(asset)
182+
require.NoError(t, err)
183+
184+
commitVersion := test.RandFlip(nil, fn.Ptr(commitment.TapCommitmentV2))
185+
taprootAssetCommitment, err := commitment.NewTapCommitment(
186+
commitVersion, assetRoot,
187+
)
188+
taprootAssetRoot := taprootAssetCommitment.TapscriptRoot(nil)
189+
require.NoError(t, err)
190+
191+
// Create an anchor transaction.
192+
var blockHash chainhash.Hash
193+
_, err = rand.Read(blockHash[:])
194+
require.NoError(t, err)
195+
196+
anchorTx := wire.NewMsgTx(2)
197+
anchorTx.AddTxIn(&wire.TxIn{})
198+
anchorTx.AddTxOut(&wire.TxOut{
199+
PkScript: bytes.Repeat([]byte{0x01}, 34),
200+
Value: 10,
201+
})
202+
203+
// We'll add the chain transaction to the database
204+
var anchorTxBuf bytes.Buffer
205+
err = anchorTx.Serialize(&anchorTxBuf)
206+
require.NoError(t, err)
207+
anchorTXID := anchorTx.TxHash()
208+
chainTXID, err := db.UpsertChainTx(ctx, ChainTxParams{
209+
Txid: anchorTXID[:],
210+
RawTx: anchorTxBuf.Bytes(),
211+
BlockHeight: sqlInt32(20),
212+
BlockHash: blockHash[:],
213+
TxIndex: sqlInt32(0),
214+
})
215+
require.NoError(t, err, "unable to insert chain tx: %w", err)
216+
217+
anchorPoint := wire.OutPoint{
218+
Hash: anchorTx.TxHash(),
219+
Index: 0,
220+
}
221+
outpointBytes, err := encodeOutpoint(anchorPoint)
222+
require.NoError(t, err)
223+
224+
randPubKey := test.RandPubKey(t)
225+
226+
// Insert an internal key.
227+
_, err = db.UpsertInternalKey(ctx, InternalKey{
228+
RawKey: randPubKey.SerializeCompressed(),
229+
KeyFamily: 1,
230+
KeyIndex: 2,
231+
})
232+
require.NoError(t, err)
233+
234+
// Insert the managed UTXO.
235+
managedUTXO := RawManagedUTXO{
236+
RawKey: randPubKey.SerializeCompressed(),
237+
Outpoint: outpointBytes,
238+
AmtSats: 10,
239+
TaprootAssetRoot: taprootAssetRoot[:],
240+
RootVersion: sql.NullInt16{
241+
Int16: int16(1),
242+
Valid: true,
243+
},
244+
MerkleRoot: taprootAssetRoot[:],
245+
TapscriptSibling: []byte{},
246+
TxnID: chainTXID,
247+
}
248+
utxoID, err := db.UpsertManagedUTXO(ctx, managedUTXO)
249+
require.NoError(t, err)
250+
251+
return utxoID
252+
}
253+
171254
// treeFromLeaves generates a tapscript tree in multiple forms from a list of
172255
// tapscript leaves.
173256
func treeFromLeaves(t *testing.T, leaves []txscript.TapLeaf) (chainhash.Hash,
@@ -790,6 +873,7 @@ type randAssetCtx struct {
790873
tapSiblingBytes []byte
791874
tapSiblingHash chainhash.Hash
792875
mintingBatch *tapgarden.MintingBatch
876+
groupGenesis *asset.Genesis
793877
}
794878

795879
func addRandAssets(t *testing.T, ctx context.Context,
@@ -848,6 +932,7 @@ func addRandAssets(t *testing.T, ctx context.Context,
848932
tapSiblingBytes: siblingBytes,
849933
tapSiblingHash: randSiblingHash,
850934
mintingBatch: mintingBatch,
935+
groupGenesis: group.Genesis,
851936
}
852937
}
853938

@@ -967,13 +1052,14 @@ func TestCommitBatchChainActions(t *testing.T) {
9671052
)
9681053
require.Equal(t, txIndex, extractSqlInt32[uint32](dbGenTx.TxIndex))
9691054

970-
// If we query for the set of all active assets, then we should get
971-
// back the same number of seedlings.
1055+
// If we query for the set of all active assets, then we should get back
1056+
// the number of seedlings AND also the genesis asset we have created
1057+
// with `addRandAssets`.
9721058
//
9731059
// TODO(roasbeef): move into isolated test
9741060
assets, err := confAssets.FetchAllAssets(ctx, false, false, nil)
9751061
require.NoError(t, err)
976-
require.Equal(t, numSeedlings, len(assets))
1062+
require.Equal(t, numSeedlings+1, len(assets))
9771063

9781064
// Count the number of assets with a group key. Each grouped asset
9791065
// should have a grouped genesis witness.
@@ -995,9 +1081,15 @@ func TestCommitBatchChainActions(t *testing.T) {
9951081
// All the assets should also have a matching asset version as the
9961082
// seedlings we created.
9971083
mintingBatch := randAssetCtx.mintingBatch
1084+
randomGenesisTag := randAssetCtx.groupGenesis.Tag
9981085
require.True(t, fn.All(assets, func(dbAsset *asset.ChainAsset) bool {
9991086
seedling, ok := mintingBatch.Seedlings[dbAsset.Genesis.Tag]
10001087
if !ok {
1088+
// The only asset that doesn't have a seedling is the
1089+
// random genesis asset created by `addRandAssets`
1090+
if dbAsset.Genesis.Tag == randomGenesisTag {
1091+
return true
1092+
}
10011093
t.Logf("seedling for %v not found",
10021094
dbAsset.Genesis.Tag)
10031095
return ok

0 commit comments

Comments
 (0)