-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: populate oldest_block_identifier and wrap jsonrpc errors #13832
Merged
mergify
merged 10 commits into
cosmos:main
from
Zondax:misc/rosetta-genesis-and-rpcErrors
Nov 12, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
670ff10
update: split services
JulianToledano 097b004
fix: use EarliestBlockHeight as genesis
JulianToledano 71fa67e
fix: parse jsonrpc errors to rosetta
JulianToledano 2169f6c
update: check if genesis is queryable
JulianToledano 5406bc1
fix: reflect err is not nil
JulianToledano 34d510a
fix: avoid reflect import
JulianToledano d019eb5
update: try to get InitialHeightBlock
JulianToledano 18b19b2
update: changelog
JulianToledano f83a019
update: rosetta changelog
JulianToledano 3a8349f
fix: remove rosetta tag from changelog
JulianToledano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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 | ||
} | ||
|
||
// AccountsCoins - relevant only for UTXO based chain | ||
// see https://www.rosetta-api.org/docs/AccountApi.html#accountcoins | ||
func (o OnlineNetwork) AccountCoins(_ context.Context, _ *types.AccountCoinsRequest) (*types.AccountCoinsResponse, *types.Error) { | ||
return nil, errors.ToRosetta(errors.ErrOffline) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/tools/rosetta/lib/errors" | ||
crgtypes "cosmossdk.io/tools/rosetta/lib/types" | ||
"github.com/coinbase/rosetta-sdk-go/types" | ||
) | ||
|
||
// 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / gosec
Potential integer overflow by integer type conversion