Get current block from indexer instead of bitcoind

This commit is contained in:
Patrick O'Grady 2020-09-28 07:49:37 -07:00
parent f4ca8b1318
commit b72461dbee
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85
4 changed files with 13 additions and 36 deletions

View file

@ -161,32 +161,6 @@ func newHTTPClient(timeout time.Duration) *http.Client {
return httpClient return httpClient
} }
// NetworkStatus returns the *types.NetworkStatusResponse for
// bitcoind.
func (b *Client) NetworkStatus(ctx context.Context) (*types.NetworkStatusResponse, error) {
rawBlock, err := b.getBlock(ctx, nil)
if err != nil {
return nil, fmt.Errorf("%w: unable to get current block", err)
}
currentBlock, err := b.parseBlockData(rawBlock)
if err != nil {
return nil, fmt.Errorf("%w: unable to parse current block", err)
}
peers, err := b.GetPeers(ctx)
if err != nil {
return nil, err
}
return &types.NetworkStatusResponse{
CurrentBlockIdentifier: currentBlock.BlockIdentifier,
CurrentBlockTimestamp: currentBlock.Timestamp,
GenesisBlockIdentifier: b.genesisBlockIdentifier,
Peers: peers,
}, nil
}
// GetPeers fetches the list of peer nodes // GetPeers fetches the list of peer nodes
func (b *Client) GetPeers(ctx context.Context) ([]*types.Peer, error) { func (b *Client) GetPeers(ctx context.Context) ([]*types.Peer, error) {
info, err := b.getPeerInfo(ctx) info, err := b.getPeerInfo(ctx)

View file

@ -15,16 +15,16 @@ type Client struct {
mock.Mock mock.Mock
} }
// NetworkStatus provides a mock function with given fields: _a0 // GetPeers provides a mock function with given fields: _a0
func (_m *Client) NetworkStatus(_a0 context.Context) (*types.NetworkStatusResponse, error) { func (_m *Client) GetPeers(_a0 context.Context) ([]*types.Peer, error) {
ret := _m.Called(_a0) ret := _m.Called(_a0)
var r0 *types.NetworkStatusResponse var r0 []*types.Peer
if rf, ok := ret.Get(0).(func(context.Context) *types.NetworkStatusResponse); ok { if rf, ok := ret.Get(0).(func(context.Context) []*types.Peer); ok {
r0 = rf(_a0) r0 = rf(_a0)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(*types.NetworkStatusResponse) r0 = ret.Get(0).([]*types.Peer)
} }
} }

View file

@ -65,7 +65,7 @@ func (s *NetworkAPIService) NetworkStatus(
return nil, wrapErr(ErrUnavailableOffline, nil) return nil, wrapErr(ErrUnavailableOffline, nil)
} }
rawStatus, err := s.client.NetworkStatus(ctx) peers, err := s.client.GetPeers(ctx)
if err != nil { if err != nil {
return nil, wrapErr(ErrBitcoind, err) return nil, wrapErr(ErrBitcoind, err)
} }
@ -75,9 +75,12 @@ func (s *NetworkAPIService) NetworkStatus(
return nil, wrapErr(ErrNotReady, nil) return nil, wrapErr(ErrNotReady, nil)
} }
rawStatus.CurrentBlockIdentifier = cachedBlockResponse.Block.BlockIdentifier return &types.NetworkStatusResponse{
CurrentBlockIdentifier: cachedBlockResponse.Block.BlockIdentifier,
return rawStatus, nil CurrentBlockTimestamp: cachedBlockResponse.Block.Timestamp,
GenesisBlockIdentifier: s.config.GenesisBlockIdentifier,
Peers: peers,
}, nil
} }
// NetworkOptions implements the /network/options endpoint. // NetworkOptions implements the /network/options endpoint.

View file

@ -44,7 +44,7 @@ var (
// Client is used by the servicers to get Peer information // Client is used by the servicers to get Peer information
// and to submit transactions. // and to submit transactions.
type Client interface { type Client interface {
NetworkStatus(context.Context) (*types.NetworkStatusResponse, error) GetPeers(context.Context) ([]*types.Peer, error)
SendRawTransaction(context.Context, string) (string, error) SendRawTransaction(context.Context, string) (string, error)
SuggestedFeeRate(context.Context, int64) (float64, error) SuggestedFeeRate(context.Context, int64) (float64, error)
RawMempool(context.Context) ([]string, error) RawMempool(context.Context) ([]string, error)