lnwallet: ensure the onion blob is copied over properly when restore log updates

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.
This commit is contained in:
Olaoluwa Osuntokun 2017-12-10 16:14:33 -08:00
parent 5b4aa82667
commit ff6993bb5d
No known key found for this signature in database
GPG Key ID: 964EA263DD637C21
2 changed files with 16 additions and 5 deletions

View File

@ -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,

View File

@ -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)