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

tests: Add unit tests for rpc client endpoints #1409

Merged
merged 24 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
97e1bcf
test: add preliminary unit tests and additional mocks for chain_info,…
Vvaradinov Oct 28, 2022
9f1b438
tests: added additional mocked client calls
Vvaradinov Oct 28, 2022
a69201d
tests: bumped coverage of call_tx to 56% and chain_info to 77%
Vvaradinov Oct 28, 2022
c0e46a6
tests: bumped call_tx coverage to 70.2% and added additional mock cli…
Vvaradinov Oct 29, 2022
f91ddde
tests: tx_info preliminary tests added for debugging.
Vvaradinov Oct 31, 2022
f55b8a7
tests: added test coverage for sign_tx and additional mocks
Vvaradinov Nov 1, 2022
c1bb153
tests: tx_info test coverage bumped to 60.3%
Vvaradinov Nov 1, 2022
e0f110d
test: coverage for tracing_tests now at 72%
Vvaradinov Nov 2, 2022
3e00316
tests: added fee makert query client mocks and bumped chain_info to 8…
Vvaradinov Nov 2, 2022
b5b1cb7
tests: failing Cosmos auth module account query.
Vvaradinov Nov 3, 2022
16fde32
tests: added FeeMarket Params mock to call_tx_test
Vvaradinov Nov 3, 2022
bca32e2
cleanup some unused code
Vvaradinov Nov 3, 2022
0ab12a9
tests: added helper function to test suite for signing a Tx and bumpe…
Vvaradinov Nov 4, 2022
51fc162
test: commented GetAccount error case and bumped chain_info to 90.3% …
Nov 9, 2022
69e89a6
Merge branch 'main' into Vvaradinov/rpc-endpoints-unit-tests
fedekunze Nov 10, 2022
d56f125
test: cleanup of tests in node_info, sign_tx and account_info
Vvaradinov Nov 14, 2022
b9dd955
Merge branch 'Vvaradinov/rpc-endpoints-unit-tests' of https://github.…
Vvaradinov Nov 14, 2022
fa34bb2
Clean up print
Vvaradinov Nov 14, 2022
7dc689c
Merge branch 'main' into Vvaradinov/rpc-endpoints-unit-tests
Vvaradinov Nov 14, 2022
5ec66d6
Apply suggestions from code review
Vvaradinov Nov 16, 2022
f0fd313
Merge branch 'main' into Vvaradinov/rpc-endpoints-unit-tests
Vvaradinov Nov 16, 2022
922abcd
fix import issues
facs95 Nov 17, 2022
21c63f0
Merge branch 'main' into Vvaradinov/rpc-endpoints-unit-tests
facs95 Nov 17, 2022
e99d6cf
fix tests
facs95 Nov 17, 2022
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
33 changes: 33 additions & 0 deletions rpc/backend/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func (suite *BackendTestSuite) TestGetProof() {
false,
&rpctypes.AccountResult{},
},
{
"fail - Block doesn't exist)",
address1,
[]string{},
rpctypes.BlockNumberOrHash{BlockNumber: &blockNrInvalid},
func(bn rpctypes.BlockNumber, addr common.Address) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
RegisterBlockError(client, bn.Int64())
},
false,
&rpctypes.AccountResult{},
},
{
"pass",
address1,
Expand Down Expand Up @@ -351,6 +363,27 @@ func (suite *BackendTestSuite) TestGetTransactionCount() {
true,
hexutil.Uint64(0),
},
// TODO: Error mocking the GetAccount call - problem with Any type
//{
// "pass - returns the number of transactions at the given address up to the given block number",
// true,
// rpctypes.NewBlockNumber(big.NewInt(1)),
// func(addr common.Address, bn rpctypes.BlockNumber) {
// client := suite.backend.clientCtx.Client.(*mocks.Client)
// account, err := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, suite.acc)
// suite.Require().NoError(err)
// request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(suite.acc.Bytes()).String()}
// requestMarshal, _ := request.Marshal()
// RegisterABCIQueryAccount(
// client,
// requestMarshal,
// tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false},
// account,
// )
// },
// true,
// hexutil.Uint64(0),
//},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
Expand Down
20 changes: 0 additions & 20 deletions rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -16,12 +15,10 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/evmos/ethermint/crypto/hd"
rpctypes "github.com/evmos/ethermint/rpc/types"
"github.com/evmos/ethermint/server/config"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
)
Expand Down Expand Up @@ -162,23 +159,6 @@ func NewBackend(
panic(err)
}

algos, _ := clientCtx.Keyring.SupportedAlgorithms()
if !algos.Contains(hd.EthSecp256k1) {
kr, err := keyring.New(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
clientCtx.KeyringDir,
clientCtx.Input,
clientCtx.Codec,
hd.EthSecp256k1Option(),
)
if err != nil {
panic(err)
}

clientCtx = clientCtx.WithKeyring(kr)
}

