From 25602add62ad84b74f13445cf1c5d5fdcd428f80 Mon Sep 17 00:00:00 2001 From: habibitcoin Date: Thu, 28 Nov 2024 15:27:45 -0500 Subject: [PATCH] itest: expand testAssetBalances to check PrevId selection functionality --- itest/assets_test.go | 100 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/itest/assets_test.go b/itest/assets_test.go index a7da341e9..8cbb5912f 100644 --- a/itest/assets_test.go +++ b/itest/assets_test.go @@ -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) @@ -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 ) @@ -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{