Merge pull request #16 from coinbase/patrick/fix-network-status

[services] Fix NetworkStatus.CurrentBlockTimestamp
This commit is contained in:
Patrick O'Grady 2020-09-28 08:23:55 -07:00 committed by GitHub
commit c8026e8013
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 18 deletions

View file

@ -431,6 +431,109 @@ func TestNetworkStatus(t *testing.T) {
} }
} }
func TestGetPeers(t *testing.T) {
tests := map[string]struct {
responses []responseFixture
expectedPeers []*types.Peer
expectedError error
}{
"successful": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("get_peer_info_response.json"),
url: url,
},
},
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,
}),
},
},
},
"blockchain warming up error": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("rpc_in_warmup_response.json"),
url: url,
},
},
expectedError: errors.New("rpc in warmup"),
},
"peer info error": {
responses: []responseFixture{
{
status: http.StatusInternalServerError,
body: "{}",
url: url,
},
},
expectedError: errors.New("invalid response: 500 Internal Server Error"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var (
assert = assert.New(t)
)
responses := make(chan responseFixture, len(test.responses))
for _, response := range test.responses {
responses <- response
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := <-responses
assert.Equal("application/json", r.Header.Get("Content-Type"))
assert.Equal("POST", r.Method)
assert.Equal(response.url, r.URL.RequestURI())
w.WriteHeader(response.status)
fmt.Fprintln(w, response.body)
}))
client := NewClient(ts.URL, MainnetGenesisBlockIdentifier, MainnetCurrency)
peers, err := client.GetPeers(context.Background())
if test.expectedError != nil {
assert.Contains(err.Error(), test.expectedError.Error())
} else {
assert.NoError(err)
assert.Equal(test.expectedPeers, peers)
}
})
}
}
func TestGetRawBlock(t *testing.T) { func TestGetRawBlock(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
blockIdentifier *types.PartialBlockIdentifier blockIdentifier *types.PartialBlockIdentifier

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

@ -27,7 +27,7 @@ import (
) )
var ( var (
middlewareVersion = "0.0.2" middlewareVersion = "0.0.3"
defaultNetworkOptions = &types.NetworkOptionsResponse{ defaultNetworkOptions = &types.NetworkOptionsResponse{
Version: &types.Version{ Version: &types.Version{
RosettaVersion: "1.4.4", RosettaVersion: "1.4.4",
@ -78,8 +78,9 @@ func TestNetworkEndpoints_Offline(t *testing.T) {
func TestNetworkEndpoints_Online(t *testing.T) { func TestNetworkEndpoints_Online(t *testing.T) {
cfg := &configuration.Configuration{ cfg := &configuration.Configuration{
Mode: configuration.Online, Mode: configuration.Online,
Network: networkIdentifier, Network: networkIdentifier,
GenesisBlockIdentifier: bitcoin.MainnetGenesisBlockIdentifier,
} }
mockIndexer := &mocks.Indexer{} mockIndexer := &mocks.Indexer{}
mockClient := &mocks.Client{} mockClient := &mocks.Client{}
@ -92,9 +93,6 @@ func TestNetworkEndpoints_Online(t *testing.T) {
networkIdentifier, networkIdentifier,
}, networkList.NetworkIdentifiers) }, networkList.NetworkIdentifiers)
rawStatus := &types.NetworkStatusResponse{
GenesisBlockIdentifier: bitcoin.MainnetGenesisBlockIdentifier,
}
blockResponse := &types.BlockResponse{ blockResponse := &types.BlockResponse{
Block: &types.Block{ Block: &types.Block{
BlockIdentifier: &types.BlockIdentifier{ BlockIdentifier: &types.BlockIdentifier{
@ -103,7 +101,11 @@ func TestNetworkEndpoints_Online(t *testing.T) {
}, },
}, },
} }
mockClient.On("NetworkStatus", ctx).Return(rawStatus, nil) mockClient.On("GetPeers", ctx).Return([]*types.Peer{
{
PeerID: "77.93.223.9:8333",
},
}, nil)
mockIndexer.On( mockIndexer.On(
"GetBlockLazy", "GetBlockLazy",
ctx, ctx,
@ -117,6 +119,11 @@ func TestNetworkEndpoints_Online(t *testing.T) {
assert.Equal(t, &types.NetworkStatusResponse{ assert.Equal(t, &types.NetworkStatusResponse{
GenesisBlockIdentifier: bitcoin.MainnetGenesisBlockIdentifier, GenesisBlockIdentifier: bitcoin.MainnetGenesisBlockIdentifier,
CurrentBlockIdentifier: blockResponse.Block.BlockIdentifier, CurrentBlockIdentifier: blockResponse.Block.BlockIdentifier,
Peers: []*types.Peer{
{
PeerID: "77.93.223.9:8333",
},
},
}, networkStatus) }, networkStatus)
networkOptions, err := servicer.NetworkOptions(ctx, nil) networkOptions, err := servicer.NetworkOptions(ctx, nil)

View file

@ -38,13 +38,13 @@ var (
// variable instead of a constant because // variable instead of a constant because
// we typically need the pointer of this // we typically need the pointer of this
// value. // value.
MiddlewareVersion = "0.0.2" MiddlewareVersion = "0.0.3"
) )
// 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)