From 18e9475a9a48877d742cb05c8602a0230c3a0605 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 6 Mar 2018 13:17:09 -0500 Subject: [PATCH] lnwallet/btcwallet: grab best header timestamp directly from wallet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this commit, we modify the way we obtain the current best header timestamp. In doing this, we fix an intermittent flake that would pop up at times on the integration tests. This could occur as if the wallet was lagging behind the chain backend for a re-org, then a hash that the backend knew of, may not be known by the wallet. To remedy this, we’ll take advantage of a recent change to btcwallet to actually include the timestamp in its sync state. --- lnwallet/btcwallet/btcwallet.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lnwallet/btcwallet/btcwallet.go b/lnwallet/btcwallet/btcwallet.go index 9554f6f7..ee2674f0 100644 --- a/lnwallet/btcwallet/btcwallet.go +++ b/lnwallet/btcwallet/btcwallet.go @@ -700,23 +700,16 @@ func (b *BtcWallet) SubscribeTransactions() (lnwallet.TransactionSubscription, e // // This is a part of the WalletController interface. func (b *BtcWallet) IsSynced() (bool, int64, error) { - // Grab the best chain state the wallet is currently aware of. We'll - // also grab the timestamp of the best block to return for determining - // sync progress. + // Grab the best chain state the wallet is currently aware of. syncState := b.wallet.Manager.SyncedTo() - walletBestHeader, err := b.chain.GetBlockHeader(&syncState.Hash) - if err != nil { - return false, 0, err - } - var ( - bestHash *chainhash.Hash - bestHeight int32 - ) + // We'll also extract the current best wallet timestamp so the caller + // can get an idea of where we are in the sync timeline. + bestTimestamp := syncState.Timestamp.Unix() // Next, query the chain backend to grab the info about the tip of the // main chain. - bestHash, bestHeight, err = b.cfg.ChainSource.GetBestBlock() + bestHash, bestHeight, err := b.cfg.ChainSource.GetBestBlock() if err != nil { return false, 0, err } @@ -724,7 +717,7 @@ func (b *BtcWallet) IsSynced() (bool, int64, error) { // If the wallet hasn't yet fully synced to the node's best chain tip, // then we're not yet fully synced. if syncState.Height < bestHeight { - return false, walletBestHeader.Timestamp.Unix(), nil + return false, bestTimestamp, nil } // If the wallet is on par with the current best chain tip, then we @@ -739,5 +732,5 @@ func (b *BtcWallet) IsSynced() (bool, int64, error) { // If the timestamp no the best header is more than 2 hours in the // past, then we're not yet synced. minus24Hours := time.Now().Add(-2 * time.Hour) - return !blockHeader.Timestamp.Before(minus24Hours), walletBestHeader.Timestamp.Unix(), nil + return !blockHeader.Timestamp.Before(minus24Hours), bestTimestamp, nil }