Skip to content

Commit db7f36c

Browse files
committed
itest: add address v2 integration test
1 parent 299a068 commit db7f36c

File tree

3 files changed

+150
-12
lines changed

3 files changed

+150
-12
lines changed

itest/addrs_group_key_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package itest
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"fmt"
7+
"time"
8+
9+
"github.com/lightninglabs/taproot-assets/taprpc"
10+
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func testAddressV2WithGroupKey(t *harnessTest) {
15+
// We begin by minting a new asset group with a group key.
16+
firstTrancheReq := CopyRequest(issuableAssets[0])
17+
18+
firstTranche := MintAssetsConfirmBatch(
19+
t.t, t.lndHarness.Miner().Client, t.tapd,
20+
[]*mintrpc.MintAssetRequest{firstTrancheReq},
21+
)
22+
firstAsset := firstTranche[0]
23+
24+
groupKey := firstTranche[0].AssetGroup.TweakedGroupKey
25+
26+
// And then we mint a second tranche of the same asset group.
27+
secondTrancheReq := CopyRequest(firstTrancheReq)
28+
secondTrancheReq.Asset.Name = "itestbuxx-money-printer-brrr-tranche-2"
29+
secondTrancheReq.Asset.GroupedAsset = true
30+
secondTrancheReq.Asset.NewGroupedAsset = false
31+
secondTrancheReq.Asset.GroupKey = groupKey
32+
33+
secondTranche := MintAssetsConfirmBatch(
34+
t.t, t.lndHarness.Miner().Client, t.tapd,
35+
[]*mintrpc.MintAssetRequest{secondTrancheReq},
36+
)
37+
secondAsset := secondTranche[0]
38+
39+
totalAmount := firstAsset.Amount + secondAsset.Amount
40+
t.Logf("Minted %d units for group %x", totalAmount, groupKey)
41+
42+
// Now we can create an address with the group key.
43+
// We'll make a second node now that'll be the receiver of all the
44+
// assets made above.
45+
bobLnd := t.lndHarness.NewNodeWithCoins("Bob", nil)
46+
bobTapd := setupTapdHarness(t.t, t, bobLnd, t.universeServer)
47+
defer func() {
48+
require.NoError(t.t, bobTapd.stop(!*noDelete))
49+
}()
50+
51+
ctxb := context.Background()
52+
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
53+
defer cancel()
54+
55+
groupAddr, err := bobTapd.NewAddr(ctxt, &taprpc.NewAddrRequest{
56+
ProofCourierAddr: fmt.Sprintf("authmailbox+universerpc://%s",
57+
t.universeServer.ListenAddr),
58+
AddressVersion: taprpc.AddrVersion_ADDR_VERSION_V2,
59+
GroupKey: groupKey,
60+
})
61+
require.NoError(t.t, err)
62+
63+
t.Logf("Got group addr: %v", toJSON(t.t, groupAddr))
64+
65+
sendResp, err := t.tapd.SendAsset(ctxt, &taprpc.SendAssetRequest{
66+
TapAddrs: []string{groupAddr.Encoded},
67+
AddressAmounts: map[string]uint64{
68+
groupAddr.Encoded: totalAmount,
69+
},
70+
})
71+
require.NoError(t.t, err)
72+
73+
t.Logf("Sent asset to group addr: %v", toJSON(t.t, sendResp))
74+
75+
MineBlocks(t.t, t.lndHarness.Miner().Client, 1, 1)
76+
77+
// AssertAddrEvent(t.t, bobTapd, groupAddr, 2, proofReceived)
78+
time.Sleep(time.Second * 5)
79+
80+
assets, err := bobTapd.ListAssets(ctxt, &taprpc.ListAssetRequest{
81+
ScriptKeyType: allScriptKeysQuery,
82+
})
83+
require.NoError(t.t, err)
84+
85+
t.Logf("Bob's assets: %v", toJSON(t.t, assets))
86+
87+
AssertBalanceByGroup(
88+
t.t, bobTapd, hex.EncodeToString(groupKey), totalAmount,
89+
WithAllScriptKeyTypes(),
90+
)
91+
92+
// TODO:
93+
// - multiple outputs with the same address
94+
// - multiple outputs with different addresses
95+
}

