Skip to content

[wallet 2/3]: refactor funding logic to use allocation code #1407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 6, 2025
Merged
30 changes: 30 additions & 0 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,36 @@ func AssertNumGroups(t *testing.T, client taprpc.TaprootAssetsClient,
require.Equal(t, num, NumGroups(t, client))
}

// AssertNumBurns makes sure a given number of burns exists, then returns them.
func AssertNumBurns(t *testing.T, client taprpc.TaprootAssetsClient,
num int, req *taprpc.ListBurnsRequest) []*taprpc.AssetBurn {

ctxb := context.Background()
if req == nil {
req = &taprpc.ListBurnsRequest{}
}

var result []*taprpc.AssetBurn
err := wait.NoError(func() error {
burns, err := client.ListBurns(ctxb, req)
if err != nil {
return err
}

if len(burns.Burns) != num {
return fmt.Errorf("wanted %d burns, got %d", num,
len(burns.Burns))
}

result = burns.Burns

return nil
}, defaultTimeout)
require.NoError(t, err)

return result
}

// NumGroups returns the current number of asset groups present.
func NumGroups(t *testing.T, client taprpc.TaprootAssetsClient) int {
ctxb := context.Background()
Expand Down
53 changes: 19 additions & 34 deletions itest/burn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ func testBurnAssets(t *harnessTest) {
// - 800 units to scriptKey4
// anchor index 3 (automatic change output):
// - 300 units to new script key
outputAmounts := []uint64{1100, 1200, 1600, 800, 300}
outputAmounts := []uint64{300, 1100, 1200, 1600, 800}
vPkt := tappsbt.ForInteractiveSend(
simpleAssetID, outputAmounts[0], scriptKey1, 0, 0, 0,
simpleAssetID, outputAmounts[1], scriptKey1, 0, 0, 0,
anchorInternalKeyDesc1, asset.V0, chainParams,
)
tappsbt.AddOutput(
vPkt, outputAmounts[1], scriptKey2, 0, anchorInternalKeyDesc1,
vPkt, outputAmounts[2], scriptKey2, 0, anchorInternalKeyDesc1,
asset.V0,
)
tappsbt.AddOutput(
vPkt, outputAmounts[2], scriptKey3, 1, anchorInternalKeyDesc2,
vPkt, outputAmounts[3], scriptKey3, 1, anchorInternalKeyDesc2,
asset.V0,
)
tappsbt.AddOutput(
vPkt, outputAmounts[3], scriptKey4, 2, anchorInternalKeyDesc3,
vPkt, outputAmounts[4], scriptKey4, 2, anchorInternalKeyDesc3,
asset.V0,
)

Expand Down Expand Up @@ -120,7 +120,7 @@ func testBurnAssets(t *harnessTest) {
Asset: &taprpc.BurnAssetRequest_AssetId{
AssetId: simpleAssetID[:],
},
AmountToBurn: outputAmounts[2],
AmountToBurn: outputAmounts[3],
ConfirmationText: taprootassets.AssetBurnConfirmationText,
})
require.ErrorContains(
Expand Down Expand Up @@ -152,7 +152,7 @@ func testBurnAssets(t *harnessTest) {
AssertAssetOutboundTransferWithOutputs(
t.t, minerClient, t.tapd, burnResp.BurnTransfer,
simpleAssetGen.AssetId,
[]uint64{burnAmt, outputAmounts[2] - burnAmt}, 1, 2, 2, true,
[]uint64{outputAmounts[3] - burnAmt, burnAmt}, 1, 2, 2, true,
)

// We'll now assert that the burned asset has the correct state.
Expand All @@ -175,11 +175,8 @@ func testBurnAssets(t *harnessTest) {
t.t, t.tapd, simpleAssetGen.AssetId, simpleAsset.Amount-burnAmt,
)

burns, err := t.tapd.ListBurns(ctxt, &taprpc.ListBurnsRequest{})
require.NoError(t.t, err)

require.Len(t.t, burns.Burns, 1)
burn := burns.Burns[0]
burns := AssertNumBurns(t.t, t.tapd, 1, nil)
burn := burns[0]
require.Equal(t.t, uint64(burnAmt), burn.Amount)
require.Equal(t.t, burnResp.BurnTransfer.AnchorTxHash, burn.AnchorTxid)
require.Equal(t.t, burn.AssetId, simpleAssetID[:])
Expand All @@ -188,7 +185,7 @@ func testBurnAssets(t *harnessTest) {
// The burned asset should be pruned from the tree when we next spend
// the anchor output it was in (together with the change). So let's test
// that we can successfully spend the change output.
secondSendAmt := outputAmounts[2] - burnAmt
secondSendAmt := outputAmounts[3] - burnAmt
fullSendAddr, stream := NewAddrWithEventStream(
t.t, t.tapd, &taprpc.NewAddrRequest{
AssetId: simpleAssetGen.AssetId,
Expand Down Expand Up @@ -232,7 +229,7 @@ func testBurnAssets(t *harnessTest) {
// two largest inputs we have, the one over 1500 we sent above and the
// 1200 from the initial fan out transfer.
const changeAmt = 300
multiBurnAmt := outputAmounts[1] + secondSendAmt - changeAmt
multiBurnAmt := outputAmounts[2] + secondSendAmt - changeAmt
burnResp, err = t.tapd.BurnAsset(ctxt, &taprpc.BurnAssetRequest{
Asset: &taprpc.BurnAssetRequest_AssetId{
AssetId: simpleAssetGen.AssetId,
Expand All @@ -250,7 +247,7 @@ func testBurnAssets(t *harnessTest) {
AssertAssetOutboundTransferWithOutputs(
t.t, minerClient, t.tapd, burnResp.BurnTransfer,
simpleAssetGen.AssetId,
[]uint64{multiBurnAmt, changeAmt}, 4, 5, 2, true,
[]uint64{changeAmt, multiBurnAmt}, 4, 5, 2, true,
)

// Our final asset balance should be reduced by both successful burn
Expand Down Expand Up @@ -290,18 +287,15 @@ func testBurnAssets(t *harnessTest) {
AssertAssetOutboundTransferWithOutputs(
t.t, minerClient, t.tapd, burnResp.BurnTransfer,
simpleGroupGen.AssetId,
[]uint64{burnAmt, simpleGroup.Amount - burnAmt}, 5, 6, 2, true,
[]uint64{simpleGroup.Amount - burnAmt, burnAmt}, 5, 6, 2, true,
)
AssertBalanceByID(
t.t, t.tapd, simpleGroupGen.AssetId, simpleGroup.Amount-burnAmt,
)

burns, err = t.tapd.ListBurns(ctxt, &taprpc.ListBurnsRequest{})
require.NoError(t.t, err)

require.Len(t.t, burns.Burns, 4)
burns = AssertNumBurns(t.t, t.tapd, 4, nil)
var groupBurn *taprpc.AssetBurn
for _, b := range burns.Burns {
for _, b := range burns {
if bytes.Equal(b.AssetId, simpleGroupGen.AssetId) {
groupBurn = b
}
Expand Down Expand Up @@ -356,30 +350,21 @@ func testBurnAssets(t *harnessTest) {

// Fetch the burns related to the simple asset id, which should have a
// total of 2 burns (tc1 & tc4).
burns, err = t.tapd.ListBurns(ctxt, &taprpc.ListBurnsRequest{
AssertNumBurns(t.t, t.tapd, 2, &taprpc.ListBurnsRequest{
AssetId: simpleAssetGen.AssetId,
})
require.NoError(t.t, err)

require.Len(t.t, burns.Burns, 2)

// Fetch the burns related to the group key of the grouped asset in tc5.
// There should be 1 burn.
burns, err = t.tapd.ListBurns(ctxt, &taprpc.ListBurnsRequest{
AssertNumBurns(t.t, t.tapd, 1, &taprpc.ListBurnsRequest{
TweakedGroupKey: simpleGroup.AssetGroup.TweakedGroupKey,
})
require.NoError(t.t, err)

require.Len(t.t, burns.Burns, 1)

// Fetch the burns associated with the txhash of the burn in tc5. There
// should be 1 burn returned.
burns, err = t.tapd.ListBurns(ctxt, &taprpc.ListBurnsRequest{
AssertNumBurns(t.t, t.tapd, 1, &taprpc.ListBurnsRequest{
AnchorTxid: groupBurnTxHash,
})
require.NoError(t.t, err)

require.Len(t.t, burns.Burns, 1)
}

// testBurnGroupedAssets tests that some amount of an asset from an asset group
Expand Down Expand Up @@ -464,7 +449,7 @@ func testBurnGroupedAssets(t *harnessTest) {
// Assert that the asset burn transfer occurred correctly.
AssertAssetOutboundTransferWithOutputs(
t.t, miner, t.tapd, burnResp.BurnTransfer,
burnAssetID, []uint64{burnAmt, postBurnAmt}, 0, 1, 2, true,
burnAssetID, []uint64{postBurnAmt, burnAmt}, 0, 1, 2, true,
)

// Ensure that the burnt asset has the correct state.
Expand Down
39 changes: 25 additions & 14 deletions itest/psbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ func runPsbtInteractiveSplitSendTest(ctxt context.Context, t *harnessTest,
ConfirmAndAssertOutboundTransferWithOutputs(
t.t, t.lndHarness.Miner().Client, sender,
sendResp, genInfo.AssetId,
[]uint64{sendAmt, changeAmt}, i/2, (i/2)+1,
[]uint64{changeAmt, sendAmt}, i/2, (i/2)+1,
numOutputs,
)

Expand Down Expand Up @@ -1015,6 +1015,17 @@ func testPsbtInteractiveAltLeafAnchoring(t *harnessTest) {
vPkt.Outputs[1].AltLeaves = nil

fundResp := fundPacket(t, sender, vPkt)

// The funding added a change output at the first index. So all the
// indexes will be shifted below. The output with index 1 becomes index
// 2 (where we cleared the alt leaves).
fundedvPkt, err := tappsbt.Decode(fundResp.FundedPsbt)
require.NoError(t.t, err)
require.Len(t.t, fundedvPkt.Outputs, 3)
require.Nil(t.t, fundedvPkt.Outputs[0].AltLeaves)
require.NotNil(t.t, fundedvPkt.Outputs[1].AltLeaves)
require.Nil(t.t, fundedvPkt.Outputs[2].AltLeaves)

signActiveResp, err := sender.SignVirtualPsbt(
ctxt, &wrpc.SignVirtualPsbtRequest{
FundedPsbt: fundResp.FundedPsbt,
Expand All @@ -1037,7 +1048,7 @@ func testPsbtInteractiveAltLeafAnchoring(t *harnessTest) {
require.NoError(t.t, err)

signedvPktCopy := signedvPkt.Copy()
require.NoError(t.t, signedvPkt.Outputs[1].SetAltLeaves(altLeaves3))
require.NoError(t.t, signedvPkt.Outputs[2].SetAltLeaves(altLeaves3))
signedvPktBytes, err := tappsbt.Encode(signedvPkt)
require.NoError(t.t, err)

Expand Down Expand Up @@ -1075,7 +1086,7 @@ func testPsbtInteractiveAltLeafAnchoring(t *harnessTest) {

// Now, let's set non-conflicting altLeaves for the second vOutput, and
// complete the transfer via the PSBT flow. This should succeed.
require.NoError(t.t, signedvPktCopy.Outputs[1].SetAltLeaves(altLeaves2))
require.NoError(t.t, signedvPktCopy.Outputs[2].SetAltLeaves(altLeaves2))
signedvPktBytes, err = tappsbt.Encode(signedvPktCopy)

require.NoError(t.t, err)
Expand Down Expand Up @@ -1105,7 +1116,7 @@ func testPsbtInteractiveAltLeafAnchoring(t *harnessTest) {
)

expectedAmounts := []uint64{
partialAmt, partialAmt * 2, partialAmt,
partialAmt, partialAmt, partialAmt * 2,
}
ConfirmAndAssertOutboundTransferWithOutputs(
t.t, t.lndHarness.Miner().Client, sender, publishResp,
Expand Down Expand Up @@ -1136,8 +1147,8 @@ func testPsbtInteractiveAltLeafAnchoring(t *harnessTest) {
string(receiverScriptKey2Bytes): allAltLeaves,
}

for _, asset := range receiverAssets.Assets {
AssertProofAltLeaves(t.t, receiver, asset, leafMap)
for _, receiverAsset := range receiverAssets.Assets {
AssertProofAltLeaves(t.t, receiver, receiverAsset, leafMap)
}
}

Expand Down Expand Up @@ -1215,7 +1226,7 @@ func testPsbtInteractiveTapscriptSibling(t *harnessTest) {

ConfirmAndAssertOutboundTransferWithOutputs(
t.t, t.lndHarness.Miner().Client, alice, sendResp,
genInfo.AssetId, []uint64{sendAmt, changeAmt}, 0, 1, 2,
genInfo.AssetId, []uint64{changeAmt, sendAmt}, 0, 1, 2,
)

// This is an interactive transfer, so we do need to manually send the
Expand Down Expand Up @@ -1314,9 +1325,9 @@ func testPsbtMultiSend(t *harnessTest) {
senderScriptKey2, _ := DeriveKeys(t.t, sender)

// We create the output at anchor index 0 for the first address.
outputAmounts := []uint64{1200, 1300, 1400, 800, 300}
outputAmounts := []uint64{300, 1200, 1300, 1400, 800}
vPkt := tappsbt.ForInteractiveSend(
id, outputAmounts[0], receiverScriptKey1, 0, 0, 0,
id, outputAmounts[1], receiverScriptKey1, 0, 0, 0,
receiverAnchorIntKeyDesc1, asset.V0, chainParams,
)

Expand All @@ -1325,15 +1336,15 @@ func testPsbtMultiSend(t *harnessTest) {
// still leave 300 units as change which we expect to end up at anchor
// index 3.
tappsbt.AddOutput(
vPkt, outputAmounts[1], receiverScriptKey2, 1,
vPkt, outputAmounts[2], receiverScriptKey2, 1,
receiverAnchorIntKeyDesc2, asset.V0,
)
tappsbt.AddOutput(
vPkt, outputAmounts[2], senderScriptKey1, 2,
vPkt, outputAmounts[3], senderScriptKey1, 2,
senderAnchorIntKeyDesc1, asset.V0,
)
tappsbt.AddOutput(
vPkt, outputAmounts[3], senderScriptKey2, 2,
vPkt, outputAmounts[4], senderScriptKey2, 2,
senderAnchorIntKeyDesc1, asset.V0,
)

Expand Down Expand Up @@ -1426,7 +1437,7 @@ func testPsbtMultiSend(t *harnessTest) {
// that shared the anchor output and the other one is treated as a
// passive asset.
sendAssetAndAssert(
ctxt, t, t.tapd, secondTapd, outputAmounts[2], 0,
ctxt, t, t.tapd, secondTapd, outputAmounts[3], 0,
genInfo, rpcAssets[0], 2, 3, 2,
)
}
Expand Down Expand Up @@ -1632,7 +1643,7 @@ func testMultiInputPsbtSingleAssetID(t *harnessTest) {
ConfirmAndAssertOutboundTransferWithOutputs(
t.t, t.lndHarness.Miner().Client, secondaryTapd,
sendResp, genInfo.AssetId,
[]uint64{sendAmt, changeAmt}, currentTransferIdx, numTransfers,
[]uint64{changeAmt, sendAmt}, currentTransferIdx, numTransfers,
numOutputs,
)

Expand Down
Loading
Loading