Skip to content

Commit

Permalink
multi: add batch mint stress test
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed May 31, 2023
1 parent 9b884c9 commit 15a277b
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 39 deletions.
4 changes: 2 additions & 2 deletions itest/addrs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func testAddresses(t *harnessTest) {
// for multiple internal asset transfers when only sending one of them
// to an external address.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
simpleAssets[0], issuableAssets[0],
},
)
Expand Down Expand Up @@ -130,7 +130,7 @@ func testAddresses(t *harnessTest) {
func testMultiAddress(t *harnessTest) {
// First, mint an asset, so we have one to create addresses for.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
simpleAssets[0], issuableAssets[0],
},
)
Expand Down
18 changes: 13 additions & 5 deletions itest/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ func copyRequests(reqs []*mintrpc.MintAssetRequest) []*mintrpc.MintAssetRequest
// testMintAssets tests that we're able to mint assets, retrieve their proofs
// and that we're able to import the proofs into a new node.
func testMintAssets(t *harnessTest) {
rpcSimpleAssets := mintAssetsConfirmBatch(t, t.tapd, simpleAssets)
rpcIssuableAssets := mintAssetsConfirmBatch(t, t.tapd, issuableAssets)
rpcSimpleAssets := mintAssetsConfirmBatch(t, t.tapd, nil, simpleAssets)
rpcIssuableAssets := mintAssetsConfirmBatch(
t, t.tapd, nil, issuableAssets,
)

// Now that all our assets have been issued, we'll use the balance
// calls to ensure that we're able to retrieve the proper balance for
Expand Down Expand Up @@ -134,6 +136,7 @@ func testMintAssets(t *harnessTest) {
// mintAssetsConfirmBatch mints all given assets in the same batch, confirms the
// batch and verifies all asset proofs of the minted assets.
func mintAssetsConfirmBatch(t *harnessTest, tapd *tapdHarness,
mempoolTimeout *time.Duration,
assetRequests []*mintrpc.MintAssetRequest) []*taprpc.Asset {

ctxb := context.Background()
Expand All @@ -154,8 +157,13 @@ func mintAssetsConfirmBatch(t *harnessTest, tapd *tapdHarness,
require.NoError(t.t, err)
require.NotEmpty(t.t, batchResp.BatchKey)

mempoolWaitTimeout := defaultWaitTimeout
if mempoolTimeout != nil {
mempoolWaitTimeout = *mempoolTimeout
}

hashes, err := waitForNTxsInMempool(
t.lndHarness.Miner.Client, 1, defaultWaitTimeout,
t.lndHarness.Miner.Client, 1, mempoolWaitTimeout,
)
require.NoError(t.t, err)

Expand Down Expand Up @@ -411,7 +419,7 @@ func testMintAssetNameCollisionError(t *harnessTest) {
},
}
rpcSimpleAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{&assetMint},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{&assetMint},
)

// Ensure minted asset with requested name was successfully minted.
Expand Down Expand Up @@ -502,7 +510,7 @@ func testMintAssetNameCollisionError(t *harnessTest) {
// Minting the asset with the name collision should work, even though
// it is also part of a cancelled batch.
rpcCollideAsset := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{&assetCollide},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{&assetCollide},
)

collideAssetName := rpcCollideAsset[0].AssetGenesis.Name
Expand Down
2 changes: 1 addition & 1 deletion itest/collectible_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func testCollectibleSend(t *harnessTest) {
// First, we'll make a collectible with emission enabled.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
issuableAssets[1],
// Our "passive" asset.
{
Expand Down
2 changes: 1 addition & 1 deletion itest/full_value_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func testFullValueSend(t *harnessTest) {
// First, we'll make an normal assets with enough units to allow us to
// send it around a few times.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
simpleAssets[0], issuableAssets[0],
},
)
Expand Down
115 changes: 115 additions & 0 deletions itest/mint_batch_stress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package itest

import (
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
)

const (
// testDataFileName is the name of the directory with the test data.
testDataFileName = "testdata"
)

var (
// Raw data of Cryptopunk 0 saved as a PNG.
ImageMetadataFileName = filepath.Join(
testDataFileName, "8k-metadata.hex",
)
)

func testMintBatchStressTest(t *harnessTest) {
// Read base metadata.
imageMetadataHex, err := os.ReadFile(ImageMetadataFileName)
require.NoError(t.t, err)

imageMetadataBytes, err := hex.DecodeString(
strings.Trim(string(imageMetadataHex), "\n"),
)
require.NoError(t.t, err)

var (
batchSize = 10000
batchRequests = make([]*mintrpc.MintAssetRequest, batchSize)
baseName = "jpeg"
metaPrefixSize = binary.MaxVarintLen16
metadataPrefix = make([]byte, metaPrefixSize)
)

// Each asset in the batch will share a name and metdata preimage, that
// will be updated based on the asset's index in the batch.
collectibleRequestTemplate := mintrpc.MintAssetRequest{
Asset: &mintrpc.MintAsset{
AssetType: taprpc.AssetType_COLLECTIBLE,
Name: baseName,
AssetMeta: &taprpc.AssetMeta{
Data: imageMetadataBytes,
Type: 0,
},
Amount: 1,
},
EnableEmission: false,
}

// Update the asset name and metadata to match an index.
incrementMintAsset := func(asset *mintrpc.MintAsset, ind int) {
asset.Name = asset.Name + strconv.Itoa(ind)
binary.PutUvarint(metadataPrefix, uint64(ind))
copy(asset.AssetMeta.Data[0:metaPrefixSize], metadataPrefix)
}

fmt.Println(len(imageMetadataBytes))

// Use the first asset of the batch as the asset group anchor.
collectibleAnchor := copyRequest(&collectibleRequestTemplate)
incrementMintAsset(collectibleAnchor.Asset, 0)
collectibleAnchor.EnableEmission = true
batchRequests[0] = collectibleAnchor

// Generate the rest of the batch, with each asset referencing the group
// anchor we created above.
for i := 1; i < batchSize; i++ {
groupedAsset := copyRequest(&collectibleRequestTemplate)
incrementMintAsset(groupedAsset.Asset, i)
groupedAsset.Asset.GroupAnchor = collectibleAnchor.Asset.Name
batchRequests[i] = groupedAsset
}

// Submit the batch for minting. Use an extended timeout for the TX
// appearing in the mempool so we can observe the minter hitting its own
// shorter default timeout.
minterTimeout := defaultWaitTimeout * 2
_ = mintAssetsConfirmBatch(t, t.tapd, &minterTimeout, batchRequests)

// groupKey := rpcBatchAssets[0].AssetGroup.TweakedGroupKey

// Assert that some expected properties of the mint hold; we should have
// one asset group, with a balance equal to the number of assets, and
// with the correct group anchor set.
groupCount := 1
assertNumGroups(t.t, t.tapd, groupCount)
ctxb := context.Background()
balancesResp, err := t.tapd.ListBalances(
ctxb, &taprpc.ListBalancesRequest{
GroupBy: &taprpc.ListBalancesRequest_GroupKey{
GroupKey: true,
},
},
)
require.NoError(t.t, err)

groupBalances := balancesResp.AssetGroupBalances
require.Len(t.t, maps.Values(groupBalances), groupCount)
groupBalance := maps.Values(groupBalances)[0]
require.Equal(t.t, groupBalance.Balance, uint64(batchSize))
}
6 changes: 4 additions & 2 deletions itest/multi_asset_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testMintMultiAssetGroups(t *harnessTest) {
// The minted batch should contain 7 assets total, and the daemon should
// now be aware of 3 asset groups. Each group should have a different
// number of assets, and a different total balance.
mintedBatch := mintAssetsConfirmBatch(t, t.tapd, complexBatch)
mintedBatch := mintAssetsConfirmBatch(t, t.tapd, nil, complexBatch)

// Once the batch is minted, we can verify that all asset groups were
// created correctly. We begin by verifying the number of asset groups.
Expand Down Expand Up @@ -282,7 +282,9 @@ func testMintMultiAssetGroupErrors(t *harnessTest) {
multiAssetGroup := []*mintrpc.MintAssetRequest{validAnchor, groupedAsset}

// The assets should be minted into the same group.
rpcGroupedAssets := mintAssetsConfirmBatch(t, t.tapd, multiAssetGroup)
rpcGroupedAssets := mintAssetsConfirmBatch(
t, t.tapd, nil, multiAssetGroup,
)
assertNumGroups(t.t, t.tapd, 1)
groupKey := rpcGroupedAssets[0].AssetGroup.TweakedGroupKey
groupKeyHex := hex.EncodeToString(groupKey)
Expand Down
16 changes: 8 additions & 8 deletions itest/psbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func testPsbtScriptHashLockSend(t *harnessTest) {
// First, we'll make a normal asset with enough units to allow us to
// send it around a few times.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)

mintedAsset := rpcAssets[0]
Expand Down Expand Up @@ -150,7 +150,7 @@ func testPsbtScriptCheckSigSend(t *harnessTest) {
// First, we'll make a normal asset with enough units to allow us to
// send it around a few times.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{issuableAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{issuableAssets[0]},
)

mintedAsset := rpcAssets[0]
Expand Down Expand Up @@ -276,7 +276,7 @@ func testPsbtNormalInteractiveFullValueSend(t *harnessTest) {
// going to send backand forth. We're also minting a passive asset that
// should remain where it is.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
simpleAssets[0],
// Our "passive" asset.
{
Expand Down Expand Up @@ -326,7 +326,7 @@ func testPsbtGroupedInteractiveFullValueSend(t *harnessTest) {
// going to send backand forth. We're also minting a passive asset that
// should remain where it is.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
issuableAssets[0],
// Our "passive" asset.
{
Expand Down Expand Up @@ -479,7 +479,7 @@ func testPsbtNormalInteractiveSplitSend(t *harnessTest) {
// going to send backand forth. We're also minting a passive asset that
// should remain where it is.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
simpleAssets[0],
// Our "passive" asset.
{
Expand Down Expand Up @@ -529,7 +529,7 @@ func testPsbtGroupedInteractiveSplitSend(t *harnessTest) {
// going to send backand forth. We're also minting a passive asset that
// should remain where it is.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
issuableAssets[0],
// Our "passive" asset.
{
Expand Down Expand Up @@ -695,7 +695,7 @@ func testPsbtInteractiveTapscriptSibling(t *harnessTest) {
// First, we'll make a normal asset with a bunch of units that we are
// going to send backand forth.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis
Expand Down Expand Up @@ -806,7 +806,7 @@ func testPsbtMultiSend(t *harnessTest) {
// going to send backand forth. We're also minting a passive asset that
// should remain where it is.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{
t, t.tapd, nil, []*mintrpc.MintAssetRequest{
simpleAssets[0],
// Our "passive" asset.
{
Expand Down
12 changes: 6 additions & 6 deletions itest/re-issuance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func testReIssuance(t *harnessTest) {
// First, we'll mint a collectible and a normal asset, both with
// emission enabled.
normalGroupGen := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{issuableAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{issuableAssets[0]},
)
collectGroupGen := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{issuableAssets[1]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{issuableAssets[1]},
)
require.Equal(t.t, 1, len(normalGroupGen))
require.Equal(t.t, 1, len(collectGroupGen))
Expand Down Expand Up @@ -116,10 +116,10 @@ func testReIssuance(t *harnessTest) {
reissuedAssets[1].Asset.GroupKey = collectGroupKey

normalReissueGen := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{reissuedAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{reissuedAssets[0]},
)
collectReissueGen := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{reissuedAssets[1]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{reissuedAssets[1]},
)
require.Equal(t.t, 1, len(normalReissueGen))
require.Equal(t.t, 1, len(collectReissueGen))
Expand Down Expand Up @@ -238,7 +238,7 @@ func testReIssuanceAmountOverflow(t *harnessTest) {
assetIssueReq.Asset.Amount = math.MaxUint64

assets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{assetIssueReq},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{assetIssueReq},
)
require.Equal(t.t, 1, len(assets))

Expand Down Expand Up @@ -269,7 +269,7 @@ func testReIssuanceAmountOverflow(t *harnessTest) {
func testMintWithGroupKeyErrors(t *harnessTest) {
// First, mint a collectible with emission enabled to create one group.
collectGroupGen := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{issuableAssets[1]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{issuableAssets[1]},
)
require.Equal(t.t, 1, len(collectGroupGen))

Expand Down
2 changes: 1 addition & 1 deletion itest/round_trip_send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func testRoundTripSend(t *harnessTest) {
// First, we'll make a normal assets with enough units to allow us to
// send it around a few times.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis
Expand Down
10 changes: 5 additions & 5 deletions itest/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func testBasicSend(t *harnessTest) {
// First, we'll make a normal assets with enough units to allow us to
// send it around a few times.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis
Expand Down Expand Up @@ -155,7 +155,7 @@ func testBasicSendPassiveAsset(t *harnessTest) {
},
},
}
rpcAssets := mintAssetsConfirmBatch(t, t.tapd, assets)
rpcAssets := mintAssetsConfirmBatch(t, t.tapd, nil, assets)
firstAsset := rpcAssets[0]

// Set up a new node that will serve as the receiving node.
Expand Down Expand Up @@ -308,7 +308,7 @@ func testReattemptFailedAssetSend(t *harnessTest) {

// Mint an asset for sending.
rpcAssets := mintAssetsConfirmBatch(
t, sendTapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, sendTapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis
Expand Down Expand Up @@ -411,7 +411,7 @@ func testOfflineReceiverEventuallyReceives(t *harnessTest) {

// Mint an asset for sending.
rpcAssets := mintAssetsConfirmBatch(
t, sendTapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, sendTapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis
Expand Down Expand Up @@ -512,7 +512,7 @@ func testMultiInputSendNonInteractiveSingleID(t *harnessTest) {

// Mint a single asset.
rpcAssets := mintAssetsConfirmBatch(
t, t.tapd, []*mintrpc.MintAssetRequest{simpleAssets[0]},
t, t.tapd, nil, []*mintrpc.MintAssetRequest{simpleAssets[0]},
)
rpcAsset := rpcAssets[0]

Expand Down
Loading

0 comments on commit 15a277b

Please sign in to comment.