Skip to content

Commit

Permalink
itest: add burn tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Sep 1, 2023
1 parent 739ae93 commit a735502
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 5 deletions.
8 changes: 4 additions & 4 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,23 +509,23 @@ func confirmAndAssetOutboundTransferWithOutputs(t *harnessTest,
numTransfers, numOutputs int) *wire.MsgBlock {

return assertAssetOutboundTransferWithOutputs(
t, sender, sendResp, assetID, expectedAmounts,
t, sender, sendResp.Transfer, assetID, expectedAmounts,
currentTransferIdx, numTransfers, numOutputs, true,
)
}

// assertAssetOutboundTransferWithOutputs makes sure the given outbound transfer
// has the correct state and number of outputs.
func assertAssetOutboundTransferWithOutputs(t *harnessTest,
sender *tapdHarness, sendResp *taprpc.SendAssetResponse,
sender *tapdHarness, transfer *taprpc.AssetTransfer,
assetID []byte, expectedAmounts []uint64, currentTransferIdx,
numTransfers, numOutputs int, confirm bool) *wire.MsgBlock {

ctxb := context.Background()

// Check that we now have two new outputs, and that they differ
// in outpoints and scripts.
outputs := sendResp.Transfer.Outputs
outputs := transfer.Outputs
require.Len(t.t, outputs, numOutputs)

outpoints := make(map[string]struct{})
Expand All @@ -538,7 +538,7 @@ func assertAssetOutboundTransferWithOutputs(t *harnessTest,
scripts[string(o.ScriptKey)] = struct{}{}
}

sendRespJSON, err := formatProtoJSON(sendResp)
sendRespJSON, err := formatProtoJSON(transfer)
require.NoError(t.t, err)
t.Logf("Got response from sending assets: %v", sendRespJSON)

Expand Down
127 changes: 127 additions & 0 deletions itest/burn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package itest

import (
"context"

taprootassets "github.com/lightninglabs/taproot-assets"
"github.com/lightninglabs/taproot-assets/address"
"github.com/lightninglabs/taproot-assets/tapfreighter"
"github.com/lightninglabs/taproot-assets/tappsbt"
"github.com/lightninglabs/taproot-assets/taprpc"
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
"github.com/stretchr/testify/require"
)

// testBurnAssets tests that we're able to mint assets and then burn assets
// again.
func testBurnAssets(t *harnessTest) {
rpcSimpleAssets := mintAssetsConfirmBatch(t, t.tapd, simpleAssets)

// We first fan out the assets we have to different outputs.
var (
chainParams = &address.RegressionNetTap
simpleAsset = rpcSimpleAssets[0]
genInfo = simpleAsset.AssetGenesis
id [32]byte
)
copy(id[:], genInfo.AssetId)

ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
defer cancel()

// We need to derive two sets of keys, one for the new script key and
// one for the internal key each.
scriptKey1, anchorInternalKeyDesc1 := deriveKeys(t.t, t.tapd)
scriptKey2, anchorInternalKeyDesc2 := deriveKeys(t.t, t.tapd)
scriptKey3, anchorInternalKeyDesc3 := deriveKeys(t.t, t.tapd)
scriptKey4, _ := deriveKeys(t.t, t.tapd)

// We create the following outputs:
// anchor index 0 (anchor internal key 1):
// - 1200 units to scriptKey1
// - 1300 units to scriptKey2
// anchor index 1 (anchor internal key 2):
// - 1400 units to scriptKey3
// anchor index 2 (anchor internal key 3):
// - 800 units to scriptKey4
// anchor index 3 (automatic change output):
// - 300 units to new script key
outputAmounts := []uint64{1200, 1300, 1400, 800, 300}
vPkt := tappsbt.ForInteractiveSend(
id, outputAmounts[0], scriptKey1, 0, anchorInternalKeyDesc1,
chainParams,
)
tappsbt.AddOutput(
vPkt, outputAmounts[1], scriptKey2, 0, anchorInternalKeyDesc1,
)
tappsbt.AddOutput(
vPkt, outputAmounts[2], scriptKey3, 1, anchorInternalKeyDesc2,
)
tappsbt.AddOutput(
vPkt, outputAmounts[3], scriptKey4, 2, anchorInternalKeyDesc3,
)

// Next, we'll attempt to complete a transfer with PSBTs from our node
// to ourselves, using the partial amount.
fundResp := fundPacket(t, t.tapd, vPkt)
signResp, err := t.tapd.SignVirtualPsbt(
ctxt, &wrpc.SignVirtualPsbtRequest{
FundedPsbt: fundResp.FundedPsbt,
},
)
require.NoError(t.t, err)

// Now we'll attempt to complete the transfer.
sendResp, err := t.tapd.AnchorVirtualPsbts(
ctxt, &wrpc.AnchorVirtualPsbtsRequest{
VirtualPsbts: [][]byte{signResp.SignedPsbt},
},
)
require.NoError(t.t, err)

// We end up with a transfer with 5 outputs: 2 grouped into the first
// anchor output and then 3 each in their own output. So there are 4 BTC
// anchor outputs but 5 asset transfer outputs.
numOutputs := 5
confirmAndAssetOutboundTransferWithOutputs(
t, t.tapd, sendResp, genInfo.AssetId, outputAmounts, 0, 1,
numOutputs,
)

// We'll now try to the exact amount of the largest output, which should
// still select exactly that one largest output, which is located alone
// in an anchor output. When attempting to burn this, we should get an
// error saying that we cannot completely burn all assets in an output.
_, err = t.tapd.BurnAsset(ctxt, &taprpc.BurnAssetRequest{
Asset: &taprpc.BurnAssetRequest_AssetId{
AssetId: id[:],
},
AmountToBurn: outputAmounts[2],
ConfirmationText: taprootassets.AssetBurnConfirmationText,
})
require.ErrorContains(
t.t, err, tapfreighter.ErrFullBurnNotSupported.Error(),
)

// We'll now try to burn a small amount of assets, which should select
// the largest output, which is located alone in an anchor output.
const burnAmt = 100
burnResp, err := t.tapd.BurnAsset(ctxt, &taprpc.BurnAssetRequest{
Asset: &taprpc.BurnAssetRequest_AssetId{
AssetId: id[:],
},
AmountToBurn: burnAmt,
ConfirmationText: taprootassets.AssetBurnConfirmationText,
})
require.NoError(t.t, err)

burnRespJSON, err := formatProtoJSON(burnResp)
require.NoError(t.t, err)
t.Logf("Got response from burning assets: %v", burnRespJSON)

assertAssetOutboundTransferWithOutputs(
t, t.tapd, burnResp.BurnTransfer, genInfo.AssetId,
[]uint64{outputAmounts[2] - burnAmt, burnAmt}, 1, 2, 2, true,
)
}
2 changes: 1 addition & 1 deletion itest/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func testSendMultipleCoins(t *harnessTest) {

sendResp := sendAssetsToAddr(t, t.tapd, bobAddrs[i])
assertAssetOutboundTransferWithOutputs(
t, t.tapd, sendResp, genInfo.AssetId,
t, t.tapd, sendResp.Transfer, genInfo.AssetId,
[]uint64{0, unitsPerPart}, i+1, i+2, 2, false,
)
}
Expand Down
4 changes: 4 additions & 0 deletions itest/test_list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ var testCases = []*testCase{
name: "re-org mint and send",
test: testReOrgMintAndSend,
},
{
name: "burn test",
test: testBurnAssets,
},
}

var optionalTestCases = []*testCase{
Expand Down

0 comments on commit a735502

Please sign in to comment.