@@ -15,6 +15,7 @@ import (
15
15
"github.com/btcsuite/btcd/btcec/v2/schnorr"
16
16
"github.com/btcsuite/btcd/btcutil"
17
17
"github.com/btcsuite/btcd/chaincfg/chainhash"
18
+ "github.com/btcsuite/btcd/wire"
18
19
"github.com/davecgh/go-spew/spew"
19
20
"github.com/lightninglabs/taproot-assets/itest"
20
21
"github.com/lightninglabs/taproot-assets/proof"
@@ -916,10 +917,19 @@ func waitForSendEvent(t *testing.T,
916
917
}
917
918
}
918
919
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.
919
929
func closeAssetChannelAndAssert (t * harnessTest , net * NetworkHarness ,
920
930
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 ) {
923
933
924
934
t .t .Helper ()
925
935
@@ -953,6 +963,41 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
953
963
954
964
waitForSendEvent (t .t , sendEvents , tapfreighter .SendStateComplete )
955
965
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
+
956
1001
// With the channel closed, we'll now assert that the co-op close
957
1002
// transaction was inserted into the local universe.
958
1003
//
@@ -972,49 +1017,50 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
972
1017
additionalOutputs ++
973
1018
}
974
1019
975
- require .Len (t .t , closeTx .TxOut , numOutputs )
1020
+ closeTxid := closeTx .TxHash ()
1021
+ require .Len (t , closeTx .TxOut , numOutputs )
976
1022
977
1023
outIdx := 0
978
1024
dummyAmt := int64 (1000 )
979
- require .LessOrEqual (t . t , closeTx .TxOut [outIdx ].Value , dummyAmt )
1025
+ require .LessOrEqual (t , closeTx .TxOut [outIdx ].Value , dummyAmt )
980
1026
981
1027
if remoteAssetBalance {
982
1028
outIdx ++
983
- require .LessOrEqual (t . t , closeTx .TxOut [outIdx ].Value , dummyAmt )
1029
+ require .LessOrEqual (t , closeTx .TxOut [outIdx ].Value , dummyAmt )
984
1030
}
985
1031
986
1032
// We also require there to be at most two additional outputs, one for
987
1033
// each of the asset outputs with balance.
988
- require .Len (t . t , closeUpdate .AdditionalOutputs , additionalOutputs )
1034
+ require .Len (t , closeUpdate .AdditionalOutputs , additionalOutputs )
989
1035
990
1036
var remoteCloseOut * lnrpc.CloseOutput
991
1037
if remoteBtcBalance {
992
1038
// The remote node has received a couple of HTLCs with an above
993
1039
// dust value, so it should also have accumulated a non-dust
994
1040
// balance, even after subtracting 1k sats for the asset output.
995
1041
remoteCloseOut = closeUpdate .RemoteCloseOutput
996
- require .NotNil (t . t , remoteCloseOut )
1042
+ require .NotNil (t , remoteCloseOut )
997
1043
998
1044
outIdx ++
999
1045
require .EqualValues (
1000
- t . t , remoteCloseOut .AmountSat - dummyAmt ,
1046
+ t , remoteCloseOut .AmountSat - dummyAmt ,
1001
1047
closeTx .TxOut [outIdx ].Value ,
1002
1048
)
1003
1049
} else if remoteAssetBalance {
1004
1050
// The remote node has received a couple of HTLCs but not enough
1005
1051
// to go above dust. So it should still have an asset balance
1006
1052
// that we can verify.
1007
1053
remoteCloseOut = closeUpdate .RemoteCloseOutput
1008
- require .NotNil (t . t , remoteCloseOut )
1054
+ require .NotNil (t , remoteCloseOut )
1009
1055
}
1010
1056
1011
1057
// The local node should have received the local BTC balance minus the
1012
1058
// TX fees and 1k sats for the asset output.
1013
1059
localCloseOut := closeUpdate .LocalCloseOutput
1014
- require .NotNil (t . t , localCloseOut )
1060
+ require .NotNil (t , localCloseOut )
1015
1061
outIdx ++
1016
1062
require .Greater (
1017
- t . t , closeTx .TxOut [outIdx ].Value ,
1063
+ t , closeTx .TxOut [outIdx ].Value ,
1018
1064
localCloseOut .AmountSat - dummyAmt ,
1019
1065
)
1020
1066
@@ -1039,39 +1085,41 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
1039
1085
1040
1086
if remoteAuxOut != nil {
1041
1087
require .Equal (
1042
- t . t , remoteAuxOut .PkScript ,
1088
+ t , remoteAuxOut .PkScript ,
1043
1089
closeTx .TxOut [remoteAssetIndex ].PkScript ,
1044
1090
)
1045
1091
}
1046
1092
1047
1093
require .Equal (
1048
- t . t , localAuxOut .PkScript ,
1094
+ t , localAuxOut .PkScript ,
1049
1095
closeTx .TxOut [localAssetIndex ].PkScript ,
1050
1096
)
1051
1097
1052
1098
// We now verify the arrival of the local balance asset proof at the
1053
1099
// universe server.
1054
1100
var localAssetCloseOut rfqmsg.JsonCloseOutput
1055
- err = json .Unmarshal (
1101
+ err : = json .Unmarshal (
1056
1102
localCloseOut .CustomChannelData , & localAssetCloseOut ,
1057
1103
)
1058
- require .NoError (t . t , err )
1104
+ require .NoError (t , err )
1059
1105
1060
1106
for assetIDStr , scriptKeyStr := range localAssetCloseOut .ScriptKeys {
1061
1107
scriptKeyBytes , err := hex .DecodeString (scriptKeyStr )
1062
- require .NoError (t . t , err )
1108
+ require .NoError (t , err )
1063
1109
1064
- require .Equal (t . t , hex .EncodeToString (assetID ), assetIDStr )
1110
+ require .Equal (t , hex .EncodeToString (assetID ), assetIDStr )
1065
1111
1066
1112
a := assertUniverseProofExists (
1067
- t . t , universeTap , assetID , groupKey , scriptKeyBytes ,
1113
+ t , universeTap , assetID , groupKey , scriptKeyBytes ,
1068
1114
fmt .Sprintf ("%v:%v" , closeTxid , localAssetIndex ),
1069
1115
)
1070
1116
1117
+ localTapd := newTapClient (t , local )
1118
+
1071
1119
scriptKey , err := btcec .ParsePubKey (scriptKeyBytes )
1072
- require .NoError (t . t , err )
1120
+ require .NoError (t , err )
1073
1121
assertAssetExists (
1074
- t . t , localTapd , assetID , a .Amount , scriptKey , true ,
1122
+ t , localTapd , assetID , a .Amount , scriptKey , true ,
1075
1123
true , false ,
1076
1124
)
1077
1125
}
@@ -1083,33 +1131,33 @@ func closeAssetChannelAndAssert(t *harnessTest, net *NetworkHarness,
1083
1131
1084
1132
// At this point the remote close output should be defined, otherwise
1085
1133
// something went wrong.
1086
- require .NotNil (t . t , remoteCloseOut )
1134
+ require .NotNil (t , remoteCloseOut )
1087
1135
1088
1136
// And then we verify the arrival of the remote balance asset proof at
1089
1137
// the universe server as well.
1090
1138
var remoteAssetCloseOut rfqmsg.JsonCloseOutput
1091
1139
err = json .Unmarshal (
1092
1140
remoteCloseOut .CustomChannelData , & remoteAssetCloseOut ,
1093
1141
)
1094
- require .NoError (t . t , err )
1142
+ require .NoError (t , err )
1095
1143
1096
1144
for assetIDStr , scriptKeyStr := range remoteAssetCloseOut .ScriptKeys {
1097
1145
scriptKeyBytes , err := hex .DecodeString (scriptKeyStr )
1098
- require .NoError (t . t , err )
1146
+ require .NoError (t , err )
1099
1147
1100
- require .Equal (t . t , hex .EncodeToString (assetID ), assetIDStr )
1148
+ require .Equal (t , hex .EncodeToString (assetID ), assetIDStr )
1101
1149
1102
1150
a := assertUniverseProofExists (
1103
- t . t , universeTap , assetID , groupKey , scriptKeyBytes ,
1151
+ t , universeTap , assetID , groupKey , scriptKeyBytes ,
1104
1152
fmt .Sprintf ("%v:%v" , closeTxid , remoteAssetIndex ),
1105
1153
)
1106
1154
1107
- remoteTapd := newTapClient (t . t , remote )
1155
+ remoteTapd := newTapClient (t , remote )
1108
1156
1109
1157
scriptKey , err := btcec .ParsePubKey (scriptKeyBytes )
1110
- require .NoError (t . t , err )
1158
+ require .NoError (t , err )
1111
1159
assertAssetExists (
1112
- t . t , remoteTapd , assetID , a .Amount , scriptKey , true ,
1160
+ t , remoteTapd , assetID , a .Amount , scriptKey , true ,
1113
1161
true , false ,
1114
1162
)
1115
1163
}
0 commit comments