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 28, 2024
1 parent a94190f commit 338cba8
Showing 1 changed file with 92 additions and 5 deletions.
97 changes: 92 additions & 5 deletions itest/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/taprpc"
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
Expand Down Expand Up @@ -623,11 +624,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 +659,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 +684,82 @@ 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.
_, err = aliceTapd.FundVirtualPsbt(
ctxt, &wrpc.FundVirtualPsbtRequest{
Template: &wrpc.FundVirtualPsbtRequest_Raw{
Raw: &wrpc.TxTemplate{
Recipients: recipients,
Inputs: []*wrpc.PrevId{
{
Outpoint: nil,
Id: assetId,
ScriptKey: scriptKey,
},
},
},
},
},
)
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.
_, err = aliceTapd.FundVirtualPsbt(
ctxt, &wrpc.FundVirtualPsbtRequest{
Template: &wrpc.FundVirtualPsbtRequest_Raw{
Raw: &wrpc.TxTemplate{
Recipients: recipients,
Inputs: []*wrpc.PrevId{
{
Outpoint: &taprpc.OutPoint{
Txid: []byte("invalid_txid"),
OutputIndex: 0,
},
Id: assetId,
ScriptKey: scriptKey,
},
},
},
},
},
)
require.Error(t.t, err)
require.Contains(t.t, err.Error(), "invalid Txid length")

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

// 4. Succeed if no Inputs are provided.
// Unlock the input so that it can be spent again.
walletAnchor := tapgarden.NewMockWalletAnchor()
err = walletAnchor.UnlockInput(ctxb, *out)
require.NoError(t.t, err)

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

0 comments on commit 338cba8

Please sign in to comment.