Skip to content

Commit 20d928f

Browse files
committed
itest: refactor co-op close channel balance check
1 parent 79fec8a commit 20d928f

File tree

2 files changed

+84
-36
lines changed

2 files changed

+84
-36
lines changed

itest/assets_test.go

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1616
"github.com/btcsuite/btcd/btcutil"
1717
"github.com/btcsuite/btcd/chaincfg/chainhash"
18+
"github.com/btcsuite/btcd/wire"
1819
"github.com/davecgh/go-spew/spew"
1920
"github.com/lightninglabs/taproot-assets/itest"
2021
"github.com/lightninglabs/taproot-assets/proof"
@@ -916,10 +917,19 @@ func waitForSendEvent(t *testing.T,
916917
}
917918
}
918919

920+
// coOpCloseBalanceCheck is a function type that can be passed into
921+
// closeAssetChannelAndAsset to asset the final balance of the closing
922+
// transaction.
923+
type coOpCloseBalanceCheck func(t *testing.T, local, remote *HarnessNode,
924+
closeTx *wire.MsgTx, closeUpdate *lnrpc.ChannelCloseUpdate,
925+
assetID, groupKey []byte, universeTap *tapClient)
926+
927+
// closeAssetChannelAndAssert closes the channel between the local and remote
928+
// node and asserts the final balances of the closing transaction.
919929
func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
920930
local, remote *HarnessNode, chanPoint *lnrpc.ChannelPoint,
921-
assetID, groupKey []byte, universeTap *tapClient, remoteBtcBalance,
922-
remoteAssetBalance bool) {
931+
assetID, groupKey []byte, universeTap *tapClient,
932+
balanceCheck coOpCloseBalanceCheck) {
923933

924934
t.t.Helper()
925935

@@ -953,6 +963,41 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
953963

954964
waitForSendEvent(t.t, sendEvents, tapfreighter.SendStateComplete)
955965

966+
// Check the final balance of the closing transaction.
967+
balanceCheck(
968+
t.t, local, remote, closeTx, closeUpdate, assetID, groupKey,
969+
universeTap,
970+
)
971+
}
972+
973+
// assertDefaultCoOpCloseBalance returns a default implementation of the co-op
974+
// close balance check that can be used in tests. It assumes the initiator has
975+
// both an asset and BTC balance left, while the responder's balance can be
976+
// specified with the boolean variables.
977+
func assertDefaultCoOpCloseBalance(remoteBtcBalance,
978+
remoteAssetBalance bool) coOpCloseBalanceCheck {
979+
980+
return func(t *testing.T, local, remote *HarnessNode,
981+
closeTx *wire.MsgTx, closeUpdate *lnrpc.ChannelCloseUpdate,
982+
assetID, groupKey []byte, universeTap *tapClient) {
983+
984+
defaultCoOpCloseBalanceCheck(
985+
t, local, remote, closeTx, closeUpdate, assetID,
986+
groupKey, universeTap, remoteBtcBalance,
987+
remoteAssetBalance,
988+
)
989+
}
990+
}
991+
992+
// defaultCoOpCloseBalanceCheck is a default implementation of the co-op close
993+
// balance check that can be used in tests. It assumes the initiator has both
994+
// an asset and BTC balance left, while the responder's balance can be specified
995+
// with the boolean variables.
996+
func defaultCoOpCloseBalanceCheck(t *testing.T, local, remote *HarnessNode,
997+
closeTx *wire.MsgTx, closeUpdate *lnrpc.ChannelCloseUpdate,
998+
assetID, groupKey []byte, universeTap *tapClient, remoteBtcBalance,
999+
remoteAssetBalance bool) {
1000+
9561001
// With the channel closed, we'll now assert that the co-op close
9571002
// transaction was inserted into the local universe.
9581003
//
@@ -972,49 +1017,50 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
9721017
additionalOutputs++
9731018
}
9741019

975-
require.Len(t.t, closeTx.TxOut, numOutputs)
1020+
closeTxid := closeTx.TxHash()
1021+
require.Len(t, closeTx.TxOut, numOutputs)
9761022

9771023
outIdx := 0
9781024
dummyAmt := int64(1000)
979-
require.LessOrEqual(t.t, closeTx.TxOut[outIdx].Value, dummyAmt)
1025+
require.LessOrEqual(t, closeTx.TxOut[outIdx].Value, dummyAmt)
9801026

9811027
if remoteAssetBalance {
9821028
outIdx++
983-
require.LessOrEqual(t.t, closeTx.TxOut[outIdx].Value, dummyAmt)
1029+
require.LessOrEqual(t, closeTx.TxOut[outIdx].Value, dummyAmt)
9841030
}
9851031

