From a9416300e55fa2adc28c92e20d748f73a034f289 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 20 Feb 2017 16:39:04 -0800 Subject: [PATCH] lnwallet: ensure that total satoshi sent/received aren't counted twice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug in the LightingChannel commitment state machine which could occasionally result in the total number of satoshis sent or received being counted twice if a redundant state transition were initiated. To fix this bug, we now only increment the number of satoshi sent/recv’d iff it’s the first time the HTLC has been processed. --- lnwallet/channel.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 206d52de0..53c424771 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1037,7 +1037,12 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance, if entry.EntryType == Add { continue } - if entry.EntryType == Settle && !remoteChain { + + // If we're settling in inbound HTLC, and it hasn't been + // processed, yet, the increment our state tracking the total + // number of satoshis we've received within the channel. + if entry.EntryType == Settle && !remoteChain && + entry.removeCommitHeightLocal == 0 { lc.channelState.TotalSatoshisReceived += uint64(entry.Amount) } @@ -1051,7 +1056,13 @@ func (lc *LightningChannel) evaluateHTLCView(view *htlcView, ourBalance, if entry.EntryType == Add { continue } - if entry.EntryType == Settle && !remoteChain { + + // If the remote party is settling one of our outbound HTLC's, + // and it hasn't been processed, yet, the increment our state + // tracking the total number of satoshis we've sent within the + // channel. + if entry.EntryType == Settle && !remoteChain && + entry.removeCommitHeightLocal == 0 { lc.channelState.TotalSatoshisSent += uint64(entry.Amount) }