lnwallet: add new method IsSynced to the WalletController interface

This commit adds a new method to the WalletController interface:
IsSynced. The role of the function is to query the local wallet about
if it thinks it has fully synced to the tip of the current main chain.
This function can be useful within U.I’s to block off certain
functionality until the wallet is fully synced to the main chain.
This commit is contained in:
Olaoluwa Osuntokun 2016-12-08 18:29:55 -08:00
parent 1e3635b5aa
commit 85defc3ec3
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 43 additions and 2 deletions

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"sync" "sync"
"time"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/roasbeef/btcd/btcec" "github.com/roasbeef/btcd/btcec"
@ -610,3 +611,39 @@ func (b *BtcWallet) SubscribeTransactions() (lnwallet.TransactionSubscription, e
return txClient, nil return txClient, nil
} }
// IsSynced returns a boolean indicating if from the PoV of the wallet,
// it has fully synced to the current best block in the main chain.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) IsSynced() (bool, error) {
// Grab the best chain state the wallet is currently aware of.
syncState := b.wallet.Manager.SyncedTo()
// Next, query the btcd node to grab the info about the tip of the main
// chain.
bestHash, bestHeight, err := b.rpc.GetBestBlock()
if err != nil {
return false, err
}
// 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, nil
}
// If the wallet is on par with the current best chain tip, then we
// still may not yet be synced as the btcd node may still be catching
// up to the main chain. So we'll grab the block header in order to
// make a guess based on the current time stamp.
blockHeader, err := b.rpc.GetBlockHeader(bestHash)
if err != nil {
return false, err
}
// 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), nil
}

@ -174,13 +174,17 @@ type WalletController interface {
// related to the wallet are seen within the network, or found in // related to the wallet are seen within the network, or found in
// blocks. // blocks.
// //
// NOTE: a non-nil error shuold be returned if notifications aren't // NOTE: a non-nil error should be returned if notifications aren't
// supported. // supported.
// //
// TODO(roasbeef): make distinct interface? // TODO(roasbeef): make distinct interface?
SubscribeTransactions() (TransactionSubscription, error) SubscribeTransactions() (TransactionSubscription, error)
// Start initializes the wallet, making any neccessary connections, // IsSynced returns a boolean indicating if from the PoV of the wallet,
// it has fully synced to the current best block in the main chain.
IsSynced() (bool, error)
// Start initializes the wallet, making any necessary connections,
// starting up required goroutines etc. // starting up required goroutines etc.
Start() error Start() error