From a292450e4eb6bbd0b24bdcc0e0cf71b42c9915f7 Mon Sep 17 00:00:00 2001 From: skosito Date: Wed, 19 Jun 2024 18:40:29 +0100 Subject: [PATCH] test: fix TODOs in rpc unit tests (#2329) --- changelog.md | 1 + rpc/backend/account_info_test.go | 55 +++++++------ rpc/backend/backend_suite_test.go | 1 + rpc/backend/call_tx.go | 2 +- rpc/backend/client_test.go | 27 ++++++- rpc/backend/node_info_test.go | 123 +++++++++++++++--------------- rpc/backend/sign_tx_test.go | 26 ++++++- 7 files changed, 149 insertions(+), 86 deletions(-) diff --git a/changelog.md b/changelog.md index 970f77a4da..02fe0dacd0 100644 --- a/changelog.md +++ b/changelog.md @@ -57,6 +57,7 @@ * [2240](https://github.com/zeta-chain/node/pull/2240) - removed hard-coded Bitcoin regnet chainID in E2E withdraw tests * [2266](https://github.com/zeta-chain/node/pull/2266) - try fixing E2E test `crosschain_swap` failure `btc transaction not signed` * [2294](https://github.com/zeta-chain/node/pull/2294) - add and fix existing ethermint rpc unit test +* [2329](https://github.com/zeta-chain/node/pull/2329) - fix TODOs in rpc unit tests * [2299](https://github.com/zeta-chain/node/pull/2299) - add `zetae2e` command to deploy test contracts ### Fixes diff --git a/rpc/backend/account_info_test.go b/rpc/backend/account_info_test.go index f54b743d5f..111b6bfb4f 100644 --- a/rpc/backend/account_info_test.go +++ b/rpc/backend/account_info_test.go @@ -5,6 +5,7 @@ import ( "math/big" tmrpcclient "github.com/cometbft/cometbft/rpc/client" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/ethereum/go-ethereum/common" @@ -82,6 +83,7 @@ func (suite *BackendTestSuite) TestGetProof() { blockNrInvalid := rpctypes.NewBlockNumber(big.NewInt(1)) blockNr := rpctypes.NewBlockNumber(big.NewInt(4)) address1 := tests.GenerateAddress() + expProofValue := big.NewInt(2) testCases := []struct { name string @@ -93,7 +95,7 @@ func (suite *BackendTestSuite) TestGetProof() { expAccRes *rpctypes.AccountResult }{ { - "fail - BlockNumeber = 1 (invalidBlockNumber)", + "fail - BlockNumber = 1 (invalidBlockNumber)", address1, []string{}, rpctypes.BlockNumberOrHash{BlockNumber: &blockNrInvalid}, @@ -139,6 +141,7 @@ func (suite *BackendTestSuite) TestGetProof() { "store/evm/key", evmtypes.StateKey(address1, common.HexToHash("0x0").Bytes()), tmrpcclient.ABCIQueryOptions{Height: iavlHeight, Prove: true}, + expProofValue.Bytes(), ) RegisterABCIQueryWithOptions( client, @@ -146,6 +149,7 @@ func (suite *BackendTestSuite) TestGetProof() { "store/acc/key", authtypes.AddressStoreKey(sdk.AccAddress(address1.Bytes())), tmrpcclient.ABCIQueryOptions{Height: iavlHeight, Prove: true}, + expProofValue.Bytes(), ) }, true, @@ -159,7 +163,7 @@ func (suite *BackendTestSuite) TestGetProof() { StorageProof: []rpctypes.StorageResult{ { Key: "0x0", - Value: (*hexutil.Big)(big.NewInt(2)), + Value: (*hexutil.Big)(expProofValue), Proof: []string{""}, }, }, @@ -415,27 +419,32 @@ func (suite *BackendTestSuite) TestGetTransactionCount() { false, hexutil.Uint64(0), }, - // TODO (https://github.com/zeta-chain/node/issues/2302): 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), - //}, + { + "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) { + var header metadata.MD + queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) + RegisterParams(queryClient, &header, 1) + suite.backend.clientCtx = suite.backend.clientCtx.WithInterfaceRegistry( + codectypes.NewInterfaceRegistry(), + ) + 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(1), + }, } for _, tc := range testCases { suite.Run(fmt.Sprintf("Case %s", tc.name), func() { diff --git a/rpc/backend/backend_suite_test.go b/rpc/backend/backend_suite_test.go index 0194d5902f..18a74edfd8 100644 --- a/rpc/backend/backend_suite_test.go +++ b/rpc/backend/backend_suite_test.go @@ -55,6 +55,7 @@ func (suite *BackendTestSuite) SetupTest() { } // Create Account with set sequence + suite.acc = sdk.AccAddress(tests.GenerateAddress().Bytes()) accounts := map[string]client.TestAccount{} accounts[suite.acc.String()] = client.TestAccount{ diff --git a/rpc/backend/call_tx.go b/rpc/backend/call_tx.go index 9df59ffd06..95bedf5a42 100644 --- a/rpc/backend/call_tx.go +++ b/rpc/backend/call_tx.go @@ -252,7 +252,7 @@ func (b *Backend) SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.Transac } if args.Nonce == nil { // get the nonce from the account retriever - // ignore error in case tge account doesn't exist yet + // ignore error in case the account doesn't exist yet nonce, err := b.getAccountNonce(*args.From, true, 0, b.logger) if err != nil { nonce = 0 diff --git a/rpc/backend/client_test.go b/rpc/backend/client_test.go index 74d6ae0bfd..c94c7cc0e0 100644 --- a/rpc/backend/client_test.go +++ b/rpc/backend/client_test.go @@ -11,7 +11,9 @@ import ( "github.com/cometbft/cometbft/types" "github.com/cosmos/cosmos-sdk/client" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" errortypes "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/cosmos/cosmos-sdk/types/tx" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/ethereum/go-ethereum/common" evmtypes "github.com/evmos/ethermint/x/evm/types" @@ -52,6 +54,11 @@ func RegisterBroadcastTx(client *mocks.Client, tx types.Tx) { Return(&tmrpctypes.ResultBroadcastTx{}, nil) } +func RegisterBroadcastTxAny(client *mocks.Client) { + client.On("BroadcastTxSync", context.Background(), mock.Anything). + Return(&tmrpctypes.ResultBroadcastTx{}, nil) +} + func RegisterBroadcastTxError(client *mocks.Client, tx types.Tx) { client.On("BroadcastTxSync", context.Background(), tx). Return(nil, errortypes.ErrInvalidRequest) @@ -274,11 +281,12 @@ func RegisterABCIQueryWithOptions( path string, data bytes.HexBytes, opts tmrpcclient.ABCIQueryOptions, + respValue []byte, ) { client.On("ABCIQueryWithOptions", context.Background(), path, data, opts). Return(&tmrpctypes.ResultABCIQuery{ Response: abci.ResponseQuery{ - Value: []byte{2}, // TODO (https://github.com/zeta-chain/node/issues/2302) replace with data.Bytes(), + Value: respValue, Height: height, }, }, nil) @@ -317,3 +325,20 @@ func RegisterABCIQueryAccount( }, }, nil) } + +func RegisterABCIQuerySimulate( + clients *mocks.Client, + opts tmrpcclient.ABCIQueryOptions, +) { + simResp := &tx.SimulateResponse{ + GasInfo: &sdk.GasInfo{GasWanted: uint64(21000), GasUsed: uint64(21000)}, + } + respBz, _ := simResp.Marshal() + clients.On("ABCIQueryWithOptions", context.Background(), "/cosmos.tx.v1beta1.Service/Simulate", mock.Anything, opts). + Return(&tmrpctypes.ResultABCIQuery{ + Response: abci.ResponseQuery{ + Value: respBz, + Height: 1, + }, + }, nil) +} diff --git a/rpc/backend/node_info_test.go b/rpc/backend/node_info_test.go index a98c433f11..7931cf4230 100644 --- a/rpc/backend/node_info_test.go +++ b/rpc/backend/node_info_test.go @@ -5,6 +5,9 @@ import ( "math/big" tmrpcclient "github.com/cometbft/cometbft/rpc/client" + "github.com/cosmos/cosmos-sdk/client" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/crypto" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/ethereum/go-ethereum/common" @@ -96,48 +99,24 @@ func (suite *BackendTestSuite) TestSetGasPrice() { } } -// TODO (https://github.com/zeta-chain/node/issues/2302): Combine these 2 into one test since the code is identical -func (suite *BackendTestSuite) TestListAccounts() { +func (suite *BackendTestSuite) TestAccounts() { testCases := []struct { - name string - registerMock func() - expAddr []common.Address - expPass bool + name string + accountsRetrievalMethod func() ([]common.Address, error) + registerMock func() + expAddr []common.Address + expPass bool }{ { - "pass - returns empty address", + "pass - returns acc from keyring from ListAccounts", + suite.backend.ListAccounts, func() {}, []common.Address{}, true, }, - } - - for _, tc := range testCases { - suite.Run(fmt.Sprintf("case %s", tc.name), func() { - suite.SetupTest() // reset test and queries - tc.registerMock() - - output, err := suite.backend.ListAccounts() - - if tc.expPass { - suite.Require().NoError(err) - suite.Require().Equal(tc.expAddr, output) - } else { - suite.Require().Error(err) - } - }) - } -} - -func (suite *BackendTestSuite) TestAccounts() { - testCases := []struct { - name string - registerMock func() - expAddr []common.Address - expPass bool - }{ { - "pass - returns empty address", + "pass - returns acc from keyring from Accounts", + suite.backend.Accounts, func() {}, []common.Address{}, true, @@ -149,7 +128,7 @@ func (suite *BackendTestSuite) TestAccounts() { suite.SetupTest() // reset test and queries tc.registerMock() - output, err := suite.backend.Accounts() + output, err := tc.accountsRetrievalMethod() if tc.expPass { suite.Require().NoError(err) @@ -272,31 +251,55 @@ func (suite *BackendTestSuite) TestSetEtherbase() { common.Address{}, false, }, - // TODO (https://github.com/zeta-chain/node/issues/2302): Finish this test case once ABCIQuery GetAccount is fixed - //{ - // "pass - set the etherbase for the miner", - // func() { - // client := suite.backend.clientCtx.Client.(*mocks.Client) - // queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) - // RegisterStatus(client) - // RegisterValidatorAccount(queryClient, suite.acc) - // c := sdk.NewDecCoin("azeta", sdk.NewIntFromBigInt(big.NewInt(1))) - // suite.backend.cfg.SetMinGasPrices(sdk.DecCoins{c}) - // delAddr, _ := suite.backend.GetCoinbase() - // account, _ := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, delAddr) - // delCommonAddr := common.BytesToAddress(delAddr.Bytes()) - // request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(delCommonAddr.Bytes()).String()} - // requestMarshal, _ := request.Marshal() - // RegisterABCIQueryAccount( - // client, - // requestMarshal, - // tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false}, - // account, - // ) - // }, - // common.Address{}, - // false, - //}, + { + "pass - set the etherbase for the miner", + func() { + priv, err := ethsecp256k1.GenerateKey() + suite.Require().NoError(err) + + armor := crypto.EncryptArmorPrivKey(priv, "", "eth_secp256k1") + suite.backend.clientCtx.Keyring.ImportPrivKey("test_key", armor, "") + + suite.acc = sdk.AccAddress(priv.PubKey().Address().Bytes()) + accounts := map[string]client.TestAccount{} + accounts[suite.acc.String()] = client.TestAccount{ + Address: suite.acc, + Num: uint64(1), + Seq: uint64(1), + } + + suite.backend.clientCtx = suite.backend.clientCtx.WithAccountRetriever( + client.TestAccountRetriever{Accounts: accounts}, + ) + var header metadata.MD + queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient) + RegisterParams(queryClient, &header, 1) + suite.backend.clientCtx = suite.backend.clientCtx.WithInterfaceRegistry( + codectypes.NewInterfaceRegistry(), + ) + client := suite.backend.clientCtx.Client.(*mocks.Client) + RegisterStatus(client) + RegisterValidatorAccount(queryClient, suite.acc) + c := sdk.NewDecCoin("azeta", sdk.NewIntFromBigInt(big.NewInt(1))) + suite.backend.cfg.SetMinGasPrices(sdk.DecCoins{c}) + delAddr, _ := suite.backend.GetCoinbase() + account, _ := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, delAddr) + delCommonAddr := common.BytesToAddress(delAddr.Bytes()) + request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(delCommonAddr.Bytes()).String()} + requestMarshal, _ := request.Marshal() + RegisterUnconfirmedTxsEmpty(client, nil) + RegisterABCIQueryAccount( + client, + requestMarshal, + tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false}, + account, + ) + RegisterABCIQuerySimulate(client, tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false}) + RegisterBroadcastTxAny(client) + }, + common.Address{}, + true, + }, } for _, tc := range testCases { diff --git a/rpc/backend/sign_tx_test.go b/rpc/backend/sign_tx_test.go index 153e326cd6..d50f6fb6bd 100644 --- a/rpc/backend/sign_tx_test.go +++ b/rpc/backend/sign_tx_test.go @@ -5,12 +5,15 @@ import ( "github.com/cosmos/cosmos-sdk/crypto" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/types" goethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/signer/core/apitypes" "github.com/evmos/ethermint/crypto/ethsecp256k1" + "github.com/evmos/ethermint/ethereum/eip712" "github.com/evmos/ethermint/tests" evmtypes "github.com/evmos/ethermint/x/evm/types" "google.golang.org/grpc/metadata" @@ -221,6 +224,18 @@ func (suite *BackendTestSuite) TestSign() { } func (suite *BackendTestSuite) TestSignTypedData() { + data := legacytx.StdSignBytes( + "0", + 1, + 1, + 0, + legacytx.StdFee{Gas: 10, Amount: sdk.Coins{}}, + []sdk.Msg{&banktypes.MsgSend{}}, + "", + nil, + ) + typedData, err := eip712.WrapTxToTypedData(0, data) + suite.Require().NoError(err) from, priv := tests.NewAddrKey() testCases := []struct { name string @@ -246,7 +261,16 @@ func (suite *BackendTestSuite) TestSignTypedData() { apitypes.TypedData{}, false, }, - // TODO (https://github.com/zeta-chain/node/issues/2302): Generate a TypedData msg + { + "sucess - valid typed data", + func() { + armor := crypto.EncryptArmorPrivKey(priv, "", "eth_secp256k1") + suite.backend.clientCtx.Keyring.ImportPrivKey("test_key", armor, "") + }, + from, + typedData, + true, + }, } for _, tc := range testCases {