From 1b4e723a5dc7ebd3814ca34a52b257fd7d43995d Mon Sep 17 00:00:00 2001 From: Andrey Samokhvalov Date: Sun, 9 Jul 2017 02:38:50 +0300 Subject: [PATCH] htlcswicth+channel: switch to store onion blobs in payment descriptor After addition of the retransmission logic in the channel link, we should make the onion blobs persistant, the proper way to do this is include the onion blobs in the payment descriptor rather than storing them in the distinct struct in the channel link. --- htlcswitch/link.go | 30 +++++++----------------------- lnwallet/channel.go | 12 +++++++++--- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 2639ae32..7163c8ac 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -166,16 +166,6 @@ type channelLink struct { // htlc cancel reasons. cancelReasons map[uint64]lnwire.OpaqueReason - // clearedOnionBlobs tracks the remote log index of the incoming - // htlc's, mapped to the htlc onion blob which encapsulates the next - // hop. HTLC's are added to this map once the HTLC has been cleared, - // meaning the commitment state reflects the update encoded within this - // HTLC. - // - // TODO(andrew.shvv) remove after payment descriptor start store - // htlc onion blobs. - clearedOnionBlobs map[uint64][lnwire.OnionPacketSize]byte - // batchCounter is the number of updates which we received from remote // side, but not include in commitment transaction yet and plus the // current number of settles that have been sent, but not yet committed @@ -239,12 +229,11 @@ func NewChannelLink(cfg ChannelLinkConfig, channel *lnwallet.LightningChannel, currentHeight uint32) ChannelLink { return &channelLink{ - cfg: cfg, - channel: channel, - clearedOnionBlobs: make(map[uint64][lnwire.OnionPacketSize]byte), - upstream: make(chan lnwire.Message), - downstream: make(chan *htlcPacket), - linkControl: make(chan interface{}), + cfg: cfg, + channel: channel, + upstream: make(chan lnwire.Message), + downstream: make(chan *htlcPacket), + linkControl: make(chan interface{}), // TODO(roasbeef): just do reserve here? availableBandwidth: uint64(channel.StateSnapshot().LocalBalance), cancelReasons: make(map[uint64]lnwire.OpaqueReason), @@ -747,11 +736,6 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { log.Tracef("Receive upstream htlc with payment hash(%x), "+ "assigning index: %v", msg.PaymentHash[:], index) - // Store the onion blob which encapsulate the htlc route and - // use in on stage of HTLC inclusion to retrieve the next hop - // and propagate the HTLC along the remaining route. - l.clearedOnionBlobs[index] = msg.OnionBlob - case *lnwire.UpdateFufillHTLC: pre := msg.PaymentPreimage idx := msg.ID @@ -1144,8 +1128,8 @@ func (l *channelLink) processLockedInHtlcs( case lnwallet.Add: // Fetch the onion blob that was included within this // processed payment descriptor. - onionBlob := l.clearedOnionBlobs[pd.HtlcIndex] - delete(l.clearedOnionBlobs, pd.HtlcIndex) + var onionBlob [lnwire.OnionPacketSize]byte + copy(onionBlob[:], pd.OnionBlob) // Retrieve onion obfuscator from onion blob in order // to produce initial obfuscation of the onion diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 3f846f97..0bf5dc8f 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -208,9 +208,11 @@ type PaymentDescriptor struct { removeCommitHeightRemote uint64 removeCommitHeightLocal uint64 - // Payload is an opaque blob which is used to complete multi-hop + // OnionBlob is an opaque blob which is used to complete multi-hop // routing. - Payload []byte + // + // NOTE: Populated only on add payment descriptor entry types. + OnionBlob []byte // [our|their|]PkScript are the raw public key scripts that encodes the // redemption rules for this particular HTLC. These fields will only be @@ -534,6 +536,7 @@ func (c *commitment) toChannelDelta(ourCommit bool) (*channeldb.ChannelDelta, er RHash: htlc.RHash, RefundTimeout: htlc.Timeout, OutputIndex: outputIndex, + OnionBlob: htlc.OnionBlob, } if ourCommit && htlc.sig != nil { @@ -1688,6 +1691,7 @@ func (lc *LightningChannel) restoreStateLogs() error { EntryType: Add, addCommitHeightRemote: pastHeight, addCommitHeightLocal: pastHeight, + OnionBlob: htlc.OnionBlob, ourPkScript: ourP2WSH, ourWitnessScript: ourWitnessScript, theirPkScript: theirP2WSH, @@ -2487,7 +2491,7 @@ func (lc *LightningChannel) ReceiveReestablish(msg *lnwire.ChannelReestablish) ( switch htlc.EntryType { case Add: var onionBlob [lnwire.OnionPacketSize]byte - copy(onionBlob[:], htlc.Payload) + copy(onionBlob[:], htlc.OnionBlob) updates = append(updates, &lnwire.UpdateAddHTLC{ ChanID: chanID, ID: htlc.Index, @@ -3102,6 +3106,7 @@ func (lc *LightningChannel) AddHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, error) Amount: htlc.Amount, LogIndex: lc.localUpdateLog.logIndex, HtlcIndex: lc.localUpdateLog.htlcCounter, + OnionBlob: htlc.OnionBlob[:], } lc.localUpdateLog.appendHtlc(pd) @@ -3128,6 +3133,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, err Amount: htlc.Amount, LogIndex: lc.remoteUpdateLog.logIndex, HtlcIndex: lc.remoteUpdateLog.htlcCounter, + OnionBlob: htlc.OnionBlob[:], } lc.remoteUpdateLog.appendHtlc(pd)