rosetta-lbry/services/network_service.go

101 lines
2.7 KiB
Go
Raw Normal View History

2020-09-16 21:03:14 +02:00
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package services
import (
"context"
"github.com/coinbase/rosetta-bitcoin/bitcoin"
"github.com/coinbase/rosetta-bitcoin/configuration"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
)
// NetworkAPIService implements the server.NetworkAPIServicer interface.
type NetworkAPIService struct {
config *configuration.Configuration
client Client
i Indexer
}
// NewNetworkAPIService creates a new instance of a NetworkAPIService.
func NewNetworkAPIService(
config *configuration.Configuration,
client Client,
i Indexer,
) server.NetworkAPIServicer {
return &NetworkAPIService{
config: config,
client: client,
i: i,
}
}
// NetworkList implements the /network/list endpoint
func (s *NetworkAPIService) NetworkList(
ctx context.Context,
request *types.MetadataRequest,
) (*types.NetworkListResponse, *types.Error) {
return &types.NetworkListResponse{
NetworkIdentifiers: []*types.NetworkIdentifier{
s.config.Network,
},
}, nil
}
// NetworkStatus implements the /network/status endpoint.
func (s *NetworkAPIService) NetworkStatus(
ctx context.Context,
request *types.NetworkRequest,
) (*types.NetworkStatusResponse, *types.Error) {
if s.config.Mode != configuration.Online {
return nil, wrapErr(ErrUnavailableOffline, nil)
}
rawStatus, err := s.client.NetworkStatus(ctx)
if err != nil {
return nil, wrapErr(ErrBitcoind, err)
}
cachedBlockResponse, err := s.i.GetBlockLazy(ctx, nil)
if err != nil {
return nil, wrapErr(ErrNotReady, nil)
}
rawStatus.CurrentBlockIdentifier = cachedBlockResponse.Block.BlockIdentifier
return rawStatus, nil
}
// NetworkOptions implements the /network/options endpoint.
func (s *NetworkAPIService) NetworkOptions(
ctx context.Context,
request *types.NetworkRequest,
) (*types.NetworkOptionsResponse, *types.Error) {
return &types.NetworkOptionsResponse{
Version: &types.Version{
2020-09-21 00:20:34 +02:00
RosettaVersion: RosettaVersion,
NodeVersion: NodeVersion,
MiddlewareVersion: &MiddlewareVersion,
2020-09-16 21:03:14 +02:00
},
Allow: &types.Allow{
OperationStatuses: bitcoin.OperationStatuses,
OperationTypes: bitcoin.OperationTypes,
Errors: Errors,
},
}, nil
}