rosetta-lbry/services/network_service.go

106 lines
3 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)
}
peers, err := s.client.GetPeers(ctx)
2020-09-16 21:03:14 +02:00
if err != nil {
return nil, wrapErr(ErrBitcoind, err)
}
cachedBlockResponse, err := s.i.GetBlockLazy(ctx, nil)
if err != nil {
return nil, wrapErr(ErrNotReady, nil)
}
return &types.NetworkStatusResponse{
CurrentBlockIdentifier: cachedBlockResponse.Block.BlockIdentifier,
CurrentBlockTimestamp: cachedBlockResponse.Block.Timestamp,
GenesisBlockIdentifier: s.config.GenesisBlockIdentifier,
Peers: peers,
}, nil
2020-09-16 21:03:14 +02:00
}
// 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-10-26 23:48:28 +01:00
RosettaVersion: types.RosettaAPIVersion,
2020-09-21 00:20:34 +02:00
NodeVersion: NodeVersion,
2020-11-13 20:36:31 +01:00
MiddlewareVersion: types.String(MiddlewareVersion),
2020-09-16 21:03:14 +02:00
},
Allow: &types.Allow{
OperationStatuses: bitcoin.OperationStatuses,
OperationTypes: bitcoin.OperationTypes,
Errors: Errors,
HistoricalBalanceLookup: HistoricalBalanceLookup,
2020-11-13 20:49:32 +01:00
MempoolCoins: MempoolCoins,
2020-09-16 21:03:14 +02:00
},
}, nil
}