Add back NetworkStatus

This commit is contained in:
Patrick O'Grady 2020-09-28 07:59:43 -07:00
parent 7a1cea08c1
commit eee610e20a
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85
2 changed files with 62 additions and 71 deletions

View file

@ -161,6 +161,32 @@ func newHTTPClient(timeout time.Duration) *http.Client {
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
func (b *Client) GetPeers(ctx context.Context) ([]*types.Peer, error) {
info, err := b.getPeerInfo(ctx)

View file

@ -293,36 +293,22 @@ var (
}
)
func TestNetworkStatus(t *testing.T) {
func TestGetPeers(t *testing.T) {
tests := map[string]struct {
responses []responseFixture
expectedStatus *types.NetworkStatusResponse
expectedPeers []*types.Peer
expectedError error
}{
"successful": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("get_blockchain_info_response.json"),
url: url,
},
{
status: http.StatusOK,
body: loadFixture("get_block_response.json"),
url: url,
},
{
status: http.StatusOK,
body: loadFixture("get_peer_info_response.json"),
url: url,
},
},
expectedStatus: &types.NetworkStatusResponse{
CurrentBlockIdentifier: blockIdentifier1000,
CurrentBlockTimestamp: block1000.Time * 1000,
GenesisBlockIdentifier: MainnetGenesisBlockIdentifier,
Peers: []*types.Peer{
expectedPeers: []*types.Peer{
{
PeerID: "77.93.223.9:8333",
Metadata: forceMarshalMap(t, &PeerInfo{
@ -355,7 +341,6 @@ func TestNetworkStatus(t *testing.T) {
},
},
},
},
"blockchain warming up error": {
responses: []responseFixture{
{
@ -366,7 +351,7 @@ func TestNetworkStatus(t *testing.T) {
},
expectedError: errors.New("rpc in warmup"),
},
"blockchain info error": {
"peer info error": {
responses: []responseFixture{
{
status: http.StatusInternalServerError,
@ -376,26 +361,6 @@ func TestNetworkStatus(t *testing.T) {
},
expectedError: errors.New("invalid response: 500 Internal Server Error"),
},
"peer info not accessible": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("get_blockchain_info_response.json"),
url: url,
},
{
status: http.StatusOK,
body: loadFixture("get_block_response.json"),
url: url,
},
{
status: http.StatusInternalServerError,
body: "{}",
url: url,
},
},
expectedError: errors.New("invalid response: 500 Internal Server Error"),
},
}
for name, test := range tests {
@ -420,12 +385,12 @@ func TestNetworkStatus(t *testing.T) {
}))
client := NewClient(ts.URL, MainnetGenesisBlockIdentifier, MainnetCurrency)
status, err := client.NetworkStatus(context.Background())
peers, err := client.GetPeers(context.Background())
if test.expectedError != nil {
assert.Contains(err.Error(), test.expectedError.Error())
} else {
assert.NoError(err)
assert.Equal(test.expectedStatus, status)
assert.Equal(test.expectedPeers, peers)
}
})
}