github.com/Finschia/finschia-sdk@v0.48.1/server/rosetta/lib/internal/service/online.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/coinbase/rosetta-sdk-go/types"
     8  
     9  	crgerrs "github.com/Finschia/finschia-sdk/server/rosetta/lib/errors"
    10  	crgtypes "github.com/Finschia/finschia-sdk/server/rosetta/lib/types"
    11  )
    12  
    13  // genesisBlockFetchTimeout defines a timeout to fetch the genesis block
    14  const genesisBlockFetchTimeout = 15 * time.Second
    15  
    16  // NewOnlineNetwork builds a single network adapter.
    17  // It will get the Genesis block on the beginning to avoid calling it everytime.
    18  func NewOnlineNetwork(network *types.NetworkIdentifier, client crgtypes.Client) (crgtypes.API, error) {
    19  	ctx, cancel := context.WithTimeout(context.Background(), genesisBlockFetchTimeout)
    20  	defer cancel()
    21  
    22  	var genesisHeight int64 = -1 // to use initial_height in genesis.json
    23  	block, err := client.BlockByHeight(ctx, &genesisHeight)
    24  	if err != nil {
    25  		return OnlineNetwork{}, err
    26  	}
    27  
    28  	return OnlineNetwork{
    29  		client:                 client,
    30  		network:                network,
    31  		networkOptions:         networkOptionsFromClient(client, block.Block),
    32  		genesisBlockIdentifier: block.Block,
    33  	}, nil
    34  }
    35  
    36  // OnlineNetwork groups together all the components required for the full rosetta implementation
    37  type OnlineNetwork struct {
    38  	client crgtypes.Client // used to query cosmos app + tendermint
    39  
    40  	network        *types.NetworkIdentifier      // identifies the network, it's static
    41  	networkOptions *types.NetworkOptionsResponse // identifies the network options, it's static
    42  
    43  	genesisBlockIdentifier *types.BlockIdentifier // identifies genesis block, it's static
    44  }
    45  
    46  // AccountsCoins - relevant only for UTXO based chain
    47  // see https://www.rosetta-api.org/docs/AccountApi.html#accountcoins
    48  func (o OnlineNetwork) AccountCoins(_ context.Context, _ *types.AccountCoinsRequest) (*types.AccountCoinsResponse, *types.Error) {
    49  	return nil, crgerrs.ToRosetta(crgerrs.ErrOffline)
    50  }
    51  
    52  // networkOptionsFromClient builds network options given the client
    53  func networkOptionsFromClient(client crgtypes.Client, genesisBlock *types.BlockIdentifier) *types.NetworkOptionsResponse {
    54  	var tsi *int64
    55  	if genesisBlock != nil {
    56  		tsi = &(genesisBlock.Index)
    57  	}
    58  	return &types.NetworkOptionsResponse{
    59  		Version: &types.Version{
    60  			RosettaVersion: crgtypes.SpecVersion,
    61  			NodeVersion:    client.Version(),
    62  		},
    63  		Allow: &types.Allow{
    64  			OperationStatuses:       client.OperationStatuses(),
    65  			OperationTypes:          client.SupportedOperations(),
    66  			Errors:                  crgerrs.SealAndListErrors(),
    67  			HistoricalBalanceLookup: true,
    68  			TimestampStartIndex:     tsi,
    69  		},
    70  	}
    71  }