Skip to content

Commit

Permalink
Fix backend unit tests (#104)
Browse files Browse the repository at this point in the history
* Update unit tests in rpc backend.

* Update unit tests in rpc backend.

* Fix unit test

* Correct function name
  • Loading branch information
deep-quality-dev authored Jun 19, 2024
1 parent 07574fc commit 2a3a818
Show file tree
Hide file tree
Showing 28 changed files with 140 additions and 125 deletions.
2 changes: 1 addition & 1 deletion app/ante/fee_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (m MockEVMKeeper) GetParams(ctx sdk.Context) evmtypes.Params {
}

func (m MockEVMKeeper) ChainID() *big.Int {
return big.NewInt(9000)
return big.NewInt(1291)
}

func TestSDKTxFeeChecker(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions cmd/swisstronikd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

"swisstronik/app"
evmtypes "swisstronik/types"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/flags"
)

func InitSDKConfig() {
Expand All @@ -27,10 +28,11 @@ func InitSDKConfig() {

// Set global coin type to be used in HD wallets
config.SetCoinType(evmtypes.Bip44CoinType)
config.SetPurpose(sdk.Purpose)
config.SetPurpose(sdk.Purpose)

// Set and seal config
config.Seal()

}

// ValidateChainID wraps a cobra command with a RunE function with base 10 integer chain-id verification.
Expand Down
12 changes: 6 additions & 6 deletions ethereum/eip712/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (

// Testing Constants
var (
chainId = "ethermint_9000-1"
chainId = "swisstronik_1291-1"
ctx = client.Context{}.WithTxConfig(
encoding.MakeConfig(app.ModuleBasics).TxConfig,
)
)
var feePayerAddress = "ethm17xpfvakm2amg962yls6f84z3kell8c5lthdzgl"
var feePayerAddress = "swtr1x9djmrsvrf60xze5vj4pv52khjhskyqpzratt8"

type TestCaseStruct struct {
txBuilder client.TxBuilder
Expand All @@ -44,7 +44,7 @@ type TestCaseStruct struct {

func TestLedgerPreprocessing(t *testing.T) {
// Update bech32 prefix
sdk.GetConfig().SetBech32PrefixForAccount("ethm", "")
sdk.GetConfig().SetBech32PrefixForAccount("swtr", "")

testCases := []TestCaseStruct{
createBasicTestCase(t),
Expand All @@ -67,7 +67,7 @@ func TestLedgerPreprocessing(t *testing.T) {
require.True(t, len(hasExtOptsTx.GetExtensionOptions()) == 1)

expectedExt := types.ExtensionOptionsWeb3Tx{
TypedDataChainID: 9000,
TypedDataChainID: 1291,
FeePayer: feePayerAddress,
FeePayerSig: tc.expectedSignatureBytes,
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func createBasicTestCase(t *testing.T) TestCaseStruct {
Sequence: 0,
}

txBuilder.SetSignatures(sigsV2)
_ = txBuilder.SetSignatures(sigsV2)
return TestCaseStruct{
txBuilder: txBuilder,
expectedFeePayer: feePayer.String(),
Expand Down Expand Up @@ -209,7 +209,7 @@ func createPopulatedTestCase(t *testing.T) TestCaseStruct {
),
}

txBuilder.SetMsgs(&msgSend)
_ = txBuilder.SetMsgs(&msgSend)

return TestCaseStruct{
txBuilder: txBuilder,
Expand Down
2 changes: 1 addition & 1 deletion ibc/testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
)

// ChainIDPrefix defines the default chain ID prefix for test chains
var ChainIDPrefix = "swisstronik_9000-"
var ChainIDPrefix = "swisstronik_1291-"

func init() {
ibcgotesting.ChainIDPrefix = ChainIDPrefix
Expand Down
2 changes: 1 addition & 1 deletion rpc/backend/account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"math/big"

errorsmod "cosmossdk.io/errors"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"

rpctypes "swisstronik/rpc/types"
evmtypes "swisstronik/x/evm/types"
)
Expand Down
10 changes: 5 additions & 5 deletions rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (
"math/big"
"time"

rpctypes "swisstronik/rpc/types"
"swisstronik/server/config"
ethermint "swisstronik/types"
evmtypes "swisstronik/x/evm/types"

"github.com/cometbft/cometbft/libs/log"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -37,6 +32,11 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/signer/core/apitypes"

rpctypes "swisstronik/rpc/types"
"swisstronik/server/config"
ethermint "swisstronik/types"
evmtypes "swisstronik/x/evm/types"
)

// BackendI implements the Cosmos and EVM backend.
Expand Down
1 change: 0 additions & 1 deletion rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

dbm "github.com/cometbft/cometbft-db"

tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down
6 changes: 3 additions & 3 deletions rpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
"math/big"
"strconv"

rpctypes "swisstronik/rpc/types"
evmtypes "swisstronik/x/evm/types"

tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -35,6 +32,9 @@ import (
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

rpctypes "swisstronik/rpc/types"
evmtypes "swisstronik/x/evm/types"
)

// BlockNumber returns the current block number in abci app state. Because abci
Expand Down
9 changes: 5 additions & 4 deletions rpc/backend/call_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func (suite *BackendTestSuite) TestDoCall() {
})
}
}

func (suite *BackendTestSuite) TestGasPrice() {
defaultGasPrice := (*hexutil.Big)(big.NewInt(1))

Expand All @@ -468,8 +469,8 @@ func (suite *BackendTestSuite) TestGasPrice() {
feeMarketClient := suite.backend.queryClient.FeeMarket.(*mocks.FeeMarketQueryClient)
RegisterFeeMarketParams(feeMarketClient, 1)
RegisterParams(queryClient, &header, 1)
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
_, _ = RegisterBlock(client, 1, nil)
_, _ = RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, sdk.NewInt(1))
},
defaultGasPrice,
Expand All @@ -484,8 +485,8 @@ func (suite *BackendTestSuite) TestGasPrice() {
feeMarketClient := suite.backend.queryClient.FeeMarket.(*mocks.FeeMarketQueryClient)
RegisterFeeMarketParamsError(feeMarketClient, 1)
RegisterParams(queryClient, &header, 1)
RegisterBlock(client, 1, nil)
RegisterBlockResults(client, 1)
_, _ = RegisterBlock(client, 1, nil)
_, _ = RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, sdk.NewInt(1))
},
defaultGasPrice,
Expand Down
10 changes: 5 additions & 5 deletions rpc/backend/chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (
"math/big"
"strconv"

rpctypes "swisstronik/rpc/types"
ethermint "swisstronik/types"
evmtypes "swisstronik/x/evm/types"
feemarkettypes "swisstronik/x/feemarket/types"

tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -33,6 +28,11 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"

rpctypes "swisstronik/rpc/types"
ethermint "swisstronik/types"
evmtypes "swisstronik/x/evm/types"
feemarkettypes "swisstronik/x/feemarket/types"
)

// ChainID is the EIP-155 replay-protection chain id for the current ethereum chain config.
Expand Down
24 changes: 11 additions & 13 deletions rpc/backend/chain_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import (
"fmt"
"math/big"

rpc "swisstronik/rpc/types"
"swisstronik/tests"

"github.com/ethereum/go-ethereum/common/hexutil"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"google.golang.org/grpc/metadata"

"github.com/cometbft/cometbft/abci/types"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"google.golang.org/grpc/metadata"

"swisstronik/rpc/backend/mocks"
rpc "swisstronik/rpc/types"
"swisstronik/tests"
evmtypes "swisstronik/x/evm/types"
feemarkettypes "swisstronik/x/feemarket/types"
)
Expand Down Expand Up @@ -153,7 +151,7 @@ func (suite *BackendTestSuite) TestBaseFee() {
}

func (suite *BackendTestSuite) TestChainId() {
expChainId := (*hexutil.Big)(big.NewInt(9000))
expChainId := (*hexutil.Big)(big.NewInt(1291))
testCases := []struct {
name string
registerMock func()
Expand Down Expand Up @@ -375,7 +373,7 @@ func (suite *BackendTestSuite) TestFeeHistory() {
func(validator sdk.AccAddress) {
client := suite.backend.clientCtx.Client.(*mocks.Client)
suite.backend.cfg.JSONRPC.FeeHistoryCap = 2
RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
_, _ = RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
RegisterBlockResultsError(client, 1)
},
1,
Expand All @@ -391,8 +389,8 @@ func (suite *BackendTestSuite) TestFeeHistory() {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
client := suite.backend.clientCtx.Client.(*mocks.Client)
suite.backend.cfg.JSONRPC.FeeHistoryCap = 2
RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
RegisterBlockResults(client, 1)
_, _ = RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
_, _ = RegisterBlockResults(client, 1)
RegisterBaseFeeError(queryClient)
RegisterValidatorAccount(queryClient, validator)
RegisterConsensusParams(client, 1)
Expand All @@ -411,8 +409,8 @@ func (suite *BackendTestSuite) TestFeeHistory() {
queryClient := suite.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
client := suite.backend.clientCtx.Client.(*mocks.Client)
suite.backend.cfg.JSONRPC.FeeHistoryCap = 2
RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
RegisterBlockResults(client, 1)
_, _ = RegisterBlock(client, ethrpc.BlockNumber(1).Int64(), nil)
_, _ = RegisterBlockResults(client, 1)
RegisterBaseFee(queryClient, baseFee)
RegisterValidatorAccount(queryClient, validator)
RegisterConsensusParams(client, 1)
Expand Down
19 changes: 9 additions & 10 deletions rpc/backend/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ import (
"context"
"testing"

"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"

"swisstronik/rpc/backend/mocks"
rpc "swisstronik/rpc/types"
evmtypes "swisstronik/x/evm/types"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/bytes"
tmrpcclient "github.com/cometbft/cometbft/rpc/client"
tmrpctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"swisstronik/rpc/backend/mocks"
rpc "swisstronik/rpc/types"
evmtypes "swisstronik/x/evm/types"
)

// Client defines a mocked object that implements the Tendermint JSON-RPC Client
Expand Down Expand Up @@ -142,7 +141,7 @@ func RegisterBlockNotFound(
func TestRegisterBlock(t *testing.T) {
client := mocks.NewClient(t)
height := rpc.BlockNumber(1).Int64()
RegisterBlock(client, height, nil)
_, _ = RegisterBlock(client, height, nil)

res, err := client.Block(rpc.ContextWithHeight(height), &height)

Expand Down Expand Up @@ -219,7 +218,7 @@ func RegisterBlockResultsError(client *mocks.Client, height int64) {
func TestRegisterBlockResults(t *testing.T) {
client := mocks.NewClient(t)
height := int64(1)
RegisterBlockResults(client, height)
_, _ = RegisterBlockResults(client, height)

res, err := client.BlockResults(rpc.ContextWithHeight(height), &height)
expRes := &tmrpctypes.ResultBlockResults{
Expand Down
18 changes: 9 additions & 9 deletions rpc/backend/evm_query_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import (
"strconv"
"testing"

"swisstronik/rpc/backend/mocks"
rpc "swisstronik/rpc/types"
"swisstronik/tests"
evmtypes "swisstronik/x/evm/types"

sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
Expand All @@ -22,6 +17,11 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

"swisstronik/rpc/backend/mocks"
rpc "swisstronik/rpc/types"
"swisstronik/tests"
evmtypes "swisstronik/x/evm/types"
)

// QueryClient defines a mocked object that implements the ethermint GRPC
Expand All @@ -35,26 +35,26 @@ var _ evmtypes.QueryClient = &mocks.EVMQueryClient{}
func RegisterTraceTransactionWithPredecessors(queryClient *mocks.EVMQueryClient, msgEthTx *evmtypes.MsgHandleTx, predecessors []*evmtypes.MsgHandleTx) {
data := []byte{0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x20, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x7d}
queryClient.On("TraceTx", rpc.ContextWithHeight(1),
&evmtypes.QueryTraceTxRequest{Msg: msgEthTx, BlockNumber: 1, Predecessors: predecessors, ChainId: 9000}).
&evmtypes.QueryTraceTxRequest{Msg: msgEthTx, BlockNumber: 1, Predecessors: predecessors, ChainId: 1291}).
Return(&evmtypes.QueryTraceTxResponse{Data: data}, nil)
}

func RegisterTraceTransaction(queryClient *mocks.EVMQueryClient, msgEthTx *evmtypes.MsgHandleTx) {
data := []byte{0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x20, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x7d}
queryClient.On("TraceTx", rpc.ContextWithHeight(1), &evmtypes.QueryTraceTxRequest{Msg: msgEthTx, BlockNumber: 1, ChainId: 9000}).
queryClient.On("TraceTx", rpc.ContextWithHeight(1), &evmtypes.QueryTraceTxRequest{Msg: msgEthTx, BlockNumber: 1, ChainId: 1291}).
Return(&evmtypes.QueryTraceTxResponse{Data: data}, nil)
}

func RegisterTraceTransactionError(queryClient *mocks.EVMQueryClient, msgEthTx *evmtypes.MsgHandleTx) {
queryClient.On("TraceTx", rpc.ContextWithHeight(1), &evmtypes.QueryTraceTxRequest{Msg: msgEthTx, BlockNumber: 1, ChainId: 9000}).
queryClient.On("TraceTx", rpc.ContextWithHeight(1), &evmtypes.QueryTraceTxRequest{Msg: msgEthTx, BlockNumber: 1, ChainId: 1291}).
Return(nil, errortypes.ErrInvalidRequest)
}

// TraceBlock
func RegisterTraceBlock(queryClient *mocks.EVMQueryClient, txs []*evmtypes.MsgHandleTx) {
data := []byte{0x7b, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x3a, 0x20, 0x22, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x22, 0x7d}
queryClient.On("TraceBlock", rpc.ContextWithHeight(1),
&evmtypes.QueryTraceBlockRequest{Txs: txs, BlockNumber: 1, TraceConfig: &evmtypes.TraceConfig{}, ChainId: 9000}).
&evmtypes.QueryTraceBlockRequest{Txs: txs, BlockNumber: 1, TraceConfig: &evmtypes.TraceConfig{}, ChainId: 1291}).
Return(&evmtypes.QueryTraceBlockResponse{Data: data}, nil)
}

Expand Down
Loading

0 comments on commit 2a3a818

Please sign in to comment.