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,66 +293,51 @@ var (
}
)
func TestNetworkStatus(t *testing.T) {
func TestGetPeers(t *testing.T) {
tests := map[string]struct {
responses []responseFixture
expectedStatus *types.NetworkStatusResponse
expectedError error
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{
{
PeerID: "77.93.223.9:8333",
Metadata: forceMarshalMap(t, &PeerInfo{
Addr: "77.93.223.9:8333",
Version: 70015,
SubVer: "/Satoshi:0.14.2/",
StartingHeight: 643579,
RelayTxes: true,
LastSend: 1597606676,
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,
LastSend: 1597606678,
LastRecv: 1597606676,
Version: 70015,
SubVer: "/Satoshi:0.18.1/",
StartingHeight: 643579,
BanScore: 0,
SyncedHeaders: 644046,
SyncedBlocks: 644046,
}),
},
expectedPeers: []*types.Peer{
{
PeerID: "77.93.223.9:8333",
Metadata: forceMarshalMap(t, &PeerInfo{
Addr: "77.93.223.9:8333",
Version: 70015,
SubVer: "/Satoshi:0.14.2/",
StartingHeight: 643579,
RelayTxes: true,
LastSend: 1597606676,
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,
LastSend: 1597606678,
LastRecv: 1597606676,
Version: 70015,
SubVer: "/Satoshi:0.18.1/",
StartingHeight: 643579,
BanScore: 0,
SyncedHeaders: 644046,
SyncedBlocks: 644046,
}),
},
},
},
@ -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)
}
})
}