From 9c7163187b4b32c87e8f12621076bc21cc0ef60f Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Wed, 16 May 2018 14:39:22 +0200 Subject: [PATCH] lnwallet/channel: don't restore htlcs uneccessary This commit removes redundant HTLC restoring. We don't have to restore outgoing HTLCs from the local commitment, as we _know_ they will always be added to the remote commitment first. Also, when receiving Settles/Fails, they will be removed from the local commitment first. This way we can be sure that outgoing HTLCs found on the local commitment always will be found on the remote commitment Similarly we don't have to restore incoming HTLCs from the remote commitment, as they will be added to the local commitment first. --- lnwallet/channel.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index f041d472..c98a5356 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1692,27 +1692,17 @@ func (lc *LightningChannel) restoreStateLogs( pendingRemoteCommitDiff *channeldb.CommitDiff, pendingRemoteKeys *CommitmentKeyRing) error { - // For each HTLC within the local commitment, we add it to the relevant - // update logc based on if it's incoming vs outgoing. For any incoming - // HTLC's, we also re-add it to the rHashMap so we can quickly look it - // up. + // For each incoming HTLC within the local commitment, we add it to the + // remote update log. Since HTLCs are added first to the receiver's + // commitment, we don't have to restore outgoing HTLCs, as they will be + // restored from the remote commitment below. for i := range localCommitment.incomingHTLCs { htlc := localCommitment.incomingHTLCs[i] lc.remoteUpdateLog.restoreHtlc(&htlc) } - for i := range localCommitment.outgoingHTLCs { - htlc := localCommitment.outgoingHTLCs[i] - lc.localUpdateLog.restoreHtlc(&htlc) - } - // We'll also do the same for the HTLC"s within the remote commitment - // party. We also insert these HTLC's as it's possible our state has - // diverged slightly in the case of a congruent update from both sides. - // The restoreHtlc method will de-dup the HTLC's to handle this case. - for i := range remoteCommitment.incomingHTLCs { - htlc := remoteCommitment.incomingHTLCs[i] - lc.remoteUpdateLog.restoreHtlc(&htlc) - } + // Similarly, we'll do the same for the outgoing HTLCs within the + // remote commitment, adding them to the local update log. for i := range remoteCommitment.outgoingHTLCs { htlc := remoteCommitment.outgoingHTLCs[i] lc.localUpdateLog.restoreHtlc(&htlc)