return &Backend{
ctx: context.Background(),
clientCtx: clientCtx,
Expand Down
35 changes: 34 additions & 1 deletion rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend
import (
"bufio"
"fmt"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -33,6 +34,7 @@ type BackendTestSuite struct {
suite.Suite
backend *Backend
acc sdk.AccAddress
signer keyring.Signer
}

func TestBackendTestSuite(t *testing.T) {
Expand Down Expand Up @@ -61,6 +63,10 @@ func (suite *BackendTestSuite) SetupTest() {
Seq: uint64(1),
}

priv, err := ethsecp256k1.GenerateKey()
suite.signer = tests.NewSigner(priv)
suite.Require().NoError(err)

encodingConfig := encoding.MakeConfig(app.ModuleBasics)
clientCtx := client.Context{}.WithChainID("ethermint_9000-1").
WithHeight(1).
Expand All @@ -70,13 +76,18 @@ func (suite *BackendTestSuite) SetupTest() {
WithAccountRetriever(client.TestAccountRetriever{Accounts: accounts})

allowUnprotectedTxs := false

idxer := indexer.NewKVIndexer(dbm.NewMemDB(), ctx.Logger, clientCtx)

suite.backend = NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, idxer)
suite.backend.queryClient.QueryClient = mocks.NewEVMQueryClient(suite.T())
suite.backend.clientCtx.Client = mocks.NewClient(suite.T())
suite.backend.queryClient.FeeMarket = mocks.NewFeeMarketQueryClient(suite.T())
suite.backend.ctx = rpctypes.ContextWithHeight(1)

// Add codec
encCfg := encoding.MakeConfig(app.ModuleBasics)
suite.backend.clientCtx.Codec = encCfg.Codec

}

// buildEthereumTx returns an example legacy Ethereum transaction
Expand Down Expand Up @@ -158,3 +169,25 @@ func (suite *BackendTestSuite) generateTestKeyring(clientDir string) (keyring.Ke
encCfg := encoding.MakeConfig(app.ModuleBasics)
return keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, clientDir, buf, encCfg.Codec, []keyring.Option{hd.EthSecp256k1Option()}...)
}

func (suite *BackendTestSuite) signAndEncodeEthTx(msgEthereumTx *evmtypes.MsgEthereumTx) []byte {
from, priv := tests.NewAddrKey()
signer := tests.NewSigner(priv)

queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterParamsWithoutHeader(queryClient, 1)

ethSigner := ethtypes.LatestSigner(suite.backend.ChainConfig())
msgEthereumTx.From = from.String()
err := msgEthereumTx.Sign(ethSigner, signer)
suite.Require().NoError(err)

tx, err := msgEthereumTx.BuildTx(suite.backend.clientCtx.TxConfig.NewTxBuilder(), "aphoton")
suite.Require().NoError(err)

txEncoder := suite.backend.clientCtx.TxConfig.TxEncoder()
txBz, err := txEncoder(tx)
suite.Require().NoError(err)

return txBz
}
10 changes: 5 additions & 5 deletions rpc/backend/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (suite *BackendTestSuite) TestBlockNumber() {
{
"fail - invalid block header height",
func() {
height := int64(1)
var header metadata.MD
height := int64(1)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterParamsInvalidHeight(queryClient, &header, int64(height))
},
Expand All @@ -41,8 +41,8 @@ func (suite *BackendTestSuite) TestBlockNumber() {
{
"fail - invalid block header",
func() {
height := int64(1)
var header metadata.MD
height := int64(1)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterParamsInvalidHeader(queryClient, &header, int64(height))
},
Expand All @@ -52,8 +52,8 @@ func (suite *BackendTestSuite) TestBlockNumber() {
{
"pass - app state header height 1",
func() {
height := int64(1)
var header metadata.MD
height := int64(1)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterParams(queryClient, &header, int64(height))
},
Expand Down Expand Up @@ -552,8 +552,8 @@ func (suite *BackendTestSuite) TestTendermintBlockByNumber() {
"fail - blockNum < 0 with app state height error",
ethrpc.BlockNumber(-1),
func(_ ethrpc.BlockNumber) {
appHeight := int64(1)
var header metadata.MD
appHeight := int64(1)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterParamsError(queryClient, &header, appHeight)
},
Expand All @@ -564,8 +564,8 @@ func (suite *BackendTestSuite) TestTendermintBlockByNumber() {
"pass - blockNum < 0 with app state height >= 1",
ethrpc.BlockNumber(-1),
func(blockNum ethrpc.BlockNumber) {
appHeight := int64(1)
var header metadata.MD
appHeight := int64(1)
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
RegisterParams(queryClient, &header, appHeight)

Expand Down
Loading