Skip to content

Commit a5c2fd0

Browse files
committed
itest: add address v2 integration test
1 parent 0f2451b commit a5c2fd0

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
@@ -1372,27 +1372,66 @@ func AssertBalanceByID(t *testing.T, client taprpc.TaprootAssetsClient,
13721372
// AssertBalanceByGroup asserts that the balance of a single asset group
13731373
// on the given daemon is correct.
13741374
func AssertBalanceByGroup(t *testing.T, client taprpc.TaprootAssetsClient,
1375-
hexGroupKey string, amt uint64) {
1375+
hexGroupKey string, amt uint64, opts ...BalanceOption) {
13761376

13771377
t.Helper()
13781378

1379+
config := &balanceConfig{}
1380+
for _, opt := range opts {
1381+
opt(config)
1382+
}
1383+
1384+
var rpcTypeQuery *taprpc.ScriptKeyTypeQuery
1385+
switch {
1386+
case config.allScriptKeyTypes:
1387+
rpcTypeQuery = &taprpc.ScriptKeyTypeQuery{
1388+
Type: &taprpc.ScriptKeyTypeQuery_AllTypes{
1389+
AllTypes: true,
1390+
},
1391+
}
1392+
1393+
case config.scriptKeyType != nil:
1394+
rpcTypeQuery = &taprpc.ScriptKeyTypeQuery{
1395+
Type: &taprpc.ScriptKeyTypeQuery_ExplicitType{
1396+
ExplicitType: rpcutils.MarshalScriptKeyType(
1397+
*config.scriptKeyType,
1398+
),
1399+
},
1400+
}
1401+
}
1402+
13791403
groupKey, err := hex.DecodeString(hexGroupKey)
13801404
require.NoError(t, err)
13811405

13821406
ctxb := context.Background()
1383-
balancesResp, err := client.ListBalances(
1384-
ctxb, &taprpc.ListBalancesRequest{
1385-
GroupBy: &taprpc.ListBalancesRequest_GroupKey{
1386-
GroupKey: true,
1407+
err = wait.NoError(func() error {
1408+
balancesResp, err := client.ListBalances(
1409+
ctxb, &taprpc.ListBalancesRequest{
1410+
GroupBy: &taprpc.ListBalancesRequest_GroupKey{
1411+
GroupKey: true,
1412+
},
1413+
GroupKeyFilter: groupKey,
1414+
ScriptKeyType: rpcTypeQuery,
13871415
},
1388-
GroupKeyFilter: groupKey,
1389-
},
1390-
)
1391-
require.NoError(t, err)
1416+
)
1417+
if err != nil {
1418+
return fmt.Errorf("error listing balances: %w", err)
1419+
}
13921420

1393-
balance, ok := balancesResp.AssetGroupBalances[hexGroupKey]
1394-
require.True(t, ok)
1395-
require.Equal(t, amt, balance.Balance)
1421+
balance, ok := balancesResp.AssetGroupBalances[hexGroupKey]
1422+
if !ok {
1423+
return fmt.Errorf("no balance found for group key %s",
1424+
hexGroupKey)
1425+
}
1426+
1427+
if balance.Balance != amt {
1428+
return fmt.Errorf("expected balance %d, got %d",
1429+
amt, balance.Balance)
1430+
}
1431+
1432+
return nil
1433+
}, defaultTimeout)
1434+
require.NoError(t, err)
13961435
}
13971436

13981437
// 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
@@ -343,6 +343,10 @@ var testCases = []*testCase{
343343
name: "asset signing after lnd restore from seed",
344344
test: testRestoreLndFromSeed,
345345
},
346+
{
347+
name: "address v2 with group key",
348+
test: testAddressV2WithGroupKey,
349+
},
346350
}
347351

348352
var optionalTestCases = []*testCase{

0 commit comments

Comments
 (0)