### Description Closes: https://github.com/cosmos/cosmos-sdk/issues/13083 https://github.com/cosmos/cosmos-sdk/issues/11402 https://github.com/cosmos/cosmos-sdk/issues/10678 https://github.com/cosmos/cosmos-sdk/issues/12358 https://github.com/cosmos/cosmos-sdk/issues/10776 https://github.com/cosmos/cosmos-sdk/issues/12934 ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
183 lines
5.9 KiB
Go
183 lines
5.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"cosmossdk.io/tools/rosetta/lib/errors"
|
|
crgtypes "cosmossdk.io/tools/rosetta/lib/types"
|
|
"github.com/coinbase/rosetta-sdk-go/types"
|
|
)
|
|
|
|
// AccountBalance retrieves the account balance of an address
|
|
// rosetta requires us to fetch the block information too
|
|
func (on OnlineNetwork) AccountBalance(ctx context.Context, request *types.AccountBalanceRequest) (*types.AccountBalanceResponse, *types.Error) {
|
|
var (
|
|
height int64
|
|
block crgtypes.BlockResponse
|
|
err error
|
|
)
|
|
|
|
switch {
|
|
case request.BlockIdentifier == nil:
|
|
syncStatus, err := on.client.Status(ctx)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
block, err = on.client.BlockByHeight(ctx, syncStatus.CurrentIndex)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
case request.BlockIdentifier.Hash != nil:
|
|
block, err = on.client.BlockByHash(ctx, *request.BlockIdentifier.Hash)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
height = block.Block.Index
|
|
case request.BlockIdentifier.Index != nil:
|
|
height = *request.BlockIdentifier.Index
|
|
block, err = on.client.BlockByHeight(ctx, &height)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
}
|
|
|
|
accountCoins, err := on.client.Balances(ctx, request.AccountIdentifier.Address, &height)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
return &types.AccountBalanceResponse{
|
|
BlockIdentifier: block.Block,
|
|
Balances: accountCoins,
|
|
Metadata: nil,
|
|
}, nil
|
|
}
|
|
|
|
// Block gets the transactions in the given block
|
|
func (on OnlineNetwork) Block(ctx context.Context, request *types.BlockRequest) (*types.BlockResponse, *types.Error) {
|
|
var (
|
|
blockResponse crgtypes.BlockTransactionsResponse
|
|
err error
|
|
)
|
|
|
|
// When fetching data by BlockIdentifier, it may be possible to only specify the index or hash.
|
|
// If neither property is specified, it is assumed that the client is making a request at the current block.
|
|
switch {
|
|
case request.BlockIdentifier == nil: // unlike AccountBalance(), BlockIdentifer is mandatory by spec 1.4.10.
|
|
err := errors.WrapError(errors.ErrBadArgument, "block identifier needs to be specified")
|
|
return nil, errors.ToRosetta(err)
|
|
|
|
case request.BlockIdentifier.Hash != nil:
|
|
blockResponse, err = on.client.BlockTransactionsByHash(ctx, *request.BlockIdentifier.Hash)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
case request.BlockIdentifier.Index != nil:
|
|
blockResponse, err = on.client.BlockTransactionsByHeight(ctx, request.BlockIdentifier.Index)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
default:
|
|
// both empty
|
|
blockResponse, err = on.client.BlockTransactionsByHeight(ctx, nil)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
}
|
|
|
|
// Both of index and hash can be specified in reuqest, so make sure they are not mismatching.
|
|
if request.BlockIdentifier.Index != nil && *request.BlockIdentifier.Index != blockResponse.Block.Index {
|
|
err := errors.WrapError(errors.ErrBadArgument, "mismatching index")
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
if request.BlockIdentifier.Hash != nil && *request.BlockIdentifier.Hash != blockResponse.Block.Hash {
|
|
err := errors.WrapError(errors.ErrBadArgument, "mismatching hash")
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
return &types.BlockResponse{
|
|
Block: &types.Block{
|
|
BlockIdentifier: blockResponse.Block,
|
|
ParentBlockIdentifier: blockResponse.ParentBlock,
|
|
Timestamp: blockResponse.MillisecondTimestamp,
|
|
Transactions: blockResponse.Transactions,
|
|
Metadata: nil,
|
|
},
|
|
OtherTransactions: nil,
|
|
}, nil
|
|
}
|
|
|
|
// BlockTransaction gets the given transaction in the specified block, we do not need to check the block itself too
|
|
// due to the fact that tendermint achieves instant finality
|
|
func (on OnlineNetwork) BlockTransaction(ctx context.Context, request *types.BlockTransactionRequest) (*types.BlockTransactionResponse, *types.Error) {
|
|
tx, err := on.client.GetTx(ctx, request.TransactionIdentifier.Hash)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
return &types.BlockTransactionResponse{
|
|
Transaction: tx,
|
|
}, nil
|
|
}
|
|
|
|
// Mempool fetches the transactions contained in the mempool
|
|
func (on OnlineNetwork) Mempool(ctx context.Context, _ *types.NetworkRequest) (*types.MempoolResponse, *types.Error) {
|
|
txs, err := on.client.Mempool(ctx)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
return &types.MempoolResponse{
|
|
TransactionIdentifiers: txs,
|
|
}, nil
|
|
}
|
|
|
|
// MempoolTransaction fetches a single transaction in the mempool
|
|
// NOTE: it is not implemented yet
|
|
func (on OnlineNetwork) MempoolTransaction(ctx context.Context, request *types.MempoolTransactionRequest) (*types.MempoolTransactionResponse, *types.Error) {
|
|
tx, err := on.client.GetUnconfirmedTx(ctx, request.TransactionIdentifier.Hash)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
return &types.MempoolTransactionResponse{
|
|
Transaction: tx,
|
|
}, nil
|
|
}
|
|
|
|
func (on OnlineNetwork) NetworkList(_ context.Context, _ *types.MetadataRequest) (*types.NetworkListResponse, *types.Error) {
|
|
return &types.NetworkListResponse{NetworkIdentifiers: []*types.NetworkIdentifier{on.network}}, nil
|
|
}
|
|
|
|
func (on OnlineNetwork) NetworkOptions(_ context.Context, _ *types.NetworkRequest) (*types.NetworkOptionsResponse, *types.Error) {
|
|
return on.networkOptions, nil
|
|
}
|
|
|
|
func (on OnlineNetwork) NetworkStatus(ctx context.Context, _ *types.NetworkRequest) (*types.NetworkStatusResponse, *types.Error) {
|
|
syncStatus, err := on.client.Status(ctx)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
block, err := on.client.BlockByHeight(ctx, syncStatus.CurrentIndex)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
peers, err := on.client.Peers(ctx)
|
|
if err != nil {
|
|
return nil, errors.ToRosetta(err)
|
|
}
|
|
|
|
return &types.NetworkStatusResponse{
|
|
CurrentBlockIdentifier: block.Block,
|
|
CurrentBlockTimestamp: block.MillisecondTimestamp,
|
|
GenesisBlockIdentifier: on.genesisBlockIdentifier,
|
|
OldestBlockIdentifier: nil,
|
|
SyncStatus: syncStatus,
|
|
Peers: peers,
|
|
}, nil
|
|
}
|