Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
rpc: test fix (#608)
Browse files Browse the repository at this point in the history
* fix rpc tests with net namespace

* skip personal test

* skip rpc pending test

* fix endpoint

* fix rpc pending test

* fix missing gas param in some rpc tests

* fix eth_getproof when the block number is equal to pending or latest

* fix rpc tests filter subscribe failed

* lint

* remove unused linter

* fix PendingTransactionFilter and TestEth_GetFilterChanges_BlockFilter

* fix eth_estimateGas

* fix TestEth_EstimateGas_ContractDeployment

* skip TestEth_ExportAccount_WithStorage

* remove sleep in rpc test

* Update changelog

* add test-rpc in github action

* bump golangci-lint version to v1.42.1
  • Loading branch information
JayT106 authored and fedekunze committed Oct 8, 2021
1 parent 63a5878 commit 094a688
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: golangci/golangci-lint-action@v2.5.2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.29
version: v1.42.1
args: --timeout 10m
github-token: ${{ secrets.github_token }}
# Check only if there are differences in the source code
Expand Down
21 changes: 20 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,23 @@ jobs:
sleep 2m
./contrib/scripts/test_localnet_liveness.sh 100 5 50 localhost
if: env.GIT_DIFF


test-rpc:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/setup-go@v2.1.4
with:
go-version: 1.17
- uses: actions/checkout@v2.3.4
- uses: technote-space/get-diff-action@v5
with:
PATTERNS: |
**/**.sol
**/**.go
go.mod
go.sum
- name: Test rpc endpoint
run: |
make test-rpc
if: env.GIT_DIFF
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (rpc, evm) [tharsis#614](https://github.com/tharsis/ethermint/issues/614) Use JSON for (un)marshaling tx `Log`s from events.
* (rpc) [tharsis#611](https://github.com/tharsis/ethermint/pull/611) Fix panic on JSON-RPC when querying for an invalid block height.
* (cmd) [tharsis#483](https://github.com/tharsis/ethermint/pull/483) Use config values on genesis accounts.
* (rpc, test) [tharsis#608](https://github.com/tharsis/ethermint/pull/608) Fix rpc test.

## [v0.6.0] - 2021-09-29

Expand Down
20 changes: 18 additions & 2 deletions rpc/ethereum/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"math/big"
"strings"

Expand Down Expand Up @@ -690,7 +691,7 @@ func (e *PublicAPI) GetTransactionReceipt(hash common.Hash) (map[string]interfac

// PendingTransactions returns the transactions that are in the transaction pool
// and have a from address that is one of the accounts this node manages.
func (e *PublicAPI) PendingTransactions() ([]*rpctypes.RPCTransaction, error) {
func (e *PublicAPI) GetPendingTransactions() ([]*rpctypes.RPCTransaction, error) {
e.logger.Debug("eth_getPendingTransactions")

txs, err := e.backend.PendingTransactions()
Expand Down Expand Up @@ -741,9 +742,24 @@ func (e *PublicAPI) GetProof(address common.Address, storageKeys []string, block
if err != nil {
return nil, err
}
height := blockNum.Int64()

height := blockNum.Int64()
ctx := rpctypes.ContextWithHeight(height)

// if the height is equal to zero, meaning the query condition of the block is either "pending" or "latest"
if height == 0 {
bn, err := e.backend.BlockNumber()
if err != nil {
return nil, err
}

if bn > math.MaxInt64 {
return nil, fmt.Errorf("not able to query block number greater than MaxInt64")
}

height = int64(bn)
}

clientCtx := e.clientCtx.WithHeight(height)

// query storage proofs
Expand Down
5 changes: 4 additions & 1 deletion rpc/ethereum/namespaces/eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (es *EventSystem) subscribe(sub *Subscription) (*Subscription, context.Canc
err = es.tmWSClient.Subscribe(es.ctx, sub.event)
case filters.BlocksSubscription:
err = es.tmWSClient.Subscribe(es.ctx, sub.event)
case filters.PendingTransactionsSubscription:
err = es.tmWSClient.Subscribe(es.ctx, sub.event)
default:
err = fmt.Errorf("invalid filter subscription type %d", sub.typ)
}
Expand Down Expand Up @@ -160,7 +162,8 @@ func (es *EventSystem) SubscribeLogs(crit filters.FilterCriteria) (*Subscription
// only interested in new mined logs, mined logs within a specific block range, or
// logs from a specific block number to new mined blocks
case (from == rpc.LatestBlockNumber && to == rpc.LatestBlockNumber),
(from >= 0 && to >= 0 && to >= from):
(from >= 0 && to >= 0 && to >= from),
(from >= 0 && to == rpc.LatestBlockNumber):
return es.subscribeLogs(crit)

default:
Expand Down
2 changes: 1 addition & 1 deletion rpc/ethereum/namespaces/eth/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (f *Filter) blockLogs(header *ethtypes.Header) ([]*ethtypes.Log, error) {
return []*ethtypes.Log{}, errors.Wrapf(err, "failed to fetch logs block number %d", header.Number.Int64())
}

var unfiltered []*ethtypes.Log // nolint: prealloc
var unfiltered []*ethtypes.Log
for _, logs := range logsList {
unfiltered = append(unfiltered, logs...)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNet_Version(t *testing.T) {
var res string
err := json.Unmarshal(rpcRes.Result, &res)
require.NoError(t, err)
require.Equal(t, "2", res)
require.Equal(t, "9000", res)
}

func TestNet_Listening(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions tests/rpc/personal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

func TestPersonal_ListAccounts(t *testing.T) {
t.Skip("skipping TestPersonal_ListAccounts")

rpcRes := Call(t, "personal_listAccounts", []string{})

var res []hexutil.Bytes
Expand All @@ -21,6 +23,8 @@ func TestPersonal_ListAccounts(t *testing.T) {
}

func TestPersonal_NewAccount(t *testing.T) {
t.Skip("skipping TestPersonal_NewAccount")

rpcRes := Call(t, "personal_newAccount", []string{"password"})
var addr common.Address
err := json.Unmarshal(rpcRes.Result, &addr)
Expand All @@ -34,6 +38,8 @@ func TestPersonal_NewAccount(t *testing.T) {
}

func TestPersonal_Sign(t *testing.T) {
t.Skip("skipping TestPersonal_Sign")

rpcRes := Call(t, "personal_unlockAccount", []interface{}{hexutil.Bytes(from), ""})
require.Nil(t, rpcRes.Error)

Expand All @@ -47,6 +53,8 @@ func TestPersonal_Sign(t *testing.T) {
}

func TestPersonal_ImportRawKey(t *testing.T) {
t.Skip("skipping TestPersonal_ImportRawKey")

privkey, err := crypto.GenerateKey()
require.NoError(t, err)

Expand All @@ -65,6 +73,8 @@ func TestPersonal_ImportRawKey(t *testing.T) {
}

func TestPersonal_EcRecover(t *testing.T) {
t.Skip("skipping TestPersonal_EcRecover")

data := hexutil.Bytes{0x88}
rpcRes := Call(t, "personal_sign", []interface{}{data, hexutil.Bytes(from), ""})

Expand All @@ -81,6 +91,8 @@ func TestPersonal_EcRecover(t *testing.T) {
}

func TestPersonal_UnlockAccount(t *testing.T) {
t.Skip("skipping TestPersonal_UnlockAccount")

pswd := "nootwashere"
rpcRes := Call(t, "personal_newAccount", []string{pswd})
var addr common.Address
Expand All @@ -106,6 +118,8 @@ func TestPersonal_UnlockAccount(t *testing.T) {
}

func TestPersonal_LockAccount(t *testing.T) {
t.Skip("skipping TestPersonal_LockAccount")

pswd := "nootwashere"
rpcRes := Call(t, "personal_newAccount", []string{pswd})
var addr common.Address
Expand Down
88 changes: 47 additions & 41 deletions tests/rpc/rpc_pending_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import (
// }

func TestEth_Pending_GetBalance(t *testing.T) {
// There is no pending block concept in Ethermint
t.Skip("skipping TestEth_Pending_GetBalance")

var res hexutil.Big
var resTxHash common.Hash
rpcRes := Call(t, "eth_getBalance", []string{addrA, "latest"})
Expand Down Expand Up @@ -97,19 +100,18 @@ func TestEth_Pending_GetTransactionCount(t *testing.T) {
t.Logf("Current nonce is %d", currentNonce)
require.Equal(t, prePendingNonce, currentNonce)

param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"

txRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.Nil(t, txRes.Error)
txRes = Call(t, "eth_sendTransaction", param)
param := makePendingTxParams()
txRes := Call(t, "eth_sendTransaction", param)
require.Nil(t, txRes.Error)

var hash hexutil.Bytes
err := json.Unmarshal(txRes.Result, &hash)
require.NoError(t, err)

receipt := waitForReceipt(t, hash)
require.NotNil(t, receipt)
require.Equal(t, "0x1", receipt["status"].(string))

pendingNonce := GetNonce(t, "pending")
latestNonce := GetNonce(t, "latest")

Expand All @@ -123,6 +125,9 @@ func TestEth_Pending_GetTransactionCount(t *testing.T) {
}

func TestEth_Pending_GetBlockTransactionCountByNumber(t *testing.T) {
// There is no pending block concept in Ethermint
t.Skip("skipping TestEth_Pending_GetBlockTransactionCountByNumber")

rpcRes := Call(t, "eth_getBlockTransactionCountByNumber", []interface{}{"pending"})
var preTxPendingTxCount hexutil.Uint
err := json.Unmarshal(rpcRes.Result, &preTxPendingTxCount)
Expand All @@ -144,7 +149,6 @@ func TestEth_Pending_GetBlockTransactionCountByNumber(t *testing.T) {
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"

txRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.Nil(t, txRes.Error)

Expand All @@ -170,6 +174,9 @@ func TestEth_Pending_GetBlockTransactionCountByNumber(t *testing.T) {
}

func TestEth_Pending_GetBlockByNumber(t *testing.T) {
// There is no pending block concept in Ethermint
t.Skip("skipping TestEth_Pending_GetBlockByNumber")

rpcRes := Call(t, "eth_getBlockByNumber", []interface{}{"latest", true})
var preTxLatestBlock map[string]interface{}
err := json.Unmarshal(rpcRes.Result, &preTxLatestBlock)
Expand Down Expand Up @@ -213,6 +220,9 @@ func TestEth_Pending_GetBlockByNumber(t *testing.T) {
}

func TestEth_Pending_GetTransactionByBlockNumberAndIndex(t *testing.T) {
// There is no pending block concept in Ethermint
t.Skip("skipping TestEth_Pending_GetTransactionByBlockNumberAndIndex")

var pendingTx []*rpctypes.RPCTransaction
resPendingTxs := Call(t, "eth_pendingTransactions", []string{})
err := json.Unmarshal(resPendingTxs.Result, &pendingTx)
Expand Down Expand Up @@ -261,30 +271,24 @@ func TestEth_Pending_GetTransactionByBlockNumberAndIndex(t *testing.T) {
func TestEth_Pending_GetTransactionByHash(t *testing.T) {
// negative case, check that it returns empty.
rpcRes := Call(t, "eth_getTransactionByHash", []interface{}{"0xec5fa15e1368d6ac314f9f64118c5794f076f63c02e66f97ea5fe1de761a8973"})
require.Nil(t, rpcRes.Result)
var tx map[string]interface{}
err := json.Unmarshal(rpcRes.Result, &tx)
require.NoError(t, err)
require.Nil(t, tx)

// create a transaction.
data := "0x608060405234801561001057600080fd5b5061011e806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806302eb691b14602d575b600080fd5b603360ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101560715780820151818401526020810190506058565b50505050905090810190601f168015609d5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60606040518060400160405280600d81526020017f617261736b61776173686572650000000000000000000000000000000000000081525090509056fea264697066735822122060917c5c2fab8c058a17afa6d3c1d23a7883b918ea3c7157131ea5b396e1aa7564736f6c63430007050033"
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
param := makePendingTxParams()
param[0]["data"] = data

txRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.Nil(t, txRes.Error)

txRes = Call(t, "eth_sendTransaction", param)
txRes := Call(t, "eth_sendTransaction", param)
var txHash common.Hash
err := txHash.UnmarshalJSON(txRes.Result)
err = txHash.UnmarshalJSON(txRes.Result)
require.NoError(t, err)

rpcRes = Call(t, "eth_getTransactionByHash", []interface{}{txHash})
var pendingBlockTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingBlockTx)
var pendingTx map[string]interface{}
err = json.Unmarshal(rpcRes.Result, &pendingTx)
require.NoError(t, err)

txsRes := Call(t, "eth_getPendingTransactions", []interface{}{})
Expand All @@ -294,24 +298,15 @@ func TestEth_Pending_GetTransactionByHash(t *testing.T) {
require.NotEmpty(t, pendingTxs)

// verify the pending tx has all the correct fields from the tx sent.
require.NotEmpty(t, pendingBlockTx)
require.NotEmpty(t, pendingBlockTx["hash"])
require.Equal(t, pendingBlockTx["value"], "0xa")
require.Equal(t, pendingBlockTx["input"], data)
require.NotEmpty(t, pendingTx)
require.NotEmpty(t, pendingTx["hash"])
require.Equal(t, pendingTx["value"], "0xa")
require.Equal(t, pendingTx["input"], data)
}

func TestEth_Pending_SendTransaction_PendingNonce(t *testing.T) {
currNonce := GetNonce(t, "latest")
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"

txRes := Call(t, "personal_unlockAccount", []interface{}{param[0]["from"], ""})
require.Nil(t, txRes.Error)
param := makePendingTxParams()

// first transaction
txRes1 := Call(t, "eth_sendTransaction", param)
Expand All @@ -335,3 +330,14 @@ func TestEth_Pending_SendTransaction_PendingNonce(t *testing.T) {
require.Greater(t, uint64(pendingNonce3), uint64(currNonce))
require.Greater(t, uint64(pendingNonce3), uint64(pendingNonce2))
}

func makePendingTxParams() []map[string]string {
param := make([]map[string]string, 1)
param[0] = make(map[string]string)
param[0]["from"] = "0x" + fmt.Sprintf("%x", from)
param[0]["to"] = addrA
param[0]["value"] = "0xA"
param[0]["gasLimit"] = "0x5208"
param[0]["gasPrice"] = "0x1"
return param
}
Loading

0 comments on commit 094a688

Please sign in to comment.