Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

add parameter check for evm query func #746

Merged
merged 3 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

* (evm) [\#747](https:/cosmos/ethermint/issues/747) Fix format errors in String() of QueryETHLogs
* (evm) [\#742](https:/cosmos/ethermint/issues/742) Add parameter check for evm query func.
* (evm) [\#687](https:/cosmos/ethermint/issues/687) Fix nonce check to explicitly check for the correct nonce, rather than a simple 'greater than' comparison.
* (api) [\#687](https:/cosmos/ethermint/issues/687) Returns error for a transaction with an incorrect nonce.
* (evm) [\#674](https:/cosmos/ethermint/issues/674) Reset all cache after account data has been committed in `EndBlock` to make sure every node state consistent.
Expand Down
40 changes: 40 additions & 0 deletions x/evm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, _ abci.RequestQuery) ([]byte, error) {
if len(path) < 1 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 1 parameter is required")
}

switch path[0] {
case types.QueryBalance:
return queryBalance(ctx, path, keeper)
Expand All @@ -45,6 +50,11 @@ func NewQuerier(keeper Keeper) sdk.Querier {
}

func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}

addr := ethcmn.HexToAddress(path[1])
balance := keeper.GetBalance(ctx, addr)
balanceStr, err := utils.MarshalBigInt(balance)
Expand Down Expand Up @@ -73,6 +83,11 @@ func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, error) {
}

func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 3 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 3 parameters is required")
}

addr := ethcmn.HexToAddress(path[1])
key := ethcmn.HexToHash(path[2])
val := keeper.GetState(ctx, addr, key)
Expand All @@ -85,6 +100,11 @@ func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error)
}

func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}

addr := ethcmn.HexToAddress(path[1])
code := keeper.GetCode(ctx, addr)
res := types.QueryResCode{Code: code}
Expand All @@ -97,6 +117,11 @@ func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
}

func queryHashToHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}

blockHash := ethcmn.FromHex(path[1])
blockNumber, found := keeper.GetBlockHash(ctx, blockHash)
if !found {
Expand All @@ -113,6 +138,11 @@ func queryHashToHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, e
}

func queryBlockBloom(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}

num, err := strconv.ParseInt(path[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("could not unmarshal block height: %w", err)
Expand All @@ -133,6 +163,11 @@ func queryBlockBloom(ctx sdk.Context, path []string, keeper Keeper) ([]byte, err
}

func queryTransactionLogs(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}

txHash := ethcmn.HexToHash(path[1])

logs, err := keeper.GetLogs(ctx, txHash)
Expand Down Expand Up @@ -161,6 +196,11 @@ func queryLogs(ctx sdk.Context, keeper Keeper) ([]byte, error) {
}

func queryAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}

addr := ethcmn.HexToAddress(path[1])
so := keeper.GetOrNewStateObject(ctx, addr)

Expand Down