Skip to content

Commit 84cb596

Browse files
committed
loadtest: add sendV2 loadtest
This commit adds a refactored version of the send test, which uses less assertions and rpc calls. This is meant to speed things up compared to the old test, plus offer some more coverage by utilizing normal assets and balances greater than 1 (case for collectibles).
1 parent 9cab442 commit 84cb596

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

itest/loadtest/load_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ var loadTestCases = []testCase{
4747
name: "send",
4848
fn: sendTest,
4949
},
50+
{
51+
name: "sendV2",
52+
fn: sendTestV2,
53+
},
5054
{
5155
name: "multisig",
5256
fn: multisigTest,

itest/loadtest/send_test.go

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
prand "math/rand"
7+
"math/rand/v2"
78
"testing"
89
"time"
910

@@ -54,7 +55,156 @@ func sendTest(t *testing.T, ctx context.Context, cfg *Config) {
5455
}
5556
}
5657

57-
// sendAssets sends the given number of assets of the given type from the given
58+
// sendTestV2 checks that we are able to send assets between the two nodes. It
59+
// is a more performant and lightweight version of sendTest, as it uses less
60+
// assertions and RPC calls.
61+
func sendTestV2(t *testing.T, ctx context.Context, cfg *Config) {
62+
// Start by initializing all our client connections.
63+
alice, bob, bitcoinClient := initClients(t, ctx, cfg)
64+
65+
ctxb := context.Background()
66+
ctxt, cancel := context.WithTimeout(ctxb, cfg.TestTimeout)
67+
defer cancel()
68+
69+
sendType := stringToAssetType(cfg.SendAssetType)
70+
71+
// Alice is set to be the minter in mintV2, so we use Alice's universe.
72+
var (
73+
uniHost = fmt.Sprintf(
74+
"%s:%d", alice.cfg.Host, alice.cfg.Port,
75+
)
76+
)
77+
78+
// Let's make sure Bob is aware of all the assets that Alice may have
79+
// minted.
80+
itest.SyncUniverses(ctx, t, bob, alice, uniHost, cfg.TestTimeout)
81+
82+
// We now retrieve Alice and Bob's balances just once, and will re-use
83+
// them in future function calls. Any update to the balances will be
84+
// directly applied to these response objects, to skip future calls to
85+
// ListBalances.
86+
resAlice, err := alice.ListBalances(ctx, &taprpc.ListBalancesRequest{
87+
GroupBy: &taprpc.ListBalancesRequest_AssetId{
88+
AssetId: true,
89+
},
90+
})
91+
require.NoError(t, err)
92+
93+
resBob, err := bob.ListBalances(ctx, &taprpc.ListBalancesRequest{
94+
GroupBy: &taprpc.ListBalancesRequest_AssetId{
95+
AssetId: true,
96+
},
97+
})
98+
require.NoError(t, err)
99+
100+
for i := 1; i <= cfg.NumSends; i++ {
101+
var (
102+
sender, receiver *rpcClient
103+
senderAssets map[string]*taprpc.AssetBalance
104+
)
105+
106+
// Assets may be sent in both directions, so we make a random
107+
// draw to conclude who the sender is.
108+
draw := rand.IntN(2)
109+
110+
switch draw {
111+
case 0:
112+
sender = alice
113+
senderAssets = resAlice.AssetBalances
114+
receiver = bob
115+
116+
case 1:
117+
sender = bob
118+
senderAssets = resBob.AssetBalances
119+
receiver = alice
120+
}
121+
122+
sendAssetV2(
123+
t, ctxt, cfg.NumAssets, sendType, senderAssets,
124+
sender, receiver, bitcoinClient, cfg.TestTimeout,
125+
)
126+
}
127+
}
128+
129+
// sendAssetV2 sends a certain amount of assets of a specific type from a sender
130+
// to a receiver. It will scan the balance of the sender and find a suitable
131+
// asset to carry out the send, then will dispatch the send and assert its
132+
// completion.
133+
func sendAssetV2(t *testing.T, ctx context.Context, numAssets uint64,
134+
assetType taprpc.AssetType, assets map[string]*taprpc.AssetBalance,
135+
sender, receiver *rpcClient, bitcoinClient *rpcclient.Client,
136+
timeout time.Duration) {
137+
138+
// Look over the sender's balances to see if any asset balance qualifies
139+
// for this send.
140+
var (
141+
assetID []byte
142+
balance *taprpc.AssetBalance
143+
)
144+
for _, v := range assets {
145+
if v.Balance >= numAssets &&
146+
v.AssetGenesis.AssetType == assetType {
147+
148+
assetID = v.AssetGenesis.AssetId
149+
balance = v
150+
151+
break
152+
}
153+
}
154+
155+
// No balance satisfies the amount of this send, we can skip this round.
156+
if assetID == nil {
157+
t.Logf("%s could not send %v assets, no available balance",
158+
sender.cfg.Name, numAssets)
159+
160+
return
161+
}
162+
163+
t.Logf("%s sending %v assets to %s", sender.cfg.Name, numAssets,
164+
receiver.cfg.Name)
165+
166+
// Receiver creates the address to receive the assets.
167+
addr, err := receiver.NewAddr(ctx, &taprpc.NewAddrRequest{
168+
AssetId: assetID,
169+
Amt: numAssets,
170+
ProofCourierAddr: fmt.Sprintf(
171+
"%s://%s:%d", proof.UniverseRpcCourierType,
172+
sender.cfg.Host, sender.cfg.Port,
173+
),
174+
})
175+
require.NoError(t, err)
176+
177+
t.Logf("%s created address %v", receiver.cfg.Name, addr.String())
178+
179+
// Sender initiates the send.
180+
_, err = sender.SendAsset(ctx, &taprpc.SendAssetRequest{
181+
TapAddrs: []string{addr.Encoded},
182+
})
183+
require.NoError(t, err)
184+
t.Logf("%s sent assets to address %v", sender.cfg.Name, addr.String())
185+
186+
// We assert the receiver detects the spend.
187+
itest.AssertAddrEventCustomTimeout(
188+
t, receiver, addr, 1, statusDetected, timeout,
189+
)
190+
t.Logf("%s detected send", receiver.cfg.Name)
191+
192+
// Mine a block to confirm the transfer.
193+
itest.MineBlocks(t, bitcoinClient, 1, 0)
194+
t.Log("Mined 1 block")
195+
196+
// Assert that the transfer is now completed
197+
itest.AssertAddrEventCustomTimeout(
198+
t, receiver, addr, 1, statusCompleted, timeout,
199+
)
200+
t.Logf("%s completed send of %v assets", sender.cfg.Name, numAssets)
201+
202+
// If everything completed correctly, subtract the asset amount from the
203+
// sender's asset balance.
204+
balance.Balance -= numAssets
205+
}
206+
207+
// sendAsset sends the given number of assets of the given type from the given
58208
// node to the other node.
59209
func sendAssets(t *testing.T, ctx context.Context, numAssets uint64,
60210
assetType taprpc.AssetType, send, receive *rpcClient,

0 commit comments

Comments
 (0)