Skip to content

Commit a292450

Browse files
authored
test: fix TODOs in rpc unit tests (#2329)
1 parent c667168 commit a292450

7 files changed

+149
-86
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* [2240](https://github.com/zeta-chain/node/pull/2240) - removed hard-coded Bitcoin regnet chainID in E2E withdraw tests
5858
* [2266](https://github.com/zeta-chain/node/pull/2266) - try fixing E2E test `crosschain_swap` failure `btc transaction not signed`
5959
* [2294](https://github.com/zeta-chain/node/pull/2294) - add and fix existing ethermint rpc unit test
60+
* [2329](https://github.com/zeta-chain/node/pull/2329) - fix TODOs in rpc unit tests
6061
* [2299](https://github.com/zeta-chain/node/pull/2299) - add `zetae2e` command to deploy test contracts
6162

6263
### Fixes

rpc/backend/account_info_test.go

+32-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math/big"
66

77
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
8+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
89
sdk "github.com/cosmos/cosmos-sdk/types"
910
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
1011
"github.com/ethereum/go-ethereum/common"
@@ -82,6 +83,7 @@ func (suite *BackendTestSuite) TestGetProof() {
8283
blockNrInvalid := rpctypes.NewBlockNumber(big.NewInt(1))
8384
blockNr := rpctypes.NewBlockNumber(big.NewInt(4))
8485
address1 := tests.GenerateAddress()
86+
expProofValue := big.NewInt(2)
8587

8688
testCases := []struct {
8789
name string
@@ -93,7 +95,7 @@ func (suite *BackendTestSuite) TestGetProof() {
9395
expAccRes *rpctypes.AccountResult
9496
}{
9597
{
96-
"fail - BlockNumeber = 1 (invalidBlockNumber)",
98+
"fail - BlockNumber = 1 (invalidBlockNumber)",
9799
address1,
98100
[]string{},
99101
rpctypes.BlockNumberOrHash{BlockNumber: &blockNrInvalid},
@@ -139,13 +141,15 @@ func (suite *BackendTestSuite) TestGetProof() {
139141
"store/evm/key",
140142
evmtypes.StateKey(address1, common.HexToHash("0x0").Bytes()),
141143
tmrpcclient.ABCIQueryOptions{Height: iavlHeight, Prove: true},
144+
expProofValue.Bytes(),
142145
)
143146
RegisterABCIQueryWithOptions(
144147
client,
145148
bn.Int64(),
146149
"store/acc/key",
147150
authtypes.AddressStoreKey(sdk.AccAddress(address1.Bytes())),
148151
tmrpcclient.ABCIQueryOptions{Height: iavlHeight, Prove: true},
152+
expProofValue.Bytes(),
149153
)
150154
},
151155
true,
@@ -159,7 +163,7 @@ func (suite *BackendTestSuite) TestGetProof() {
159163
StorageProof: []rpctypes.StorageResult{
160164
{
161165
Key: "0x0",
162-
Value: (*hexutil.Big)(big.NewInt(2)),
166+
Value: (*hexutil.Big)(expProofValue),
163167
Proof: []string{""},
164168
},
165169
},
@@ -415,27 +419,32 @@ func (suite *BackendTestSuite) TestGetTransactionCount() {
415419
false,
416420
hexutil.Uint64(0),
417421
},
418-
// TODO (https://github.com/zeta-chain/node/issues/2302): Error mocking the GetAccount call - problem with Any type
419-
//{
420-
// "pass - returns the number of transactions at the given address up to the given block number",
421-
// true,
422-
// rpctypes.NewBlockNumber(big.NewInt(1)),
423-
// func(addr common.Address, bn rpctypes.BlockNumber) {
424-
// client := suite.backend.clientCtx.Client.(*mocks.Client)
425-
// account, err := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, suite.acc)
426-
// suite.Require().NoError(err)
427-
// request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(suite.acc.Bytes()).String()}
428-
// requestMarshal, _ := request.Marshal()
429-
// RegisterABCIQueryAccount(
430-
// client,
431-
// requestMarshal,
432-
// tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false},
433-
// account,
434-
// )
435-
// },
436-
// true,
437-
// hexutil.Uint64(0),
438-
//},
422+
{
423+
"pass - returns the number of transactions at the given address up to the given block number",
424+
true,
425+
rpctypes.NewBlockNumber(big.NewInt(1)),
426+
func(addr common.Address, bn rpctypes.BlockNumber) {
427+
var header metadata.MD
428+
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
429+
RegisterParams(queryClient, &header, 1)
430+
suite.backend.clientCtx = suite.backend.clientCtx.WithInterfaceRegistry(
431+
codectypes.NewInterfaceRegistry(),
432+
)
433+
client := suite.backend.clientCtx.Client.(*mocks.Client)
434+
account, err := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, suite.acc)
435+
suite.Require().NoError(err)
436+
request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(suite.acc.Bytes()).String()}
437+
requestMarshal, _ := request.Marshal()
438+
RegisterABCIQueryAccount(
439+
client,
440+
requestMarshal,
441+
tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false},
442+
account,
443+
)
444+
},
445+
true,
446+
hexutil.Uint64(1),
447+
},
439448
}
440449
for _, tc := range testCases {
441450
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {

rpc/backend/backend_suite_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (suite *BackendTestSuite) SetupTest() {
5555
}
5656

5757
// Create Account with set sequence
58+
5859
suite.acc = sdk.AccAddress(tests.GenerateAddress().Bytes())
5960
accounts := map[string]client.TestAccount{}
6061
accounts[suite.acc.String()] = client.TestAccount{

rpc/backend/call_tx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (b *Backend) SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.Transac
252252
}
253253
if args.Nonce == nil {
254254
// get the nonce from the account retriever
255-
// ignore error in case tge account doesn't exist yet
255+
// ignore error in case the account doesn't exist yet
256256
nonce, err := b.getAccountNonce(*args.From, true, 0, b.logger)
257257
if err != nil {
258258
nonce = 0

rpc/backend/client_test.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"github.com/cometbft/cometbft/types"
1212
"github.com/cosmos/cosmos-sdk/client"
1313
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
14+
sdk "github.com/cosmos/cosmos-sdk/types"
1415
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
16+
"github.com/cosmos/cosmos-sdk/types/tx"
1517
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
1618
"github.com/ethereum/go-ethereum/common"
1719
evmtypes "github.com/evmos/ethermint/x/evm/types"
@@ -52,6 +54,11 @@ func RegisterBroadcastTx(client *mocks.Client, tx types.Tx) {
5254
Return(&tmrpctypes.ResultBroadcastTx{}, nil)
5355
}
5456

57+
func RegisterBroadcastTxAny(client *mocks.Client) {
58+
client.On("BroadcastTxSync", context.Background(), mock.Anything).
59+
Return(&tmrpctypes.ResultBroadcastTx{}, nil)
60+
}
61+
5562
func RegisterBroadcastTxError(client *mocks.Client, tx types.Tx) {
5663
client.On("BroadcastTxSync", context.Background(), tx).
5764
Return(nil, errortypes.ErrInvalidRequest)
@@ -274,11 +281,12 @@ func RegisterABCIQueryWithOptions(
274281
path string,
275282
data bytes.HexBytes,
276283
opts tmrpcclient.ABCIQueryOptions,
284+
respValue []byte,
277285
) {
278286
client.On("ABCIQueryWithOptions", context.Background(), path, data, opts).
279287
Return(&tmrpctypes.ResultABCIQuery{
280288
Response: abci.ResponseQuery{
281-
Value: []byte{2}, // TODO (https://github.com/zeta-chain/node/issues/2302) replace with data.Bytes(),
289+
Value: respValue,
282290
Height: height,
283291
},
284292
}, nil)
@@ -317,3 +325,20 @@ func RegisterABCIQueryAccount(
317325
},
318326
}, nil)
319327
}
328+
329+
func RegisterABCIQuerySimulate(
330+
clients *mocks.Client,
331+
opts tmrpcclient.ABCIQueryOptions,
332+
) {
333+
simResp := &tx.SimulateResponse{
334+
GasInfo: &sdk.GasInfo{GasWanted: uint64(21000), GasUsed: uint64(21000)},
335+
}
336+
respBz, _ := simResp.Marshal()
337+
clients.On("ABCIQueryWithOptions", context.Background(), "/cosmos.tx.v1beta1.Service/Simulate", mock.Anything, opts).
338+
Return(&tmrpctypes.ResultABCIQuery{
339+
Response: abci.ResponseQuery{
340+
Value: respBz,
341+
Height: 1,
342+
},
343+
}, nil)
344+
}

rpc/backend/node_info_test.go

+63-60
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"math/big"
66

77
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
8+
"github.com/cosmos/cosmos-sdk/client"
9+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
10+
"github.com/cosmos/cosmos-sdk/crypto"
811
sdk "github.com/cosmos/cosmos-sdk/types"
912
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
1013
"github.com/ethereum/go-ethereum/common"
@@ -96,48 +99,24 @@ func (suite *BackendTestSuite) TestSetGasPrice() {
9699
}
97100
}
98101

99-
// TODO (https://github.com/zeta-chain/node/issues/2302): Combine these 2 into one test since the code is identical
100-
func (suite *BackendTestSuite) TestListAccounts() {
102+
func (suite *BackendTestSuite) TestAccounts() {
101103
testCases := []struct {
102-
name string
103-
registerMock func()
104-
expAddr []common.Address
105-
expPass bool
104+
name string
105+
accountsRetrievalMethod func() ([]common.Address, error)
106+
registerMock func()
107+
expAddr []common.Address
108+
expPass bool
106109
}{
107110
{
108-
"pass - returns empty address",
111+
"pass - returns acc from keyring from ListAccounts",
112+
suite.backend.ListAccounts,
109113
func() {},
110114
[]common.Address{},
111115
true,
112116
},
113-
}
114-
115-
for _, tc := range testCases {
116-
suite.Run(fmt.Sprintf("case %s", tc.name), func() {
117-
suite.SetupTest() // reset test and queries
118-
tc.registerMock()
119-
120-
output, err := suite.backend.ListAccounts()
121-
122-
if tc.expPass {
123-
suite.Require().NoError(err)
124-
suite.Require().Equal(tc.expAddr, output)
125-
} else {
126-
suite.Require().Error(err)
127-
}
128-
})
129-
}
130-
}
131-
132-
func (suite *BackendTestSuite) TestAccounts() {
133-
testCases := []struct {
134-
name string
135-
registerMock func()
136-
expAddr []common.Address
137-
expPass bool
138-
}{
139117
{
140-
"pass - returns empty address",
118+
"pass - returns acc from keyring from Accounts",
119+
suite.backend.Accounts,
141120
func() {},
142121
[]common.Address{},
143122
true,
@@ -149,7 +128,7 @@ func (suite *BackendTestSuite) TestAccounts() {
149128
suite.SetupTest() // reset test and queries
150129
tc.registerMock()
151130

152-
output, err := suite.backend.Accounts()
131+
output, err := tc.accountsRetrievalMethod()
153132

154133
if tc.expPass {
155134
suite.Require().NoError(err)
@@ -272,31 +251,55 @@ func (suite *BackendTestSuite) TestSetEtherbase() {
272251
common.Address{},
273252
false,
274253
},
275-
// TODO (https://github.com/zeta-chain/node/issues/2302): Finish this test case once ABCIQuery GetAccount is fixed
276-
//{
277-
// "pass - set the etherbase for the miner",
278-
// func() {
279-
// client := suite.backend.clientCtx.Client.(*mocks.Client)
280-
// queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
281-
// RegisterStatus(client)
282-
// RegisterValidatorAccount(queryClient, suite.acc)
283-
// c := sdk.NewDecCoin("azeta", sdk.NewIntFromBigInt(big.NewInt(1)))
284-
// suite.backend.cfg.SetMinGasPrices(sdk.DecCoins{c})
285-
// delAddr, _ := suite.backend.GetCoinbase()
286-
// account, _ := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, delAddr)
287-
// delCommonAddr := common.BytesToAddress(delAddr.Bytes())
288-
// request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(delCommonAddr.Bytes()).String()}
289-
// requestMarshal, _ := request.Marshal()
290-
// RegisterABCIQueryAccount(
291-
// client,
292-
// requestMarshal,
293-
// tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false},
294-
// account,
295-
// )
296-
// },
297-
// common.Address{},
298-
// false,
299-
//},
254+
{
255+
"pass - set the etherbase for the miner",
256+
func() {
257+
priv, err := ethsecp256k1.GenerateKey()
258+
suite.Require().NoError(err)
259+
260+
armor := crypto.EncryptArmorPrivKey(priv, "", "eth_secp256k1")
261+
suite.backend.clientCtx.Keyring.ImportPrivKey("test_key", armor, "")
262+
263+
suite.acc = sdk.AccAddress(priv.PubKey().Address().Bytes())
264+
accounts := map[string]client.TestAccount{}
265+
accounts[suite.acc.String()] = client.TestAccount{
266+
Address: suite.acc,
267+
Num: uint64(1),
268+
Seq: uint64(1),
269+
}
270+
271+
suite.backend.clientCtx = suite.backend.clientCtx.WithAccountRetriever(
272+
client.TestAccountRetriever{Accounts: accounts},
273+
)
274+
var header metadata.MD
275+
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
276+
RegisterParams(queryClient, &header, 1)
277+
suite.backend.clientCtx = suite.backend.clientCtx.WithInterfaceRegistry(
278+
codectypes.NewInterfaceRegistry(),
279+
)
280+
client := suite.backend.clientCtx.Client.(*mocks.Client)
281+
RegisterStatus(client)
282+
RegisterValidatorAccount(queryClient, suite.acc)
283+
c := sdk.NewDecCoin("azeta", sdk.NewIntFromBigInt(big.NewInt(1)))
284+
suite.backend.cfg.SetMinGasPrices(sdk.DecCoins{c})
285+
delAddr, _ := suite.backend.GetCoinbase()
286+
account, _ := suite.backend.clientCtx.AccountRetriever.GetAccount(suite.backend.clientCtx, delAddr)
287+
delCommonAddr := common.BytesToAddress(delAddr.Bytes())
288+
request := &authtypes.QueryAccountRequest{Address: sdk.AccAddress(delCommonAddr.Bytes()).String()}
289+
requestMarshal, _ := request.Marshal()
290+
RegisterUnconfirmedTxsEmpty(client, nil)
291+
RegisterABCIQueryAccount(
292+
client,
293+
requestMarshal,
294+
tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false},
295+
account,
296+
)
297+
RegisterABCIQuerySimulate(client, tmrpcclient.ABCIQueryOptions{Height: int64(1), Prove: false})
298+
RegisterBroadcastTxAny(client)
299+
},
300+
common.Address{},
301+
true,
302+
},
300303
}
301304

302305
for _, tc := range testCases {

rpc/backend/sign_tx_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55

66
"github.com/cosmos/cosmos-sdk/crypto"
77
sdk "github.com/cosmos/cosmos-sdk/types"
8+
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
9+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
810
"github.com/ethereum/go-ethereum/common"
911
"github.com/ethereum/go-ethereum/common/hexutil"
1012
ethtypes "github.com/ethereum/go-ethereum/core/types"
1113
goethcrypto "github.com/ethereum/go-ethereum/crypto"
1214
"github.com/ethereum/go-ethereum/signer/core/apitypes"
1315
"github.com/evmos/ethermint/crypto/ethsecp256k1"
16+
"github.com/evmos/ethermint/ethereum/eip712"
1417
"github.com/evmos/ethermint/tests"
1518
evmtypes "github.com/evmos/ethermint/x/evm/types"
1619
"google.golang.org/grpc/metadata"
@@ -221,6 +224,18 @@ func (suite *BackendTestSuite) TestSign() {
221224
}
222225

223226
func (suite *BackendTestSuite) TestSignTypedData() {
227+
data := legacytx.StdSignBytes(
228+
"0",
229+
1,
230+
1,
231+
0,
232+
legacytx.StdFee{Gas: 10, Amount: sdk.Coins{}},
233+
[]sdk.Msg{&banktypes.MsgSend{}},
234+
"",
235+
nil,
236+
)
237+
typedData, err := eip712.WrapTxToTypedData(0, data)
238+
suite.Require().NoError(err)
224239
from, priv := tests.NewAddrKey()
225240
testCases := []struct {
226241
name string
@@ -246,7 +261,16 @@ func (suite *BackendTestSuite) TestSignTypedData() {
246261
apitypes.TypedData{},
247262
false,
248263
},
249-
// TODO (https://github.com/zeta-chain/node/issues/2302): Generate a TypedData msg
264+
{
265+
"sucess - valid typed data",
266+
func() {
267+
armor := crypto.EncryptArmorPrivKey(priv, "", "eth_secp256k1")
268+
suite.backend.clientCtx.Keyring.ImportPrivKey("test_key", armor, "")
269+
},
270+
from,
271+
typedData,
272+
true,
273+
},
250274
}
251275

252276
for _, tc := range testCases {

0 commit comments

Comments
 (0)