Skip to content

Commit 9594f86

Browse files
committed
lnwallet: add noop case to retransmit test
To make sure we don't cause force-closures because of commit sig mismatches, we add a test case to verify that the retransmitted HTLC matches the original HTLC.
1 parent d91ca9f commit 9594f86

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

lnwallet/channel_test.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,9 @@ func restartChannel(channelOld *LightningChannel) (*LightningChannel, error) {
32323232
// he receives Alice's CommitSig message, then Alice concludes that she needs
32333233
// to re-send the CommitDiff. After the diff has been sent, both nodes should
32343234
// resynchronize and be able to complete the dangling commit.
3235-
func testChanSyncOweCommitment(t *testing.T, chanType channeldb.ChannelType) {
3235+
func testChanSyncOweCommitment(t *testing.T,
3236+
chanType channeldb.ChannelType, noop bool) {
3237+
32363238
// Create a test channel which will be used for the duration of this
32373239
// unittest. The channel will be funded evenly with Alice having 5 BTC,
32383240
// and Bob having 5 BTC.
@@ -3242,6 +3244,17 @@ func testChanSyncOweCommitment(t *testing.T, chanType channeldb.ChannelType) {
32423244
var fakeOnionBlob [lnwire.OnionPacketSize]byte
32433245
copy(fakeOnionBlob[:], bytes.Repeat([]byte{0x05}, lnwire.OnionPacketSize))
32443246

3247+
// Let's create the noop add TLV record. This will only be
3248+
// effective for channels that have a tapscript root.
3249+
noopRecord := tlv.NewPrimitiveRecord[NoOpHtlcTLVType, bool](true)
3250+
records, err := tlv.RecordsToMap([]tlv.Record{noopRecord.Record()})
3251+
require.NoError(t, err)
3252+
3253+
// If the noop flag is not set for this test, nullify the records.
3254+
if !noop {
3255+
records = nil
3256+
}
3257+
32453258
// We'll start off the scenario with Bob sending 3 HTLC's to Alice in a
32463259
// single state update.
32473260
htlcAmt := lnwire.NewMSatFromSatoshis(20000)
@@ -3251,10 +3264,11 @@ func testChanSyncOweCommitment(t *testing.T, chanType channeldb.ChannelType) {
32513264
for i := 0; i < 3; i++ {
32523265
rHash := sha256.Sum256(bobPreimage[:])
32533266
h := &lnwire.UpdateAddHTLC{
3254-
PaymentHash: rHash,
3255-
Amount: htlcAmt,
3256-
Expiry: uint32(10),
3257-
OnionBlob: fakeOnionBlob,
3267+
PaymentHash: rHash,
3268+
Amount: htlcAmt,
3269+
Expiry: uint32(10),
3270+
OnionBlob: fakeOnionBlob,
3271+
CustomRecords: records,
32583272
}
32593273

32603274
htlcIndex, err := bobChannel.AddHTLC(h, nil)
@@ -3290,15 +3304,17 @@ func testChanSyncOweCommitment(t *testing.T, chanType channeldb.ChannelType) {
32903304
t.Fatalf("unable to settle htlc: %v", err)
32913305
}
32923306
}
3307+
32933308
var alicePreimage [32]byte
32943309
copy(alicePreimage[:], bytes.Repeat([]byte{0xaa}, 32))
32953310
rHash := sha256.Sum256(alicePreimage[:])
32963311
aliceHtlc := &lnwire.UpdateAddHTLC{
3297-
ChanID: chanID,
3298-
PaymentHash: rHash,
3299-
Amount: htlcAmt,
3300-
Expiry: uint32(10),
3301-
OnionBlob: fakeOnionBlob,
3312+
ChanID: chanID,
3313+
PaymentHash: rHash,
3314+
Amount: htlcAmt,
3315+
Expiry: uint32(10),
3316+
OnionBlob: fakeOnionBlob,
3317+
CustomRecords: records,
33023318
}
33033319
aliceHtlcIndex, err := aliceChannel.AddHTLC(aliceHtlc, nil)
33043320
require.NoError(t, err, "unable to add alice's htlc")
@@ -3519,22 +3535,25 @@ func testChanSyncOweCommitment(t *testing.T, chanType channeldb.ChannelType) {
35193535

35203536
// At this point, the final balances of both parties should properly
35213537
// reflect the amount of HTLC's sent.
3522-
bobMsatSent := numBobHtlcs * htlcAmt
3523-
if aliceChannel.channelState.TotalMSatSent != htlcAmt {
3524-
t.Fatalf("wrong value for msat sent: expected %v, got %v",
3525-
htlcAmt, aliceChannel.channelState.TotalMSatSent)
3526-
}
3527-
if aliceChannel.channelState.TotalMSatReceived != bobMsatSent {
3528-
t.Fatalf("wrong value for msat recv: expected %v, got %v",
3529-
bobMsatSent, aliceChannel.channelState.TotalMSatReceived)
3530-
}
3531-
if bobChannel.channelState.TotalMSatSent != bobMsatSent {
3532-
t.Fatalf("wrong value for msat sent: expected %v, got %v",
3533-
bobMsatSent, bobChannel.channelState.TotalMSatSent)
3534-
}
3535-
if bobChannel.channelState.TotalMSatReceived != htlcAmt {
3536-
t.Fatalf("wrong value for msat recv: expected %v, got %v",
3537-
htlcAmt, bobChannel.channelState.TotalMSatReceived)
3538+
if noop {
3539+
// If this test-case includes noop HTLCs, then we don't expect
3540+
// any balance changes.
3541+
require.Zero(t, aliceChannel.channelState.TotalMSatSent)
3542+
require.Zero(t, aliceChannel.channelState.TotalMSatReceived)
3543+
require.Zero(t, bobChannel.channelState.TotalMSatSent)
3544+
require.Zero(t, bobChannel.channelState.TotalMSatReceived)
3545+
} else {
3546+
// Otherwise, calculate the expected changes and assert them.
3547+
bobMsatSent := numBobHtlcs * htlcAmt
3548+
3549+
aliceChan := aliceChannel.channelState
3550+
bobChan := bobChannel.channelState
3551+
3552+
require.Equal(t, aliceChan.TotalMSatSent, htlcAmt)
3553+
require.Equal(t, aliceChan.TotalMSatReceived, bobMsatSent)
3554+
3555+
require.Equal(t, bobChan.TotalMSatSent, bobMsatSent)
3556+
require.Equal(t, bobChan.TotalMSatReceived, htlcAmt)
35383557
}
35393558
}
35403559

@@ -3548,6 +3567,7 @@ func TestChanSyncOweCommitment(t *testing.T) {
35483567
testCases := []struct {
35493568
name string
35503569
chanType channeldb.ChannelType
3570+
noop bool
35513571
}{
35523572
{
35533573
name: "tweakless",
@@ -3571,10 +3591,18 @@ func TestChanSyncOweCommitment(t *testing.T) {
35713591
channeldb.SimpleTaprootFeatureBit |
35723592
channeldb.TapscriptRootBit,
35733593
},
3594+
{
3595+
name: "tapscript root with noop",
3596+
chanType: channeldb.SingleFunderTweaklessBit |
3597+
channeldb.AnchorOutputsBit |
3598+
channeldb.SimpleTaprootFeatureBit |
3599+
channeldb.TapscriptRootBit,
3600+
noop: true,
3601+
},
35743602
}
35753603
for _, tc := range testCases {
35763604
t.Run(tc.name, func(t *testing.T) {
3577-
testChanSyncOweCommitment(t, tc.chanType)
3605+
testChanSyncOweCommitment(t, tc.chanType, tc.noop)
35783606
})
35793607
}
35803608
}

0 commit comments

Comments
 (0)