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 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

@ -293,66 +293,51 @@ var (
} }
) )
func TestNetworkStatus(t *testing.T) { func TestGetPeers(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
responses []responseFixture responses []responseFixture
expectedStatus *types.NetworkStatusResponse expectedPeers []*types.Peer
expectedError error expectedError error
}{ }{
"successful": { "successful": {
responses: []responseFixture{ 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, status: http.StatusOK,
body: loadFixture("get_peer_info_response.json"), body: loadFixture("get_peer_info_response.json"),
url: url, url: url,
}, },
}, },
expectedStatus: &types.NetworkStatusResponse{ expectedPeers: []*types.Peer{
CurrentBlockIdentifier: blockIdentifier1000, {
CurrentBlockTimestamp: block1000.Time * 1000, PeerID: "77.93.223.9:8333",
GenesisBlockIdentifier: MainnetGenesisBlockIdentifier, Metadata: forceMarshalMap(t, &PeerInfo{
Peers: []*types.Peer{ Addr: "77.93.223.9:8333",
{ Version: 70015,
PeerID: "77.93.223.9:8333", SubVer: "/Satoshi:0.14.2/",
Metadata: forceMarshalMap(t, &PeerInfo{ StartingHeight: 643579,
Addr: "77.93.223.9:8333", RelayTxes: true,
Version: 70015, LastSend: 1597606676,
SubVer: "/Satoshi:0.14.2/", LastRecv: 1597606677,
StartingHeight: 643579, BanScore: 0,
RelayTxes: true, SyncedHeaders: 644046,
LastSend: 1597606676, SyncedBlocks: 644046,
LastRecv: 1597606677, }),
BanScore: 0, },
SyncedHeaders: 644046, {
SyncedBlocks: 644046, PeerID: "172.105.93.179:8333",
}), Metadata: forceMarshalMap(t, &PeerInfo{
}, Addr: "172.105.93.179:8333",
{ RelayTxes: true,
PeerID: "172.105.93.179:8333", LastSend: 1597606678,
Metadata: forceMarshalMap(t, &PeerInfo{ LastRecv: 1597606676,
Addr: "172.105.93.179:8333", Version: 70015,
RelayTxes: true, SubVer: "/Satoshi:0.18.1/",
LastSend: 1597606678, StartingHeight: 643579,
LastRecv: 1597606676, BanScore: 0,
Version: 70015, SyncedHeaders: 644046,
SubVer: "/Satoshi:0.18.1/", SyncedBlocks: 644046,
StartingHeight: 643579, }),
BanScore: 0,
SyncedHeaders: 644046,
SyncedBlocks: 644046,
}),
},
}, },
}, },
}, },
@ -366,7 +351,7 @@ func TestNetworkStatus(t *testing.T) {
}, },
expectedError: errors.New("rpc in warmup"), expectedError: errors.New("rpc in warmup"),
}, },
"blockchain info error": { "peer info error": {
responses: []responseFixture{ responses: []responseFixture{
{ {
status: http.StatusInternalServerError, status: http.StatusInternalServerError,
@ -376,26 +361,6 @@ func TestNetworkStatus(t *testing.T) {
}, },
expectedError: errors.New("invalid response: 500 Internal Server Error"), 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 { for name, test := range tests {
@ -420,12 +385,12 @@ func TestNetworkStatus(t *testing.T) {
})) }))
client := NewClient(ts.URL, MainnetGenesisBlockIdentifier, MainnetCurrency) client := NewClient(ts.URL, MainnetGenesisBlockIdentifier, MainnetCurrency)
status, err := client.NetworkStatus(context.Background()) peers, err := client.GetPeers(context.Background())
if test.expectedError != nil { if test.expectedError != nil {
assert.Contains(err.Error(), test.expectedError.Error()) assert.Contains(err.Error(), test.expectedError.Error())
} else { } else {
assert.NoError(err) assert.NoError(err)
assert.Equal(test.expectedStatus, status) assert.Equal(test.expectedPeers, peers)
} }
}) })
} }