Add offline protection for mempool

This commit is contained in:
Patrick O'Grady 2020-10-16 11:04:55 -07:00
parent 2fc421ab6e
commit 19184f9777
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85
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.21/go.mod h1:RXAVzbGrSGmDkDnHymruTAIEjUR3E4TX0EOpaj702sI=
github.com/ethereum/go-ethereum v1.9.22 h1:/Fea9n2EWJuNJ9oahMq9luqjRBcbW7QWdThbcJl13ek= 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.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/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.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=

View file

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

View file

@ -18,15 +18,39 @@ import (
"context" "context"
"testing" "testing"
"github.com/coinbase/rosetta-bitcoin/configuration"
mocks "github.com/coinbase/rosetta-bitcoin/mocks/services" mocks "github.com/coinbase/rosetta-bitcoin/mocks/services"
"github.com/coinbase/rosetta-sdk-go/types" "github.com/coinbase/rosetta-sdk-go/types"
"github.com/stretchr/testify/assert" "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{} 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() ctx := context.Background()
mockClient.On("RawMempool", ctx).Return([]string{ mockClient.On("RawMempool", ctx).Return([]string{

View file

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