Skip to content

Commit

Permalink
some test fixes regarding default example chain params
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Sep 23, 2024
1 parent d73bd42 commit f8f29e5
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 38 deletions.
2 changes: 1 addition & 1 deletion x/erc20/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (suite *KeeperTestSuite) TestTokenPair() {
func (suite *KeeperTestSuite) TestQueryParams() {
suite.SetupTest()
ctx := suite.network.GetContext()
expParams := types.DefaultParams()
expParams := exampleapp.NewErc20GenesisState().Params

res, err := suite.queryClient.Params(ctx, &types.QueryParamsRequest{})
suite.Require().NoError(err)
Expand Down
13 changes: 8 additions & 5 deletions x/erc20/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package keeper_test

import (
"reflect"

sdk "github.com/cosmos/cosmos-sdk/types"
testconstants "github.com/evmos/os/testutil/constants"
"github.com/evmos/os/x/erc20/types"
)

Expand All @@ -19,7 +18,12 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - Checks if the default params are set correctly",
func() interface{} {
return types.DefaultParams()
erc20Params := types.DefaultParams()
// NOTE: we need to add the example token pair address which is not in the default params but in the genesis state
// of the test suite app and therefore is returned by the query client.
erc20Params.NativePrecompiles = append(erc20Params.NativePrecompiles, testconstants.WEVMOSContractMainnet)

return erc20Params
},
func() interface{} {
return suite.network.App.Erc20Keeper.GetParams(ctx)
Expand Down Expand Up @@ -61,8 +65,7 @@ func (suite *KeeperTestSuite) TestParams() {
suite.SetupTest()
ctx = suite.network.GetContext()

outcome := reflect.DeepEqual(tc.paramsFun(), tc.getFun())
suite.Require().Equal(tc.expected, outcome)
suite.Require().Equal(tc.paramsFun(), tc.getFun())
})
}
}
18 changes: 11 additions & 7 deletions x/erc20/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"slices"
"testing"

testconstants "github.com/evmos/os/testutil/constants"

"github.com/ethereum/go-ethereum/common"
exampleapp "github.com/evmos/os/example_chain"
testconstants "github.com/evmos/os/testutil/constants"
"github.com/evmos/os/x/erc20/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -161,7 +161,7 @@ func (suite *ParamsTestSuite) TestIsNativePrecompile() {
}{
{
"default",
types.DefaultParams,
func() types.Params { return exampleapp.NewErc20GenesisState().Params },
common.HexToAddress(testconstants.WEVMOSContractMainnet),
true,
},
Expand Down Expand Up @@ -190,8 +190,10 @@ func (suite *ParamsTestSuite) TestIsNativePrecompile() {
}

for _, tc := range testCases {
p := tc.malleate()
suite.Require().Equal(tc.expRes, p.IsNativePrecompile(tc.addr), tc.name)
suite.Run(tc.name, func() {
p := tc.malleate()
suite.Require().Equal(tc.expRes, p.IsNativePrecompile(tc.addr), tc.name)
})
}
}

Expand Down Expand Up @@ -233,8 +235,10 @@ func (suite *ParamsTestSuite) TestIsDynamicPrecompile() {
}

for _, tc := range testCases {
p := tc.malleate()
suite.Require().Equal(tc.expRes, p.IsDynamicPrecompile(tc.addr), tc.name)
suite.Run(tc.name, func() {
p := tc.malleate()
suite.Require().Equal(tc.expRes, p.IsDynamicPrecompile(tc.addr), tc.name)
})
}
}