9861032
// We also require there to be at most two additional outputs, one for
9871033
// each of the asset outputs with balance.
988-
require.Len(t.t, closeUpdate.AdditionalOutputs, additionalOutputs)
1034+
require.Len(t, closeUpdate.AdditionalOutputs, additionalOutputs)
9891035

9901036
var remoteCloseOut *lnrpc.CloseOutput
9911037
if remoteBtcBalance {
9921038
// The remote node has received a couple of HTLCs with an above
9931039
// dust value, so it should also have accumulated a non-dust
9941040
// balance, even after subtracting 1k sats for the asset output.
9951041
remoteCloseOut = closeUpdate.RemoteCloseOutput
996-
require.NotNil(t.t, remoteCloseOut)
1042+
require.NotNil(t, remoteCloseOut)
9971043

9981044
outIdx++
9991045
require.EqualValues(
1000-
t.t, remoteCloseOut.AmountSat-dummyAmt,
1046+
t, remoteCloseOut.AmountSat-dummyAmt,
10011047
closeTx.TxOut[outIdx].Value,
10021048
)
10031049
} else if remoteAssetBalance {
10041050
// The remote node has received a couple of HTLCs but not enough
10051051
// to go above dust. So it should still have an asset balance
10061052
// that we can verify.
10071053
remoteCloseOut = closeUpdate.RemoteCloseOutput
1008-
require.NotNil(t.t, remoteCloseOut)
1054+
require.NotNil(t, remoteCloseOut)
10091055
}
10101056

10111057
// The local node should have received the local BTC balance minus the
10121058
// TX fees and 1k sats for the asset output.
10131059
localCloseOut := closeUpdate.LocalCloseOutput
1014-
require.NotNil(t.t, localCloseOut)
1060+
require.NotNil(t, localCloseOut)
10151061
outIdx++
10161062
require.Greater(
1017-
t.t, closeTx.TxOut[outIdx].Value,
1063+
t, closeTx.TxOut[outIdx].Value,
10181064
localCloseOut.AmountSat-dummyAmt,
10191065
)
10201066

@@ -1039,39 +1085,41 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
10391085

10401086
if remoteAuxOut != nil {
10411087
require.Equal(
1042-
t.t, remoteAuxOut.PkScript,
1088+
t, remoteAuxOut.PkScript,
10431089
closeTx.TxOut[remoteAssetIndex].PkScript,
10441090
)
10451091
}
10461092

10471093
require.Equal(
1048-
t.t, localAuxOut.PkScript,
1094+
t, localAuxOut.PkScript,
10491095
closeTx.TxOut[localAssetIndex].PkScript,
10501096
)
10511097

