From 000c17b0c5cf0541feffff18b972082ab01ff959 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 16 Sep 2021 13:19:53 +0200 Subject: [PATCH] fix: rosetta server boot and add burn event type to pass check:data (#10150) (#10165) ## Description closes: #10121 closes: #10088 rosetta server fails to run with genesis.json has initial_heights 2 or larger rosetta-cli check:data fails to parse EventTypeCoinBurn --- ### 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 - [ ] 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/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] 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) (cherry picked from commit 5a72b5e02b3339eca0d4b750dffd47583a879ded) Co-authored-by: Geoff Lee --- server/rosetta/client_online.go | 25 ++++++++++++++++++- .../rosetta/lib/internal/service/offline.go | 2 +- server/rosetta/lib/internal/service/online.go | 11 +++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/rosetta/client_online.go b/server/rosetta/client_online.go index e78d9a6f045d..0a34b8b1a878 100644 --- a/server/rosetta/client_online.go +++ b/server/rosetta/client_online.go @@ -76,7 +76,9 @@ func NewClient(cfg *Config) (*Client, error) { supportedOperations = append( supportedOperations, - bank.EventTypeCoinSpent, bank.EventTypeCoinReceived, + bank.EventTypeCoinSpent, + bank.EventTypeCoinReceived, + bank.EventTypeCoinBurn, ) return &Client{ @@ -185,6 +187,10 @@ func (c *Client) BlockByHash(ctx context.Context, hash string) (crgtypes.BlockRe } func (c *Client) BlockByHeight(ctx context.Context, height *int64) (crgtypes.BlockResponse, error) { + height, err := c.getHeight(ctx, height) + if err != nil { + return crgtypes.BlockResponse{}, crgerrs.WrapError(crgerrs.ErrBadGateway, err.Error()) + } block, err := c.tmRPC.Block(ctx, height) if err != nil { return crgtypes.BlockResponse{}, crgerrs.WrapError(crgerrs.ErrBadGateway, err.Error()) @@ -204,6 +210,10 @@ func (c *Client) BlockTransactionsByHash(ctx context.Context, hash string) (crgt } func (c *Client) BlockTransactionsByHeight(ctx context.Context, height *int64) (crgtypes.BlockTransactionsResponse, error) { + height, err := c.getHeight(ctx, height) + if err != nil { + return crgtypes.BlockTransactionsResponse{}, crgerrs.WrapError(crgerrs.ErrBadGateway, err.Error()) + } blockTxResp, err := c.blockTxs(ctx, height) if err != nil { return crgtypes.BlockTransactionsResponse{}, err @@ -468,3 +478,16 @@ func (c *Client) blockTxs(ctx context.Context, height *int64) (crgtypes.BlockTra Transactions: finalTxs, }, nil } + +func (c *Client) getHeight(ctx context.Context, height *int64) (realHeight *int64, err error) { + if height != nil && *height == -1 { + genesis, err := c.tmRPC.Genesis(ctx) + if err != nil { + return nil, err + } + realHeight = &(genesis.Genesis.InitialHeight) + } else { + realHeight = height + } + return +} diff --git a/server/rosetta/lib/internal/service/offline.go b/server/rosetta/lib/internal/service/offline.go index cc420cc431eb..0d0f4eb1b4c0 100644 --- a/server/rosetta/lib/internal/service/offline.go +++ b/server/rosetta/lib/internal/service/offline.go @@ -17,7 +17,7 @@ func NewOffline(network *types.NetworkIdentifier, client crgtypes.Client) (crgty OnlineNetwork{ client: client, network: network, - networkOptions: networkOptionsFromClient(client), + networkOptions: networkOptionsFromClient(client, nil), }, }, nil } diff --git a/server/rosetta/lib/internal/service/online.go b/server/rosetta/lib/internal/service/online.go index 4853a0fcd29c..c4315417267b 100644 --- a/server/rosetta/lib/internal/service/online.go +++ b/server/rosetta/lib/internal/service/online.go @@ -19,7 +19,7 @@ func NewOnlineNetwork(network *types.NetworkIdentifier, client crgtypes.Client) ctx, cancel := context.WithTimeout(context.Background(), genesisBlockFetchTimeout) defer cancel() - var genesisHeight int64 = 1 + var genesisHeight int64 = -1 // to use initial_height in genesis.json block, err := client.BlockByHeight(ctx, &genesisHeight) if err != nil { return OnlineNetwork{}, err @@ -28,7 +28,7 @@ func NewOnlineNetwork(network *types.NetworkIdentifier, client crgtypes.Client) return OnlineNetwork{ client: client, network: network, - networkOptions: networkOptionsFromClient(client), + networkOptions: networkOptionsFromClient(client, block.Block), genesisBlockIdentifier: block.Block, }, nil } @@ -50,7 +50,11 @@ func (o OnlineNetwork) AccountCoins(_ context.Context, _ *types.AccountCoinsRequ } // networkOptionsFromClient builds network options given the client -func networkOptionsFromClient(client crgtypes.Client) *types.NetworkOptionsResponse { +func networkOptionsFromClient(client crgtypes.Client, genesisBlock *types.BlockIdentifier) *types.NetworkOptionsResponse { + var tsi *int64 = nil + if genesisBlock != nil { + tsi = &(genesisBlock.Index) + } return &types.NetworkOptionsResponse{ Version: &types.Version{ RosettaVersion: crgtypes.SpecVersion, @@ -61,6 +65,7 @@ func networkOptionsFromClient(client crgtypes.Client) *types.NetworkOptionsRespo OperationTypes: client.SupportedOperations(), Errors: crgerrs.SealAndListErrors(), HistoricalBalanceLookup: true, + TimestampStartIndex: tsi, }, } }