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.
This commit is contained in:
Joost Jager 2020-02-10 13:52:19 +01:00
parent 2cd26d7556
commit 88eae6eafe
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
2 changed files with 16 additions and 2 deletions

@ -1064,7 +1064,7 @@ func (u *updateLog) appendUpdate(pd *PaymentDescriptor) {
// also added to index accordingly. This function differs from appendUpdate in // also added to index accordingly. This function differs from appendUpdate in
// that it won't increment the log index counter. // that it won't increment the log index counter.
func (u *updateLog) restoreUpdate(pd *PaymentDescriptor) { 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 // appendHtlc appends a new HTLC offer to the tip of the update log. The entry

@ -3146,7 +3146,7 @@ func TestChanSyncOweCommitmentPendingRemote(t *testing.T) {
} }
// Bob signs the commitment he owes. // Bob signs the commitment he owes.
_, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() bobCommit, bobHtlcSigs, _, err := bobChannel.SignNextCommitment()
if err != nil { if err != nil {
t.Fatalf("unable to sign commitment: %v", err) t.Fatalf("unable to sign commitment: %v", err)
} }
@ -3155,6 +3155,20 @@ func TestChanSyncOweCommitmentPendingRemote(t *testing.T) {
if len(bobHtlcSigs) != 0 { if len(bobHtlcSigs) != 0 {
t.Fatalf("no htlcs expected, but got %v", len(bobHtlcSigs)) 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 // TestChanSyncOweRevocation tests that if Bob restarts (and then Alice) before