10521098
// We now verify the arrival of the local balance asset proof at the
10531099
// universe server.
10541100
var localAssetCloseOut rfqmsg.JsonCloseOutput
1055-
err = json.Unmarshal(
1101+
err := json.Unmarshal(
10561102
localCloseOut.CustomChannelData, &localAssetCloseOut,
10571103
)
1058-
require.NoError(t.t, err)
1104+
require.NoError(t, err)
10591105

10601106
for assetIDStr, scriptKeyStr := range localAssetCloseOut.ScriptKeys {
10611107
scriptKeyBytes, err := hex.DecodeString(scriptKeyStr)
1062-
require.NoError(t.t, err)
1108+
require.NoError(t, err)
10631109

1064-
require.Equal(t.t, hex.EncodeToString(assetID), assetIDStr)
1110+
require.Equal(t, hex.EncodeToString(assetID), assetIDStr)
10651111

10661112
a := assertUniverseProofExists(
1067-
t.t, universeTap, assetID, groupKey, scriptKeyBytes,
1113+
t, universeTap, assetID, groupKey, scriptKeyBytes,
10681114
fmt.Sprintf("%v:%v", closeTxid, localAssetIndex),
10691115
)
10701116

1117+
localTapd := newTapClient(t, local)
1118+
10711119
scriptKey, err := btcec.ParsePubKey(scriptKeyBytes)
1072-
require.NoError(t.t, err)
1120+
require.NoError(t, err)
10731121
assertAssetExists(
1074-
t.t, localTapd, assetID, a.Amount, scriptKey, true,
1122+
t, localTapd, assetID, a.Amount, scriptKey, true,
10751123
true, false,
10761124
)
10771125
}
@@ -1083,33 +1131,33 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
10831131

10841132
// At this point the remote close output should be defined, otherwise
10851133
// something went wrong.
1086-
require.NotNil(t.t, remoteCloseOut)
1134+
require.NotNil(t, remoteCloseOut)
10871135

10881136
// And then we verify the arrival of the remote balance asset proof at
10891137
// the universe server as well.
10901138
var remoteAssetCloseOut rfqmsg.JsonCloseOutput
10911139
err = json.Unmarshal(
10921140
remoteCloseOut.CustomChannelData, &remoteAssetCloseOut,
10931141
)
1094-
require.NoError(t.t, err)
1142+
require.NoError(t, err)
10951143

10961144
for assetIDStr, scriptKeyStr := range remoteAssetCloseOut.ScriptKeys {
10971145
scriptKeyBytes, err := hex.DecodeString(scriptKeyStr)
1098-
require.NoError(t.t, err)
1146+
require.NoError(t, err)
10991147

1100-
require.Equal(t.t, hex.EncodeToString(assetID), assetIDStr)
1148+
require.Equal(t, hex.EncodeToString(assetID), assetIDStr)
11011149

11021150
a := assertUniverseProofExists(
1103-
t.t, universeTap, assetID, groupKey, scriptKeyBytes,
1151+
t, universeTap, assetID, groupKey, scriptKeyBytes,
11041152
fmt.Sprintf("%v:%v", closeTxid, remoteAssetIndex),
11051153
)
11061154

1107-
remoteTapd := newTapClient(t.t, remote)
1155+
remoteTapd := newTapClient(t, remote)
11081156

11091157
scriptKey, err := btcec.ParsePubKey(scriptKeyBytes)
1110-
require.NoError(t.t, err)
1158+
require.NoError(t, err)
11111159
assertAssetExists(
1112-
t.t, remoteTapd, assetID, a.Amount, scriptKey, true,
1160+
t, remoteTapd, assetID, a.Amount, scriptKey, true,
11131161
true, false,
11141162
)
11151163
}

itest/litd_custom_channels_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,19 @@ func testCustomChannels(_ context.Context, net *NetworkHarness,
562562
t.Logf("Closing Charlie -> Dave channel")
563563
closeAssetChannelAndAssert(
564564
t, net, charlie, dave, charlieChanPoint, assetID, nil,
565-
universeTap, true, true,
565+
universeTap, assertDefaultCoOpCloseBalance(true, true),
566566
)
567567

568568
t.Logf("Closing Dave -> Yara channel, close initiated by Yara")
569569
closeAssetChannelAndAssert(
570570
t, net, yara, dave, daveChanPoint, assetID, nil,
571-
universeTap, false, true,
571+
universeTap, assertDefaultCoOpCloseBalance(false, true),
572572
)
573573

574574
t.Logf("Closing Erin -> Fabia channel")
575575
closeAssetChannelAndAssert(
576576
t, net, erin, fabia, erinChanPoint, assetID, nil,
577-
universeTap, true, true,
577+
universeTap, assertDefaultCoOpCloseBalance(true, true),
578578
)
579579

580580
// We've been tracking the off-chain channel balances all this time, so
@@ -627,7 +627,7 @@ func testCustomChannels(_ context.Context, net *NetworkHarness,
627627
t.Logf("Closing Charlie -> Dave channel")
628628
closeAssetChannelAndAssert(
629629
t, net, charlie, dave, charlieChanPoint, assetID, nil,
630-
universeTap, false, false,
630+
universeTap, assertDefaultCoOpCloseBalance(false, false),
631631
)
632632

633633
// Charlie should still have four asset pieces, two with the same size.
@@ -988,19 +988,19 @@ func testCustomChannelsGroupedAsset(_ context.Context, net *NetworkHarness,
988988
t.Logf("Closing Charlie -> Dave channel")
989989
closeAssetChannelAndAssert(
990990
t, net, charlie, dave, charlieChanPoint, assetID, groupID,
991-
universeTap, true, true,
991+
universeTap, assertDefaultCoOpCloseBalance(true, true),
992992
)
993993

994994
t.Logf("Closing Dave -> Yara channel, close initiated by Yara")
995995
closeAssetChannelAndAssert(
996996
t, net, yara, dave, daveChanPoint, assetID, groupID,
997-
universeTap, false, true,
997+
universeTap, assertDefaultCoOpCloseBalance(false, true),
998998
)
999999

10001000
t.Logf("Closing Erin -> Fabia channel")
10011001
closeAssetChannelAndAssert(
10021002
t, net, erin, fabia, erinChanPoint, assetID, groupID,
1003-
universeTap, true, true,
1003+
universeTap, assertDefaultCoOpCloseBalance(true, true),
10041004
)
10051005

10061006
// We've been tracking the off-chain channel balances all this time, so
@@ -1053,7 +1053,7 @@ func testCustomChannelsGroupedAsset(_ context.Context, net *NetworkHarness,
10531053
t.Logf("Closing Charlie -> Dave channel")
10541054
closeAssetChannelAndAssert(
10551055
t, net, charlie, dave, charlieChanPoint, assetID, groupID,
1056-
universeTap, false, false,
1056+
universeTap, assertDefaultCoOpCloseBalance(false, false),
10571057
)
10581058

10591059
// Charlie should still have four asset pieces, two with the same size.

0 commit comments

Comments
 (0)