From ca63777014f4bc893c60fab03136a9c21dc300ca Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Mon, 10 Feb 2025 11:15:57 +0700 Subject: [PATCH] clean up example chain --- example_chain/activators.go | 2 +- example_chain/ante/ante.go | 55 --- example_chain/ante/cosmos_handler.go | 41 -- example_chain/ante/evm_benchmark_test.go | 157 ------- example_chain/ante/evm_handler.go | 21 - example_chain/ante/handler_options.go | 66 --- example_chain/ante/handler_options_test.go | 146 ------ example_chain/ante/integration_test.go | 173 ------- example_chain/app.go | 2 +- example_chain/eips/README.md | 258 ---------- example_chain/eips/eips.go | 36 -- example_chain/eips/eips_test.go | 441 ------------------ example_chain/eips/testdata/Counter.json | 38 -- example_chain/eips/testdata/Counter.sol | 15 - .../eips/testdata/CounterFactory.json | 56 --- .../eips/testdata/CounterFactory.sol | 25 - example_chain/eips/testdata/contracts.go | 17 - example_chain/osd/cmd/root.go | 415 ---------------- example_chain/osd/config/config.go | 42 -- example_chain/osd/main.go | 31 -- example_chain/test_helpers.go | 2 +- example_chain/testutil/abci.go | 280 ----------- example_chain/testutil/contract.go | 170 ------- example_chain/testutil/eth_setup.go | 205 -------- example_chain/testutil/fund.go | 43 -- example_chain/testutil/gas.go | 18 - example_chain/testutil/integration.go | 81 ---- testutil/integration/os/network/network.go | 2 +- 28 files changed, 4 insertions(+), 2834 deletions(-) delete mode 100644 example_chain/ante/ante.go delete mode 100644 example_chain/ante/cosmos_handler.go delete mode 100644 example_chain/ante/evm_benchmark_test.go delete mode 100644 example_chain/ante/evm_handler.go delete mode 100644 example_chain/ante/handler_options.go delete mode 100644 example_chain/ante/handler_options_test.go delete mode 100644 example_chain/ante/integration_test.go delete mode 100644 example_chain/eips/README.md delete mode 100644 example_chain/eips/eips.go delete mode 100644 example_chain/eips/eips_test.go delete mode 100644 example_chain/eips/testdata/Counter.json delete mode 100644 example_chain/eips/testdata/Counter.sol delete mode 100644 example_chain/eips/testdata/CounterFactory.json delete mode 100644 example_chain/eips/testdata/CounterFactory.sol delete mode 100644 example_chain/eips/testdata/contracts.go delete mode 100644 example_chain/osd/cmd/root.go delete mode 100644 example_chain/osd/config/config.go delete mode 100644 example_chain/osd/main.go delete mode 100644 example_chain/testutil/abci.go delete mode 100644 example_chain/testutil/contract.go delete mode 100644 example_chain/testutil/eth_setup.go delete mode 100644 example_chain/testutil/fund.go delete mode 100644 example_chain/testutil/gas.go delete mode 100644 example_chain/testutil/integration.go diff --git a/example_chain/activators.go b/example_chain/activators.go index 265e84a5..fbcff013 100644 --- a/example_chain/activators.go +++ b/example_chain/activators.go @@ -4,7 +4,7 @@ package example_chain import ( "github.com/evmos/os/x/evm/core/vm" - "github.com/realiotech/realio-network/example_chain/eips" + "github.com/evmos/os/example_chain/eips" ) // evmosActivators defines a map of opcode modifiers associated diff --git a/example_chain/ante/ante.go b/example_chain/ante/ante.go deleted file mode 100644 index 059dfcea..00000000 --- a/example_chain/ante/ante.go +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package ante - -import ( - errorsmod "cosmossdk.io/errors" - sdk "github.com/cosmos/cosmos-sdk/types" - errortypes "github.com/cosmos/cosmos-sdk/types/errors" - authante "github.com/cosmos/cosmos-sdk/x/auth/ante" -) - -// NewAnteHandler returns an ante handler responsible for attempting to route an -// Ethereum or SDK transaction to an internal ante handler for performing -// transaction-level processing (e.g. fee payment, signature verification) before -// being passed onto it's respective handler. -func NewAnteHandler(options HandlerOptions) sdk.AnteHandler { - return func( - ctx sdk.Context, tx sdk.Tx, sim bool, - ) (newCtx sdk.Context, err error) { - var anteHandler sdk.AnteHandler - - txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx) - if ok { - opts := txWithExtensions.GetExtensionOptions() - if len(opts) > 0 { - switch typeURL := opts[0].GetTypeUrl(); typeURL { - case "/os.evm.v1.ExtensionOptionsEthereumTx": - // handle as *evmtypes.MsgEthereumTx - anteHandler = newMonoEVMAnteHandler(options) - case "/os.types.v1.ExtensionOptionDynamicFeeTx": - // cosmos-sdk tx with dynamic fee extension - anteHandler = newCosmosAnteHandler(options) - default: - return ctx, errorsmod.Wrapf( - errortypes.ErrUnknownExtensionOptions, - "rejecting tx with unsupported extension option: %s", typeURL, - ) - } - - return anteHandler(ctx, tx, sim) - } - } - - // handle as totally normal Cosmos SDK tx - switch tx.(type) { - case sdk.Tx: - anteHandler = newCosmosAnteHandler(options) - default: - return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid transaction type: %T", tx) - } - - return anteHandler(ctx, tx, sim) - } -} diff --git a/example_chain/ante/cosmos_handler.go b/example_chain/ante/cosmos_handler.go deleted file mode 100644 index 0f934754..00000000 --- a/example_chain/ante/cosmos_handler.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package ante - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/ante" - sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" - ibcante "github.com/cosmos/ibc-go/v8/modules/core/ante" - evmoscosmosante "github.com/evmos/os/ante/cosmos" - evmante "github.com/evmos/os/ante/evm" - evmtypes "github.com/evmos/os/x/evm/types" -) - -// newCosmosAnteHandler creates the default ante handler for Cosmos transactions -func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler { - return sdk.ChainAnteDecorators( - evmoscosmosante.NewRejectMessagesDecorator(), // reject MsgEthereumTxs - evmoscosmosante.NewAuthzLimiterDecorator( // disable the Msg types that cannot be included on an authz.MsgExec msgs field - sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}), - sdk.MsgTypeURL(&sdkvesting.MsgCreateVestingAccount{}), - ), - ante.NewSetUpContextDecorator(), - ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker), - ante.NewValidateBasicDecorator(), - ante.NewTxTimeoutHeightDecorator(), - ante.NewValidateMemoDecorator(options.AccountKeeper), - evmoscosmosante.NewMinGasPriceDecorator(options.FeeMarketKeeper, options.EvmKeeper), - ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), - ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker), - // SetPubKeyDecorator must be called before all signature verification decorators - ante.NewSetPubKeyDecorator(options.AccountKeeper), - ante.NewValidateSigCountDecorator(options.AccountKeeper), - ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer), - ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler), - ante.NewIncrementSequenceDecorator(options.AccountKeeper), - ibcante.NewRedundantRelayDecorator(options.IBCKeeper), - evmante.NewGasWantedDecorator(options.EvmKeeper, options.FeeMarketKeeper), - ) -} diff --git a/example_chain/ante/evm_benchmark_test.go b/example_chain/ante/evm_benchmark_test.go deleted file mode 100644 index d603d38c..00000000 --- a/example_chain/ante/evm_benchmark_test.go +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) -package ante_test - -import ( - "fmt" - "math/big" - "testing" - - "cosmossdk.io/errors" - "cosmossdk.io/math" - sdktypes "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/evmos/os/ante" - ethante "github.com/evmos/os/ante/evm" - cmmnfactory "github.com/evmos/os/testutil/integration/common/factory" - "github.com/evmos/os/testutil/integration/os/factory" - "github.com/evmos/os/testutil/integration/os/grpc" - testkeyring "github.com/evmos/os/testutil/integration/os/keyring" - evmostypes "github.com/evmos/os/types" - evmtypes "github.com/evmos/os/x/evm/types" - chainante "github.com/realiotech/realio-network/example_chain/ante" - "github.com/realiotech/realio-network/testutil/integration/os/network" -) - -type benchmarkSuite struct { - network *network.UnitTestNetwork - grpcHandler grpc.Handler - txFactory factory.TxFactory - keyring testkeyring.Keyring -} - -// Setup -var table = []struct { - name string - txType string - simulate bool -}{ - { - "evm_transfer_sim", - "evm_transfer", - true, - }, - { - "evm_transfer", - "evm_transfer", - false, - }, - { - "bank_msg_send_sim", - "bank_msg_send", - true, - }, - { - "bank_msg_send", - "bank_msg_send", - false, - }, -} - -func BenchmarkAnteHandler(b *testing.B) { - keyring := testkeyring.New(2) - - for _, v := range table { - // Reset chain on every tx type to have a clean state - // and a fair benchmark - b.StopTimer() - unitNetwork := network.NewUnitTestNetwork( - network.WithPreFundedAccounts(keyring.GetAllAccAddrs()...), - ) - grpcHandler := grpc.NewIntegrationHandler(unitNetwork) - txFactory := factory.New(unitNetwork, grpcHandler) - suite := benchmarkSuite{ - network: unitNetwork, - grpcHandler: grpcHandler, - txFactory: txFactory, - keyring: keyring, - } - - handlerOptions := suite.generateHandlerOptions() - ante := chainante.NewAnteHandler(handlerOptions) - b.StartTimer() - - b.Run(fmt.Sprintf("tx_type_%v", v.name), func(b *testing.B) { - for i := 0; i < b.N; i++ { - // Stop timer while building the tx setup - b.StopTimer() - // Start with a clean block - if err := unitNetwork.NextBlock(); err != nil { - b.Fatal(errors.Wrap(err, "failed to create block")) - } - ctx := unitNetwork.GetContext() - - // Generate fresh tx type - tx, err := suite.generateTxType(v.txType) - if err != nil { - b.Fatal(errors.Wrap(err, "failed to generate tx type")) - } - b.StartTimer() - - // Run benchmark - _, err = ante(ctx, tx, v.simulate) - if err != nil { - b.Fatal(errors.Wrap(err, "failed to run ante handler")) - } - } - }) - } -} - -func (s *benchmarkSuite) generateTxType(txType string) (sdktypes.Tx, error) { - switch txType { - case "evm_transfer": - senderPriv := s.keyring.GetPrivKey(0) - receiver := s.keyring.GetKey(1) - txArgs := evmtypes.EvmTxArgs{ - To: &receiver.Addr, - Amount: big.NewInt(1000), - } - return s.txFactory.GenerateSignedEthTx(senderPriv, txArgs) - case "bank_msg_send": - sender := s.keyring.GetKey(1) - receiver := s.keyring.GetAccAddr(0) - bankmsg := banktypes.NewMsgSend( - sender.AccAddr, - receiver, - sdktypes.NewCoins( - sdktypes.NewCoin( - s.network.GetBaseDenom(), - math.NewInt(1000), - ), - ), - ) - txArgs := cmmnfactory.CosmosTxArgs{Msgs: []sdktypes.Msg{bankmsg}} - return s.txFactory.BuildCosmosTx(sender.Priv, txArgs) - default: - return nil, fmt.Errorf("invalid tx type") - } -} - -func (s *benchmarkSuite) generateHandlerOptions() chainante.HandlerOptions { - encCfg := s.network.GetEncodingConfig() - return chainante.HandlerOptions{ - Cdc: s.network.App.AppCodec(), - AccountKeeper: s.network.App.AccountKeeper, - BankKeeper: s.network.App.BankKeeper, - ExtensionOptionChecker: evmostypes.HasDynamicFeeExtensionOption, - EvmKeeper: s.network.App.EVMKeeper, - FeegrantKeeper: s.network.App.FeeGrantKeeper, - IBCKeeper: s.network.App.IBCKeeper, - FeeMarketKeeper: s.network.App.FeeMarketKeeper, - SignModeHandler: encCfg.TxConfig.SignModeHandler(), - SigGasConsumer: ante.SigVerificationGasConsumer, - MaxTxGasWanted: 1_000_000_000, - TxFeeChecker: ethante.NewDynamicFeeChecker(s.network.App.FeeMarketKeeper), - } -} diff --git a/example_chain/ante/evm_handler.go b/example_chain/ante/evm_handler.go deleted file mode 100644 index fdb7e54c..00000000 --- a/example_chain/ante/evm_handler.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package ante - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - evmante "github.com/evmos/os/ante/evm" -) - -// newMonoEVMAnteHandler creates the sdk.AnteHandler implementation for the EVM transactions. -func newMonoEVMAnteHandler(options HandlerOptions) sdk.AnteHandler { - return sdk.ChainAnteDecorators( - evmante.NewEVMMonoDecorator( - options.AccountKeeper, - options.FeeMarketKeeper, - options.EvmKeeper, - options.MaxTxGasWanted, - ), - ) -} diff --git a/example_chain/ante/handler_options.go b/example_chain/ante/handler_options.go deleted file mode 100644 index 8dcaa065..00000000 --- a/example_chain/ante/handler_options.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package ante - -import ( - errorsmod "cosmossdk.io/errors" - storetypes "cosmossdk.io/store/types" - txsigning "cosmossdk.io/x/tx/signing" - "github.com/cosmos/cosmos-sdk/codec" - errortypes "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - "github.com/cosmos/cosmos-sdk/x/auth/ante" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper" - anteinterfaces "github.com/evmos/os/ante/interfaces" -) - -// HandlerOptions defines the list of module keepers required to run the Evmos -// AnteHandler decorators. -type HandlerOptions struct { - Cdc codec.BinaryCodec - AccountKeeper anteinterfaces.AccountKeeper - BankKeeper anteinterfaces.BankKeeper - IBCKeeper *ibckeeper.Keeper - FeeMarketKeeper anteinterfaces.FeeMarketKeeper - EvmKeeper anteinterfaces.EVMKeeper - FeegrantKeeper ante.FeegrantKeeper - ExtensionOptionChecker ante.ExtensionOptionChecker - SignModeHandler *txsigning.HandlerMap - SigGasConsumer func(meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params) error - MaxTxGasWanted uint64 - TxFeeChecker ante.TxFeeChecker -} - -// Validate checks if the keepers are defined -func (options HandlerOptions) Validate() error { - if options.Cdc == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "codec is required for AnteHandler") - } - if options.AccountKeeper == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "account keeper is required for AnteHandler") - } - if options.BankKeeper == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "bank keeper is required for AnteHandler") - } - if options.IBCKeeper == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "ibc keeper is required for AnteHandler") - } - if options.FeeMarketKeeper == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "fee market keeper is required for AnteHandler") - } - if options.EvmKeeper == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "evm keeper is required for AnteHandler") - } - if options.SigGasConsumer == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "signature gas consumer is required for AnteHandler") - } - if options.SignModeHandler == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "sign mode handler is required for AnteHandler") - } - if options.TxFeeChecker == nil { - return errorsmod.Wrap(errortypes.ErrLogic, "tx fee checker is required for AnteHandler") - } - return nil -} diff --git a/example_chain/ante/handler_options_test.go b/example_chain/ante/handler_options_test.go deleted file mode 100644 index ee0cb9a6..00000000 --- a/example_chain/ante/handler_options_test.go +++ /dev/null @@ -1,146 +0,0 @@ -package ante_test - -import ( - "testing" - - "github.com/evmos/os/ante" - ethante "github.com/evmos/os/ante/evm" - "github.com/evmos/os/types" - chainante "github.com/realiotech/realio-network/example_chain/ante" - "github.com/realiotech/realio-network/testutil/integration/os/network" - "github.com/stretchr/testify/require" -) - -func TestValidateHandlerOptions(t *testing.T) { - nw := network.NewUnitTestNetwork() - cases := []struct { - name string - options chainante.HandlerOptions - expPass bool - }{ - { - "fail - empty options", - chainante.HandlerOptions{}, - false, - }, - { - "fail - empty account keeper", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nil, - }, - false, - }, - { - "fail - empty bank keeper", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nil, - }, - false, - }, - { - "fail - empty IBC keeper", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - IBCKeeper: nil, - }, - false, - }, - { - "fail - empty fee market keeper", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - IBCKeeper: nw.App.IBCKeeper, - FeeMarketKeeper: nil, - }, - false, - }, - { - "fail - empty EVM keeper", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - IBCKeeper: nw.App.IBCKeeper, - FeeMarketKeeper: nw.App.FeeMarketKeeper, - EvmKeeper: nil, - }, - false, - }, - { - "fail - empty signature gas consumer", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - IBCKeeper: nw.App.IBCKeeper, - FeeMarketKeeper: nw.App.FeeMarketKeeper, - EvmKeeper: nw.App.EVMKeeper, - SigGasConsumer: nil, - }, - false, - }, - { - "fail - empty signature mode handler", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - IBCKeeper: nw.App.IBCKeeper, - FeeMarketKeeper: nw.App.FeeMarketKeeper, - EvmKeeper: nw.App.EVMKeeper, - SigGasConsumer: ante.SigVerificationGasConsumer, - SignModeHandler: nil, - }, - false, - }, - { - "fail - empty tx fee checker", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - IBCKeeper: nw.App.IBCKeeper, - FeeMarketKeeper: nw.App.FeeMarketKeeper, - EvmKeeper: nw.App.EVMKeeper, - SigGasConsumer: ante.SigVerificationGasConsumer, - SignModeHandler: nw.App.GetTxConfig().SignModeHandler(), - TxFeeChecker: nil, - }, - false, - }, - { - "success - default app options", - chainante.HandlerOptions{ - Cdc: nw.App.AppCodec(), - AccountKeeper: nw.App.AccountKeeper, - BankKeeper: nw.App.BankKeeper, - ExtensionOptionChecker: types.HasDynamicFeeExtensionOption, - EvmKeeper: nw.App.EVMKeeper, - FeegrantKeeper: nw.App.FeeGrantKeeper, - IBCKeeper: nw.App.IBCKeeper, - FeeMarketKeeper: nw.App.FeeMarketKeeper, - SignModeHandler: nw.GetEncodingConfig().TxConfig.SignModeHandler(), - SigGasConsumer: ante.SigVerificationGasConsumer, - MaxTxGasWanted: 40000000, - TxFeeChecker: ethante.NewDynamicFeeChecker(nw.App.FeeMarketKeeper), - }, - true, - }, - } - - for _, tc := range cases { - err := tc.options.Validate() - if tc.expPass { - require.NoError(t, err, tc.name) - } else { - require.Error(t, err, tc.name) - } - } -} diff --git a/example_chain/ante/integration_test.go b/example_chain/ante/integration_test.go deleted file mode 100644 index ed5240c4..00000000 --- a/example_chain/ante/integration_test.go +++ /dev/null @@ -1,173 +0,0 @@ -package ante_test - -import ( - "cosmossdk.io/math" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - commonfactory "github.com/evmos/os/testutil/integration/common/factory" - "github.com/evmos/os/testutil/integration/os/factory" - "github.com/evmos/os/testutil/integration/os/grpc" - testkeyring "github.com/evmos/os/testutil/integration/os/keyring" - integrationutils "github.com/evmos/os/testutil/integration/os/utils" - "github.com/realiotech/realio-network/testutil/integration/os/network" - testutiltx "github.com/realiotech/realio-network/testutil/tx" - - //nolint:revive // dot imports are fine for Ginkgo - . "github.com/onsi/ginkgo/v2" - //nolint:revive // dot imports are fine for Ginkgo - . "github.com/onsi/gomega" -) - -type IntegrationTestSuite struct { - network network.Network - factory factory.TxFactory - grpcHandler grpc.Handler - keyring testkeyring.Keyring -} - -var _ = Describe("when sending a Cosmos transaction", Label("AnteHandler"), Ordered, func() { - var ( - s *IntegrationTestSuite - addr sdk.AccAddress - priv cryptotypes.PrivKey - msg sdk.Msg - ) - - BeforeAll(func() { - keyring := testkeyring.New(3) - - integrationNetwork := network.New( - network.WithPreFundedAccounts(keyring.GetAllAccAddrs()...), - ) - grpcHandler := grpc.NewIntegrationHandler(integrationNetwork) - txFactory := factory.New(integrationNetwork, grpcHandler) - s = &IntegrationTestSuite{ - network: integrationNetwork, - factory: txFactory, - grpcHandler: grpcHandler, - keyring: keyring, - } - }) - - Context("and the sender account has enough balance to pay for the transaction cost", Ordered, func() { - var ( - // rewards are the real accrued rewards - rewards sdk.DecCoins - // minExpRewards are the minimun rewards that should be accrued - // for the test case - minExpRewards = sdk.DecCoins{sdk.DecCoin{Amount: math.LegacyNewDec(1e5), Denom: s.network.GetBaseDenom()}} - delegationCoin = sdk.Coin{Amount: math.NewInt(1e15), Denom: s.network.GetBaseDenom()} - transferAmt = math.NewInt(1e14) - ) - - BeforeEach(func() { - key := s.keyring.GetKey(0) - addr = key.AccAddr - priv = key.Priv - - msg = &banktypes.MsgSend{ - FromAddress: addr.String(), - ToAddress: "evmos1dx67l23hz9l0k9hcher8xz04uj7wf3yu26l2yn", - Amount: sdk.Coins{sdk.Coin{Amount: transferAmt, Denom: s.network.GetBaseDenom()}}, - } - - valAddr := s.network.GetValidators()[0].OperatorAddress - err := s.factory.Delegate(priv, valAddr, delegationCoin) - Expect(err).To(BeNil()) - - rewards, err = integrationutils.WaitToAccrueRewards(s.network, s.grpcHandler, addr.String(), minExpRewards) - Expect(err).To(BeNil()) - }) - - It("should succeed & not withdraw any staking rewards", func() { - prevBalanceRes, err := s.grpcHandler.GetBalanceFromBank(addr, s.network.GetBaseDenom()) - Expect(err).To(BeNil()) - - baseFeeRes, err := s.grpcHandler.GetEvmBaseFee() - Expect(err).To(BeNil()) - Expect(baseFeeRes).ToNot(BeNil(), "baseFeeRes is nil") - - gasPrice := baseFeeRes.BaseFee - - res, err := s.factory.ExecuteCosmosTx( - priv, - commonfactory.CosmosTxArgs{ - Msgs: []sdk.Msg{msg}, - GasPrice: gasPrice, - }, - ) - Expect(err).To(BeNil()) - Expect(res.IsOK()).To(BeTrue()) - - // include the tx in a block to update state - err = s.network.NextBlock() - Expect(err).To(BeNil()) - - // fees should be deducted from balance - Expect(baseFeeRes.BaseFee).ToNot(BeNil(), "baseFeeRes.BaseFee is nil") - - feesAmt := math.NewInt(res.GasWanted).Mul(*baseFeeRes.BaseFee) - balanceRes, err := s.grpcHandler.GetBalanceFromBank(addr, s.network.GetBaseDenom()) - Expect(err).To(BeNil()) - Expect(balanceRes.Balance.Amount).To(Equal(prevBalanceRes.Balance.Amount.Sub(transferAmt).Sub(feesAmt))) - - rewardsRes, err := s.grpcHandler.GetDelegationTotalRewards(addr.String()) - Expect(err).To(BeNil()) - - // rewards should not be used. Should be more - // than the previous value queried - Expect(rewardsRes.Total.Sub(rewards).IsAllPositive()).To(BeTrue()) - }) - }) - - Context("and the sender account neither has enough balance nor sufficient staking rewards to pay for the transaction cost", func() { - BeforeEach(func() { - addr, priv = testutiltx.NewAccAddressAndKey() - - // this is a new address that does not exist on chain. - // Transfer 1 aevmos to this account so it is - // added on chain - err := s.factory.FundAccount( - s.keyring.GetKey(0), - addr, - sdk.Coins{ - sdk.Coin{ - Amount: math.NewInt(1), - Denom: s.network.GetBaseDenom(), - }, - }, - ) - Expect(err).To(BeNil()) - // persist the state changes - Expect(s.network.NextBlock()).To(BeNil()) - - msg = &banktypes.MsgSend{ - FromAddress: addr.String(), - ToAddress: "evmos1dx67l23hz9l0k9hcher8xz04uj7wf3yu26l2yn", - Amount: sdk.Coins{sdk.Coin{Amount: math.NewInt(1e14), Denom: s.network.GetBaseDenom()}}, - } - }) - - It("should fail", func() { - var gas uint64 = 200_000 // specify gas to avoid failing on simulation tx (internal call in the ExecuteCosmosTx if gas not specified) - res, err := s.factory.ExecuteCosmosTx( - priv, - commonfactory.CosmosTxArgs{ - Msgs: []sdk.Msg{msg}, - Gas: &gas, - }, - ) - Expect(res.IsErr()).To(BeTrue()) - Expect(res.GetLog()).To(ContainSubstring("insufficient funds")) - Expect(err).To(BeNil()) - Expect(s.network.NextBlock()).To(BeNil()) - }) - - It("should not withdraw any staking rewards", func() { - rewardsRes, err := s.grpcHandler.GetDelegationTotalRewards(addr.String()) - Expect(err).To(BeNil()) - Expect(rewardsRes.Total.Empty()).To(BeTrue()) - }) - }) -}) diff --git a/example_chain/app.go b/example_chain/app.go index 9691db6e..02828b4a 100644 --- a/example_chain/app.go +++ b/example_chain/app.go @@ -118,7 +118,7 @@ import ( "github.com/evmos/os/x/feemarket" feemarketkeeper "github.com/evmos/os/x/feemarket/keeper" feemarkettypes "github.com/evmos/os/x/feemarket/types" - chainante "github.com/realiotech/realio-network/example_chain/ante" + chainante "github.com/evmos/os/example_chain/ante" "github.com/spf13/cast" // NOTE: override ICS20 keeper to support IBC transfers of ERC20 tokens diff --git a/example_chain/eips/README.md b/example_chain/eips/README.md deleted file mode 100644 index 18c1f4ef..00000000 --- a/example_chain/eips/README.md +++ /dev/null @@ -1,258 +0,0 @@ -# Evmos Custom EIPs - -This document explain how **evmOS** allows chain built on top of it to define custom EIPs to modify the behavior of EVM -opcodes. - -## Custom EIPs - -Inside an EVM, every state transition or query is executed by evaluating opcodes. Custom EIPs are functions used to -change the behavior of these opcodes to tailor the EVM functionalities to fit the app-chain requirements. - -Custom EIPs should be defined in an `eips` package inside the `./app/eips/` folder of chains using the **evmOS** -framework. This organization of custom implementations is not a strict requirement, but is the suggested approach to -have a clean organization of functionalities. In this file, only the custom modifier should be defined. - -Inside this package, custom EIP should be defined in a file called `eips.go`. In this file, the EIPs modifier should be -defined with the signature: - -```go -func(jt *vm.JumpTable) {} -``` - -where `vm` is the package `"github.com/evmos/os/x/evm/core/vm"`. - -Custom EIPs are used to modify the behavior of opcodes, which are described by the `operation` structure: - -```go -type operation struct { - // execute is the operation function - execute executionFunc - constantGas uint64 - dynamicGas gasFunc - // minStack tells how many stack items are required - minStack int - // maxStack specifies the max length the stack can have for this operation - // to not overflow the stack. - maxStack int - - // memorySize returns the memory size required for the operation - memorySize memorySizeFunc -} -``` - -With the **evmOS** framework, it is possible to modify any of the fields defined in the type via the `operation` setter -methods: - -- `SetExecute`: update the execution logic for the opcode. - -- `SetConstantGas`: update the value used for the constant gas cost. - -- `SetDynamicGas`: update the function used to compute the dynamic gas cost. - -- `SetMinStack`: update the minimum number of items in the stack required to execute the `operation`. - -- `SetMaxStack`: update the maximum number of items that will be in the stack after executing the `operation`. - -- `SetMemorySize`: the memory size required by the `operation`. - -An example for an EIP which modifies the constant gas used for the `CREATE` opcode is reported below: - -```go -// Enable a custom EIP-0000 -func Enable0000(jt *vm.JumpTable) { - jt[vm.CREATE].SetConstantGas(1) -} -``` - -In the same folder should also be defined tests and contracts used to verify the EIPs logic. - -## Activate Custom EIPs - -The activation of custom EIPs should be done inside the `config.go` file defined in the `./app/` folder. This file has -the role of the single source for modify the EVM implementation which is defined in the -[`x/evm/`](https://github.com/evmos/os/tree/main/x/evm) folder -of **evmOS**. - -In this file, 3 main components should be defined: - -- The custom EIPs, also called activators. -- The additional default EIPs enabled. -- The EVM configurator instance. - -All these components will be described in the following sections. - -### Opcode & EIP Activators - -Activators is the name provided by [Go-ethereum](https://geth.ethereum.org/) to the definition of the structure -grouping all possible non-default EIPs: - -```go -var activators = map[int]func(*JumpTable){ - 3855: enable3855, - ... -} -``` - -It can be interpreted as a list of available functionalities that can be toggled to change opcodes behavior. The -structure is a map where the key is the EIP number in the octal representation, and the value is the custom EIP -function that has to be evaluated. - -In **evmOS**, custom activators should be defined in a structure with the same data type, like in the example below: - -```go -// Activate custom EIPs: 0000, 0001, 0002, etc -evmosActivators = map[int]func(*vm.JumpTable){ - "evmos_0": eips.Enable0000, - "evmos_1": eips.Enable0001, - "evmos_2": eips.Enable0002, -} -``` - -It should be noted that the value of each key in the example is the modifier defined in the `eips` package in the -example provided at the of the [Custom EIPs](#custom-eips) section. - -### Default EIPs - -Custom EIPs defined in the `activators` map are not enabled by default. This type is only used to define the list of -custom functionalities that can be activated. To specify which custom EIP activate, we should modify the -**evmOS** `x/evm` module params. The parameter orchestrating enabled custom EIPs is the `DefaultExtraEIPs` and -**evmOS** provide an easy and safe way to customize it. - -To specify which activator enable in the chain, a new variable containing a slice of keys of the custom activators -should be defined. An example is reported below: - -```go -evmosEnabledEIPs = []int64{ - "evmos_0", -} -``` - -In this way, even though the custom activators defined $3$ new EIPs, we are going to activate only the number `evmos_0` - -### EVM Configurator - -The EVM configuration is the type used to modify the EVM configuration before starting a node. The type is defined as: - -```go -type EVMConfigurator struct { - extendedEIPs map[int]func(*vm.JumpTable) - extendedDefaultExtraEIPs []int64 - sealed bool -} -``` - -Currently, only 2 customizations are possible: - -- `WithExtendedEips`: extended the default available EIPs. - -- `WithExtendedDefaultExtraEIPs`: extended the default active EIPs. - -It is important to notice that the configurator will only allow to append new entries to the default ones defined by -**evmOS**. The reason behind this choice is to ensure the correct and safe execution of the virtual machine but still -allowing partners to customize their implementation. - -The `EVMConfigurator` type should be constructed using the builder pattern inside the `init()` function of the file so -that it is run during the creation of the application. - -An example of the usage of the configurator is reported below: - -```go -configurator := evmconfig.NewEVMConfigurator(). - WithExtendedEips(customActivators). - WithExtendedDefaultExtraEIPs(defaultEnabledEIPs...). - Configure() - -err := configurator.Configure() -``` - -Errors are raised when the configurator tries to append an item with the same name of one of the default one. Since -this type is used to configure the EVM before starting the node, it is safe, and suggested, to panic: - -```go -if err != nil { - panic(err) -} -``` - -## Custom EIPs Deep Dive - -When the chain receives an EVM transaction, it is handled by the `MsgServer` of the `x/evm` within the method -`EthereumTx`. The method then calls `ApplyTransaction` where the EVM configuration is created: - -```go -cfg, err := k.EVMConfig(ctx, sdk.ConsAddress(ctx.BlockHeader().ProposerAddress), k.eip155ChainID) -``` - -During the creation of this type, a query is made to retrieve the `x/evm` params. After this step, the request is -passed inside the `ApplyMessageWithConfig` where a new instance of the EVM is created: - -```go -evm := k.NewEVM(ctx, msg, cfg, tracer, stateDB) -``` - -The `NewEVM` method calls the `NewEVMWithHooks` where a new instance of the virtual machine interpreter is created: - -```go -evm.interpreter = NewEVMInterpreter(evm, config) -``` - -The management of activators is handled in this function: - -```go -func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter { - // If jump table was not initialised we set the default one. - if cfg.JumpTable == nil { - cfg.JumpTable = DefaultJumpTable(evm.chainRules) - for i, eip := range cfg.ExtraEips { - // Deep-copy jumptable to prevent modification of opcodes in other tables - copy := CopyJumpTable(cfg.JumpTable) - if err := EnableEIP(eip, copy); err != nil { - // Disable it, so caller can check if it's activated or not - cfg.ExtraEips = append(cfg.ExtraEips[:i], cfg.ExtraEips[i+1:]...) - log.Error("EIP activation failed", "eip", eip, "error", err) - } - cfg.JumpTable = copy - } - } - - return &EVMInterpreter{ - evm: evm, - cfg: cfg, - } -} -``` - -As we can see, a new `JumpTable` is created if it is not received from previous evm executions in the same transaction. -After that, the function iterate over the `ExtraEips` defined in the configuration. Then, it is checked if the EIP is -associated with an activator. If yes, the activator function is execute, otherwise an error is returned and the EIP is -removed from the VM configuration. At this point, all the opcodes are ready to be executed. - -## How to Use It - -In previous sections has been described required structures and files to use the EVM configurator to enable custom -EIPs. In this the general procedure is taken into considerations. Two different scenarios are described: - -- New chain. - -- Running chain. - -### New Chain - -For a new chain starting from block genesis, the procedure described in the sections above is enough. To summarize it: - -- Create the eip file with custom activators. - -- Create the config file with custom activators, default EIPs, and the configurator. - -After starting the chain, the genesis validation will perform all the required checks and the chain will be ready using -the new custom EIPs. - -### Running Chain - -The proper approach to include and enable new EIPs, with the current state of the development, is via coordinate chain -upgrade. During the chain upgrade it is important to define the custom activators since they are not stored in the -chain. To enable them there are two possibilities: - -- Write a migration to add the new enabled EIPsm during the upgrade. - -- After the upgrade, create a governance proposal to modify the `x/evm` params. diff --git a/example_chain/eips/eips.go b/example_chain/eips/eips.go deleted file mode 100644 index 2c309414..00000000 --- a/example_chain/eips/eips.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package eips - -import ( - "github.com/evmos/os/x/evm/core/vm" -) - -var ( - Multiplier = uint64(10) - SstoreConstantGas = uint64(500) -) - -// enable0000 contains the logic to modify the CREATE and CREATE2 opcodes -// constant gas value. -func Enable0000(jt *vm.JumpTable) { - currentValCreate := jt[vm.CREATE].GetConstantGas() - jt[vm.CREATE].SetConstantGas(currentValCreate * Multiplier) - - currentValCreate2 := jt[vm.CREATE2].GetConstantGas() - jt[vm.CREATE2].SetConstantGas(currentValCreate2 * Multiplier) -} - -// enable0001 contains the logic to modify the CALL opcode -// constant gas value. -func Enable0001(jt *vm.JumpTable) { - currentVal := jt[vm.CALL].GetConstantGas() - jt[vm.CALL].SetConstantGas(currentVal * Multiplier) -} - -// enable0002 contains the logic to modify the SSTORE opcode -// constant gas value. -func Enable0002(jt *vm.JumpTable) { - jt[vm.SSTORE].SetConstantGas(SstoreConstantGas) -} diff --git a/example_chain/eips/eips_test.go b/example_chain/eips/eips_test.go deleted file mode 100644 index 80ad7357..00000000 --- a/example_chain/eips/eips_test.go +++ /dev/null @@ -1,441 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package eips_test - -import ( - "fmt" - "math/big" - "testing" - - "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/ethereum/go-ethereum/common" - "github.com/evmos/os/example_chain/eips" - "github.com/evmos/os/example_chain/eips/testdata" - "github.com/evmos/os/testutil/integration/os/factory" - "github.com/evmos/os/testutil/integration/os/grpc" - "github.com/evmos/os/testutil/integration/os/keyring" - integrationutils "github.com/evmos/os/testutil/integration/os/utils" - "github.com/realiotech/realio-network/testutil/integration/os/network" - - evmtypes "github.com/evmos/os/x/evm/types" - - "github.com/ethereum/go-ethereum/params" - - //nolint:revive // dot imports are fine for Ginkgo - . "github.com/onsi/ginkgo/v2" - //nolint:revive // dot imports are fine for Ginkgo - . "github.com/onsi/gomega" -) - -// Below tests are divided in 3 steps: -// 1. Deploy and interact with contracts to compute the gas used BEFORE enabling -// the IP. -// 2. Activate the IP under test. -// 3. Deploy and interact with contracts to compute the gas used AFTER enabling -// the IP. - -func TestIPs(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "EvmosIPs Suite") -} - -var _ = Describe("Improvement proposal evmos_0 - ", Ordered, func() { - var ( - in network.Network - tf factory.TxFactory - gh grpc.Handler - k keyring.Keyring - - senderPriv types.PrivKey - senderPriv2 types.PrivKey - senderAddr2 common.Address - - // Gas used before enabling the IP. - gasUsedPre int64 - ) - - // Multiplier used to modify the opcodes associated with evmos_0 IP. - ipMultiplier := uint64(5) - - // The factory counter is used because it will create a new instance of - // the counter contract, allowing to test the CREATE opcode. - counterFactoryContract, err := testdata.LoadCounterFactoryContract() - Expect(err).ToNot(HaveOccurred(), "failed to load Counter Factory contract") - - deploymentData := factory.ContractDeploymentData{ - Contract: counterFactoryContract, - ConstructorArgs: []interface{}{}, - } - - BeforeAll(func() { - k = keyring.New(2) - in = network.New( - network.WithPreFundedAccounts(k.GetAllAccAddrs()...), - ) - gh = grpc.NewIntegrationHandler(in) - tf = factory.New(in, gh) - - // Account used to deploy the contract before enabling the IP. - senderPriv = k.GetPrivKey(0) - // Account used to deploy the contract after enabling the IP. A second - // account is used to avoid possible additional gas costs due to the change - // in the Nonce. - senderPriv2 = k.GetPrivKey(1) - senderAddr2 = k.GetAddr(1) - - // Set extra IPs to empty to allow testing a single modifier. - defaultParams := evmtypes.DefaultParams() - defaultParams.ExtraEIPs = []string{} - - err := integrationutils.UpdateEvmParams( - integrationutils.UpdateParamsInput{ - Tf: tf, - Network: in, - Pk: senderPriv, - Params: defaultParams, - }, - ) - Expect(err).To(BeNil(), "failed during update of evm params") - Expect(in.NextBlock()).To(BeNil()) - }) - - It("should deploy the contract before enabling the IP", func() { - deploymentTxArgs, err := tf.GenerateDeployContractArgs(senderAddr2, evmtypes.EvmTxArgs{}, deploymentData) - Expect(err).To(BeNil(), "failed to create deployment tx args") - - res, err := tf.ExecuteEthTx(senderPriv2, deploymentTxArgs) - Expect(err).To(BeNil(), "failed during contract deployment") - gasUsedPre = res.GasUsed - }) - - It("should enable the new IP", func() { - eips.Multiplier = ipMultiplier - newIP := "evmos_0" - - qRes, err := gh.GetEvmParams() - Expect(err).To(BeNil(), "failed during query to evm params") - qRes.Params.ExtraEIPs = append(qRes.Params.ExtraEIPs, newIP) - err = integrationutils.UpdateEvmParams( - integrationutils.UpdateParamsInput{ - Tf: tf, - Network: in, - Pk: senderPriv, - Params: qRes.Params, - }, - ) - Expect(err).To(BeNil(), "failed during update of evm params") - - Expect(in.NextBlock()).To(BeNil()) - - qRes, err = gh.GetEvmParams() - Expect(err).To(BeNil(), "failed during query to evm params") - Expect(qRes.Params.ExtraEIPs).To(ContainElement(newIP), "expected to have IP evmos_0 in evm params") - }) - - It("should change CREATE opcode constant gas after enabling evmos_0 IP", func() { - gasCostPre := params.CreateGas - - deploymentTxArgs, err := tf.GenerateDeployContractArgs(senderAddr2, evmtypes.EvmTxArgs{}, deploymentData) - Expect(err).To(BeNil(), "failed to create deployment tx args") - - res, err := tf.ExecuteEthTx(senderPriv2, deploymentTxArgs) - Expect(err).To(BeNil(), "failed during contract deployment") - // commit block to update sender nonce - Expect(in.NextBlock()).To(BeNil()) - - gasUsedPost := res.GasUsed - - // The difference in gas is the new cost of the opcode, minus the cost of the - // opcode before enabling the new eip. - gasUsedDiff := ipMultiplier*gasCostPre - gasCostPre - expectedGas := gasUsedPre + int64(gasUsedDiff) - Expect(gasUsedPost).To(Equal(expectedGas)) - }) -}) - -var _ = Describe("Improvement proposal evmos_1 - ", Ordered, func() { - var ( - in network.Network - tf factory.TxFactory - gh grpc.Handler - k keyring.Keyring - - senderPriv types.PrivKey - - // Gas used before enabling the IP. - gasUsedPre int64 - - // The address of the factory counter. - counterFactoryAddr common.Address - ) - - // Multiplier used to modify the opcodes associated with evmos_1. - eipMultiplier := uint64(5) - initialCounterValue := 1 - - // The counter factory contract is used to deploy a counter contract and - // perform state transition using the CALL opcode. - counterFactoryContract, err := testdata.LoadCounterFactoryContract() - Expect(err).ToNot(HaveOccurred(), "failed to load Counter Factory contract") - - BeforeAll(func() { - k = keyring.New(1) - in = network.New( - network.WithPreFundedAccounts(k.GetAllAccAddrs()...), - ) - gh = grpc.NewIntegrationHandler(in) - tf = factory.New(in, gh) - - senderPriv = k.GetPrivKey(0) - - // Set extra IPs to empty to allow testing a single modifier. - defaultParams := evmtypes.DefaultParams() - defaultParams.ExtraEIPs = []string{} - err = integrationutils.UpdateEvmParams( - integrationutils.UpdateParamsInput{ - Tf: tf, - Network: in, - Pk: senderPriv, - Params: defaultParams, - }, - ) - Expect(err).To(BeNil(), "failed during update of evm params") - - Expect(in.NextBlock()).To(BeNil()) - }) - - It("should deploy the contract before enabling the IP", func() { - counterFactoryAddr, err = tf.DeployContract( - senderPriv, - evmtypes.EvmTxArgs{}, - factory.ContractDeploymentData{ - Contract: counterFactoryContract, - ConstructorArgs: []interface{}{}, - }, - ) - Expect(err).ToNot(HaveOccurred(), "failed to deploy counter factory contract") - Expect(in.NextBlock()).To(BeNil()) - - res, err := tf.ExecuteContractCall( - senderPriv, - evmtypes.EvmTxArgs{To: &counterFactoryAddr}, - factory.CallArgs{ - ContractABI: counterFactoryContract.ABI, - MethodName: "incrementCounter", - Args: []interface{}{}, - }, - ) - Expect(err).ToNot(HaveOccurred(), "failed to increment counter value") - gasUsedPre = res.GasUsed - - Expect(in.NextBlock()).To(BeNil()) - - // Query the counter value to check proper state transition later. - res, err = tf.ExecuteContractCall( - senderPriv, - evmtypes.EvmTxArgs{To: &counterFactoryAddr}, - factory.CallArgs{ - ContractABI: counterFactoryContract.ABI, - MethodName: "getCounterValue", - Args: []interface{}{}, - }, - ) - Expect(err).ToNot(HaveOccurred(), "failed to get counter value") - Expect(in.NextBlock()).To(BeNil()) - - ethRes, err := evmtypes.DecodeTxResponse(res.Data) - Expect(err).ToNot(HaveOccurred(), "failed to decode tx response") - - unpacked, err := counterFactoryContract.ABI.Unpack( - "getCounterValue", - ethRes.Ret, - ) - Expect(err).ToNot(HaveOccurred(), "failed to unpack counter value") - - counter, ok := unpacked[0].(*big.Int) - Expect(ok).To(BeTrue(), "failed to convert counter to big.Int") - Expect(counter.String()).To(Equal(fmt.Sprintf("%d", initialCounterValue+1)), "counter is not correct") - }) - It("should enable the new IP", func() { - eips.Multiplier = eipMultiplier - newIP := "evmos_1" - - qRes, err := gh.GetEvmParams() - Expect(err).To(BeNil(), "failed during query to evm params") - qRes.Params.ExtraEIPs = append(qRes.Params.ExtraEIPs, newIP) - - err = integrationutils.UpdateEvmParams( - integrationutils.UpdateParamsInput{ - Tf: tf, - Network: in, - Pk: senderPriv, - Params: qRes.Params, - }, - ) - Expect(err).To(BeNil(), "failed during update of evm params") - - Expect(in.NextBlock()).To(BeNil()) - - qRes, err = gh.GetEvmParams() - Expect(err).To(BeNil(), "failed during query to evm params") - Expect(qRes.Params.ExtraEIPs).To(ContainElement(newIP), "expected to have ip evmos_1 in evm params") - }) - It("should change CALL opcode constant gas after enabling IP", func() { - // Constant gas cost used before enabling the new IP. - gasCostPre := params.WarmStorageReadCostEIP2929 - - res, err := tf.ExecuteContractCall( - senderPriv, - evmtypes.EvmTxArgs{To: &counterFactoryAddr}, - factory.CallArgs{ - ContractABI: counterFactoryContract.ABI, - MethodName: "incrementCounter", - Args: []interface{}{}, - }, - ) - Expect(err).ToNot(HaveOccurred(), "failed to increment counter value") - gasUsedPost := res.GasUsed - Expect(in.NextBlock()).To(BeNil()) - - res, err = tf.ExecuteContractCall( - senderPriv, - evmtypes.EvmTxArgs{To: &counterFactoryAddr}, - factory.CallArgs{ - ContractABI: counterFactoryContract.ABI, - MethodName: "getCounterValue", - Args: []interface{}{}, - }, - ) - Expect(err).ToNot(HaveOccurred(), "failed to get counter value") - Expect(in.NextBlock()).To(BeNil()) - - ethRes, err := evmtypes.DecodeTxResponse(res.Data) - Expect(err).ToNot(HaveOccurred(), "failed to decode tx response") - - unpacked, err := counterFactoryContract.ABI.Unpack( - "getCounterValue", - ethRes.Ret, - ) - Expect(err).ToNot(HaveOccurred(), "failed to unpack counter value") - - counter, ok := unpacked[0].(*big.Int) - Expect(ok).To(BeTrue(), "failed to convert counter to big.Int") - Expect(counter.String()).To(Equal(fmt.Sprintf("%d", initialCounterValue+2)), "counter is not updated correctly") - - // The difference in gas is the new cost of the opcode, minus the cost of the - // opcode before enabling the new eip. - gasUsedDiff := eipMultiplier*gasCostPre - gasCostPre - expectedGas := gasUsedPre + int64(gasUsedDiff) - Expect(gasUsedPost).To(Equal(expectedGas)) - }) -}) - -var _ = Describe("Improvement proposal evmos_2 - ", Ordered, func() { - var ( - in network.Network - tf factory.TxFactory - gh grpc.Handler - k keyring.Keyring - - senderPriv types.PrivKey - senderAddr common.Address - senderPriv2 types.PrivKey - senderAddr2 common.Address - gasUsedPre int64 - ) - // Constant gas used to modify the opcodes associated with evmos_2. - constantGas := uint64(500) - - counterContract, err := testdata.LoadCounterContract() - Expect(err).ToNot(HaveOccurred(), "failed to load Counter contract") - - deploymentData := factory.ContractDeploymentData{ - Contract: counterContract, - ConstructorArgs: []interface{}{}, - } - BeforeAll(func() { - k = keyring.New(2) - in = network.New( - network.WithPreFundedAccounts(k.GetAllAccAddrs()...), - ) - gh = grpc.NewIntegrationHandler(in) - tf = factory.New(in, gh) - - // Account used to deploy the contract before enabling the IP. - senderPriv = k.GetPrivKey(0) - senderAddr = k.GetAddr(0) - // Account used to deploy the contract after enabling the IP. A second - // account is used to avoid possible additional gas costs due to the change - // in the Nonce. - senderPriv2 = k.GetPrivKey(0) - senderAddr2 = k.GetAddr(0) - - // Set extra IPs to empty to allow testing a single modifier. - defaultParams := evmtypes.DefaultParams() - defaultParams.ExtraEIPs = []string{} - - err = integrationutils.UpdateEvmParams( - integrationutils.UpdateParamsInput{ - Tf: tf, - Network: in, - Pk: senderPriv, - Params: defaultParams, - }, - ) - Expect(err).To(BeNil(), "failed during update of evm params") - - Expect(in.NextBlock()).To(BeNil()) - }) - - It("should deploy the contract before enabling the IP", func() { - deploymentTxArgs, err := tf.GenerateDeployContractArgs(senderAddr, evmtypes.EvmTxArgs{}, deploymentData) - Expect(err).To(BeNil(), "failed to create deployment tx args") - - res, err := tf.ExecuteEthTx(senderPriv, deploymentTxArgs) - Expect(err).To(BeNil(), "failed during contract deployment") - Expect(in.NextBlock()).To(BeNil()) - - gasUsedPre = res.GasUsed - }) - - It("should enable the new IP", func() { - eips.SstoreConstantGas = constantGas - newIP := "evmos_2" - - qRes, err := gh.GetEvmParams() - Expect(err).To(BeNil(), "failed during query to evm params") - qRes.Params.ExtraEIPs = append(qRes.Params.ExtraEIPs, newIP) - err = integrationutils.UpdateEvmParams( - integrationutils.UpdateParamsInput{ - Tf: tf, - Network: in, - Pk: senderPriv, - Params: qRes.Params, - }, - ) - Expect(err).To(BeNil(), "failed during update of evm params") - - Expect(in.NextBlock()).To(BeNil()) - - qRes, err = gh.GetEvmParams() - Expect(err).To(BeNil(), "failed during query to evm params") - Expect(qRes.Params.ExtraEIPs).To(ContainElement(newIP), "expected to have ip evmos_2 in evm params") - }) - - It("should change SSTORE opcode constant gas after enabling IP", func() { - deploymentTxArgs, err := tf.GenerateDeployContractArgs(senderAddr2, evmtypes.EvmTxArgs{}, deploymentData) - Expect(err).To(BeNil(), "failed to create deployment tx args") - - res, err := tf.ExecuteEthTx(senderPriv2, deploymentTxArgs) - Expect(err).To(BeNil(), "failed during contract deployment") - Expect(in.NextBlock()).To(BeNil()) - - gasUsedPost := res.GasUsed - - // The expected gas is previous gas plus the constant gas because - // previous this eip, SSTORE was using only the dynamic gas. - expectedGas := gasUsedPre + int64(constantGas) - Expect(gasUsedPost).To(Equal(expectedGas)) - }) -}) diff --git a/example_chain/eips/testdata/Counter.json b/example_chain/eips/testdata/Counter.json deleted file mode 100644 index 31819578..00000000 --- a/example_chain/eips/testdata/Counter.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "_format": "hh-sol-artifact-1", - "contractName": "Counter", - "sourceName": "solidity/example_chain/eips/testdata/Counter.sol", - "abi": [ - { - "inputs": [], - "name": "counter", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "decrement", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [], - "name": "increment", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - } - ], - "bytecode": "0x6080604052600160005534801561001557600080fd5b506101ba806100256000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632baeceb71461004657806361bc221a14610050578063d09de08a1461006e575b600080fd5b61004e610078565b005b610058610091565b60405161006591906100c9565b60405180910390f35b610076610097565b005b60008081548092919061008a90610113565b9190505550565b60005481565b6000808154809291906100a99061013c565b9190505550565b6000819050919050565b6100c3816100b0565b82525050565b60006020820190506100de60008301846100ba565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061011e826100b0565b915060008203610131576101306100e4565b5b600182039050919050565b6000610147826100b0565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610179576101786100e4565b5b60018201905091905056fea2646970667358221220e2cfc61bbf42463bc63fe07f96984cffab77d85daac62b0017cf4c9306d056d064736f6c63430008140033", - "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100415760003560e01c80632baeceb71461004657806361bc221a14610050578063d09de08a1461006e575b600080fd5b61004e610078565b005b610058610091565b60405161006591906100c9565b60405180910390f35b610076610097565b005b60008081548092919061008a90610113565b9190505550565b60005481565b6000808154809291906100a99061013c565b9190505550565b6000819050919050565b6100c3816100b0565b82525050565b60006020820190506100de60008301846100ba565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061011e826100b0565b915060008203610131576101306100e4565b5b600182039050919050565b6000610147826100b0565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610179576101786100e4565b5b60018201905091905056fea2646970667358221220e2cfc61bbf42463bc63fe07f96984cffab77d85daac62b0017cf4c9306d056d064736f6c63430008140033", - "linkReferences": {}, - "deployedLinkReferences": {} -} diff --git a/example_chain/eips/testdata/Counter.sol b/example_chain/eips/testdata/Counter.sol deleted file mode 100644 index 30ba0869..00000000 --- a/example_chain/eips/testdata/Counter.sol +++ /dev/null @@ -1,15 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-only - -pragma solidity >=0.7.0 <0.9.0; - -contract Counter { - uint256 public counter = 1; - - function increment() external { - counter++; - } - - function decrement() external { - counter--; - } -} diff --git a/example_chain/eips/testdata/CounterFactory.json b/example_chain/eips/testdata/CounterFactory.json deleted file mode 100644 index d3c9674d..00000000 --- a/example_chain/eips/testdata/CounterFactory.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "_format": "hh-sol-artifact-1", - "contractName": "Counterfactory", - "sourceName": "solidity/example_chain/eips/testdata/CounterFactory.sol", - "abi": [ - { - "inputs": [], - "stateMutability": "nonpayable", - "type": "constructor" - }, - { - "inputs": [], - "name": "counterInstance", - "outputs": [ - { - "internalType": "contract Counter", - "name": "", - "type": "address" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "decrementCounter", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [], - "name": "getCounterValue", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "incrementCounter", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - } - ], - "bytecode": "0x608060405234801561001057600080fd5b5060405161001d9061007e565b604051809103906000f080158015610039573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061008b565b6101df8061045c83390190565b6103c28061009a6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80635b34b9661461005157806372142b891461005b578063aef38e7214610079578063f5c5ad8314610097575b600080fd5b6100596100a1565b005b610063610123565b6040516100709190610279565b60405180910390f35b6100816101ba565b60405161008e9190610313565b60405180910390f35b61009f6101de565b005b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663d09de08a6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561010957600080fd5b505af115801561011d573d6000803e3d6000fd5b50505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166361bc221a6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610191573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101b5919061035f565b905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16632baeceb76040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561024657600080fd5b505af115801561025a573d6000803e3d6000fd5b50505050565b6000819050919050565b61027381610260565b82525050565b600060208201905061028e600083018461026a565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60006102d96102d46102cf84610294565b6102b4565b610294565b9050919050565b60006102eb826102be565b9050919050565b60006102fd826102e0565b9050919050565b61030d816102f2565b82525050565b60006020820190506103286000830184610304565b92915050565b600080fd5b61033c81610260565b811461034757600080fd5b50565b60008151905061035981610333565b92915050565b6000602082840312156103755761037461032e565b5b60006103838482850161034a565b9150509291505056fea26469706673582212209247a66c2dbee615e99f0a204e17785fb9d8102833d627c3f5cc5f86d3d4793964736f6c634300081400336080604052600160005534801561001557600080fd5b506101ba806100256000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632baeceb71461004657806361bc221a14610050578063d09de08a1461006e575b600080fd5b61004e610078565b005b610058610091565b60405161006591906100c9565b60405180910390f35b610076610097565b005b60008081548092919061008a90610113565b9190505550565b60005481565b6000808154809291906100a99061013c565b9190505550565b6000819050919050565b6100c3816100b0565b82525050565b60006020820190506100de60008301846100ba565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061011e826100b0565b915060008203610131576101306100e4565b5b600182039050919050565b6000610147826100b0565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8203610179576101786100e4565b5b60018201905091905056fea2646970667358221220e2cfc61bbf42463bc63fe07f96984cffab77d85daac62b0017cf4c9306d056d064736f6c63430008140033", - "deployedBytecode": "0x608060405234801561001057600080fd5b506004361061004c5760003560e01c80635b34b9661461005157806372142b891461005b578063aef38e7214610079578063f5c5ad8314610097575b600080fd5b6100596100a1565b005b610063610123565b6040516100709190610279565b60405180910390f35b6100816101ba565b60405161008e9190610313565b60405180910390f35b61009f6101de565b005b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663d09de08a6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561010957600080fd5b505af115801561011d573d6000803e3d6000fd5b50505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166361bc221a6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610191573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101b5919061035f565b905090565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16632baeceb76040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561024657600080fd5b505af115801561025a573d6000803e3d6000fd5b50505050565b6000819050919050565b61027381610260565b82525050565b600060208201905061028e600083018461026a565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60006102d96102d46102cf84610294565b6102b4565b610294565b9050919050565b60006102eb826102be565b9050919050565b60006102fd826102e0565b9050919050565b61030d816102f2565b82525050565b60006020820190506103286000830184610304565b92915050565b600080fd5b61033c81610260565b811461034757600080fd5b50565b60008151905061035981610333565b92915050565b6000602082840312156103755761037461032e565b5b60006103838482850161034a565b9150509291505056fea26469706673582212209247a66c2dbee615e99f0a204e17785fb9d8102833d627c3f5cc5f86d3d4793964736f6c63430008140033", - "linkReferences": {}, - "deployedLinkReferences": {} -} diff --git a/example_chain/eips/testdata/CounterFactory.sol b/example_chain/eips/testdata/CounterFactory.sol deleted file mode 100644 index 7b64412d..00000000 --- a/example_chain/eips/testdata/CounterFactory.sol +++ /dev/null @@ -1,25 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-only - -pragma solidity >=0.7.0 <0.9.0; - -import "./Counter.sol"; - -contract Counterfactory { - Counter public counterInstance; - - constructor() { - counterInstance = new Counter(); - } - - function incrementCounter() public { - counterInstance.increment(); - } - - function decrementCounter() public { - counterInstance.decrement(); - } - - function getCounterValue() public view returns (uint256) { - return counterInstance.counter(); - } -} diff --git a/example_chain/eips/testdata/contracts.go b/example_chain/eips/testdata/contracts.go deleted file mode 100644 index 2d336b52..00000000 --- a/example_chain/eips/testdata/contracts.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testdata - -import ( - contractutils "github.com/evmos/os/contracts/utils" - evmtypes "github.com/evmos/os/x/evm/types" -) - -func LoadCounterContract() (evmtypes.CompiledContract, error) { - return contractutils.LoadContractFromJSONFile("Counter.json") -} - -func LoadCounterFactoryContract() (evmtypes.CompiledContract, error) { - return contractutils.LoadContractFromJSONFile("CounterFactory.json") -} diff --git a/example_chain/osd/cmd/root.go b/example_chain/osd/cmd/root.go deleted file mode 100644 index b2e34ba8..00000000 --- a/example_chain/osd/cmd/root.go +++ /dev/null @@ -1,415 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package cmd - -import ( - "errors" - "io" - "os" - - "cosmossdk.io/log" - "cosmossdk.io/store" - snapshottypes "cosmossdk.io/store/snapshots/types" - storetypes "cosmossdk.io/store/types" - confixcmd "cosmossdk.io/tools/confix/cmd" - tmcfg "github.com/cometbft/cometbft/config" - cmtcli "github.com/cometbft/cometbft/libs/cli" - dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/client" - clientcfg "github.com/cosmos/cosmos-sdk/client/config" - "github.com/cosmos/cosmos-sdk/client/debug" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/client/pruning" - "github.com/cosmos/cosmos-sdk/client/rpc" - "github.com/cosmos/cosmos-sdk/client/snapshot" - sdkserver "github.com/cosmos/cosmos-sdk/server" - serverconfig "github.com/cosmos/cosmos-sdk/server/config" - servertypes "github.com/cosmos/cosmos-sdk/server/types" - sdk "github.com/cosmos/cosmos-sdk/types" - sdkmempool "github.com/cosmos/cosmos-sdk/types/mempool" - sdktestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - "github.com/cosmos/cosmos-sdk/x/auth/tx" - txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" - evmoscmd "github.com/evmos/os/client" - evmoscmdconfig "github.com/evmos/os/cmd/config" - evmoskeyring "github.com/evmos/os/crypto/keyring" - evmosserver "github.com/evmos/os/server" - evmosserverconfig "github.com/evmos/os/server/config" - srvflags "github.com/evmos/os/server/flags" - "github.com/realiotech/realio-network/example_chain" - cmdcfg "github.com/realiotech/realio-network/example_chain/osd/config" - "github.com/spf13/cast" - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -type emptyAppOptions struct{} - -func (ao emptyAppOptions) Get(_ string) interface{} { return nil } - -// NewRootCmd creates a new root command for osd. It is called once in the -// main function. -func NewRootCmd() *cobra.Command { - // we "pre"-instantiate the application for getting the injected/configured encoding configuration - // and the CLI options for the modules - // add keyring to autocli opts - tempApp := example_chain.NewExampleApp( - log.NewNopLogger(), - dbm.NewMemDB(), - nil, - true, - emptyAppOptions{}, - example_chain.EvmosAppOptions, - ) - - encodingConfig := sdktestutil.TestEncodingConfig{ - InterfaceRegistry: tempApp.InterfaceRegistry(), - Codec: tempApp.AppCodec(), - TxConfig: tempApp.GetTxConfig(), - Amino: tempApp.LegacyAmino(), - } - - initClientCtx := client.Context{}. - WithCodec(encodingConfig.Codec). - WithInterfaceRegistry(encodingConfig.InterfaceRegistry). - WithTxConfig(encodingConfig.TxConfig). - WithLegacyAmino(encodingConfig.Amino). - WithInput(os.Stdin). - WithAccountRetriever(authtypes.AccountRetriever{}). - WithBroadcastMode(flags.FlagBroadcastMode). - WithHomeDir(example_chain.DefaultNodeHome). - WithViper(""). // In simapp, we don't use any prefix for env variables. - // evmOS specific setup - WithKeyringOptions(evmoskeyring.Option()). - WithLedgerHasProtobuf(true) - - rootCmd := &cobra.Command{ - Use: "osd", - Short: "exemplary evmOS app", - PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { - // set the default command outputs - cmd.SetOut(cmd.OutOrStdout()) - cmd.SetErr(cmd.ErrOrStderr()) - - initClientCtx = initClientCtx.WithCmdContext(cmd.Context()) - initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags()) - if err != nil { - return err - } - - initClientCtx, err = clientcfg.ReadFromClientConfig(initClientCtx) - if err != nil { - return err - } - - // This needs to go after ReadFromClientConfig, as that function - // sets the RPC client needed for SIGN_MODE_TEXTUAL. This sign mode - // is only available if the client is online. - if !initClientCtx.Offline { - enabledSignModes := append(tx.DefaultSignModes, signing.SignMode_SIGN_MODE_TEXTUAL) //nolint:gocritic - txConfigOpts := tx.ConfigOptions{ - EnabledSignModes: enabledSignModes, - TextualCoinMetadataQueryFn: txmodule.NewGRPCCoinMetadataQueryFn(initClientCtx), - } - txConfig, err := tx.NewTxConfigWithOptions( - initClientCtx.Codec, - txConfigOpts, - ) - if err != nil { - return err - } - - initClientCtx = initClientCtx.WithTxConfig(txConfig) - } - - if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil { - return err - } - - customAppTemplate, customAppConfig := InitAppConfig(cmdcfg.BaseDenom) - customTMConfig := initTendermintConfig() - - return sdkserver.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, customTMConfig) - }, - } - - initRootCmd(rootCmd, tempApp) - - autoCliOpts := tempApp.AutoCliOpts() - initClientCtx, _ = clientcfg.ReadFromClientConfig(initClientCtx) - autoCliOpts.ClientCtx = initClientCtx - - if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil { - panic(err) - } - - return rootCmd -} - -// initTendermintConfig helps to override default Tendermint Config values. -// return tmcfg.DefaultConfig if no custom configuration is required for the application. -func initTendermintConfig() *tmcfg.Config { - cfg := tmcfg.DefaultConfig() - - // these values put a higher strain on node memory - // cfg.P2P.MaxNumInboundPeers = 100 - // cfg.P2P.MaxNumOutboundPeers = 40 - - return cfg -} - -// InitAppConfig helps to override default appConfig template and configs. -// return "", nil if no custom configuration is required for the application. -func InitAppConfig(denom string) (string, interface{}) { - type CustomAppConfig struct { - serverconfig.Config - - EVM evmosserverconfig.EVMConfig - JSONRPC evmosserverconfig.JSONRPCConfig - TLS evmosserverconfig.TLSConfig - } - - // Optionally allow the chain developer to overwrite the SDK's default - // server config. - srvCfg := serverconfig.DefaultConfig() - // The SDK's default minimum gas price is set to "" (empty value) inside - // app.toml. If left empty by validators, the node will halt on startup. - // However, the chain developer can set a default app.toml value for their - // validators here. - // - // In summary: - // - if you leave srvCfg.MinGasPrices = "", all validators MUST tweak their - // own app.toml config, - // - if you set srvCfg.MinGasPrices non-empty, validators CAN tweak their - // own app.toml to override, or use this default value. - // - // In this example application, we set the min gas prices to 0. - srvCfg.MinGasPrices = "0" + denom - - customAppConfig := CustomAppConfig{ - Config: *srvCfg, - EVM: *evmosserverconfig.DefaultEVMConfig(), - JSONRPC: *evmosserverconfig.DefaultJSONRPCConfig(), - TLS: *evmosserverconfig.DefaultTLSConfig(), - } - - customAppTemplate := serverconfig.DefaultConfigTemplate + - evmosserverconfig.DefaultEVMConfigTemplate - - return customAppTemplate, customAppConfig -} - -func initRootCmd(rootCmd *cobra.Command, osApp *example_chain.ExampleChain) { - cfg := sdk.GetConfig() - cfg.Seal() - - rootCmd.AddCommand( - genutilcli.InitCmd( - osApp.BasicModuleManager, - example_chain.DefaultNodeHome, - ), - genutilcli.Commands(osApp.TxConfig(), osApp.BasicModuleManager, example_chain.DefaultNodeHome), - cmtcli.NewCompletionCmd(rootCmd, true), - debug.Cmd(), - confixcmd.ConfigCommand(), - pruning.Cmd(newApp, example_chain.DefaultNodeHome), - snapshot.Cmd(newApp), - ) - - // add evmOS' flavored TM commands to start server, etc. - evmosserver.AddCommands( - rootCmd, - evmosserver.NewDefaultStartOptions(newApp, example_chain.DefaultNodeHome), - appExport, - addModuleInitFlags, - ) - - // add evmOS key commands - rootCmd.AddCommand( - evmoscmd.KeyCommands(example_chain.DefaultNodeHome, true), - ) - - // add keybase, auxiliary RPC, query, genesis, and tx child commands - rootCmd.AddCommand( - sdkserver.StatusCommand(), - queryCommand(), - txCommand(), - ) - - // add general tx flags to the root command - var err error - rootCmd, err = srvflags.AddTxFlags(rootCmd) - if err != nil { - panic(err) - } -} - -func addModuleInitFlags(_ *cobra.Command) {} - -func queryCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "query", - Aliases: []string{"q"}, - Short: "Querying subcommands", - DisableFlagParsing: false, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - - cmd.AddCommand( - rpc.QueryEventForTxCmd(), - rpc.ValidatorCommand(), - authcmd.QueryTxsByEventsCmd(), - authcmd.QueryTxCmd(), - sdkserver.QueryBlockCmd(), - sdkserver.QueryBlockResultsCmd(), - ) - - cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID") - - return cmd -} - -func txCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "tx", - Short: "Transactions subcommands", - DisableFlagParsing: false, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - - cmd.AddCommand( - authcmd.GetSignCommand(), - authcmd.GetSignBatchCommand(), - authcmd.GetMultiSignCommand(), - authcmd.GetMultiSignBatchCmd(), - authcmd.GetValidateSignaturesCommand(), - authcmd.GetBroadcastCommand(), - authcmd.GetEncodeCommand(), - authcmd.GetDecodeCommand(), - authcmd.GetSimulateCmd(), - ) - - cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID") - - return cmd -} - -// newApp creates the application -func newApp( - logger log.Logger, - db dbm.DB, - traceStore io.Writer, - appOpts servertypes.AppOptions, -) servertypes.Application { - var cache storetypes.MultiStorePersistentCache - - if cast.ToBool(appOpts.Get(sdkserver.FlagInterBlockCache)) { - cache = store.NewCommitKVStoreCacheManager() - } - - pruningOpts, err := sdkserver.GetPruningOptionsFromFlags(appOpts) - if err != nil { - panic(err) - } - - homeDir := cast.ToString(appOpts.Get(flags.FlagHome)) - chainID := cast.ToString(appOpts.Get(flags.FlagChainID)) - if chainID == "" { - chainID, err = evmoscmdconfig.GetChainIDFromHome(homeDir) - if err != nil { - panic(err) - } - } - - snapshotStore, err := sdkserver.GetSnapshotStore(appOpts) - if err != nil { - panic(err) - } - - snapshotOptions := snapshottypes.NewSnapshotOptions( - cast.ToUint64(appOpts.Get(sdkserver.FlagStateSyncSnapshotInterval)), - cast.ToUint32(appOpts.Get(sdkserver.FlagStateSyncSnapshotKeepRecent)), - ) - - baseappOptions := []func(*baseapp.BaseApp){ - baseapp.SetPruning(pruningOpts), - baseapp.SetMinGasPrices(cast.ToString(appOpts.Get(sdkserver.FlagMinGasPrices))), - baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(sdkserver.FlagHaltHeight))), - baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(sdkserver.FlagHaltTime))), - baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(sdkserver.FlagMinRetainBlocks))), - baseapp.SetInterBlockCache(cache), - baseapp.SetTrace(cast.ToBool(appOpts.Get(sdkserver.FlagTrace))), - baseapp.SetIndexEvents(cast.ToStringSlice(appOpts.Get(sdkserver.FlagIndexEvents))), - baseapp.SetSnapshot(snapshotStore, snapshotOptions), - baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(sdkserver.FlagIAVLCacheSize))), - baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(sdkserver.FlagDisableIAVLFastNode))), - baseapp.SetChainID(chainID), - } - - // Set up the required mempool and ABCI proposal handlers for evmOS - baseappOptions = append(baseappOptions, func(app *baseapp.BaseApp) { - mempool := sdkmempool.NoOpMempool{} - app.SetMempool(mempool) - - handler := baseapp.NewDefaultProposalHandler(mempool, app) - app.SetPrepareProposal(handler.PrepareProposalHandler()) - app.SetProcessProposal(handler.ProcessProposalHandler()) - }) - - return example_chain.NewExampleApp( - logger, db, traceStore, true, - appOpts, - example_chain.EvmosAppOptions, - baseappOptions..., - ) -} - -// appExport creates a new application (optionally at a given height) and exports state. -func appExport( - logger log.Logger, - db dbm.DB, - traceStore io.Writer, - height int64, - forZeroHeight bool, - jailAllowedAddrs []string, - appOpts servertypes.AppOptions, - modulesToExport []string, -) (servertypes.ExportedApp, error) { - var exampleApp *example_chain.ExampleChain - - // this check is necessary as we use the flag in x/upgrade. - // we can exit more gracefully by checking the flag here. - homePath, ok := appOpts.Get(flags.FlagHome).(string) - if !ok || homePath == "" { - return servertypes.ExportedApp{}, errors.New("application home not set") - } - - viperAppOpts, ok := appOpts.(*viper.Viper) - if !ok { - return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper") - } - - // overwrite the FlagInvCheckPeriod - viperAppOpts.Set(sdkserver.FlagInvCheckPeriod, 1) - appOpts = viperAppOpts - - if height != -1 { - exampleApp = example_chain.NewExampleApp(logger, db, traceStore, false, appOpts, example_chain.EvmosAppOptions) - - if err := exampleApp.LoadHeight(height); err != nil { - return servertypes.ExportedApp{}, err - } - } else { - exampleApp = example_chain.NewExampleApp(logger, db, traceStore, true, appOpts, example_chain.EvmosAppOptions) - } - - return exampleApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport) -} diff --git a/example_chain/osd/config/config.go b/example_chain/osd/config/config.go deleted file mode 100644 index 9899d4c9..00000000 --- a/example_chain/osd/config/config.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package config - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -const ( - // Bech32Prefix defines the Bech32 prefix used for accounts on the exemplary evmOS blockchain. - Bech32Prefix = "evmos" - - // Bech32PrefixAccAddr defines the Bech32 prefix of an account's address. - Bech32PrefixAccAddr = Bech32Prefix - // Bech32PrefixAccPub defines the Bech32 prefix of an account's public key. - Bech32PrefixAccPub = Bech32Prefix + sdk.PrefixPublic - // Bech32PrefixValAddr defines the Bech32 prefix of a validator's operator address. - Bech32PrefixValAddr = Bech32Prefix + sdk.PrefixValidator + sdk.PrefixOperator - // Bech32PrefixValPub defines the Bech32 prefix of a validator's operator public key. - Bech32PrefixValPub = Bech32Prefix + sdk.PrefixValidator + sdk.PrefixOperator + sdk.PrefixPublic - // Bech32PrefixConsAddr defines the Bech32 prefix of a consensus node address. - Bech32PrefixConsAddr = Bech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus - // Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key. - Bech32PrefixConsPub = Bech32Prefix + sdk.PrefixValidator + sdk.PrefixConsensus + sdk.PrefixPublic -) - -const ( - // DisplayDenom defines the denomination displayed to users in client applications. - DisplayDenom = "evmos" - // BaseDenom defines to the default denomination used in the evmOS example chain. - BaseDenom = "aevmos" - // BaseDenomUnit defines the precision of the base denomination. - BaseDenomUnit = 18 -) - -// SetBech32Prefixes sets the global prefixes to be used when serializing addresses and public keys to Bech32 strings. -func SetBech32Prefixes(config *sdk.Config) { - config.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub) - config.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub) - config.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub) -} diff --git a/example_chain/osd/main.go b/example_chain/osd/main.go deleted file mode 100644 index 14e1b39e..00000000 --- a/example_chain/osd/main.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package main - -import ( - "fmt" - "os" - - svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" - sdk "github.com/cosmos/cosmos-sdk/types" - examplechain "github.com/realiotech/realio-network/example_chain" - "github.com/realiotech/realio-network/example_chain/osd/cmd" - chainconfig "github.com/realiotech/realio-network/example_chain/osd/config" -) - -func main() { - setupSDKConfig() - - rootCmd := cmd.NewRootCmd() - if err := svrcmd.Execute(rootCmd, "osd", examplechain.DefaultNodeHome); err != nil { - fmt.Fprintln(rootCmd.OutOrStderr(), err) - os.Exit(1) - } -} - -func setupSDKConfig() { - config := sdk.GetConfig() - chainconfig.SetBech32Prefixes(config) - config.Seal() -} diff --git a/example_chain/test_helpers.go b/example_chain/test_helpers.go index 768eb17e..6c4b412e 100644 --- a/example_chain/test_helpers.go +++ b/example_chain/test_helpers.go @@ -26,7 +26,7 @@ import ( ibctesting "github.com/cosmos/ibc-go/v8/testing" "github.com/evmos/os/cmd/config" feemarkettypes "github.com/evmos/os/x/feemarket/types" - chainconfig "github.com/realiotech/realio-network/example_chain/osd/config" + chainconfig "github.com/evmos/os/example_chain/osd/config" "github.com/stretchr/testify/require" ) diff --git a/example_chain/testutil/abci.go b/example_chain/testutil/abci.go deleted file mode 100644 index b29efc00..00000000 --- a/example_chain/testutil/abci.go +++ /dev/null @@ -1,280 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testutil - -import ( - "fmt" - "time" - - errorsmod "cosmossdk.io/errors" - sdkmath "cosmossdk.io/math" - abci "github.com/cometbft/cometbft/abci/types" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - cmttypes "github.com/cometbft/cometbft/types" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - errortypes "github.com/cosmos/cosmos-sdk/types/errors" - - app "github.com/realiotech/realio-network/example_chain" - "github.com/realiotech/realio-network/testutil/tx" -) - -// Commit commits a block at a given time. Reminder: At the end of each -// Tendermint Consensus round the following methods are run -// 1. BeginBlock -// 2. DeliverTx -// 3. EndBlock -// 4. Commit -func Commit(ctx sdk.Context, app *app.ExampleChain, t time.Duration, vs *cmttypes.ValidatorSet) (sdk.Context, error) { - header, err := commit(ctx, app, t, vs) - if err != nil { - return ctx, err - } - - return ctx.WithBlockHeader(header), nil -} - -// CommitAndCreateNewCtx commits a block at a given time creating a ctx with the current settings -// This is useful to keep test settings that could be affected by EndBlockers, e.g. -// setting a baseFee == 0 and expecting this condition to continue after commit -func CommitAndCreateNewCtx(ctx sdk.Context, app *app.ExampleChain, t time.Duration, vs *cmttypes.ValidatorSet) (sdk.Context, error) { - header, err := commit(ctx, app, t, vs) - if err != nil { - return ctx, err - } - - // NewContext function keeps the multistore - // but resets other context fields - // GasMeter is set as InfiniteGasMeter - newCtx := app.BaseApp.NewContextLegacy(false, header) - // set the reseted fields to keep the current ctx settings - newCtx = newCtx.WithMinGasPrices(ctx.MinGasPrices()) - newCtx = newCtx.WithEventManager(ctx.EventManager()) - newCtx = newCtx.WithKVGasConfig(ctx.KVGasConfig()) - newCtx = newCtx.WithTransientKVGasConfig(ctx.TransientKVGasConfig()) - - return newCtx, nil -} - -// DeliverTx delivers a cosmos tx for a given set of msgs -func DeliverTx( - ctx sdk.Context, - exampleApp *app.ExampleChain, - priv cryptotypes.PrivKey, - gasPrice *sdkmath.Int, - msgs ...sdk.Msg, -) (abci.ExecTxResult, error) { - txConfig := exampleApp.GetTxConfig() - tx, err := tx.PrepareCosmosTx( - ctx, - exampleApp, - tx.CosmosTxArgs{ - TxCfg: txConfig, - Priv: priv, - ChainID: ctx.ChainID(), - Gas: 10_000_000, - GasPrice: gasPrice, - Msgs: msgs, - }, - ) - if err != nil { - return abci.ExecTxResult{}, err - } - return BroadcastTxBytes(exampleApp, txConfig.TxEncoder(), tx) -} - -// DeliverEthTx generates and broadcasts a Cosmos Tx populated with MsgEthereumTx messages. -// If a private key is provided, it will attempt to sign all messages with the given private key, -// otherwise, it will assume the messages have already been signed. -func DeliverEthTx( - exampleApp *app.ExampleChain, - priv cryptotypes.PrivKey, - msgs ...sdk.Msg, -) (abci.ExecTxResult, error) { - txConfig := exampleApp.GetTxConfig() - - tx, err := tx.PrepareEthTx(txConfig, priv, msgs...) - if err != nil { - return abci.ExecTxResult{}, err - } - res, err := BroadcastTxBytes(exampleApp, txConfig.TxEncoder(), tx) - if err != nil { - return res, err - } - - codec := exampleApp.AppCodec() - if _, err := CheckEthTxResponse(res, codec); err != nil { - return res, err - } - return res, nil -} - -// DeliverEthTxWithoutCheck generates and broadcasts a Cosmos Tx populated with MsgEthereumTx messages. -// If a private key is provided, it will attempt to sign all messages with the given private key, -// otherwise, it will assume the messages have already been signed. It does not check if the Eth tx is -// successful or not. -func DeliverEthTxWithoutCheck( - exampleApp *app.ExampleChain, - priv cryptotypes.PrivKey, - msgs ...sdk.Msg, -) (abci.ExecTxResult, error) { - txConfig := exampleApp.GetTxConfig() - - tx, err := tx.PrepareEthTx(txConfig, priv, msgs...) - if err != nil { - return abci.ExecTxResult{}, err - } - - res, err := BroadcastTxBytes(exampleApp, txConfig.TxEncoder(), tx) - if err != nil { - return abci.ExecTxResult{}, err - } - - return res, nil -} - -// CheckTx checks a cosmos tx for a given set of msgs -func CheckTx( - ctx sdk.Context, - exampleApp *app.ExampleChain, - priv cryptotypes.PrivKey, - gasPrice *sdkmath.Int, - msgs ...sdk.Msg, -) (abci.ResponseCheckTx, error) { - txConfig := exampleApp.GetTxConfig() - - tx, err := tx.PrepareCosmosTx( - ctx, - exampleApp, - tx.CosmosTxArgs{ - TxCfg: txConfig, - Priv: priv, - ChainID: ctx.ChainID(), - GasPrice: gasPrice, - Gas: 10_000_000, - Msgs: msgs, - }, - ) - if err != nil { - return abci.ResponseCheckTx{}, err - } - return checkTxBytes(exampleApp, txConfig.TxEncoder(), tx) -} - -// CheckEthTx checks a Ethereum tx for a given set of msgs -func CheckEthTx( - exampleApp *app.ExampleChain, - priv cryptotypes.PrivKey, - msgs ...sdk.Msg, -) (abci.ResponseCheckTx, error) { - txConfig := exampleApp.GetTxConfig() - - tx, err := tx.PrepareEthTx(txConfig, priv, msgs...) - if err != nil { - return abci.ResponseCheckTx{}, err - } - return checkTxBytes(exampleApp, txConfig.TxEncoder(), tx) -} - -// BroadcastTxBytes encodes a transaction and calls DeliverTx on the app. -func BroadcastTxBytes(app *app.ExampleChain, txEncoder sdk.TxEncoder, tx sdk.Tx) (abci.ExecTxResult, error) { - // bz are bytes to be broadcasted over the network - bz, err := txEncoder(tx) - if err != nil { - return abci.ExecTxResult{}, err - } - - req := abci.RequestFinalizeBlock{Txs: [][]byte{bz}} - - res, err := app.BaseApp.FinalizeBlock(&req) - if err != nil { - return abci.ExecTxResult{}, err - } - if len(res.TxResults) != 1 { - return abci.ExecTxResult{}, fmt.Errorf("unexpected transaction results. Expected 1, got: %d", len(res.TxResults)) - } - txRes := res.TxResults[0] - if txRes.Code != 0 { - return abci.ExecTxResult{}, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "log: %s", txRes.Log) - } - - return *txRes, nil -} - -// commit is a private helper function that runs the EndBlocker logic, commits the changes, -// updates the header, runs the BeginBlocker function and returns the updated header -func commit(ctx sdk.Context, app *app.ExampleChain, t time.Duration, vs *cmttypes.ValidatorSet) (tmproto.Header, error) { - header := ctx.BlockHeader() - req := abci.RequestFinalizeBlock{Height: header.Height} - - if vs != nil { - res, err := app.FinalizeBlock(&req) - if err != nil { - return header, err - } - - nextVals, err := applyValSetChanges(vs, res.ValidatorUpdates) - if err != nil { - return header, err - } - header.ValidatorsHash = vs.Hash() - header.NextValidatorsHash = nextVals.Hash() - } else { - if _, err := app.EndBlocker(ctx); err != nil { - return header, err - } - } - - if _, err := app.Commit(); err != nil { - return header, err - } - - header.Height++ - header.Time = header.Time.Add(t) - header.AppHash = app.LastCommitID().Hash - - if _, err := app.BeginBlocker(ctx); err != nil { - return header, err - } - - return header, nil -} - -// checkTxBytes encodes a transaction and calls checkTx on the app. -func checkTxBytes(app *app.ExampleChain, txEncoder sdk.TxEncoder, tx sdk.Tx) (abci.ResponseCheckTx, error) { - bz, err := txEncoder(tx) - if err != nil { - return abci.ResponseCheckTx{}, err - } - - req := abci.RequestCheckTx{Tx: bz} - res, err := app.BaseApp.CheckTx(&req) - if err != nil { - return abci.ResponseCheckTx{}, err - } - - if res.Code != 0 { - return abci.ResponseCheckTx{}, errorsmod.Wrapf(errortypes.ErrInvalidRequest, res.Log) - } - - return *res, nil -} - -// applyValSetChanges takes in cmttypes.ValidatorSet and []abci.ValidatorUpdate and will return a new cmttypes.ValidatorSet which has the -// provided validator updates applied to the provided validator set. -func applyValSetChanges(valSet *cmttypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) (*cmttypes.ValidatorSet, error) { - updates, err := cmttypes.PB2TM.ValidatorUpdates(valUpdates) - if err != nil { - return nil, err - } - - // must copy since validator set will mutate with UpdateWithChangeSet - newVals := valSet.Copy() - err = newVals.UpdateWithChangeSet(updates) - if err != nil { - return nil, err - } - - return newVals, nil -} diff --git a/example_chain/testutil/contract.go b/example_chain/testutil/contract.go deleted file mode 100644 index e78717c5..00000000 --- a/example_chain/testutil/contract.go +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testutil - -import ( - "fmt" - "math/big" - - "github.com/cosmos/gogoproto/proto" - - abci "github.com/cometbft/cometbft/abci/types" - "github.com/cosmos/cosmos-sdk/codec" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" - ethtypes "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - - evm "github.com/evmos/os/x/evm/types" - evmtypes "github.com/evmos/os/x/evm/types" - exampleapp "github.com/realiotech/realio-network/example_chain" - "github.com/realiotech/realio-network/testutil/tx" -) - -// ContractArgs are the params used for calling a smart contract. -type ContractArgs struct { - // Addr is the address of the contract to call. - Addr common.Address - // ABI is the ABI of the contract to call. - ABI abi.ABI - // MethodName is the name of the method to call. - MethodName string - // Args are the arguments to pass to the method. - Args []interface{} -} - -// ContractCallArgs is the arguments for calling a smart contract. -type ContractCallArgs struct { - // Contract are the contract-specific arguments required for the contract call. - Contract ContractArgs - // Nonce is the nonce to use for the transaction. - Nonce *big.Int - // Amount is the amount of the native denomination to send in the transaction. - Amount *big.Int - // GasLimit to use for the transaction - GasLimit uint64 - // PrivKey is the private key to be used for the transaction. - PrivKey cryptotypes.PrivKey -} - -// DeployContract deploys a contract with the provided private key, -// compiled contract data and constructor arguments -func DeployContract( - ctx sdk.Context, - app *exampleapp.ExampleChain, - priv cryptotypes.PrivKey, - queryClientEvm evm.QueryClient, - contract evm.CompiledContract, - constructorArgs ...interface{}, -) (common.Address, error) { - chainID := evmtypes.GetEthChainConfig().ChainID - from := common.BytesToAddress(priv.PubKey().Address().Bytes()) - nonce := app.EVMKeeper.GetNonce(ctx, from) - - ctorArgs, err := contract.ABI.Pack("", constructorArgs...) - if err != nil { - return common.Address{}, err - } - - data := append(contract.Bin, ctorArgs...) //nolint:gocritic - gas, err := tx.GasLimit(ctx, from, data, queryClientEvm) - if err != nil { - return common.Address{}, err - } - - baseFeeRes, err := queryClientEvm.BaseFee(ctx, &evmtypes.QueryBaseFeeRequest{}) - if err != nil { - return common.Address{}, err - } - - msgEthereumTx := evm.NewTx(&evm.EvmTxArgs{ - ChainID: chainID, - Nonce: nonce, - GasLimit: gas, - GasFeeCap: baseFeeRes.BaseFee.BigInt(), - GasTipCap: big.NewInt(1), - Input: data, - Accesses: ðtypes.AccessList{}, - }) - msgEthereumTx.From = from.String() - - res, err := DeliverEthTx(app, priv, msgEthereumTx) - if err != nil { - return common.Address{}, err - } - - if _, err := CheckEthTxResponse(res, app.AppCodec()); err != nil { - return common.Address{}, err - } - - return crypto.CreateAddress(from, nonce), nil -} - -// DeployContractWithFactory deploys a contract using a contract factory -// with the provided factoryAddress -func DeployContractWithFactory( - ctx sdk.Context, - exampleApp *exampleapp.ExampleChain, - priv cryptotypes.PrivKey, - factoryAddress common.Address, -) (common.Address, abci.ExecTxResult, error) { - chainID := evmtypes.GetEthChainConfig().ChainID - from := common.BytesToAddress(priv.PubKey().Address().Bytes()) - factoryNonce := exampleApp.EVMKeeper.GetNonce(ctx, factoryAddress) - nonce := exampleApp.EVMKeeper.GetNonce(ctx, from) - - msgEthereumTx := evm.NewTx(&evm.EvmTxArgs{ - ChainID: chainID, - Nonce: nonce, - To: &factoryAddress, - GasLimit: uint64(100000), - GasPrice: big.NewInt(1000000000), - }) - msgEthereumTx.From = from.String() - - res, err := DeliverEthTx(exampleApp, priv, msgEthereumTx) - if err != nil { - return common.Address{}, abci.ExecTxResult{}, err - } - - if _, err := CheckEthTxResponse(res, exampleApp.AppCodec()); err != nil { - return common.Address{}, abci.ExecTxResult{}, err - } - - return crypto.CreateAddress(factoryAddress, factoryNonce), res, err -} - -// CheckEthTxResponse checks that the transaction was executed successfully -func CheckEthTxResponse(r abci.ExecTxResult, cdc codec.Codec) ([]*evm.MsgEthereumTxResponse, error) { - if !r.IsOK() { - return nil, fmt.Errorf("tx failed. Code: %d, Logs: %s", r.Code, r.Log) - } - - var txData sdk.TxMsgData - if err := cdc.Unmarshal(r.Data, &txData); err != nil { - return nil, err - } - - if len(txData.MsgResponses) == 0 { - return nil, fmt.Errorf("no message responses found") - } - - responses := make([]*evm.MsgEthereumTxResponse, 0, len(txData.MsgResponses)) - for i := range txData.MsgResponses { - var res evm.MsgEthereumTxResponse - if err := proto.Unmarshal(txData.MsgResponses[i].Value, &res); err != nil { - return nil, err - } - - if res.Failed() { - return nil, fmt.Errorf("tx failed. VmError: %s", res.VmError) - } - responses = append(responses, &res) - } - - return responses, nil -} diff --git a/example_chain/testutil/eth_setup.go b/example_chain/testutil/eth_setup.go deleted file mode 100644 index a8d00f15..00000000 --- a/example_chain/testutil/eth_setup.go +++ /dev/null @@ -1,205 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testutil - -import ( - "encoding/json" - "time" - - evmostypes "github.com/evmos/os/types" - - "cosmossdk.io/log" - "cosmossdk.io/math" - abci "github.com/cometbft/cometbft/abci/types" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - cmtypes "github.com/cometbft/cometbft/types" - dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/cosmos/cosmos-sdk/testutil/mock" - simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - exampleapp "github.com/realiotech/realio-network/example_chain" -) - -// DefaultConsensusParams defines the default Tendermint consensus params used in -// Evmos testing. -var DefaultConsensusParams = &tmproto.ConsensusParams{ - Block: &tmproto.BlockParams{ - MaxBytes: 200000, - MaxGas: -1, // no limit - }, - Evidence: &tmproto.EvidenceParams{ - MaxAgeNumBlocks: 302400, - MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration - MaxBytes: 10000, - }, - Validator: &tmproto.ValidatorParams{ - PubKeyTypes: []string{ - cmtypes.ABCIPubKeyTypeEd25519, - }, - }, -} - -// EthDefaultConsensusParams defines the default Tendermint consensus params used in -// evmOS app testing. -// -// TODO: currently not used -var EthDefaultConsensusParams = &cmtypes.ConsensusParams{ - Block: cmtypes.BlockParams{ - MaxBytes: 200000, - MaxGas: -1, // no limit - }, - Evidence: cmtypes.EvidenceParams{ - MaxAgeNumBlocks: 302400, - MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration - MaxBytes: 10000, - }, - Validator: cmtypes.ValidatorParams{ - PubKeyTypes: []string{ - cmtypes.ABCIPubKeyTypeEd25519, - }, - }, -} - -// EthSetup initializes a new evmOS application. A Nop logger is set in ExampleChain. -func EthSetup(isCheckTx bool, chainID string, patchGenesis func(*exampleapp.ExampleChain, evmostypes.GenesisState) evmostypes.GenesisState) *exampleapp.ExampleChain { - return EthSetupWithDB(isCheckTx, chainID, patchGenesis, dbm.NewMemDB()) -} - -// EthSetupWithDB initializes a new ExampleChain. A Nop logger is set in ExampleChain. -func EthSetupWithDB(isCheckTx bool, chainID string, patchGenesis func(*exampleapp.ExampleChain, evmostypes.GenesisState) evmostypes.GenesisState, db dbm.DB) *exampleapp.ExampleChain { - app := exampleapp.NewExampleApp(log.NewNopLogger(), - db, - nil, - true, - simtestutil.NewAppOptionsWithFlagHome(exampleapp.DefaultNodeHome), - exampleapp.EvmosAppOptions, - baseapp.SetChainID(chainID), - ) - if !isCheckTx { - // init chain must be called to stop deliverState from being nil - genesisState := NewTestGenesisState(app) - if patchGenesis != nil { - genesisState = patchGenesis(app, genesisState) - } - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - if err != nil { - panic(err) - } - - // Initialize the chain - app.InitChain( - &abci.RequestInitChain{ - ChainId: chainID, - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - } - - return app -} - -// NewTestGenesisState generate genesis state with single validator -// -// It is also setting up the EVM parameters to use sensible defaults. -// -// TODO: are these different genesis functions necessary or can they all be refactored into one? -// there's also other genesis state functions; some like app.DefaultGenesis() or others in test helpers only. -func NewTestGenesisState(app *exampleapp.ExampleChain) evmostypes.GenesisState { - privVal := mock.NewPV() - pubKey, err := privVal.GetPubKey() - if err != nil { - panic(err) - } - // create validator set with single validator - validator := cmtypes.NewValidator(pubKey, 1) - valSet := cmtypes.NewValidatorSet([]*cmtypes.Validator{validator}) - - // generate genesis account - senderPrivKey := secp256k1.GenPrivKey() - acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0) - balance := banktypes.Balance{ - Address: acc.GetAddress().String(), - Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100000000000000))), - } - - genesisState := app.DefaultGenesis() - return genesisStateWithValSet(app.AppCodec(), genesisState, valSet, []authtypes.GenesisAccount{acc}, balance) -} - -func genesisStateWithValSet(codec codec.Codec, genesisState evmostypes.GenesisState, - valSet *cmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, - balances ...banktypes.Balance, -) evmostypes.GenesisState { - // set genesis accounts - authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) - genesisState[authtypes.ModuleName] = codec.MustMarshalJSON(authGenesis) - - validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) - delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - - bondAmt := sdk.DefaultPowerReduction - - for _, val := range valSet.Validators { - pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) - if err != nil { - panic(err) - } - pkAny, err := codectypes.NewAnyWithValue(pk) - if err != nil { - panic(err) - } - validator := stakingtypes.Validator{ - OperatorAddress: sdk.ValAddress(val.Address).String(), - ConsensusPubkey: pkAny, - Jailed: false, - Status: stakingtypes.Bonded, - Tokens: bondAmt, - DelegatorShares: math.LegacyOneDec(), - Description: stakingtypes.Description{}, - UnbondingHeight: int64(0), - UnbondingTime: time.Unix(0, 0).UTC(), - Commission: stakingtypes.NewCommission(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec()), - MinSelfDelegation: math.ZeroInt(), - } - validators = append(validators, validator) - delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress().String(), val.Address.String(), math.LegacyOneDec())) - } - // set validators and delegations - stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) - genesisState[stakingtypes.ModuleName] = codec.MustMarshalJSON(stakingGenesis) - - totalSupply := sdk.NewCoins() - for _, b := range balances { - // add genesis acc tokens to total supply - totalSupply = totalSupply.Add(b.Coins...) - } - - for range delegations { - // add delegated tokens to total supply - totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)) - } - - // add bonded amount to bonded pool module account - balances = append(balances, banktypes.Balance{ - Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), - Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, - }) - - // update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.SendEnabled{}) - genesisState[banktypes.ModuleName] = codec.MustMarshalJSON(bankGenesis) - - return genesisState -} diff --git a/example_chain/testutil/fund.go b/example_chain/testutil/fund.go deleted file mode 100644 index d0efaba9..00000000 --- a/example_chain/testutil/fund.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testutil - -import ( - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - "github.com/evmos/os/testutil/constants" -) - -// FundAccount is a utility function that funds an account by minting and -// sending the coins to the address. -func FundAccount(ctx sdk.Context, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, amounts sdk.Coins) error { - if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil { - return err - } - - return bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts) -} - -// FundAccountWithBaseDenom is a utility function that uses the FundAccount function -// to fund an account with the default denomination. -// -// TODO: as per Freddy these methods should be replaced with a bank transfer from a main account, not by minting in the process -func FundAccountWithBaseDenom(ctx sdk.Context, bankKeeper bankkeeper.Keeper, addr sdk.AccAddress, amount int64) error { - coins := sdk.NewCoins( - sdk.NewCoin(constants.ExampleAttoDenom, math.NewInt(amount)), - ) - return FundAccount(ctx, bankKeeper, addr, coins) -} - -// FundModuleAccount is a utility function that funds a module account by -// minting and sending the coins to the address. -func FundModuleAccount(ctx sdk.Context, bankKeeper bankkeeper.Keeper, recipientMod string, amounts sdk.Coins) error { - if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, amounts); err != nil { - return err - } - - return bankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, recipientMod, amounts) -} diff --git a/example_chain/testutil/gas.go b/example_chain/testutil/gas.go deleted file mode 100644 index 5410c448..00000000 --- a/example_chain/testutil/gas.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testutil - -import ( - "cosmossdk.io/math" -) - -var ( - // ExampleMinGasPrices defines 20B related to atto units as the minimum gas price value on the fee market module. - // See https://commonwealth.im/evmos/discussion/5073-global-min-gas-price-value-for-cosmos-sdk-and-evm-transaction-choosing-a-value for reference - ExampleMinGasPrices = math.LegacyNewDec(20_000_000_000) - - // ExampleMinGasMultiplier defines the min gas multiplier value on the fee market module. - // 50% of the leftover gas will be refunded - ExampleMinGasMultiplier = math.LegacyNewDecWithPrec(5, 1) -) diff --git a/example_chain/testutil/integration.go b/example_chain/testutil/integration.go deleted file mode 100644 index 33f2e15c..00000000 --- a/example_chain/testutil/integration.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright Tharsis Labs Ltd.(Evmos) -// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE) - -package testutil - -import ( - "strconv" - - errorsmod "cosmossdk.io/errors" - "cosmossdk.io/math" - abci "github.com/cometbft/cometbft/abci/types" - sdk "github.com/cosmos/cosmos-sdk/types" - govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/evmos/os/crypto/ethsecp256k1" - exampleapp "github.com/realiotech/realio-network/example_chain" -) - -// SubmitProposal delivers a submit proposal tx for a given gov content. -// Depending on the content type, the eventNum needs to specify submit_proposal -// event. -func SubmitProposal( - ctx sdk.Context, - appEvmos *exampleapp.ExampleChain, - pk *ethsecp256k1.PrivKey, - content govv1beta1.Content, - eventNum int, -) (id uint64, err error) { - accountAddress := sdk.AccAddress(pk.PubKey().Address().Bytes()) - stakeDenom := stakingtypes.DefaultParams().BondDenom - - deposit := sdk.NewCoins(sdk.NewCoin(stakeDenom, math.NewInt(100000000))) - msg, err := govv1beta1.NewMsgSubmitProposal(content, deposit, accountAddress) - if err != nil { - return id, err - } - res, err := DeliverTx(ctx, appEvmos, pk, nil, msg) - if err != nil { - return id, err - } - - submitEvent := res.GetEvents()[eventNum] - if submitEvent.Type != "submit_proposal" || submitEvent.Attributes[0].Key != "proposal_id" { - return id, errorsmod.Wrapf(errorsmod.Error{}, "eventNumber %d in SubmitProposal calls %s instead of submit_proposal", eventNum, submitEvent.Type) - } - - return strconv.ParseUint(submitEvent.Attributes[0].Value, 10, 64) -} - -// Delegate delivers a delegate tx -func Delegate( - ctx sdk.Context, - appEvmos *exampleapp.ExampleChain, - priv *ethsecp256k1.PrivKey, - delegateAmount sdk.Coin, - validator stakingtypes.Validator, -) (abci.ExecTxResult, error) { - accountAddress := sdk.AccAddress(priv.PubKey().Address().Bytes()) - - val, err := sdk.ValAddressFromBech32(validator.OperatorAddress) - if err != nil { - return abci.ExecTxResult{}, err - } - - delegateMsg := stakingtypes.NewMsgDelegate(accountAddress.String(), val.String(), delegateAmount) - return DeliverTx(ctx, appEvmos, priv, nil, delegateMsg) -} - -// Vote delivers a vote tx with the VoteOption "yes" -func Vote( - ctx sdk.Context, - appEvmos *exampleapp.ExampleChain, - priv *ethsecp256k1.PrivKey, - proposalID uint64, - voteOption govv1beta1.VoteOption, -) (abci.ExecTxResult, error) { - accountAddress := sdk.AccAddress(priv.PubKey().Address().Bytes()) - - voteMsg := govv1beta1.NewMsgVote(accountAddress, proposalID, voteOption) - return DeliverTx(ctx, appEvmos, priv, nil, voteMsg) -} diff --git a/testutil/integration/os/network/network.go b/testutil/integration/os/network/network.go index 5ea49b17..5ad88585 100644 --- a/testutil/integration/os/network/network.go +++ b/testutil/integration/os/network/network.go @@ -23,7 +23,7 @@ import ( minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" gethparams "github.com/ethereum/go-ethereum/params" - chainutil "github.com/realiotech/realio-network/example_chain/testutil" + chainutil "github.com/evmos/os/example_chain/testutil" commonnetwork "github.com/evmos/os/testutil/integration/common/network" "github.com/evmos/os/types" erc20types "github.com/evmos/os/x/erc20/types"