103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
// 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)
|
|
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
|
|
}
|
|
|
|
// 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{
|
|
RosettaVersion: RosettaVersion,
|
|
NodeVersion: NodeVersion,
|
|
MiddlewareVersion: &MiddlewareVersion,
|
|
},
|
|
Allow: &types.Allow{
|
|
OperationStatuses: bitcoin.OperationStatuses,
|
|
OperationTypes: bitcoin.OperationTypes,
|
|
Errors: Errors,
|
|
},
|
|
}, nil
|
|
}
|