Skip to content

Commit b4e16b1

Browse files
committed
itest: parse old channel format, more compat tests
1 parent b9f847c commit b4e16b1

File tree

2 files changed

+77
-31
lines changed

2 files changed

+77
-31
lines changed

itest/assets_test.go

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -984,18 +984,12 @@ func assertPendingChannels(t *testing.T, node *HarnessNode,
984984
require.NoError(t, err)
985985
require.Len(t, pendingChannelsResp.PendingOpenChannels, numChannels)
986986

987-
// For older versions (during the backward compatibility test), if the
988-
// channel custom data is in the old format, we can't do further checks.
989-
if node.Cfg.OldChannelFormat {
990-
return
991-
}
992-
993987
pendingChan := pendingChannelsResp.PendingOpenChannels[0]
994-
var pendingJSON rfqmsg.JsonAssetChannel
995-
err = json.Unmarshal(
996-
pendingChan.Channel.CustomChannelData, &pendingJSON,
988+
pendingJSON, err := parseChannelData(
989+
node, pendingChan.Channel.CustomChannelData,
997990
)
998991
require.NoError(t, err)
992+
999993
require.GreaterOrEqual(t, len(pendingJSON.FundingAssets), 1)
1000994

1001995
require.NotZero(t, pendingJSON.Capacity)
@@ -1040,12 +1034,6 @@ func haveFundingAsset(assetChannel *rfqmsg.JsonAssetChannel,
10401034
func assertAssetChan(t *testing.T, src, dst *HarnessNode, fundingAmount uint64,
10411035
channelAssets []*taprpc.Asset) {
10421036

1043-
if src.Cfg.OldChannelFormat {
1044-
t.Logf("Skipping asset channel check for %s->%s, old format",
1045-
src.Cfg.Name, dst.Cfg.Name)
1046-
return
1047-
}
1048-
10491037
err := wait.NoError(func() error {
10501038
a, err := getChannelCustomData(src, dst)
10511039
if err != nil {
@@ -1153,8 +1141,7 @@ func getChannelCustomData(src, dst *HarnessNode) (*rfqmsg.JsonAssetChannel,
11531141

11541142
targetChan := assetChannels[0]
11551143

1156-
var assetData rfqmsg.JsonAssetChannel
1157-
err = json.Unmarshal(targetChan.CustomChannelData, &assetData)
1144+
assetData, err := parseChannelData(src, targetChan.CustomChannelData)
11581145
if err != nil {
11591146
return nil, fmt.Errorf("unable to unmarshal asset data: %w",
11601147
err)
@@ -1165,7 +1152,7 @@ func getChannelCustomData(src, dst *HarnessNode) (*rfqmsg.JsonAssetChannel,
11651152
len(assetData.FundingAssets))
11661153
}
11671154

1168-
return &assetData, nil
1155+
return assetData, nil
11691156
}
11701157

11711158
func getAssetChannelBalance(t *testing.T, node *HarnessNode, assetIDs [][]byte,
@@ -1272,8 +1259,9 @@ func assertChannelAssetBalanceWithDelta(t *testing.T, node *HarnessNode,
12721259

12731260
targetChan := fetchChannel(t, node, chanPoint)
12741261

1275-
var assetBalance rfqmsg.JsonAssetChannel
1276-
err := json.Unmarshal(targetChan.CustomChannelData, &assetBalance)
1262+
assetBalance, err := parseChannelData(
1263+
node, targetChan.CustomChannelData,
1264+
)
12771265
require.NoError(t, err)
12781266

12791267
require.Len(t, assetBalance.FundingAssets, 1)
@@ -1287,8 +1275,9 @@ func channelAssetBalance(t *testing.T, node *HarnessNode,
12871275

12881276
targetChan := fetchChannel(t, node, chanPoint)
12891277

1290-
var assetBalance rfqmsg.JsonAssetChannel
1291-
err := json.Unmarshal(targetChan.CustomChannelData, &assetBalance)
1278+
assetBalance, err := parseChannelData(
1279+
node, targetChan.CustomChannelData,
1280+
)
12921281
require.NoError(t, err)
12931282

12941283
require.GreaterOrEqual(t, len(assetBalance.FundingAssets), 1)
@@ -2312,9 +2301,8 @@ func defaultCoOpCloseBalanceCheck(t *testing.T, local, remote *HarnessNode,
23122301
var closedJsonChannel *rfqmsg.JsonAssetChannel
23132302
for _, closedChan := range closedChans.Channels {
23142303
if closedChan.ClosingTxHash == closeTx.TxHash().String() {
2315-
closedJsonChannel = &rfqmsg.JsonAssetChannel{}
2316-
err = json.Unmarshal(
2317-
closedChan.CustomChannelData, closedJsonChannel,
2304+
closedJsonChannel, err = parseChannelData(
2305+
local, closedChan.CustomChannelData,
23182306
)
23192307
require.NoError(t, err)
23202308

@@ -2977,8 +2965,7 @@ func newCloseExpiryInfo(t *testing.T, node *HarnessNode) forceCloseExpiryInfo {
29772965
cltvs[payHash] = htlc.ExpirationHeight
29782966
}
29792967

2980-
var assetData rfqmsg.JsonAssetChannel
2981-
err = json.Unmarshal(mainChan.CustomChannelData, &assetData)
2968+
assetData, err := parseChannelData(node, mainChan.CustomChannelData)
29822969
require.NoError(t, err)
29832970

29842971
return forceCloseExpiryInfo{
@@ -3092,8 +3079,9 @@ func assertPendingChannelAssetData(t *testing.T, node *HarnessNode,
30923079
"custom channel data", targetChanPointStr)
30933080
}
30943081

3095-
var closeData rfqmsg.JsonAssetChannel
3096-
err = json.Unmarshal(targetChan.CustomChannelData, &closeData)
3082+
closeData, err := parseChannelData(
3083+
node, targetChan.CustomChannelData,
3084+
)
30973085
if err != nil {
30983086
return fmt.Errorf("error unmarshalling custom channel "+
30993087
"data: %v", err)
@@ -3191,8 +3179,7 @@ func assertClosedChannelAssetData(t *testing.T, node *HarnessNode,
31913179
require.NotNil(t, closedChan)
31923180
require.NotEmpty(t, closedChan.CustomChannelData)
31933181

3194-
var closeData rfqmsg.JsonAssetChannel
3195-
err = json.Unmarshal(closedChan.CustomChannelData, &closeData)
3182+
closeData, err := parseChannelData(node, closedChan.CustomChannelData)
31963183
require.NoError(t, err)
31973184

31983185
require.GreaterOrEqual(t, len(closeData.FundingAssets), 1)
@@ -3643,3 +3630,53 @@ func assertForceCloseSweeps(ctx context.Context, net *NetworkHarness,
36433630

36443631
return aliceExpectedBalance, bobExpectedBalance
36453632
}
3633+
3634+
// oldJsonAssetChanInfo is a struct that represents the old channel information
3635+
// of a single asset within a channel, as it looked for litd v0.14.1 and before.
3636+
type oldJsonAssetChanInfo struct {
3637+
AssetInfo rfqmsg.JsonAssetUtxo `json:"asset_utxo"`
3638+
Capacity uint64 `json:"capacity"`
3639+
LocalBalance uint64 `json:"local_balance"`
3640+
RemoteBalance uint64 `json:"remote_balance"`
3641+
}
3642+
3643+
// oldJsonAssetChannel is a struct that represents the old channel information
3644+
// of all assets within a channel, as it looked for litd v0.14.1 and before.
3645+
type oldJsonAssetChannel struct {
3646+
Assets []oldJsonAssetChanInfo `json:"assets"`
3647+
}
3648+
3649+
// parseChannelData parses the given channel data into a rfqmsg.JsonAssetChannel
3650+
// struct. It can deal with the old (litd v0.14.1 and before) and new (litd
3651+
// v0.15.0 and after) channel data formats.
3652+
func parseChannelData(node *HarnessNode,
3653+
data []byte) (*rfqmsg.JsonAssetChannel, error) {
3654+
3655+
var closeData rfqmsg.JsonAssetChannel
3656+
if node.Cfg.OldChannelFormat {
3657+
var oldData oldJsonAssetChannel
3658+
err := json.Unmarshal(data, &oldData)
3659+
if err != nil {
3660+
return nil, fmt.Errorf("error unmarshalling old "+
3661+
"channel data: %w", err)
3662+
}
3663+
3664+
oldAsset := oldData.Assets[0]
3665+
closeData.FundingAssets = []rfqmsg.JsonAssetUtxo{
3666+
oldAsset.AssetInfo,
3667+
}
3668+
closeData.Capacity = oldAsset.Capacity
3669+
closeData.LocalBalance = oldAsset.LocalBalance
3670+
closeData.RemoteBalance = oldAsset.RemoteBalance
3671+
3672+
return &closeData, nil
3673+
}
3674+
3675+
err := json.Unmarshal(data, &closeData)
3676+
if err != nil {
3677+
return nil, fmt.Errorf("error unmarshalling channel data: %w",
3678+
err)
3679+
}
3680+
3681+
return &closeData, nil
3682+
}

itest/litd_test_list_on_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,13 @@ var allTestCases = []*testCase{
138138
test: testCustomChannelsMultiChannelPathfinding,
139139
noAliceBob: true,
140140
},
141+
{
142+
name: "custom channels self-payment backward " +
143+
"compatibility",
144+
test: testCustomChannelsSelfPayment,
145+
noAliceBob: true,
146+
backwardCompat: map[string]string{
147+
"Alice": "v0.14.1-alpha",
148+
},
149+
},
141150
}

0 commit comments

Comments
 (0)