chain: add IsCurrent method to chain.Interface

IsCurrent allows us to determine if the chain backend considers itself
"current" with the chain.
This commit is contained in:
Wilmer Paulino 2019-05-14 13:17:58 -07:00
parent 1ee2a239de
commit 39f81c630b
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F
5 changed files with 44 additions and 0 deletions

View file

@ -174,6 +174,20 @@ func (c *BitcoindClient) GetBlockHeaderVerbose(
return c.chainConn.client.GetBlockHeaderVerbose(hash)
}
// IsCurrent returns whether the chain backend considers its view of the network
// as "current".
func (c *BitcoindClient) IsCurrent() bool {
bestHash, _, err := c.GetBestBlock()
if err != nil {
return false
}
bestHeader, err := c.GetBlockHeader(bestHash)
if err != nil {
return false
}
return bestHeader.Timestamp.After(time.Now().Add(-isCurrentDelta))
}
// GetRawTransactionVerbose returns a transaction from the tx hash.
func (c *BitcoindClient) GetRawTransactionVerbose(
hash *chainhash.Hash) (*btcjson.TxRawResult, error) {

View file

@ -10,6 +10,11 @@ import (
"github.com/btcsuite/btcwallet/wtxmgr"
)
// isCurrentDelta is the delta duration we'll use from the present time to
// determine if a backend is considered "current", i.e. synced to the tip of
// the chain.
const isCurrentDelta = 2 * time.Hour
// BackEnds returns a list of the available back ends.
// TODO: Refactor each into a driver and use dynamic registration.
func BackEnds() []string {
@ -31,6 +36,7 @@ type Interface interface {
GetBlock(*chainhash.Hash) (*wire.MsgBlock, error)
GetBlockHash(int64) (*chainhash.Hash, error)
GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error)
IsCurrent() bool
FilterBlocks(*FilterBlocksRequest) (*FilterBlocksResponse, error)
BlockStamp() (*waddrmgr.BlockStamp, error)
SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash, error)

View file

@ -157,6 +157,12 @@ func (s *NeutrinoClient) GetBlockHeader(
return s.CS.GetBlockHeader(blockHash)
}
// IsCurrent returns whether the chain backend considers its view of the network
// as "current".
func (s *NeutrinoClient) IsCurrent() bool {
return s.CS.IsCurrent()
}
// SendRawTransaction replicates the RPC client's SendRawTransaction command.
func (s *NeutrinoClient) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (
*chainhash.Hash, error) {

View file

@ -140,6 +140,20 @@ func (c *RPCClient) Stop() {
c.quitMtx.Unlock()
}
// IsCurrent returns whether the chain backend considers its view of the network
// as "current".
func (c *RPCClient) IsCurrent() bool {
bestHash, _, err := c.GetBestBlock()
if err != nil {
return false
}
bestHeader, err := c.GetBlockHeader(bestHash)
if err != nil {
return false
}
return bestHeader.Timestamp.After(time.Now().Add(-isCurrentDelta))
}
// Rescan wraps the normal Rescan command with an additional paramter that
// allows us to map an oupoint to the address in the chain that it pays to.
// This is useful when using BIP 158 filters as they include the prev pkScript

View file

@ -41,6 +41,10 @@ func (m *mockChainClient) GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader,
return nil, nil
}
func (m *mockChainClient) IsCurrent() bool {
return false
}
func (m *mockChainClient) FilterBlocks(*chain.FilterBlocksRequest) (
*chain.FilterBlocksResponse, error) {
return nil, nil