Skip to content

Commit

Permalink
itest: expand testAssetBalances to check PrevId selection functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
habibitcoin committed Nov 29, 2024
1 parent bd5968c commit 25602ad
Showing 1 changed file with 95 additions and 5 deletions.
100 changes: 95 additions & 5 deletions itest/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,19 @@ func testMintBatchAndTransfer(t *harnessTest) {
require.True(t.t, proto.Equal(originalBatch, afterBatch))
}

// testAssetBalances tests the balance retrieval functionality for issued
// assets. The function mints two batches of assets and asserts if the tapcli
// `assets balance` returns the correct balances. It then funds a vPSBT, putting
// a lease on one of the two batches. It then asserts whether the endpoint still
// returns the correct balances, taking into account the `include_leased` flag.
// testAssetBalances validates the balance retrieval and virtual PSBT funding
// functionality for issued assets. The test performs the following steps:
// 1. Mints two batches of assets and verifies that the tapcli
// `assets balance` returns the correct balances.
// 2. Tests funding a vPSBT, putting a lease on one of the two batches, with
// the `FundVirtualPsbt` RPC for various scenarios:
// - Fails if the Inputs field contains a nil Outpoint.
// - Fails if the Inputs field contains an invalid Outpoint.
// - Succeeds if a valid Outpoint is provided in the Inputs field.
// - Succeeds if the Inputs field is not provided at all, using asset
// coin selection instead.
// 3. Ensures that leased assets are reflected correctly in the balance
// retrieval, with and without the `include_leased` flag.
func testAssetBalances(t *harnessTest) {
ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
Expand All @@ -650,6 +658,8 @@ func testAssetBalances(t *harnessTest) {

var (
targetAssetGenesis = targetAsset.AssetGenesis
assetId = targetAssetGenesis.AssetId
scriptKey = targetAsset.ScriptKey
aliceTapd = t.tapd
bobLnd = t.lndHarness.Bob
)
Expand All @@ -673,6 +683,86 @@ func testAssetBalances(t *harnessTest) {
recipients := map[string]uint64{
bobAddr.Encoded: bobAddr.Amount,
}
out, err := wire.NewOutPointFromString(
targetAsset.ChainAnchor.AnchorOutpoint,
)
require.NoError(t.t, err)

// 1. Fail if Inputs are provided but Outpoint is nil.
emptyOutpointPrevId := &wrpc.PrevId{
Outpoint: nil,
Id: assetId,
ScriptKey: scriptKey,
}
_, err = aliceTapd.FundVirtualPsbt(
ctxt, &wrpc.FundVirtualPsbtRequest{
Template: &wrpc.FundVirtualPsbtRequest_Raw{
Raw: &wrpc.TxTemplate{
Recipients: recipients,
Inputs: []*wrpc.PrevId{
emptyOutpointPrevId,
},
},
},
},
)
require.Error(t.t, err)
require.Contains(t.t, err.Error(), "index 0 has a nil Outpoint")

// 2. Fail if Inputs contain an invalid Outpoint.
invalidTxidPrevId := &wrpc.PrevId{
Outpoint: &taprpc.OutPoint{
Txid: []byte("invalid_txid"),
OutputIndex: 0,
},
Id: assetId,
ScriptKey: scriptKey,
}
_, err = aliceTapd.FundVirtualPsbt(
ctxt, &wrpc.FundVirtualPsbtRequest{
Template: &wrpc.FundVirtualPsbtRequest_Raw{
Raw: &wrpc.TxTemplate{
Recipients: recipients,
Inputs: []*wrpc.PrevId{
invalidTxidPrevId,
},
},
},
},
)
require.Error(t.t, err)
require.Contains(t.t, err.Error(), "invalid Txid length")

// 3. Succeed if a valid Outpoint is provided.
validOutpointPrevId := &wrpc.PrevId{
Outpoint: &taprpc.OutPoint{
Txid: out.Hash.CloneBytes(),
OutputIndex: out.Index,
},
Id: assetId,
ScriptKey: scriptKey,
}
_, err = aliceTapd.FundVirtualPsbt(
ctxt, &wrpc.FundVirtualPsbtRequest{
Template: &wrpc.FundVirtualPsbtRequest_Raw{
Raw: &wrpc.TxTemplate{
Recipients: recipients,
Inputs: []*wrpc.PrevId{
validOutpointPrevId,
},
},
},
},
)
require.NoError(t.t, err)

// 4. Succeed if no Inputs are provided.
// Unlock the input so that it can be spent again.
_, err = aliceTapd.RemoveUTXOLease(ctxb, &wrpc.RemoveUTXOLeaseRequest{
Outpoint: validOutpointPrevId.Outpoint,
})
require.NoError(t.t, err)

_, err = aliceTapd.FundVirtualPsbt(
ctxt, &wrpc.FundVirtualPsbtRequest{
Template: &wrpc.FundVirtualPsbtRequest_Raw{
Expand Down

0 comments on commit 25602ad

Please sign in to comment.