itest/assertions.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,27 +1387,66 @@ func AssertBalanceByID(t *testing.T, client taprpc.TaprootAssetsClient,
13871387
// AssertBalanceByGroup asserts that the balance of a single asset group
13881388
// on the given daemon is correct.
13891389
func AssertBalanceByGroup(t *testing.T, client taprpc.TaprootAssetsClient,
1390-
hexGroupKey string, amt uint64) {
1390+
hexGroupKey string, amt uint64, opts ...BalanceOption) {
13911391

13921392
t.Helper()
13931393

1394+
config := &balanceConfig{}
1395+
for _, opt := range opts {
1396+
opt(config)
1397+
}
1398+
1399+
var rpcTypeQuery *taprpc.ScriptKeyTypeQuery
1400+
switch {
1401+
case config.allScriptKeyTypes:
1402+
rpcTypeQuery = &taprpc.ScriptKeyTypeQuery{
1403+
Type: &taprpc.ScriptKeyTypeQuery_AllTypes{
1404+
AllTypes: true,
1405+
},
1406+
}
1407+
1408+
case config.scriptKeyType != nil:
1409+
rpcTypeQuery = &taprpc.ScriptKeyTypeQuery{
1410+
Type: &taprpc.ScriptKeyTypeQuery_ExplicitType{
1411+
ExplicitType: rpcutils.MarshalScriptKeyType(
1412+
*config.scriptKeyType,
1413+
),
1414+
},
1415+
}
1416+
}
1417+
13941418
groupKey, err := hex.DecodeString(hexGroupKey)
13951419
require.NoError(t, err)
13961420

13971421
ctxb := context.Background()
1398-
balancesResp, err := client.ListBalances(
1399-
ctxb, &taprpc.ListBalancesRequest{
1400-
GroupBy: &taprpc.ListBalancesRequest_GroupKey{
1401-
GroupKey: true,
1422+
err = wait.NoError(func() error {
1423+
balancesResp, err := client.ListBalances(
1424+
ctxb, &taprpc.ListBalancesRequest{
1425+
GroupBy: &taprpc.ListBalancesRequest_GroupKey{
1426+
GroupKey: true,
1427+
},
1428+
GroupKeyFilter: groupKey,
1429+
ScriptKeyType: rpcTypeQuery,
14021430
},
1403-
GroupKeyFilter: groupKey,
1404-
},
1405-
)
1406-
require.NoError(t, err)
1431+
)
1432+
if err != nil {
1433+
return fmt.Errorf("error listing balances: %w", err)
1434+
}
14071435

1408-
balance, ok := balancesResp.AssetGroupBalances[hexGroupKey]
1409-
require.True(t, ok)
1410-
require.Equal(t, amt, balance.Balance)
1436+
balance, ok := balancesResp.AssetGroupBalances[hexGroupKey]
1437+
if !ok {
1438+
return fmt.Errorf("no balance found for group key %s",
1439+
hexGroupKey)
1440+
}
1441+
1442+
if balance.Balance != amt {
1443+
return fmt.Errorf("expected balance %d, got %d",
1444+
amt, balance.Balance)
1445+
}
1446+
1447+
return nil
1448+
}, defaultTimeout)
1449+
require.NoError(t, err)
14111450
}
14121451

14131452
// AssertTransfer asserts that the value of each transfer initiated on the

itest/test_list_on_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ var testCases = []*testCase{
351351
name: "auth mailbox message store and fetch",
352352
test: testAuthMailboxStoreAndFetchMessage,
353353
},
354+
{
355+
name: "address v2 with group key",
356+
test: testAddressV2WithGroupKey,
357+
},
354358
}
355359

356360
var optionalTestCases = []*testCase{

0 commit comments

Comments
 (0)