From ff6993bb5d0b5f038a138859864c45451a968655 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 10 Dec 2017 16:14:33 -0800 Subject: [PATCH] lnwallet: ensure the onion blob is copied over properly when restore log updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, we fix an existing bug that would cause issues within the switch due to a value not being properly set. Before this commit we would copy a byte array into a slice without first creating the necessary capacity for that slice. To fix this, we’ll now ensure that the blob has the proper capacity before copying over. Several tests have been updated to always set a fake onion blob. --- lnwallet/channel.go | 3 ++- lnwallet/channel_test.go | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index b7570156..fd209a8b 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -643,7 +643,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate btcutil.Amount, // If this is a pending commit, then the HTLC was only included in the // commitment of the remote party, so we only set that commit height. // Otherwise, we'll set the commit height for both chains as the HTLC - // was written to dis after it was fully locked in. + // was written to disk after it was fully locked in. if isPendingCommit { pd.addCommitHeightRemote = commitHeight } else { @@ -1427,6 +1427,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, LogIndex: logUpdate.LogIndex, addCommitHeightRemote: commitHeight, } + pd.OnionBlob = make([]byte, len(wireMsg.OnionBlob)) copy(pd.OnionBlob[:], wireMsg.OnionBlob[:]) isDustRemote := htlcIsDust(false, false, feeRate, diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 8c2c5fba..db1948ff 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -1381,6 +1381,9 @@ func TestStateUpdatePersistence(t *testing.T) { const numHtlcs = 4 htlcAmt := lnwire.NewMSatFromSatoshis(5000) + var fakeOnionBlob [lnwire.OnionPacketSize]byte + copy(fakeOnionBlob[:], bytes.Repeat([]byte{0x05}, lnwire.OnionPacketSize)) + // Alice adds 3 HTLCs to the update log, while Bob adds a single HTLC. var alicePreimage [32]byte copy(alicePreimage[:], bytes.Repeat([]byte{0xaa}, 32)) @@ -1392,6 +1395,7 @@ func TestStateUpdatePersistence(t *testing.T) { PaymentHash: rHash, Amount: htlcAmt, Expiry: uint32(10), + OnionBlob: fakeOnionBlob, } if _, err := aliceChannel.AddHTLC(h); err != nil { @@ -1406,6 +1410,7 @@ func TestStateUpdatePersistence(t *testing.T) { PaymentHash: rHash, Amount: htlcAmt, Expiry: uint32(10), + OnionBlob: fakeOnionBlob, } if _, err := bobChannel.AddHTLC(bobh); err != nil { t.Fatalf("unable to add bob's htlc: %v", err) @@ -1530,7 +1535,7 @@ func TestStateUpdatePersistence(t *testing.T) { // proper pk scripts // Newly generated pkScripts for HTLCs should be the same as in the old channel. - for _, entry := range aliceChannel.localUpdateLog.updateIndex { + for _, entry := range aliceChannel.localUpdateLog.htlcIndex { htlc := entry.Value.(*PaymentDescriptor) restoredHtlc := aliceChannelNew.localUpdateLog.lookupHtlc(htlc.HtlcIndex) if !bytes.Equal(htlc.ourPkScript, restoredHtlc.ourPkScript) { @@ -1542,7 +1547,7 @@ func TestStateUpdatePersistence(t *testing.T) { htlc.theirPkScript[:5], restoredHtlc.theirPkScript[:5]) } } - for _, entry := range aliceChannel.remoteUpdateLog.updateIndex { + for _, entry := range aliceChannel.remoteUpdateLog.htlcIndex { htlc := entry.Value.(*PaymentDescriptor) restoredHtlc := aliceChannelNew.remoteUpdateLog.lookupHtlc(htlc.HtlcIndex) if !bytes.Equal(htlc.ourPkScript, restoredHtlc.ourPkScript) { @@ -1554,7 +1559,7 @@ func TestStateUpdatePersistence(t *testing.T) { htlc.theirPkScript[:5], restoredHtlc.theirPkScript[:5]) } } - for _, entry := range bobChannel.localUpdateLog.updateIndex { + for _, entry := range bobChannel.localUpdateLog.htlcIndex { htlc := entry.Value.(*PaymentDescriptor) restoredHtlc := bobChannelNew.localUpdateLog.lookupHtlc(htlc.HtlcIndex) if !bytes.Equal(htlc.ourPkScript, restoredHtlc.ourPkScript) { @@ -1566,7 +1571,7 @@ func TestStateUpdatePersistence(t *testing.T) { htlc.theirPkScript[:5], restoredHtlc.theirPkScript[:5]) } } - for _, entry := range bobChannel.remoteUpdateLog.updateIndex { + for _, entry := range bobChannel.remoteUpdateLog.htlcIndex { htlc := entry.Value.(*PaymentDescriptor) restoredHtlc := bobChannelNew.remoteUpdateLog.lookupHtlc(htlc.HtlcIndex) if !bytes.Equal(htlc.ourPkScript, restoredHtlc.ourPkScript) { @@ -2550,6 +2555,9 @@ func TestChanSyncOweCommitment(t *testing.T) { } defer cleanUp() + var fakeOnionBlob [lnwire.OnionPacketSize]byte + copy(fakeOnionBlob[:], bytes.Repeat([]byte{0x05}, lnwire.OnionPacketSize)) + // We'll start off the scenario with Bob sending 3 HTLC's to Alice in a // single state update. htlcAmt := lnwire.NewMSatFromSatoshis(20000) @@ -2562,6 +2570,7 @@ func TestChanSyncOweCommitment(t *testing.T) { PaymentHash: rHash, Amount: htlcAmt, Expiry: uint32(10), + OnionBlob: fakeOnionBlob, } if _, err := bobChannel.AddHTLC(h); err != nil { @@ -2602,6 +2611,7 @@ func TestChanSyncOweCommitment(t *testing.T) { PaymentHash: rHash, Amount: htlcAmt, Expiry: uint32(10), + OnionBlob: fakeOnionBlob, } if _, err := aliceChannel.AddHTLC(aliceHtlc); err != nil { t.Fatalf("unable to add alice's htlc: %v", err)