Skip to content

Commit

Permalink
test: fix TODOs in rpc unit tests (#2329)
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito authored Jun 19, 2024
1 parent c667168 commit a292450
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 86 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 32 additions & 23 deletions rpc/backend/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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},
Expand Down Expand Up @@ -139,13 +141,15 @@ func (suite *BackendTestSuite) TestGetProof() {
"store/evm/key",
evmtypes.StateKey(address1, common.HexToHash("0x0").Bytes()),
tmrpcclient.ABCIQueryOptions{Height: iavlHeight, Prove: true},
expProofValue.Bytes(),
)
RegisterABCIQueryWithOptions(
client,
bn.Int64(),
"store/acc/key",
authtypes.AddressStoreKey(sdk.AccAddress(address1.Bytes())),
tmrpcclient.ABCIQueryOptions{Height: iavlHeight, Prove: true},
expProofValue.Bytes(),
)
},
true,
Expand All @@ -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{""},
},
},
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion rpc/backend/call_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion rpc/backend/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
123 changes: 63 additions & 60 deletions rpc/backend/node_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 25 additions & 1 deletion rpc/backend/sign_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit a292450

Please sign in to comment.