From 5085dad7e03a879fa04e8f73045682aed0265f97 Mon Sep 17 00:00:00 2001 From: Unique Divine <51418232+Unique-Divine@users.noreply.github.com> Date: Sat, 14 Sep 2024 12:51:20 +0900 Subject: [PATCH] refactor(rpc-backend): remove unnecessary interface code (#2039) --- CHANGELOG.md | 1 + eth/rpc/backend/backend.go | 132 ++++----------------------------- eth/rpc/rpcapi/debugapi/api.go | 4 +- eth/rpc/rpcapi/eth_api.go | 4 +- 4 files changed, 19 insertions(+), 122 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96dae1703..26d1f96b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -119,6 +119,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#2023](https://github.com/NibiruChain/nibiru/pull/2023) - fix(evm)!: adjusted generation and parsing of the block bloom events - [#2030](https://github.com/NibiruChain/nibiru/pull/2030) - refactor(eth/rpc): Delete unused code and improve logging in the eth and debug namespaces - [#2031](https://github.com/NibiruChain/nibiru/pull/2031) - fix(evm): debug calls with custom tracer and tracer options +- [#2039](https://github.com/NibiruChain/nibiru/pull/2039) - refactor(rpc-backend): remove unnecessary interface code #### Dapp modules: perp, spot, oracle, etc diff --git a/eth/rpc/backend/backend.go b/eth/rpc/backend/backend.go index aecadec75..9f3ad79ba 100644 --- a/eth/rpc/backend/backend.go +++ b/eth/rpc/backend/backend.go @@ -4,134 +4,20 @@ package backend import ( "context" "math/big" - "time" "github.com/cometbft/cometbft/libs/log" - tmrpctypes "github.com/cometbft/cometbft/rpc/core/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/server" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" - gethcore "github.com/ethereum/go-ethereum/core/types" - params "github.com/ethereum/go-ethereum/params" - gethrpc "github.com/ethereum/go-ethereum/rpc" - "github.com/ethereum/go-ethereum/signer/core/apitypes" "github.com/NibiruChain/nibiru/v2/app/server/config" "github.com/NibiruChain/nibiru/v2/eth" "github.com/NibiruChain/nibiru/v2/eth/rpc" - "github.com/NibiruChain/nibiru/v2/x/evm" ) -// BackendI implements the Cosmos and EVM backend. -type BackendI interface { //nolint: revive - CosmosBackend - EVMBackend -} - -// CosmosBackend: Currently unused. Backend functionality for the shared -// "cosmos" RPC namespace. Implements [BackendI] in combination with [EVMBackend]. -// TODO: feat(eth): Implement the cosmos JSON-RPC defined by Wallet Connect V2: -// https://docs.walletconnect.com/2.0/json-rpc/cosmos. -type CosmosBackend interface { - // TODO: GetAccounts() - // TODO: SignDirect() - // TODO: SignAmino() -} - -// EVMBackend implements the functionality shared within ethereum namespaces -// as defined by EIP-1474: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md -// Implemented by Backend. -type EVMBackend interface { - // Node specific queries - Accounts() ([]common.Address, error) - Syncing() (interface{}, error) - RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection - RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection - RPCTxFeeCap() float64 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for send-transaction variants. The unit is ether. - RPCMinGasPrice() int64 - - // Sign Tx - Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) - SendTransaction(args evm.JsonTxArgs) (common.Hash, error) - SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error) - - // Blocks Info - BlockNumber() (hexutil.Uint64, error) - GetBlockByNumber(blockNum rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) - GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) - GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint - GetBlockTransactionCountByNumber(blockNum rpc.BlockNumber) *hexutil.Uint - TendermintBlockByNumber(blockNum rpc.BlockNumber) (*tmrpctypes.ResultBlock, error) - TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error) - TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error) - BlockNumberFromTendermint(blockNrOrHash rpc.BlockNumberOrHash) (rpc.BlockNumber, error) - BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.Int, error) - EthMsgsFromTendermintBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evm.MsgEthereumTx - BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (gethcore.Bloom, error) - HeaderByNumber(blockNum rpc.BlockNumber) (*gethcore.Header, error) - HeaderByHash(blockHash common.Hash) (*gethcore.Header, error) - RPCBlockFromTendermintBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults, fullTx bool) (map[string]interface{}, error) - EthBlockByNumber(blockNum rpc.BlockNumber) (*gethcore.Block, error) - EthBlockFromTendermintBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) (*gethcore.Block, error) - - // Account Info - GetCode(address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) - GetBalance(address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) - GetStorageAt(address common.Address, key string, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) - GetProof(address common.Address, storageKeys []string, blockNrOrHash rpc.BlockNumberOrHash) (*rpc.AccountResult, error) - GetTransactionCount(address common.Address, blockNum rpc.BlockNumber) (*hexutil.Uint64, error) - - // Chain Info - ChainID() (*hexutil.Big, error) - ChainConfig() *params.ChainConfig - // TODO: feat: Dynamic fees - BaseFee(blockRes *tmrpctypes.ResultBlockResults) (*big.Int, error) - CurrentHeader() (*gethcore.Header, error) - PendingTransactions() ([]*sdk.Tx, error) - FeeHistory(blockCount gethrpc.DecimalOrHex, lastBlock gethrpc.BlockNumber, rewardPercentiles []float64) (*rpc.FeeHistoryResult, error) - SuggestGasTipCap(baseFee *big.Int) (*big.Int, error) - - // Tx Info - GetTransactionByHash(txHash common.Hash) (*rpc.EthTxJsonRPC, error) - GetTxByEthHash(txHash common.Hash) (*eth.TxResult, error) - GetTxByTxIndex(height int64, txIndex uint) (*eth.TxResult, error) - GetTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock, idx hexutil.Uint) (*rpc.EthTxJsonRPC, error) - GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error) - GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpc.EthTxJsonRPC, error) - GetTransactionByBlockNumberAndIndex(blockNum rpc.BlockNumber, idx hexutil.Uint) (*rpc.EthTxJsonRPC, error) - - // Send Transaction - Resend(args evm.JsonTxArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error) - SendRawTransaction(data hexutil.Bytes) (common.Hash, error) - SetTxDefaults(args evm.JsonTxArgs) (evm.JsonTxArgs, error) - EstimateGas(args evm.JsonTxArgs, blockNrOptional *rpc.BlockNumber) (hexutil.Uint64, error) - DoCall(args evm.JsonTxArgs, blockNr rpc.BlockNumber) (*evm.MsgEthereumTxResponse, error) - GasPrice() (*hexutil.Big, error) - - // Filter API - GetLogs(hash common.Hash) ([][]*gethcore.Log, error) - GetLogsByHeight(height *int64) ([][]*gethcore.Log, error) - BloomStatus() (uint64, uint64) - - // Tracing - TraceTransaction(hash common.Hash, config *evm.TraceConfig) (interface{}, error) - TraceBlock( - height rpc.BlockNumber, - config *evm.TraceConfig, - block *tmrpctypes.ResultBlock, - ) ([]*evm.TxTraceResult, error) - TraceCall( - txArgs evm.JsonTxArgs, - contextHeight rpc.BlockNumber, - config *evm.TraceConfig, - ) (interface{}, error) -} - -var _ BackendI = (*Backend)(nil) - -// Backend implements the BackendI interface +// Backend implements implements the functionality shared within ethereum namespaces +// as defined by [EIP-1474]. +// +// [EIP-1474]: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md type Backend struct { ctx context.Context clientCtx client.Context @@ -172,3 +58,13 @@ func NewBackend( indexer: indexer, } } + +// CosmosBackend: Currently unused. Backend functionality for the shared +// "cosmos" RPC namespace. Implements [BackendI] in combination with [EVMBackend]. +// TODO: feat(eth): Implement the cosmos JSON-RPC defined by Wallet Connect V2: +// https://docs.walletconnect.com/2.0/json-rpc/cosmos. +type CosmosBackend interface { + // TODO: GetAccounts() + // TODO: SignDirect() + // TODO: SignAmino() +} diff --git a/eth/rpc/rpcapi/debugapi/api.go b/eth/rpc/rpcapi/debugapi/api.go index ebb2cebaf..c70673273 100644 --- a/eth/rpc/rpcapi/debugapi/api.go +++ b/eth/rpc/rpcapi/debugapi/api.go @@ -45,7 +45,7 @@ type HandlerT struct { type DebugAPI struct { ctx *server.Context logger log.Logger - backend backend.EVMBackend + backend *backend.Backend handler *HandlerT } @@ -53,7 +53,7 @@ type DebugAPI struct { // Ethereum service. func NewImplDebugAPI( ctx *server.Context, - backend backend.EVMBackend, + backend *backend.Backend, ) *DebugAPI { return &DebugAPI{ ctx: ctx, diff --git a/eth/rpc/rpcapi/eth_api.go b/eth/rpc/rpcapi/eth_api.go index 75f7ffc85..9695832df 100644 --- a/eth/rpc/rpcapi/eth_api.go +++ b/eth/rpc/rpcapi/eth_api.go @@ -131,11 +131,11 @@ var _ IEthAPI = (*EthAPI)(nil) type EthAPI struct { ctx context.Context logger log.Logger - backend backend.EVMBackend + backend *backend.Backend } // NewImplEthAPI creates an instance of the public ETH Web3 API. -func NewImplEthAPI(logger log.Logger, backend backend.EVMBackend) *EthAPI { +func NewImplEthAPI(logger log.Logger, backend *backend.Backend) *EthAPI { api := &EthAPI{ ctx: context.Background(), logger: logger.With("client", "json-rpc"),