Skip to content

Commit

Permalink
chain: add IsCurrent method to chain.Interface
Browse files Browse the repository at this point in the history
IsCurrent allows us to determine if the chain backend considers itself
"current" with the chain.
  • Loading branch information
wpaulino committed Jun 14, 2019
1 parent 1ee2a23 commit 39f81c6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions chain/bitcoind_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions chain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions chain/neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions chain/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions wallet/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 39f81c6

Please sign in to comment.