From 88eae6eafe8ad54945a9501537244cf4cb6b3de1 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Mon, 10 Feb 2020 13:52:19 +0100 Subject: [PATCH] lnwallet: fix invalid value use in restoreUpdate Updates were always restored with the same log index. This could cause a crash when the logs were compacted and possibly other problems elsewhere. Extended unit test to cover the crash scenario. --- lnwallet/channel.go | 2 +- lnwallet/channel_test.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index bc472598..131d8632 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1064,7 +1064,7 @@ func (u *updateLog) appendUpdate(pd *PaymentDescriptor) { // also added to index accordingly. This function differs from appendUpdate in // that it won't increment the log index counter. func (u *updateLog) restoreUpdate(pd *PaymentDescriptor) { - u.updateIndex[u.logIndex] = u.PushBack(pd) + u.updateIndex[pd.LogIndex] = u.PushBack(pd) } // appendHtlc appends a new HTLC offer to the tip of the update log. The entry diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 03ac925f..1e871ab6 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -3146,7 +3146,7 @@ func TestChanSyncOweCommitmentPendingRemote(t *testing.T) { } // Bob signs the commitment he owes. - _, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobCommit, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() if err != nil { t.Fatalf("unable to sign commitment: %v", err) } @@ -3155,6 +3155,20 @@ func TestChanSyncOweCommitmentPendingRemote(t *testing.T) { if len(bobHtlcSigs) != 0 { t.Fatalf("no htlcs expected, but got %v", len(bobHtlcSigs)) } + + // Get Alice to revoke and trigger Bob to compact his logs. + err = aliceChannel.ReceiveNewCommitment(bobCommit, bobHtlcSigs) + if err != nil { + t.Fatal(err) + } + aliceRevoke, _, err := aliceChannel.RevokeCurrentCommitment() + if err != nil { + t.Fatal(err) + } + _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevoke) + if err != nil { + t.Fatal(err) + } } // TestChanSyncOweRevocation tests that if Bob restarts (and then Alice) before