From da45a12052c0a67c7905ababab96b1ba0f7c985b Mon Sep 17 00:00:00 2001 From: kenta92115 Date: Fri, 29 Mar 2024 01:45:16 +0000 Subject: [PATCH] chore: refactor --- app/ante/eip712.go | 9 ++-- app/ante/fee_checker.go | 5 +- app/ante/fee_checker_test.go | 6 +-- app/app.go | 7 --- cmd/swisstronikd/cmd/root.go | 6 +-- crypto/hd/benchmark_test.go | 2 +- encoding/config.go | 6 +-- ethereum/eip712/eip712_test.go | 3 +- ethereum/eip712/encoding.go | 10 ++-- indexer/kv_indexer.go | 14 +++--- indexer/kv_indexer_test.go | 4 +- rpc/apis.go | 24 +++++----- rpc/backend/backend.go | 13 ++--- rpc/backend/call_tx.go | 11 +++-- rpc/backend/node_info.go | 7 +-- rpc/backend/tx_info.go | 11 +++-- rpc/namespaces/ethereum/eth/api.go | 4 +- rpc/namespaces/ethereum/net/api.go | 4 +- rpc/namespaces/ethereum/personal/api.go | 6 +-- rpc/stream/rpc.go | 6 ++- rpc/types/block.go | 6 +-- rpc/types/events.go | 7 +-- server/indexer_service.go | 3 +- server/json_rpc.go | 2 +- tests/rpc/rpc_test.go | 5 +- testutil/network/network.go | 19 ++++---- testutil/network/network_test.go | 2 +- x/evm/types/utils.go | 63 ++++--------------------- x/feemarket/keeper/keeper_test.go | 5 +- 29 files changed, 113 insertions(+), 157 deletions(-) diff --git a/app/ante/eip712.go b/app/ante/eip712.go index 814eb724..d1874bcd 100644 --- a/app/ante/eip712.go +++ b/app/ante/eip712.go @@ -32,7 +32,6 @@ import ( "swisstronik/crypto/ethsecp256k1" "swisstronik/ethereum/eip712" - ethermint "swisstronik/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/secp256k1" @@ -40,6 +39,8 @@ import ( evmtypes "swisstronik/x/evm/types" + swisstroniktypes "swisstronik/types" + txsigning "cosmossdk.io/x/tx/signing" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -48,7 +49,7 @@ var ethermintCodec codec.ProtoCodecMarshaler func init() { registry := codectypes.NewInterfaceRegistry() - ethermint.RegisterInterfaces(registry) + swisstroniktypes.RegisterInterfaces(registry) ethermintCodec = codec.NewProtoCodec(registry) } @@ -240,7 +241,7 @@ func VerifySignature( msgs, tx.GetMemo(), ) - signerChainID, err := ethermint.ParseChainID(signerData.ChainID) + signerChainID, err := swisstroniktypes.ParseChainID(signerData.ChainID) if err != nil { return errorsmod.Wrapf(err, "failed to parse chain-id: %s", signerData.ChainID) } @@ -254,7 +255,7 @@ func VerifySignature( return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "tx doesnt contain expected amount of extension options") } - extOpt, ok := opts[0].GetCachedValue().(*ethermint.ExtensionOptionsWeb3Tx) + extOpt, ok := opts[0].GetCachedValue().(*swisstroniktypes.ExtensionOptionsWeb3Tx) if !ok { return errorsmod.Wrap(errortypes.ErrUnknownExtensionOptions, "unknown extension option") } diff --git a/app/ante/fee_checker.go b/app/ante/fee_checker.go index 7aac75fd..21d6319f 100644 --- a/app/ante/fee_checker.go +++ b/app/ante/fee_checker.go @@ -22,9 +22,10 @@ import ( errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" - ethermint "swisstronik/types" "swisstronik/x/evm/types" + swisstroniktypes "swisstronik/types" + sdk "github.com/cosmos/cosmos-sdk/types" errortypes "github.com/cosmos/cosmos-sdk/types/errors" authante "github.com/cosmos/cosmos-sdk/x/auth/ante" @@ -66,7 +67,7 @@ func NewDynamicFeeChecker(k DynamicFeeEVMKeeper) authante.TxFeeChecker { // get the priority tip cap from the extension option. if hasExtOptsTx, ok := tx.(authante.HasExtensionOptionsTx); ok { for _, opt := range hasExtOptsTx.GetExtensionOptions() { - if extOpt, ok := opt.GetCachedValue().(*ethermint.ExtensionOptionDynamicFeeTx); ok { + if extOpt, ok := opt.GetCachedValue().(*swisstroniktypes.ExtensionOptionDynamicFeeTx); ok { maxPriorityPrice = extOpt.MaxPriorityPrice break } diff --git a/app/ante/fee_checker_test.go b/app/ante/fee_checker_test.go index 68b15cf5..5142fe2f 100644 --- a/app/ante/fee_checker_test.go +++ b/app/ante/fee_checker_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/require" "swisstronik/encoding" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" "swisstronik/x/evm/types" evmtypes "swisstronik/x/evm/types" @@ -173,7 +173,7 @@ func TestSDKTxFeeChecker(t *testing.T) { txBuilder.SetGasLimit(1) txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("uswtr", sdkmath.NewInt(10).Mul(types.DefaultPriorityReduction)))) - option, err := codectypes.NewAnyWithValue(ðermint.ExtensionOptionDynamicFeeTx{}) + option, err := codectypes.NewAnyWithValue(&swisstroniktypes.ExtensionOptionDynamicFeeTx{}) require.NoError(t, err) txBuilder.SetExtensionOptions(option) return txBuilder.GetTx() @@ -193,7 +193,7 @@ func TestSDKTxFeeChecker(t *testing.T) { txBuilder.SetGasLimit(1) txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin("uswtr", sdkmath.NewInt(10).Mul(types.DefaultPriorityReduction).Add(sdkmath.NewInt(10))))) - option, err := codectypes.NewAnyWithValue(ðermint.ExtensionOptionDynamicFeeTx{ + option, err := codectypes.NewAnyWithValue(&swisstroniktypes.ExtensionOptionDynamicFeeTx{ MaxPriorityPrice: sdkmath.NewInt(5).Mul(types.DefaultPriorityReduction), }) require.NoError(t, err) diff --git a/app/app.go b/app/app.go index 510e3f9f..e972e111 100644 --- a/app/app.go +++ b/app/app.go @@ -139,7 +139,6 @@ import ( "swisstronik/ethereum/eip712" sdkmath "cosmossdk.io/math" - "cosmossdk.io/simapp" addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/types/mempool" "github.com/cosmos/cosmos-sdk/x/consensus" @@ -312,12 +311,6 @@ func New( interfaceRegistry := encodingConfig.InterfaceRegistry eip712.SetEncodingConfig(encodingConfig) - // create and set dummy vote extension handler - voteExtOp := func(bApp *baseapp.BaseApp) { - voteExtHandler := simapp.NewVoteExtensionHandler() - voteExtHandler.SetHandlers(bApp) - } - baseAppOptions = append(baseAppOptions, voteExtOp, baseapp.SetOptimisticExecution()) // Setup Mempool and Proposal Handlers baseAppOptions = append(baseAppOptions, func(app *baseapp.BaseApp) { mempool := mempool.NoOpMempool{} diff --git a/cmd/swisstronikd/cmd/root.go b/cmd/swisstronikd/cmd/root.go index 1d5ce8e8..3809158d 100644 --- a/cmd/swisstronikd/cmd/root.go +++ b/cmd/swisstronikd/cmd/root.go @@ -56,7 +56,7 @@ import ( "swisstronik/utils" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" "github.com/cosmos/cosmos-sdk/client/snapshot" "github.com/cosmos/cosmos-sdk/codec" @@ -71,7 +71,7 @@ import ( const ShortBlockWindow uint32 = 20 // NewRootCmd creates a new root command for a Cosmos SDK application -func NewRootCmd() (*cobra.Command, ethermint.EncodingConfig) { +func NewRootCmd() (*cobra.Command, swisstroniktypes.EncodingConfig) { // Initialize the SDK config the first before doing anything else. InitSDKConfig() @@ -281,7 +281,7 @@ func addModuleInitFlags(startCmd *cobra.Command) { } type appCreator struct { - encodingConfig ethermint.EncodingConfig + encodingConfig swisstroniktypes.EncodingConfig } // newApp creates a new Cosmos SDK app diff --git a/crypto/hd/benchmark_test.go b/crypto/hd/benchmark_test.go index 59a5885a..600cdff4 100644 --- a/crypto/hd/benchmark_test.go +++ b/crypto/hd/benchmark_test.go @@ -1,9 +1,9 @@ package hd import ( + ethermint "swisstronik/types" "testing" - ethermint "swisstronik/types" "github.com/cosmos/cosmos-sdk/crypto/keyring" ) diff --git a/encoding/config.go b/encoding/config.go index fa7c9876..a2c31ff9 100644 --- a/encoding/config.go +++ b/encoding/config.go @@ -25,7 +25,7 @@ import ( enccodec "swisstronik/encoding/codec" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" "cosmossdk.io/x/tx/signing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -47,7 +47,7 @@ func customGetSignerFn(path string) func(msg proto.Message) ([][]byte, error) { } // MakeConfig creates an EncodingConfig for testing -func MakeConfig() ethermint.EncodingConfig { +func MakeConfig() swisstroniktypes.EncodingConfig { cdc := amino.NewLegacyAmino() addrPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix() valAddrPrefix := sdk.GetConfig().GetBech32ValidatorAddrPrefix() @@ -68,7 +68,7 @@ func MakeConfig() ethermint.EncodingConfig { } codec := amino.NewProtoCodec(interfaceRegistry) - encodingConfig := ethermint.EncodingConfig{ + encodingConfig := swisstroniktypes.EncodingConfig{ InterfaceRegistry: interfaceRegistry, Codec: codec, TxConfig: tx.NewTxConfig(codec, tx.DefaultSignModes), diff --git a/ethereum/eip712/eip712_test.go b/ethereum/eip712/eip712_test.go index a16c0fdd..5c40dbfd 100644 --- a/ethereum/eip712/eip712_test.go +++ b/ethereum/eip712/eip712_test.go @@ -7,6 +7,7 @@ import ( "cosmossdk.io/math" "swisstronik/ethereum/eip712" + ethermint "swisstronik/types" "github.com/cosmos/cosmos-sdk/client" @@ -25,8 +26,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx/signing" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" - ethermint "swisstronik/types" - v1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1" txv1beta1 "cosmossdk.io/api/cosmos/tx/v1beta1" diff --git a/ethereum/eip712/encoding.go b/ethereum/eip712/encoding.go index 1fb8b9b2..dcb741f5 100644 --- a/ethereum/eip712/encoding.go +++ b/ethereum/eip712/encoding.go @@ -25,10 +25,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" txTypes "github.com/cosmos/cosmos-sdk/types/tx" - ethermint "swisstronik/types" - apitypes "github.com/ethereum/go-ethereum/signer/core/apitypes" + swisstroniktypes "swisstronik/types" + "github.com/cosmos/cosmos-sdk/codec" ) @@ -46,7 +46,7 @@ var ( // The process of unmarshaling SignDoc bytes into a SignDoc object requires having a codec // populated with all relevant message types. As a result, we must call this method on app // initialization with the app's encoding config. -func SetEncodingConfig(cfg ethermint.EncodingConfig) { +func SetEncodingConfig(cfg swisstroniktypes.EncodingConfig) { aminoCodec = cfg.Amino protoCodec = codec.NewProtoCodec(cfg.InterfaceRegistry) } @@ -137,7 +137,7 @@ func decodeAminoSignDoc(signDocBytes []byte) (apitypes.TypedData, error) { FeePayer: feePayer, } - chainID, err := ethermint.ParseChainID(aminoDoc.ChainID) + chainID, err := swisstroniktypes.ParseChainID(aminoDoc.ChainID) if err != nil { return apitypes.TypedData{}, errors.New("invalid chain ID passed as argument") } @@ -207,7 +207,7 @@ func decodeProtobufSignDoc(signDocBytes []byte) (apitypes.TypedData, error) { signerInfo := authInfo.SignerInfos[0] - chainID, err := ethermint.ParseChainID(signDoc.ChainId) + chainID, err := swisstroniktypes.ParseChainID(signDoc.ChainId) if err != nil { return apitypes.TypedData{}, fmt.Errorf("invalid chain ID passed as argument: %w", err) } diff --git a/indexer/kv_indexer.go b/indexer/kv_indexer.go index 257a089b..1c3e35e3 100644 --- a/indexer/kv_indexer.go +++ b/indexer/kv_indexer.go @@ -31,7 +31,7 @@ import ( authante "github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/ethereum/go-ethereum/common" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" evmtypes "swisstronik/x/evm/types" ) @@ -43,7 +43,7 @@ const ( TxIndexKeyLength = 1 + 8 + 8 ) -var _ ethermint.EVMTxIndexer = &KVIndexer{} +var _ swisstroniktypes.EVMTxIndexer = &KVIndexer{} // KVIndexer implements a eth tx indexer on a KV db. type KVIndexer struct { @@ -97,7 +97,7 @@ func (kv *KVIndexer) IndexBlock(block *tmtypes.Block, txResults []*abci.ExecTxRe ethMsg := msg.(*evmtypes.MsgHandleTx) txHash := common.HexToHash(ethMsg.Hash) - txResult := ethermint.TxResult{ + txResult := swisstroniktypes.TxResult{ Height: height, TxIndex: uint32(txIndex), MsgIndex: uint32(msgIndex), @@ -147,7 +147,7 @@ func (kv *KVIndexer) FirstIndexedBlock() (int64, error) { } // GetByTxHash finds eth tx by eth tx hash -func (kv *KVIndexer) GetByTxHash(hash common.Hash) (*ethermint.TxResult, error) { +func (kv *KVIndexer) GetByTxHash(hash common.Hash) (*swisstroniktypes.TxResult, error) { bz, err := kv.db.Get(TxHashKey(hash)) if err != nil { return nil, errorsmod.Wrapf(err, "GetByTxHash %s", hash.Hex()) @@ -155,7 +155,7 @@ func (kv *KVIndexer) GetByTxHash(hash common.Hash) (*ethermint.TxResult, error) if len(bz) == 0 { return nil, fmt.Errorf("tx not found, hash: %s", hash.Hex()) } - var txKey ethermint.TxResult + var txKey swisstroniktypes.TxResult if err := kv.clientCtx.Codec.Unmarshal(bz, &txKey); err != nil { return nil, errorsmod.Wrapf(err, "GetByTxHash %s", hash.Hex()) } @@ -163,7 +163,7 @@ func (kv *KVIndexer) GetByTxHash(hash common.Hash) (*ethermint.TxResult, error) } // GetByBlockAndIndex finds eth tx by block number and eth tx index -func (kv *KVIndexer) GetByBlockAndIndex(blockNumber int64, txIndex int32) (*ethermint.TxResult, error) { +func (kv *KVIndexer) GetByBlockAndIndex(blockNumber int64, txIndex int32) (*swisstroniktypes.TxResult, error) { bz, err := kv.db.Get(TxIndexKey(blockNumber, txIndex)) if err != nil { return nil, errorsmod.Wrapf(err, "GetByBlockAndIndex %d %d", blockNumber, txIndex) @@ -226,7 +226,7 @@ func isEthTx(tx sdk.Tx) bool { } // saveTxResult index the txResult into the kv db batch -func saveTxResult(codec codec.Codec, batch dbm.Batch, txHash common.Hash, txResult *ethermint.TxResult) error { +func saveTxResult(codec codec.Codec, batch dbm.Batch, txHash common.Hash, txResult *swisstroniktypes.TxResult) error { bz := codec.MustMarshal(txResult) if err := batch.Set(TxHashKey(txHash), bz); err != nil { return errorsmod.Wrap(err, "set tx-hash key") diff --git a/indexer/kv_indexer_test.go b/indexer/kv_indexer_test.go index 93afb485..aa31730a 100644 --- a/indexer/kv_indexer_test.go +++ b/indexer/kv_indexer_test.go @@ -10,7 +10,7 @@ import ( "swisstronik/tests" "swisstronik/x/evm/types" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" tmlog "cosmossdk.io/log" abci "github.com/cometbft/cometbft/abci/types" @@ -185,6 +185,6 @@ func TestKVIndexer(t *testing.T) { } // MakeEncodingConfig creates the EncodingConfig -func MakeEncodingConfig() ethermint.EncodingConfig { +func MakeEncodingConfig() swisstroniktypes.EncodingConfig { return evmenc.MakeConfig() } diff --git a/rpc/apis.go b/rpc/apis.go index 2ea92181..84c5904d 100644 --- a/rpc/apis.go +++ b/rpc/apis.go @@ -35,7 +35,7 @@ import ( "swisstronik/rpc/namespaces/ethereum/web3" "swisstronik/rpc/namespaces/utils" "swisstronik/rpc/stream" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" ) // RPC namespaces and API version @@ -66,7 +66,7 @@ type APICreator = func( clientCtx client.Context, stream *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) []rpc.API // apiCreators defines the JSON-RPC API namespaces. @@ -78,7 +78,7 @@ func init() { clientCtx client.Context, stream *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) []rpc.API { evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer) return []rpc.API{ @@ -96,7 +96,7 @@ func init() { }, } }, - Web3Namespace: func(*server.Context, client.Context, *stream.RPCStream, bool, ethermint.EVMTxIndexer) []rpc.API { + Web3Namespace: func(*server.Context, client.Context, *stream.RPCStream, bool, swisstroniktypes.EVMTxIndexer) []rpc.API { return []rpc.API{ { Namespace: Web3Namespace, @@ -106,7 +106,7 @@ func init() { }, } }, - NetNamespace: func(_ *server.Context, clientCtx client.Context, _ *stream.RPCStream, _ bool, _ ethermint.EVMTxIndexer) []rpc.API { + NetNamespace: func(_ *server.Context, clientCtx client.Context, _ *stream.RPCStream, _ bool, _ swisstroniktypes.EVMTxIndexer) []rpc.API { return []rpc.API{ { Namespace: NetNamespace, @@ -120,7 +120,7 @@ func init() { clientCtx client.Context, _ *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) []rpc.API { evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer) return []rpc.API{ @@ -132,7 +132,7 @@ func init() { }, } }, - TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *stream.RPCStream, _ bool, _ ethermint.EVMTxIndexer) []rpc.API { + TxPoolNamespace: func(ctx *server.Context, _ client.Context, _ *stream.RPCStream, _ bool, _ swisstroniktypes.EVMTxIndexer) []rpc.API { return []rpc.API{ { Namespace: TxPoolNamespace, @@ -146,7 +146,7 @@ func init() { clientCtx client.Context, _ *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) []rpc.API { evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer) return []rpc.API{ @@ -162,7 +162,7 @@ func init() { clientCtx client.Context, _ *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) []rpc.API { evmBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer) return []rpc.API{ @@ -178,7 +178,7 @@ func init() { clientCtx client.Context, _ *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) []rpc.API { didBackend := backend.NewBackend(ctx, ctx.Logger, clientCtx, allowUnprotectedTxs, indexer) return []rpc.API{ @@ -194,7 +194,7 @@ func init() { _ client.Context, _ *stream.RPCStream, _ bool, - _ ethermint.EVMTxIndexer, + _ swisstroniktypes.EVMTxIndexer, ) []rpc.API { return []rpc.API{ { @@ -213,7 +213,7 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, stream *stream.RPCStream, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, selectedAPIs []string, ) []rpc.API { var apis []rpc.API diff --git a/rpc/backend/backend.go b/rpc/backend/backend.go index 358367d7..3766aec5 100644 --- a/rpc/backend/backend.go +++ b/rpc/backend/backend.go @@ -22,11 +22,12 @@ import ( rpctypes "swisstronik/rpc/types" "swisstronik/server/config" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" didtypes "swisstronik/x/did/types" + swisstroniktypes "swisstronik/types" + "cosmossdk.io/log" sdkmath "cosmossdk.io/math" tmrpctypes "github.com/cometbft/cometbft/rpc/core/types" @@ -121,8 +122,8 @@ type EVMBackend interface { // Tx Info GetTransactionByHash(txHash common.Hash) (*rpctypes.RPCTransaction, error) - GetTxByEthHash(txHash common.Hash) (*ethermint.TxResult, error) - GetTxByTxIndex(height int64, txIndex uint) (*ethermint.TxResult, error) + GetTxByEthHash(txHash common.Hash) (*swisstroniktypes.TxResult, error) + GetTxByTxIndex(height int64, txIndex uint) (*swisstroniktypes.TxResult, error) GetTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) @@ -163,7 +164,7 @@ type Backend struct { chainID *big.Int cfg config.Config allowUnprotectedTxs bool - indexer ethermint.EVMTxIndexer + indexer swisstroniktypes.EVMTxIndexer } // NewBackend creates a new Backend instance for cosmos and ethereum namespaces @@ -172,9 +173,9 @@ func NewBackend( logger log.Logger, clientCtx client.Context, allowUnprotectedTxs bool, - indexer ethermint.EVMTxIndexer, + indexer swisstroniktypes.EVMTxIndexer, ) *Backend { - chainID, err := ethermint.ParseChainID(clientCtx.ChainID) + chainID, err := swisstroniktypes.ParseChainID(clientCtx.ChainID) if err != nil { panic(err) } diff --git a/rpc/backend/call_tx.go b/rpc/backend/call_tx.go index 3a5a67fc..ddfe464f 100644 --- a/rpc/backend/call_tx.go +++ b/rpc/backend/call_tx.go @@ -22,10 +22,11 @@ import ( "fmt" "math/big" - errorsmod "cosmossdk.io/errors" rpctypes "swisstronik/rpc/types" ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" + + errorsmod "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" @@ -310,25 +311,25 @@ func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rp if blockNrOptional != nil { blockNr = *blockNrOptional } - + bz, err := json.Marshal(&args) if err != nil { return 0, err } - + header, err := b.TendermintBlockByNumber(blockNr) if err != nil { // the error message imitates geth behavior return 0, errors.New("header not found") } - + req := evmtypes.EthCallRequest{ Args: bz, GasCap: b.RPCGasCap(), ProposerAddress: sdk.ConsAddress(header.Block.ProposerAddress), ChainId: b.chainID.Int64(), } - + // From ContextWithHeight: if the provided height is 0, // it will return an empty context and the gRPC query will use // the latest block height for querying. diff --git a/rpc/backend/node_info.go b/rpc/backend/node_info.go index 8f3a7d66..554f4188 100644 --- a/rpc/backend/node_info.go +++ b/rpc/backend/node_info.go @@ -24,9 +24,10 @@ import ( "swisstronik/crypto/ethsecp256k1" rpctypes "swisstronik/rpc/types" "swisstronik/server/config" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" + swisstroniktypes "swisstronik/types" + errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" tmtypes "github.com/cometbft/cometbft/types" @@ -337,13 +338,13 @@ func (b *Backend) RPCBlockRangeCap() int32 { func (b *Backend) RPCMinGasPrice() int64 { evmParams, err := b.queryClient.Params(b.ctx, &evmtypes.QueryParamsRequest{}) if err != nil { - return ethermint.DefaultGasPrice + return swisstroniktypes.DefaultGasPrice } minGasPrice := b.cfg.GetMinGasPrices() amt := minGasPrice.AmountOf(evmParams.Params.EvmDenom).TruncateInt64() if amt == 0 { - return ethermint.DefaultGasPrice + return swisstroniktypes.DefaultGasPrice } return amt diff --git a/rpc/backend/tx_info.go b/rpc/backend/tx_info.go index 843e86d5..c11e2840 100644 --- a/rpc/backend/tx_info.go +++ b/rpc/backend/tx_info.go @@ -21,9 +21,10 @@ import ( "math/big" rpctypes "swisstronik/rpc/types" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" + swisstroniktypes "swisstronik/types" + errorsmod "cosmossdk.io/errors" tmrpcclient "github.com/cometbft/cometbft/rpc/client" tmrpctypes "github.com/cometbft/cometbft/rpc/core/types" @@ -139,7 +140,7 @@ func (b *Backend) getTransactionByHashPending(txHash common.Hash) (*rpctypes.RPC } // GetGasUsed returns gasUsed from transaction -func (b *Backend) GetGasUsed(res *ethermint.TxResult, price *big.Int, gas uint64) uint64 { +func (b *Backend) GetGasUsed(res *swisstroniktypes.TxResult, price *big.Int, gas uint64) uint64 { // patch gasUsed if tx is reverted and happened before height on which fixed was introduced // to return real gas charged // more info at https://github.com/evmos/ethermint/pull/1557 @@ -315,7 +316,7 @@ func (b *Backend) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNum // GetTxByEthHash uses `/tx_query` to find transaction by ethereum tx hash // TODO: Don't need to convert once hashing is fixed on Tendermint // https://github.com/cometbft/cometbft/issues/6539 -func (b *Backend) GetTxByEthHash(hash common.Hash) (*ethermint.TxResult, error) { +func (b *Backend) GetTxByEthHash(hash common.Hash) (*swisstroniktypes.TxResult, error) { if b.indexer != nil { return b.indexer.GetByTxHash(hash) } @@ -332,7 +333,7 @@ func (b *Backend) GetTxByEthHash(hash common.Hash) (*ethermint.TxResult, error) } // GetTxByTxIndex uses `/tx_query` to find transaction by tx index of valid ethereum txs -func (b *Backend) GetTxByTxIndex(height int64, index uint) (*ethermint.TxResult, error) { +func (b *Backend) GetTxByTxIndex(height int64, index uint) (*swisstroniktypes.TxResult, error) { if b.indexer != nil { return b.indexer.GetByBlockAndIndex(height, int32(index)) } @@ -352,7 +353,7 @@ func (b *Backend) GetTxByTxIndex(height int64, index uint) (*ethermint.TxResult, } // queryTendermintTxIndexer query tx in tendermint tx indexer -func (b *Backend) queryTendermintTxIndexer(query string, txGetter func(*rpctypes.ParsedTxs) *rpctypes.ParsedTx) (*ethermint.TxResult, error) { +func (b *Backend) queryTendermintTxIndexer(query string, txGetter func(*rpctypes.ParsedTxs) *rpctypes.ParsedTx) (*swisstroniktypes.TxResult, error) { resTxs, err := b.clientCtx.Client.TxSearch(b.ctx, query, false, nil, nil, "") if err != nil { return nil, err diff --git a/rpc/namespaces/ethereum/eth/api.go b/rpc/namespaces/ethereum/eth/api.go index 30cd30b8..557402d0 100644 --- a/rpc/namespaces/ethereum/eth/api.go +++ b/rpc/namespaces/ethereum/eth/api.go @@ -31,7 +31,7 @@ import ( "swisstronik/rpc/backend" rpctypes "swisstronik/rpc/types" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" evmtypes "swisstronik/x/evm/types" ) @@ -314,7 +314,7 @@ func (e *PublicAPI) GetNodePublicKey(blockNrOrHash rpctypes.BlockNumberOrHash) ( // ProtocolVersion returns the supported Ethereum protocol version. func (e *PublicAPI) ProtocolVersion() hexutil.Uint { e.logger.Debug("eth_protocolVersion") - return hexutil.Uint(ethermint.ProtocolVersion) + return hexutil.Uint(swisstroniktypes.ProtocolVersion) } // GasPrice returns the current gas price based on Ethermint's gas price oracle. diff --git a/rpc/namespaces/ethereum/net/api.go b/rpc/namespaces/ethereum/net/api.go index 99f730fb..4e944ed0 100644 --- a/rpc/namespaces/ethereum/net/api.go +++ b/rpc/namespaces/ethereum/net/api.go @@ -19,7 +19,7 @@ import ( "context" "fmt" - ethermint "swisstronik/types" + swisstroniktypes "swisstronik/types" rpcclient "github.com/cometbft/cometbft/rpc/client" "github.com/cosmos/cosmos-sdk/client" @@ -34,7 +34,7 @@ type PublicAPI struct { // NewPublicAPI creates an instance of the public Net Web3 API. func NewPublicAPI(clientCtx client.Context) *PublicAPI { // parse the chainID from a integer string - chainIDEpoch, err := ethermint.ParseChainID(clientCtx.ChainID) + chainIDEpoch, err := swisstroniktypes.ParseChainID(clientCtx.ChainID) if err != nil { panic(err) } diff --git a/rpc/namespaces/ethereum/personal/api.go b/rpc/namespaces/ethereum/personal/api.go index 169008a3..1bc6acfb 100644 --- a/rpc/namespaces/ethereum/personal/api.go +++ b/rpc/namespaces/ethereum/personal/api.go @@ -24,7 +24,6 @@ import ( "swisstronik/rpc/backend" "swisstronik/crypto/hd" - ethermint "swisstronik/types" "cosmossdk.io/log" @@ -36,6 +35,7 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" + swisstroniktypes "swisstronik/types" evmtypes "swisstronik/x/evm/types" ) @@ -43,7 +43,7 @@ import ( type PrivateAccountAPI struct { backend backend.EVMBackend logger log.Logger - hdPathIter ethermint.HDPathIterator + hdPathIter swisstroniktypes.HDPathIterator } // NewAPI creates an instance of the public Personal Eth API. @@ -54,7 +54,7 @@ func NewAPI( cfg := sdk.GetConfig() basePath := cfg.GetFullBIP44Path() - iterator, err := ethermint.NewHDPathIterator(basePath, true) + iterator, err := swisstroniktypes.NewHDPathIterator(basePath, true) if err != nil { panic(err) } diff --git a/rpc/stream/rpc.go b/rpc/stream/rpc.go index 6bf983d8..211b49f6 100644 --- a/rpc/stream/rpc.go +++ b/rpc/stream/rpc.go @@ -193,13 +193,15 @@ func (s *RPCStream) start( s.logger.Error("event data type mismatch", "type", fmt.Sprintf("%T", ev.Data)) continue } - txLogs, err := evmtypes.DecodeTxLogsFromEvents(dataTx.TxResult.Result.Data, uint64(dataTx.TxResult.Height)) + txLogs, err := evmtypes.DecodeTransactionLogs(dataTx.TxResult.Result.Data) if err != nil { s.logger.Error("fail to decode evm tx response", "error", err.Error()) continue } - s.logStream.Add(txLogs...) + // Convert swistronik type tranasction log into ethereum tx log + ethTxLogs := evmtypes.ConvertLogToEthereumType(txLogs) + s.logStream.Add(ethTxLogs...) } if chBlocks == nil && chTx == nil && chLogs == nil { diff --git a/rpc/types/block.go b/rpc/types/block.go index 11a4255a..30025dd9 100644 --- a/rpc/types/block.go +++ b/rpc/types/block.go @@ -30,9 +30,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" + swisstroniktypes "swisstronik/types" - ethermint "swisstronik/types" + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" ) // BlockNumber represents decoding hex string to block values @@ -198,7 +198,7 @@ func (bnh *BlockNumberOrHash) decodeFromString(input string) error { return err } - bnInt, err := ethermint.SafeInt64(blockNumber) + bnInt, err := swisstroniktypes.SafeInt64(blockNumber) if err != nil { return err } diff --git a/rpc/types/events.go b/rpc/types/events.go index 9bdd031a..f2651b69 100644 --- a/rpc/types/events.go +++ b/rpc/types/events.go @@ -19,9 +19,10 @@ import ( "fmt" "strconv" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" + swisstroniktypes "swisstronik/types" + abci "github.com/cometbft/cometbft/abci/types" tmrpctypes "github.com/cometbft/cometbft/rpc/core/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -153,7 +154,7 @@ func ParseTxResult(result *abci.ExecTxResult, tx sdk.Tx) (*ParsedTxs, error) { } // ParseTxIndexerResult parse tm tx result to a format compatible with the custom tx indexer. -func ParseTxIndexerResult(txResult *tmrpctypes.ResultTx, tx sdk.Tx, getter func(*ParsedTxs) *ParsedTx) (*ethermint.TxResult, error) { +func ParseTxIndexerResult(txResult *tmrpctypes.ResultTx, tx sdk.Tx, getter func(*ParsedTxs) *ParsedTx) (*swisstroniktypes.TxResult, error) { txs, err := ParseTxResult(&txResult.TxResult, tx) if err != nil { return nil, fmt.Errorf("failed to parse tx events: block %d, index %d, %v", txResult.Height, txResult.Index, err) @@ -164,7 +165,7 @@ func ParseTxIndexerResult(txResult *tmrpctypes.ResultTx, tx sdk.Tx, getter func( return nil, fmt.Errorf("ethereum tx not found in msgs: block %d, index %d", txResult.Height, txResult.Index) } - return ðermint.TxResult{ + return &swisstroniktypes.TxResult{ Height: txResult.Height, TxIndex: txResult.Index, MsgIndex: uint32(parsedTx.MsgIndex), diff --git a/server/indexer_service.go b/server/indexer_service.go index eab97e37..39ba08d0 100644 --- a/server/indexer_service.go +++ b/server/indexer_service.go @@ -18,14 +18,13 @@ package server import ( "context" "strings" + ethermint "swisstronik/types" "time" "github.com/cometbft/cometbft/libs/service" rpcclient "github.com/cometbft/cometbft/rpc/client" ctypes "github.com/cometbft/cometbft/rpc/core/types" "github.com/cometbft/cometbft/types" - - ethermint "swisstronik/types" ) const ( diff --git a/server/json_rpc.go b/server/json_rpc.go index 06c3328e..64e81484 100644 --- a/server/json_rpc.go +++ b/server/json_rpc.go @@ -26,6 +26,7 @@ import ( "swisstronik/rpc" "swisstronik/rpc/stream" + ethermint "swisstronik/types" rpcclient "github.com/cometbft/cometbft/rpc/client" "github.com/cosmos/cosmos-sdk/client" @@ -34,7 +35,6 @@ import ( ethrpc "github.com/ethereum/go-ethereum/rpc" "swisstronik/server/config" - ethermint "swisstronik/types" ) const ( diff --git a/tests/rpc/rpc_test.go b/tests/rpc/rpc_test.go index 069143b8..a0602803 100644 --- a/tests/rpc/rpc_test.go +++ b/tests/rpc/rpc_test.go @@ -18,9 +18,10 @@ import ( "github.com/stretchr/testify/require" rpctypes "swisstronik/rpc/types" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" + swisstroniktypes "swisstronik/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -136,7 +137,7 @@ func callWithError(method string, params interface{}) (*Response, error) { } func TestEth_protocolVersion(t *testing.T) { - expectedRes := hexutil.Uint(ethermint.ProtocolVersion) + expectedRes := hexutil.Uint(swisstroniktypes.ProtocolVersion) rpcRes := call(t, "eth_protocolVersion", []string{}) diff --git a/testutil/network/network.go b/testutil/network/network.go index 7d3bfa30..494b8edb 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -65,13 +65,14 @@ import ( "swisstronik/crypto/hd" "swisstronik/server/config" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" "swisstronik/app" "swisstronik/encoding" + swisstroniktypes "swisstronik/types" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" ) @@ -149,11 +150,11 @@ func DefaultConfig() Config { TimeoutCommit: 2 * time.Second, ChainID: chainID, NumValidators: 4, - BondDenom: ethermint.SwtrDenom, - MinGasPrices: fmt.Sprintf("0.000006%s", ethermint.SwtrDenom), - AccountTokens: sdk.TokensFromConsensusPower(1000, ethermint.PowerReduction), - StakingTokens: sdk.TokensFromConsensusPower(500, ethermint.PowerReduction), - BondedTokens: sdk.TokensFromConsensusPower(100, ethermint.PowerReduction), + BondDenom: swisstroniktypes.SwtrDenom, + MinGasPrices: fmt.Sprintf("0.000006%s", swisstroniktypes.SwtrDenom), + AccountTokens: sdk.TokensFromConsensusPower(1000, swisstroniktypes.PowerReduction), + StakingTokens: sdk.TokensFromConsensusPower(500, swisstroniktypes.PowerReduction), + BondedTokens: sdk.TokensFromConsensusPower(100, swisstroniktypes.PowerReduction), PruningStrategy: pruningtypes.PruningOptionNothing, CleanupDir: true, SigningAlgo: string(hd.EthSecp256k1Type), @@ -245,7 +246,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { l.Log("acquiring test network lock") lock.Lock() - if !ethermint.IsValidChainID(cfg.ChainID) { + if !swisstroniktypes.IsValidChainID(cfg.ChainID) { return nil, fmt.Errorf("invalid chain-id: %s", cfg.ChainID) } @@ -433,7 +434,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { genFiles = append(genFiles, cmtCfg.GenesisFile()) genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: balances.Sort()}) - genAccounts = append(genAccounts, ðermint.EthAccount{ + genAccounts = append(genAccounts, &swisstroniktypes.EthAccount{ BaseAccount: authtypes.NewBaseAccount(addr, nil, 0, 0), CodeHash: common.BytesToHash(evmtypes.EmptyCodeHash).Hex(), }) @@ -491,7 +492,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { return nil, err } - customAppTemplate, _ := config.AppConfig(ethermint.SwtrDenom) + customAppTemplate, _ := config.AppConfig(swisstroniktypes.SwtrDenom) srvconfig.SetConfigTemplate(customAppTemplate) srvconfig.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), appCfg) diff --git a/testutil/network/network_test.go b/testutil/network/network_test.go index ab15dc41..00824c3f 100644 --- a/testutil/network/network_test.go +++ b/testutil/network/network_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/suite" - "github.com/evmos/ethermint/testutil/network" + "swisstronik/testutil/network" ) type IntegrationTestSuite struct { diff --git a/x/evm/types/utils.go b/x/evm/types/utils.go index 81335a66..df5eefaa 100644 --- a/x/evm/types/utils.go +++ b/x/evm/types/utils.go @@ -68,66 +68,19 @@ func DecodeTransactionLogs(data []byte) (TransactionLogs, error) { if err != nil { return TransactionLogs{}, err } - return logs, nil -} - -// DecodeTxResponses decodes a protobuf-encoded byte slice into TxResponses -func DecodeTxResponses(in []byte) ([]*MsgEthereumTxResponse, error) { - var txMsgData sdk.TxMsgData - if err := proto.Unmarshal(in, &txMsgData); err != nil { - return nil, err - } - responses := make([]*MsgEthereumTxResponse, 0, len(txMsgData.MsgResponses)) - for _, res := range txMsgData.MsgResponses { - var response MsgEthereumTxResponse - if res.TypeUrl != "/"+proto.MessageName(&response) { - continue - } - err := proto.Unmarshal(res.Value, &response) - if err != nil { - return nil, errorsmod.Wrap(err, "failed to unmarshal tx response message data") - } - responses = append(responses, &response) - } - return responses, nil -} -func logsFromTxResponse(dst []*ethtypes.Log, rsp *MsgEthereumTxResponse, blockNumber uint64) []*ethtypes.Log { - if len(rsp.Logs) == 0 { - return nil - } - - if dst == nil { - dst = make([]*ethtypes.Log, 0, len(rsp.Logs)) - } - - txHash := common.HexToHash(rsp.Hash) - for _, log := range rsp.Logs { - // fill in the tx/block informations - l := log.ToEthereum() - l.TxHash = txHash - l.BlockNumber = blockNumber - // TODO: - // Comment the following as we don't have BlockHash in MsgEthereumTxResponse - // if len(rsp.BlockHash) > 0 { - // l.BlockHash = common.BytesToHash(rsp.BlockHash) - // } - dst = append(dst, l) - } - return dst + return logs, nil } -// DecodeTxLogsFromEvents decodes a protobuf-encoded byte slice into ethereum logs -func DecodeTxLogsFromEvents(in []byte, blockNumber uint64) ([]*ethtypes.Log, error) { - txResponses, err := DecodeTxResponses(in) - if err != nil { - return nil, err - } +// ConvertLogToEthereumType convert transaction log into go ethereum log type +func ConvertLogToEthereumType(txLog TransactionLogs) []*ethtypes.Log { var logs []*ethtypes.Log - for _, response := range txResponses { - logs = logsFromTxResponse(logs, response, blockNumber) + for _, log := range txLog.Logs { + ethLog := log.ToEthereum() + logs = append(logs, ethLog) } - return logs, nil + + return logs } // UnwrapEthereumMsg extract MsgEthereumTx from wrapping sdk.Tx diff --git a/x/feemarket/keeper/keeper_test.go b/x/feemarket/keeper/keeper_test.go index ac607571..f78ac8a8 100644 --- a/x/feemarket/keeper/keeper_test.go +++ b/x/feemarket/keeper/keeper_test.go @@ -25,10 +25,11 @@ import ( "swisstronik/crypto/ethsecp256k1" "swisstronik/encoding" "swisstronik/tests" - ethermint "swisstronik/types" evmtypes "swisstronik/x/evm/types" "swisstronik/x/feemarket/types" + swisstroniktypes "swisstronik/types" + sdkmath "cosmossdk.io/math" "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" @@ -115,7 +116,7 @@ func (suite *KeeperTestSuite) SetupApp(checkTx bool) { types.RegisterQueryServer(queryHelper, suite.app.FeeMarketKeeper) suite.queryClient = types.NewQueryClient(queryHelper) - acc := ðermint.EthAccount{ + acc := &swisstroniktypes.EthAccount{ BaseAccount: authtypes.NewBaseAccount(sdk.AccAddress(suite.address.Bytes()), nil, 0, 0), CodeHash: common.BytesToHash(crypto.Keccak256(nil)).String(), }