From 9676d476c9bf9a9ae3c5e7c5b5bc4abbf87c59be Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 8 Jun 2017 22:24:10 -0700 Subject: [PATCH] lnwallet: fix race condition in channel state machine, use single mutex This commit fixes a race condition that was discovered as a result of the new htlcswitch package. The StateSnapshot method and all of the other methods which mutate the state of the channel state machine were using distinct mutexes. The fix is trivial: all methods accessing the internal channel state variable now use the same mutex. --- lnwallet/channel.go | 6 ++---- lnwallet/wallet.go | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 57d25179..b3ef0dd9 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -622,8 +622,6 @@ type LightningChannel struct { // able to broadcast safely. localCommitChain *commitmentChain - // stateMtx protects concurrent access to the state struct. - stateMtx sync.RWMutex channelState *channeldb.OpenChannel // [local|remote]Log is a (mostly) append-only log storing all the HTLC @@ -2738,8 +2736,8 @@ func (lc *LightningChannel) DeleteState(c *channeldb.ChannelCloseSummary) error // StateSnapshot returns a snapshot of the current fully committed state within // the channel. func (lc *LightningChannel) StateSnapshot() *channeldb.ChannelSnapshot { - lc.stateMtx.RLock() - defer lc.stateMtx.RUnlock() + lc.RLock() + defer lc.RUnlock() return lc.channelState.Snapshot() } diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index 837c8b59..8de8204a 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -1453,7 +1453,7 @@ func coinSelect(feeRate uint64, amt btcutil.Amount, estimatedSize = ((len(selectedUtxos) * p2wkhSpendSize) + p2wshOutputSize + txOverhead) - // The difference bteween the selected amount and the amount + // The difference between the selected amount and the amount // requested will be used to pay fees, and generate a change // output with the remaining. overShootAmt := totalSat - amtNeeded @@ -1468,7 +1468,8 @@ func coinSelect(feeRate uint64, amt btcutil.Amount, continue } - // If the fee is sufficient, then calculate the size of the change output. + // If the fee is sufficient, then calculate the size of the + // change output. changeAmt := overShootAmt - requiredFee return selectedUtxos, changeAmt, nil