rosetta-lbry/services/network_service.go
Thomas Zarebczan 3ba4c37047
LBRY
LBRY Updates
2020-12-08 00:08:02 -05:00

105 lines
3 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/lbryio/rosetta-lbry/configuration"
"github.com/lbryio/rosetta-lbry/lbry"
"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(ErrLbrycrdd, 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: types.RosettaAPIVersion,
NodeVersion: NodeVersion,
MiddlewareVersion: types.String(MiddlewareVersion),
},
Allow: &types.Allow{
OperationStatuses: lbry.OperationStatuses,
OperationTypes: lbry.OperationTypes,
Errors: Errors,
HistoricalBalanceLookup: HistoricalBalanceLookup,
MempoolCoins: MempoolCoins,
},
}, nil
}