Skip to content
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

update development branch #6

Merged
merged 3 commits into from
Jan 27, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Bug Fixes

* (evm) [\#747](https://github.com/cosmos/ethermint/issues/747) Fix format errors in String() of QueryETHLogs
* (evm) [\#742](https://github.com/cosmos/ethermint/issues/742) Add parameter check for evm query func.
* (evm) [\#687](https://github.com/cosmos/ethermint/issues/687) Fix nonce check to explicitly check for the correct nonce, rather than a simple 'greater than' comparison.
* (api) [\#687](https://github.com/cosmos/ethermint/issues/687) Returns error for a transaction with an incorrect nonce.
* (evm) [\#674](https://github.com/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
8 changes: 7 additions & 1 deletion x/evm/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ type QueryETHLogs struct {
}

func (q QueryETHLogs) String() string {
return fmt.Sprintf("%+v", q.Logs)
var logsStr string
logsLen := len(q.Logs)
for i := 0; i < logsLen; i++ {
logsStr = fmt.Sprintf("%s%v\n", logsStr, *q.Logs[i])
}

return logsStr
}

// QueryBloomFilter is response type for tx logs query
Expand Down
26 changes: 26 additions & 0 deletions x/evm/types/querier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
"strings"
"testing"
)

func TestQueryETHLogs_String(t *testing.T) {
const expectedQueryETHLogsStr = `{0x0000000000000000000000000000000000000000 [] [1 2 3 4] 9 0x0000000000000000000000000000000000000000000000000000000000000000 0 0x0000000000000000000000000000000000000000000000000000000000000000 0 false}
{0x0000000000000000000000000000000000000000 [] [5 6 7 8] 10 0x0000000000000000000000000000000000000000000000000000000000000000 0 0x0000000000000000000000000000000000000000000000000000000000000000 0 false}
`
logs := []*ethtypes.Log{
{
Data: []byte{1, 2, 3, 4},
BlockNumber: 9,
},
{
Data: []byte{5, 6, 7, 8},
BlockNumber: 10,
},
}

require.True(t, strings.EqualFold(expectedQueryETHLogsStr, QueryETHLogs{logs}.String()))
}
12 changes: 9 additions & 3 deletions x/evm/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ type ResultData struct {

// String implements fmt.Stringer interface.
func (rd ResultData) String() string {
var logsStr string
logsLen := len(rd.Logs)
for i := 0; i < logsLen; i++ {
logsStr = fmt.Sprintf("%s\t\t%v\n ", logsStr, *rd.Logs[i])
}

return strings.TrimSpace(fmt.Sprintf(`ResultData:
ContractAddress: %s
Bloom: %s
Logs: %v
Ret: %v
TxHash: %s
`, rd.ContractAddress.String(), rd.Bloom.Big().String(), rd.Logs, rd.Ret, rd.TxHash.String()))
TxHash: %s
Logs:
%s`, rd.ContractAddress.String(), rd.Bloom.Big().String(), rd.Ret, rd.TxHash.String(), logsStr))
}

// EncodeResultData takes all of the necessary data from the EVM execution
Expand Down
32 changes: 32 additions & 0 deletions x/evm/types/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -34,3 +35,34 @@ func TestEvmDataEncoding(t *testing.T) {
require.Equal(t, data.Logs, res.Logs)
require.Equal(t, ret, res.Ret)
}

func TestResultData_String(t *testing.T) {
const expectedResultDataStr = `ResultData:
ContractAddress: 0x5dE8a020088a2D6d0a23c204FFbeD02790466B49
Bloom: 259
Ret: [5 8]
TxHash: 0x0000000000000000000000000000000000000000000000000000000000000000
Logs:
{0x0000000000000000000000000000000000000000 [] [1 2 3 4] 17 0x0000000000000000000000000000000000000000000000000000000000000000 0 0x0000000000000000000000000000000000000000000000000000000000000000 0 false}
{0x0000000000000000000000000000000000000000 [] [5 6 7 8] 18 0x0000000000000000000000000000000000000000000000000000000000000000 0 0x0000000000000000000000000000000000000000000000000000000000000000 0 false}`
addr := ethcmn.HexToAddress("0x5dE8a020088a2D6d0a23c204FFbeD02790466B49")
bloom := ethtypes.BytesToBloom([]byte{0x1, 0x3})
ret := []byte{0x5, 0x8}

data := ResultData{
ContractAddress: addr,
Bloom: bloom,
Logs: []*ethtypes.Log{
{
Data: []byte{1, 2, 3, 4},
BlockNumber: 17,
},
{
Data: []byte{5, 6, 7, 8},
BlockNumber: 18,
}},
Ret: ret,
}

require.True(t, strings.EqualFold(expectedResultDataStr, data.String()))
}