mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
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:
parent
1ee2a239de
commit
39f81c630b
5 changed files with 44 additions and 0 deletions
|
@ -174,6 +174,20 @@ func (c *BitcoindClient) GetBlockHeaderVerbose(
|
||||||
return c.chainConn.client.GetBlockHeaderVerbose(hash)
|
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.
|
// GetRawTransactionVerbose returns a transaction from the tx hash.
|
||||||
func (c *BitcoindClient) GetRawTransactionVerbose(
|
func (c *BitcoindClient) GetRawTransactionVerbose(
|
||||||
hash *chainhash.Hash) (*btcjson.TxRawResult, error) {
|
hash *chainhash.Hash) (*btcjson.TxRawResult, error) {
|
||||||
|
|
|
@ -10,6 +10,11 @@ import (
|
||||||
"github.com/btcsuite/btcwallet/wtxmgr"
|
"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.
|
// BackEnds returns a list of the available back ends.
|
||||||
// TODO: Refactor each into a driver and use dynamic registration.
|
// TODO: Refactor each into a driver and use dynamic registration.
|
||||||
func BackEnds() []string {
|
func BackEnds() []string {
|
||||||
|
@ -31,6 +36,7 @@ type Interface interface {
|
||||||
GetBlock(*chainhash.Hash) (*wire.MsgBlock, error)
|
GetBlock(*chainhash.Hash) (*wire.MsgBlock, error)
|
||||||
GetBlockHash(int64) (*chainhash.Hash, error)
|
GetBlockHash(int64) (*chainhash.Hash, error)
|
||||||
GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error)
|
GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader, error)
|
||||||
|
IsCurrent() bool
|
||||||
FilterBlocks(*FilterBlocksRequest) (*FilterBlocksResponse, error)
|
FilterBlocks(*FilterBlocksRequest) (*FilterBlocksResponse, error)
|
||||||
BlockStamp() (*waddrmgr.BlockStamp, error)
|
BlockStamp() (*waddrmgr.BlockStamp, error)
|
||||||
SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash, error)
|
SendRawTransaction(*wire.MsgTx, bool) (*chainhash.Hash, error)
|
||||||
|
|
|
@ -157,6 +157,12 @@ func (s *NeutrinoClient) GetBlockHeader(
|
||||||
return s.CS.GetBlockHeader(blockHash)
|
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.
|
// SendRawTransaction replicates the RPC client's SendRawTransaction command.
|
||||||
func (s *NeutrinoClient) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (
|
func (s *NeutrinoClient) SendRawTransaction(tx *wire.MsgTx, allowHighFees bool) (
|
||||||
*chainhash.Hash, error) {
|
*chainhash.Hash, error) {
|
||||||
|
|
14
chain/rpc.go
14
chain/rpc.go
|
@ -140,6 +140,20 @@ func (c *RPCClient) Stop() {
|
||||||
c.quitMtx.Unlock()
|
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
|
// 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.
|
// 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
|
// This is useful when using BIP 158 filters as they include the prev pkScript
|
||||||
|
|
|
@ -41,6 +41,10 @@ func (m *mockChainClient) GetBlockHeader(*chainhash.Hash) (*wire.BlockHeader,
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *mockChainClient) IsCurrent() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mockChainClient) FilterBlocks(*chain.FilterBlocksRequest) (
|
func (m *mockChainClient) FilterBlocks(*chain.FilterBlocksRequest) (
|
||||||
*chain.FilterBlocksResponse, error) {
|
*chain.FilterBlocksResponse, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue