Skip to content

Commit

Permalink
refactor(rpc-backend): remove unnecessary interface code (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Sep 14, 2024
1 parent 60167ca commit 5085dad
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 122 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
132 changes: 14 additions & 118 deletions eth/rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
4 changes: 2 additions & 2 deletions eth/rpc/rpcapi/debugapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ type HandlerT struct {
type DebugAPI struct {
ctx *server.Context
logger log.Logger
backend backend.EVMBackend
backend *backend.Backend
handler *HandlerT
}

// NewImplDebugAPI creates a new API definition for the tracing methods of the
// Ethereum service.
func NewImplDebugAPI(
ctx *server.Context,
backend backend.EVMBackend,
backend *backend.Backend,
) *DebugAPI {
return &DebugAPI{
ctx: ctx,
Expand Down
4 changes: 2 additions & 2 deletions eth/rpc/rpcapi/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 5085dad

Please sign in to comment.