Expand Down
1 change: 1 addition & 0 deletions x/evm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ func (suite *KeeperTestSuite) TestQueryTxLogs() {
func (suite *KeeperTestSuite) TestQueryParams() {
ctx := suite.network.GetContext()
expParams := types.DefaultParamsWithEVMDenom(testconstants.ExampleAttoDenom)
expParams.ActiveStaticPrecompiles = types.AvailableStaticPrecompiles

res, err := suite.network.GetEvmClient().Params(ctx, &types.QueryParamsRequest{})
suite.Require().NoError(err)
Expand Down
18 changes: 11 additions & 7 deletions x/evm/keeper/params_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package keeper_test

import (
testconstants "github.com/evmos/os/testutil/constants"
"reflect"

exampleapp "github.com/evmos/os/example_chain"
"github.com/evmos/os/x/evm/types"
)

func (suite *KeeperTestSuite) TestParams() {
params := types.DefaultParamsWithEVMDenom(testconstants.ExampleAttoDenom)
defaultChainEVMParams := exampleapp.NewEVMGenesisState().Params
defaultChainEVMParams.ActiveStaticPrecompiles = types.AvailableStaticPrecompiles

testCases := []struct {
name string
Expand All @@ -19,7 +18,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - Checks if the default params are set correctly",
func() interface{} {
return types.DefaultParamsWithEVMDenom(testconstants.ExampleAttoDenom)
return defaultChainEVMParams
},
func() interface{} {
return suite.network.App.EVMKeeper.GetParams(suite.network.GetContext())
Expand All @@ -29,6 +28,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - EvmDenom param is set to \"inj\" and can be retrieved correctly",
func() interface{} {
params := defaultChainEVMParams
params.EvmDenom = "inj"
err := suite.network.App.EVMKeeper.SetParams(suite.network.GetContext(), params)
suite.Require().NoError(err)
Expand All @@ -43,6 +43,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - Check Access Control Create param is set to restricted and can be retrieved correctly",
func() interface{} {
params := defaultChainEVMParams
params.AccessControl = types.AccessControl{
Create: types.AccessControlType{
AccessType: types.AccessTypeRestricted,
Expand All @@ -61,6 +62,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - Check Access control param is set to restricted and can be retrieved correctly",
func() interface{} {
params := defaultChainEVMParams
params.AccessControl = types.AccessControl{
Call: types.AccessControlType{
AccessType: types.AccessTypeRestricted,
Expand All @@ -79,6 +81,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - Check AllowUnprotectedTxs param is set to false and can be retrieved correctly",
func() interface{} {
params := defaultChainEVMParams
params.AllowUnprotectedTxs = false
err := suite.network.App.EVMKeeper.SetParams(suite.network.GetContext(), params)
suite.Require().NoError(err)
Expand All @@ -93,6 +96,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
"success - Check ChainConfig param is set to the default value and can be retrieved correctly",
func() interface{} {
params := defaultChainEVMParams
params.ChainConfig = types.DefaultChainConfig()
err := suite.network.App.EVMKeeper.SetParams(suite.network.GetContext(), params)
suite.Require().NoError(err)
Expand All @@ -107,6 +111,7 @@ func (suite *KeeperTestSuite) TestParams() {
{
name: "success - Active precompiles are sorted when setting params",
paramsFun: func() interface{} {
params := defaultChainEVMParams
params.ActiveStaticPrecompiles = []string{
"0x0000000000000000000000000000000000000801",
"0x0000000000000000000000000000000000000800",
Expand All @@ -131,8 +136,7 @@ func (suite *KeeperTestSuite) TestParams() {
suite.Run(tc.name, func() {
suite.SetupTest()

outcome := reflect.DeepEqual(tc.paramsFun(), tc.getFun())
suite.Require().Equal(tc.expected, outcome)
suite.Require().Equal(tc.paramsFun(), tc.getFun(), "expected different params")
})
}
}
1 change: 1 addition & 0 deletions x/evm/keeper/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (suite *KeeperTestSuite) SetupTest() {
if !s.enableLondonHF {
evmGenesis := evmtypes.DefaultGenesisState()
maxInt := sdkmath.NewInt(math.MaxInt64)
evmGenesis.Params.EvmDenom = testconstants.ExampleAttoDenom
evmGenesis.Params.ChainConfig.LondonBlock = &maxInt
evmGenesis.Params.ChainConfig.ArrowGlacierBlock = &maxInt
evmGenesis.Params.ChainConfig.GrayGlacierBlock = &maxInt
Expand Down
10 changes: 7 additions & 3 deletions x/evm/keeper/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ethereum/go-ethereum/core"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
exampleapp "github.com/evmos/os/example_chain"
testconstants "github.com/evmos/os/testutil/constants"
"github.com/evmos/os/testutil/integration/os/factory"
"github.com/evmos/os/testutil/integration/os/grpc"
Expand Down Expand Up @@ -536,6 +537,9 @@ func (suite *KeeperTestSuite) TestResetGasMeterAndConsumeGas() {

func (suite *KeeperTestSuite) TestEVMConfig() {
suite.SetupTest()

defaultChainEVMParams := exampleapp.NewEVMGenesisState().Params

proposerAddress := suite.network.GetContext().BlockHeader().ProposerAddress
eip155ChainID := suite.network.GetEIP155ChainID()
cfg, err := suite.network.App.EVMKeeper.EVMConfig(
Expand All @@ -544,17 +548,17 @@ func (suite *KeeperTestSuite) TestEVMConfig() {
eip155ChainID,
)
suite.Require().NoError(err)
suite.Require().Equal(types.DefaultParamsWithEVMDenom(testconstants.ExampleAttoDenom), cfg.Params)
suite.Require().Equal(defaultChainEVMParams, cfg.Params)
// london hardfork is enabled by default
suite.Require().Equal(big.NewInt(0), cfg.BaseFee)
suite.Require().Equal(types.DefaultParamsWithEVMDenom(testconstants.ExampleAttoDenom).ChainConfig.EthereumConfig(big.NewInt(9001)), cfg.ChainConfig)
suite.Require().Equal(defaultChainEVMParams.ChainConfig.EthereumConfig(big.NewInt(testconstants.ExampleEIP155ChainID)), cfg.ChainConfig)

validators := suite.network.GetValidators()
proposerHextAddress := utils.ValidatorConsAddressToHex(validators[0].OperatorAddress)
suite.Require().Equal(proposerHextAddress, cfg.CoinBase)

networkChainID := suite.network.GetEIP155ChainID()
networkConfig := types.DefaultParamsWithEVMDenom(testconstants.ExampleAttoDenom).ChainConfig.EthereumConfig(networkChainID)
networkConfig := defaultChainEVMParams.ChainConfig.EthereumConfig(networkChainID)
suite.Require().Equal(networkConfig, cfg.ChainConfig)
}

Expand Down
6 changes: 2 additions & 4 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/evmos/os/contracts"
testconstants "github.com/evmos/os/testutil/constants"
testfactory "github.com/evmos/os/testutil/integration/os/factory"
testhandler "github.com/evmos/os/testutil/integration/os/grpc"
testkeyring "github.com/evmos/os/testutil/integration/os/keyring"
Expand Down Expand Up @@ -691,8 +691,6 @@ func (suite *KeeperTestSuite) CreateTestTx(msg *types.MsgEthereumTx, priv crypto
clientCtx := client.Context{}.WithTxConfig(suite.network.App.GetTxConfig())
ethSigner := ethtypes.LatestSignerForChainID(suite.network.App.EVMKeeper.ChainID())

println("got chain id", suite.network.App.EVMKeeper.ChainID())

txBuilder := clientCtx.TxConfig.NewTxBuilder()
builder, ok := txBuilder.(authtx.ExtensionOptionsTxBuilder)
suite.Require().True(ok)
Expand Down Expand Up @@ -740,7 +738,7 @@ func (suite *KeeperTestSuite) TestAddLog() {
msg2.From = addr.Hex()

ethTx3Params := &types.EvmTxArgs{
ChainID: big.NewInt(9001),
ChainID: big.NewInt(testconstants.ExampleEIP155ChainID),
Nonce: 0,
To: &toAddr,
Amount: big.NewInt(1),
Expand Down
22 changes: 22 additions & 0 deletions x/evm/statedb/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to deploy ERC-20 contract")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

// Mint some tokens to the deployer.
_, err = factory.ExecuteContractCall(
deployer.Priv,
Expand All @@ -119,6 +121,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to mint tokens")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

// Check the balance of the deployer on the ERC20 contract.
res, err := factory.ExecuteContractCall(
deployer.Priv,
Expand All @@ -133,6 +137,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to get balance")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

ethRes, err := evmtypes.DecodeTxResponse(res.Data)
Expect(err).ToNot(HaveOccurred(), "failed to decode balance of tx response")

Expand All @@ -156,6 +162,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to deploy flash loan contract")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

// Approve the flash loan contract to spend tokens. This is required because
// the contract will get funds from the caller to perform actions.
_, err = factory.ExecuteContractCall(
Expand All @@ -171,6 +179,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to approve flash loan contract")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

// Check the allowance.
res, err = factory.ExecuteContractCall(
deployer.Priv,
Expand All @@ -185,6 +195,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to get allowance")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

ethRes, err = evmtypes.DecodeTxResponse(res.Data)
Expect(err).ToNot(HaveOccurred(), "failed to decode allowance tx response")

Expand Down Expand Up @@ -215,6 +227,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to approve flash loan contract")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

// Check the allowance.
res, err = factory.ExecuteContractCall(
deployer.Priv,
Expand All @@ -229,6 +243,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to get allowance")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

ethRes, err = evmtypes.DecodeTxResponse(res.Data)
Expect(err).ToNot(HaveOccurred(), "failed to decode allowance tx response")

Expand Down Expand Up @@ -256,6 +272,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to execute flash loan")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

delRes, err := handler.GetDelegation(deployer.AccAddr.String(), validatorToDelegateTo)
Expect(err).ToNot(HaveOccurred(), "failed to get delegation")
delAmtPost := delRes.DelegationResponse.Balance.Amount
Expand All @@ -282,6 +300,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to get balance")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

ethRes, err := evmtypes.DecodeTxResponse(res.Data)
Expect(err).ToNot(HaveOccurred(), "failed to decode balance of tx response")

Expand Down Expand Up @@ -309,6 +329,8 @@ var _ = Describe("testing the flash loan exploit", Ordered, func() {
)
Expect(err).ToNot(HaveOccurred(), "failed to get balance")

Expect(network.NextBlock()).ToNot(HaveOccurred(), "failed to commit block")

ethRes, err = evmtypes.DecodeTxResponse(res.Data)
Expect(err).ToNot(HaveOccurred(), "failed to decode balance of tx response")

Expand Down
Loading

0 comments on commit f8f29e5

Please sign in to comment.