Skip to content

Commit bc3e474

Browse files
committed
itest: add and use rpcassert.ListUtxosRPC
Add test helper function rpcassert.ListUtxosRPC, which calls the ListUtxos RPC endpoint on the provided client. The call is wrapped with wait.NoError to allow for more lenient timing expectations, thus reducing test flakes. Use the new helper in AssertBalances.
1 parent 751b00a commit bc3e474

File tree

2 files changed

+100
-51
lines changed

2 files changed

+100
-51
lines changed

itest/assertions.go

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,74 +2550,86 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
25502550
},
25512551
)
25522552

2553-
utxoList, err := client.ListUtxos(ctxt, &taprpc.ListUtxosRequest{
2554-
IncludeLeased: config.includeLeased,
2555-
ScriptKeyType: rpcTypeQuery,
2556-
})
2557-
require.NoError(t, err)
2558-
25592553
// Make sure the ListUtxos call returns the same number of (asset) UTXOs
25602554
// as the ListAssets call.
2561-
var (
2562-
numAnchorUtxos uint32
2563-
totalBalance uint64
2564-
numUtxos uint32
2565-
)
2555+
assertPredUtxos := func(resp *taprpc.ListUtxosResponse) error {
2556+
var (
2557+
numAnchorUtxos uint32
2558+
totalBalance uint64
2559+
numUtxos uint32
2560+
)
25662561

2567-
for _, btcUtxo := range utxoList.ManagedUtxos {
2568-
numAnchorUtxos++
2569-
for _, assetUtxo := range btcUtxo.Assets {
2570-
if len(config.assetID) > 0 {
2571-
if !bytes.Equal(
2572-
assetUtxo.AssetGenesis.AssetId,
2573-
config.assetID,
2574-
) {
2562+
for _, btcUtxo := range resp.ManagedUtxos {
2563+
numAnchorUtxos++
2564+
for _, assetUtxo := range btcUtxo.Assets {
2565+
if len(config.assetID) > 0 {
2566+
if !bytes.Equal(
2567+
assetUtxo.AssetGenesis.AssetId,
2568+
config.assetID,
2569+
) {
25752570

2576-
continue
2571+
continue
2572+
}
25772573
}
2578-
}
25792574

2580-
if len(config.groupKey) > 0 {
2581-
if assetUtxo.AssetGroup == nil {
2582-
continue
2583-
}
2575+
if len(config.groupKey) > 0 {
2576+
if assetUtxo.AssetGroup == nil {
2577+
continue
2578+
}
25842579

2585-
if !bytes.Equal(
2586-
assetUtxo.AssetGroup.TweakedGroupKey,
2587-
config.groupKey,
2588-
) {
2580+
// nolint: lll
2581+
actualGK := assetUtxo.AssetGroup.TweakedGroupKey
25892582

2590-
continue
2583+
if !bytes.Equal(
2584+
actualGK, config.groupKey,
2585+
) {
2586+
2587+
continue
2588+
}
25912589
}
2592-
}
25932590

2594-
if len(config.scriptKey) > 0 {
2595-
if !bytes.Equal(
2596-
assetUtxo.ScriptKey, config.scriptKey,
2597-
) {
2591+
if len(config.scriptKey) > 0 {
2592+
if !bytes.Equal(
2593+
assetUtxo.ScriptKey,
2594+
config.scriptKey,
2595+
) {
25982596

2599-
continue
2597+
continue
2598+
}
26002599
}
2600+
2601+
totalBalance += assetUtxo.Amount
2602+
numUtxos++
26012603
}
2604+
}
26022605

2603-
totalBalance += assetUtxo.Amount
2604-
numUtxos++
2606+
if balance != totalBalance {
2607+
return fmt.Errorf("ListUtxos balance, wanted %d, "+
2608+
"got: %v", balance, toJSON(t, resp))
26052609
}
2606-
}
2607-
require.Equal(t, balance, totalBalance, "ListUtxos balance")
26082610

2609-
if config.numAnchorUtxos > 0 {
2610-
require.Equal(
2611-
t, config.numAnchorUtxos, numAnchorUtxos, "num anchor "+
2612-
"utxos",
2613-
)
2614-
}
2615-
if config.numAssetUtxos > 0 {
2616-
require.Equal(
2617-
t, config.numAssetUtxos, numUtxos, "ListUtxos num "+
2618-
"asset utxos",
2619-
)
2611+
if config.numAnchorUtxos > 0 {
2612+
if config.numAnchorUtxos != numAnchorUtxos {
2613+
return fmt.Errorf("unexpected number of " +
2614+
"anchor UTXOs")
2615+
}
2616+
}
2617+
if config.numAssetUtxos > 0 {
2618+
if config.numAssetUtxos != numUtxos {
2619+
return fmt.Errorf("unexpected number of " +
2620+
"asset UTXOs")
2621+
}
2622+
}
2623+
2624+
return nil
26202625
}
2626+
2627+
rpcassert.ListUtxosRPC(t, ctxt, client, assertPredUtxos,
2628+
&taprpc.ListUtxosRequest{
2629+
IncludeLeased: config.includeLeased,
2630+
ScriptKeyType: rpcTypeQuery,
2631+
},
2632+
)
26212633
}
26222634

26232635
func assertGroups(t *testing.T, client taprpc.TaprootAssetsClient,

itest/rpcassert/taprpc.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,40 @@ func ListAssetsRPC(t *testing.T, ctx context.Context,
124124

125125
return resp
126126
}
127+
128+
// ListUtxosRPC calls ListUtxos RPC with the given request until the
129+
// given assertion predicate returns no error or the timeout is reached. If the
130+
// predicate is nil, only basic checks are performed (non-nil response).
131+
//
132+
// If the assertion fails, the test is failed.
133+
func ListUtxosRPC(t *testing.T, ctx context.Context,
134+
client taprpc.TaprootAssetsClient,
135+
assertPredicate func(*taprpc.ListUtxosResponse) error,
136+
req *taprpc.ListUtxosRequest) *taprpc.ListUtxosResponse {
137+
138+
t.Helper()
139+
140+
var resp *taprpc.ListUtxosResponse
141+
err := wait.NoError(func() error {
142+
var err error
143+
resp, err = client.ListUtxos(ctx, req)
144+
if err != nil {
145+
return err
146+
}
147+
148+
if resp == nil {
149+
return fmt.Errorf("nil response")
150+
}
151+
152+
if assertPredicate != nil {
153+
return assertPredicate(resp)
154+
}
155+
156+
return nil
157+
}, defaultWaitTimeout)
158+
if err != nil {
159+
t.Fatal(err)
160+
}
161+
162+
return resp
163+
}

0 commit comments

Comments
 (0)