From 31acace69266a4ad2edb38aacbdda313830a2f93 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 11 Apr 2017 21:48:38 -0700 Subject: [PATCH] lnwallet: convert PendingUpdates to FullySynced MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit improves the channel state machine by converting the objective PendingUpdates method to a subjective FullySynced method which is to be used in place of PendingUpdates. The new FullySynced method is fully encompassing and replaces any upper state required by the state machine which wraps this one. The new FullySynced method is identical to PendingUpdates aside from the fact that: it now also factors in the log message index of the remote commitment chain, and also introduces a concept of an “owed commitment”. A commitment chain owes a commitment if the height of the local commitment chain is higher than that of the remote chain. --- lnwallet/channel.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 6ffaf2ff..69a3039e 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -1725,21 +1725,24 @@ func (lc *LightningChannel) ReceiveNewCommitment(rawSig []byte) error { return nil } -// PendingUpdates returns a boolean value reflecting if there are any pending -// updates which need to be committed. The state machine has pending updates if -// the local log index on the local and remote chain tip aren't identical. This -// indicates that either we have pending updates they need to commit, or vice -// versa. -func (lc *LightningChannel) PendingUpdates() bool { +// FullSynced returns a boolean value reflecting if both commitment chains +// (remote+local) are fully in sync. Both commitment chains are fully in sync +// if the tip of each chain includes the latest committed changes from both +// sides. +func (lc *LightningChannel) FullySynced() bool { lc.RLock() defer lc.RUnlock() - // TODO(roasbeef): instead check our current counter? + oweCommitment := (lc.localCommitChain.tip().height > + lc.remoteCommitChain.tip().height) - fullySynced := (lc.localCommitChain.tip().ourMessageIndex == + localUpdatesSynced := (lc.localCommitChain.tip().ourMessageIndex == lc.remoteCommitChain.tip().ourMessageIndex) - return !fullySynced + remoteUpdatesSynced := (lc.localCommitChain.tip().theirMessageIndex == + lc.remoteCommitChain.tip().theirMessageIndex) + + return !oweCommitment && localUpdatesSynced && remoteUpdatesSynced } // RevokeCurrentCommitment revokes the next lowest unrevoked commitment