Merge pull request #26 from coinbase/patrick/mempool-offline-protection

[servicer] /mempool/* offline mode protection
This commit is contained in:
Patrick O'Grady 2020-10-16 11:12:38 -07:00 committed by GitHub
commit 7a7e8ce690
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 4 deletions

1
go.sum
View file

@ -120,6 +120,7 @@ github.com/ethereum/go-ethereum v1.9.21 h1:8qRlhzrItnmUGdVlBzZLI2Tb46S0RdSNjFwIC
github.com/ethereum/go-ethereum v1.9.21/go.mod h1:RXAVzbGrSGmDkDnHymruTAIEjUR3E4TX0EOpaj702sI=
github.com/ethereum/go-ethereum v1.9.22 h1:/Fea9n2EWJuNJ9oahMq9luqjRBcbW7QWdThbcJl13ek=
github.com/ethereum/go-ethereum v1.9.22/go.mod h1:FQjK3ZwD8C5DYn7ukTmFee36rq1dOMESiUfXr5RUc1w=
github.com/ethereum/go-ethereum v1.9.23 h1:SIKhg/z4Q7AbvqcxuPYvMxf36che/Rq/Pp0IdYEkbtw=
github.com/ethereum/go-ethereum v1.9.23/go.mod h1:JIfVb6esrqALTExdz9hRYvrP0xBDf6wCncIu1hNwHpM=
github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=

View file

@ -17,18 +17,25 @@ package services
import (
"context"
"github.com/coinbase/rosetta-bitcoin/configuration"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
)
// MempoolAPIService implements the server.MempoolAPIServicer interface.
type MempoolAPIService struct {
config *configuration.Configuration
client Client
}
// NewMempoolAPIService creates a new instance of a MempoolAPIService.
func NewMempoolAPIService(client Client) server.MempoolAPIServicer {
func NewMempoolAPIService(
config *configuration.Configuration,
client Client,
) server.MempoolAPIServicer {
return &MempoolAPIService{
config: config,
client: client,
}
}
@ -38,6 +45,10 @@ func (s *MempoolAPIService) Mempool(
ctx context.Context,
request *types.NetworkRequest,
) (*types.MempoolResponse, *types.Error) {
if s.config.Mode != configuration.Online {
return nil, wrapErr(ErrUnavailableOffline, nil)
}
mempoolTransactions, err := s.client.RawMempool(ctx)
if err != nil {
return nil, wrapErr(ErrBitcoind, err)
@ -58,5 +69,9 @@ func (s *MempoolAPIService) MempoolTransaction(
ctx context.Context,
request *types.MempoolTransactionRequest,
) (*types.MempoolTransactionResponse, *types.Error) {
if s.config.Mode != configuration.Online {
return nil, wrapErr(ErrUnavailableOffline, nil)
}
return nil, wrapErr(ErrUnimplemented, nil)
}

View file

@ -18,15 +18,39 @@ import (
"context"
"testing"
"github.com/coinbase/rosetta-bitcoin/configuration"
mocks "github.com/coinbase/rosetta-bitcoin/mocks/services"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/stretchr/testify/assert"
)
func TestMempoolEndpoints(t *testing.T) {
func TestMempoolEndpoints_Offline(t *testing.T) {
cfg := &configuration.Configuration{
Mode: configuration.Offline,
}
mockClient := &mocks.Client{}
servicer := NewMempoolAPIService(mockClient)
servicer := NewMempoolAPIService(cfg, mockClient)
ctx := context.Background()
mem, err := servicer.Mempool(ctx, nil)
assert.Nil(t, mem)
assert.Equal(t, ErrUnavailableOffline.Code, err.Code)
assert.Equal(t, ErrUnavailableOffline.Message, err.Message)
memTransaction, err := servicer.MempoolTransaction(ctx, nil)
assert.Nil(t, memTransaction)
assert.Equal(t, ErrUnavailableOffline.Code, err.Code)
assert.Equal(t, ErrUnavailableOffline.Message, err.Message)
mockClient.AssertExpectations(t)
}
func TestMempoolEndpoints_Online(t *testing.T) {
cfg := &configuration.Configuration{
Mode: configuration.Online,
}
mockClient := &mocks.Client{}
servicer := NewMempoolAPIService(cfg, mockClient)
ctx := context.Background()
mockClient.On("RawMempool", ctx).Return([]string{

View file

@ -55,7 +55,7 @@ func NewBlockchainRouter(
asserter,
)
mempoolAPIService := NewMempoolAPIService(client)
mempoolAPIService := NewMempoolAPIService(config, client)
mempoolAPIController := server.NewMempoolAPIController(
mempoolAPIService,
asserter,