From f4b065d7f792c70063cd9fb409a3dab9cf76275e Mon Sep 17 00:00:00 2001 From: Carmen Liu Date: Thu, 19 Dec 2024 11:27:40 +0800 Subject: [PATCH] remove the unnecessary middleware warp and add the return function for callWithStackTrace --- packages/client/src/rpc/helpers.ts | 2 +- packages/client/src/rpc/modules/admin.ts | 6 ++-- packages/client/src/rpc/modules/eth.ts | 40 +++++++---------------- packages/client/src/rpc/modules/net.ts | 24 ++++---------- packages/client/src/rpc/modules/txpool.ts | 6 ++-- packages/client/src/rpc/modules/web3.ts | 8 +---- packages/client/src/rpc/validation.ts | 2 +- 7 files changed, 26 insertions(+), 62 deletions(-) diff --git a/packages/client/src/rpc/helpers.ts b/packages/client/src/rpc/helpers.ts index 24cc138b40..dbcc8b457f 100644 --- a/packages/client/src/rpc/helpers.ts +++ b/packages/client/src/rpc/helpers.ts @@ -13,7 +13,7 @@ type RPCError = { data?: string } -export function callWithStackTrace(handler: Function, debug: boolean) { +export function callWithStackTrace(handler: Function, debug: boolean): (...args: any) => any { return async (...args: any) => { try { const res = await handler(...args) diff --git a/packages/client/src/rpc/modules/admin.ts b/packages/client/src/rpc/modules/admin.ts index 9760a23bdf..48051f9d1b 100644 --- a/packages/client/src/rpc/modules/admin.ts +++ b/packages/client/src/rpc/modules/admin.ts @@ -31,8 +31,8 @@ export class Admin { this._client = client this._rpcDebug = rpcDebug - this.nodeInfo = middleware(callWithStackTrace(this.nodeInfo.bind(this), this._rpcDebug), 0, []) - this.peers = middleware(callWithStackTrace(this.peers.bind(this), this._rpcDebug), 0, []) + this.nodeInfo = callWithStackTrace(this.nodeInfo.bind(this), this._rpcDebug) + this.peers = callWithStackTrace(this.peers.bind(this), this._rpcDebug) this.addPeer = middleware(callWithStackTrace(this.addPeer.bind(this), this._rpcDebug), 1, [ [ validators.object({ @@ -48,7 +48,7 @@ export class Admin { * Returns information about the currently running node. * see for reference: https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin#admin_peers */ - async nodeInfo() { + nodeInfo() { const rlpxInfo = this._client.config.server!.getRlpxInfo() const { enode, id, ip, listenAddr, ports } = rlpxInfo const { discovery, listener } = ports diff --git a/packages/client/src/rpc/modules/eth.ts b/packages/client/src/rpc/modules/eth.ts index 6c5dc49b8c..167e46c6df 100644 --- a/packages/client/src/rpc/modules/eth.ts +++ b/packages/client/src/rpc/modules/eth.ts @@ -315,17 +315,14 @@ export class Eth { const ethProtocol = this.service.protocols.find((p) => p.name === 'eth') as EthProtocol this.ethVersion = Math.max(...ethProtocol.versions) - this.blockNumber = middleware( - callWithStackTrace(this.blockNumber.bind(this), this._rpcDebug), - 0, - ) + this.blockNumber = callWithStackTrace(this.blockNumber.bind(this), this._rpcDebug) this.call = middleware(callWithStackTrace(this.call.bind(this), this._rpcDebug), 2, [ [validators.transaction()], [validators.blockOption], ]) - this.chainId = middleware(callWithStackTrace(this.chainId.bind(this), this._rpcDebug), 0, []) + this.chainId = callWithStackTrace(this.chainId.bind(this), this._rpcDebug) this.estimateGas = middleware( callWithStackTrace(this.estimateGas.bind(this), this._rpcDebug), @@ -339,7 +336,7 @@ export class Eth { [[validators.address], [validators.blockOption]], ) - this.coinbase = middleware(callWithStackTrace(this.coinbase.bind(this), this._rpcDebug), 0, []) + this.coinbase = callWithStackTrace(this.coinbase.bind(this), this._rpcDebug) this.getBlockByNumber = middleware( callWithStackTrace(this.getBlockByNumber.bind(this), this._rpcDebug), @@ -443,13 +440,9 @@ export class Eth { [[validators.hex]], ) - this.protocolVersion = middleware( - callWithStackTrace(this.protocolVersion.bind(this), this._rpcDebug), - 0, - [], - ) + this.protocolVersion = callWithStackTrace(this.protocolVersion.bind(this), this._rpcDebug) - this.syncing = middleware(callWithStackTrace(this.syncing.bind(this), this._rpcDebug), 0, []) + this.syncing = callWithStackTrace(this.syncing.bind(this), this._rpcDebug) this.getProof = middleware(callWithStackTrace(this.getProof.bind(this), this._rpcDebug), 3, [ [validators.address], @@ -463,7 +456,7 @@ export class Eth { [[validators.blockOption]], ) - this.gasPrice = middleware(callWithStackTrace(this.gasPrice.bind(this), this._rpcDebug), 0, []) + this.gasPrice = callWithStackTrace(this.gasPrice.bind(this), this._rpcDebug) this.feeHistory = middleware( callWithStackTrace(this.feeHistory.bind(this), this._rpcDebug), @@ -475,18 +468,13 @@ export class Eth { ], ) - this.blobBaseFee = middleware( - callWithStackTrace(this.blobBaseFee.bind(this), this._rpcDebug), - 0, - [], - ) + this.blobBaseFee = callWithStackTrace(this.blobBaseFee.bind(this), this._rpcDebug) } /** * Returns number of the most recent block. - * @param params An empty array */ - async blockNumber(_params = []) { + async blockNumber() { return bigIntToHex(this._chain.headers.latest?.number ?? BIGINT_0) } @@ -540,10 +528,9 @@ export class Eth { /** * Returns the currently configured chain id, a value used in replay-protected transaction signing as introduced by EIP-155. - * @param _params An empty array * @returns The chain ID. */ - async chainId(_params = []) { + async chainId() { const chainId = this._chain.config.chainCommon.chainId() return bigIntToHex(chainId) } @@ -660,10 +647,9 @@ export class Eth { /** * Returns the currently configured coinbase address. - * @param _params An empty array * @returns The chain ID. */ - async coinbase(_params = []) { + async coinbase() { const cb = this.client.config.minerCoinbase if (cb === undefined) { throw { @@ -905,9 +891,8 @@ export class Eth { /** * Returns the current ethereum protocol version as a hex-encoded string - * @param params An empty array */ - protocolVersion(_params = []) { + protocolVersion() { return intToHex(this.ethVersion) } @@ -1272,13 +1257,12 @@ export class Eth { /** * Returns an object with data about the sync status or false. - * @param params An empty array * @returns An object with sync status data or false (when not syncing) * * startingBlock - The block at which the import started (will only be reset after the sync reached his head) * * currentBlock - The current block, same as eth_blockNumber * * highestBlock - The estimated highest block */ - async syncing(_params = []) { + async syncing() { if (this.client.config.synchronized) { return false } diff --git a/packages/client/src/rpc/modules/net.ts b/packages/client/src/rpc/modules/net.ts index bc50608781..726de62e38 100644 --- a/packages/client/src/rpc/modules/net.ts +++ b/packages/client/src/rpc/modules/net.ts @@ -1,7 +1,6 @@ import { addHexPrefix } from '@ethereumjs/util' import { callWithStackTrace } from '../helpers.js' -import { middleware } from '../validation.js' import type { Chain } from '../../blockchain/index.js' import type { EthereumClient } from '../../index.js' @@ -29,40 +28,29 @@ export class Net { this._peerPool = service.pool this._rpcDebug = rpcDebug - this.version = middleware(callWithStackTrace(this.version.bind(this), this._rpcDebug), 0, []) - this.listening = middleware( - callWithStackTrace(this.listening.bind(this), this._rpcDebug), - 0, - [], - ) - this.peerCount = middleware( - callWithStackTrace(this.peerCount.bind(this), this._rpcDebug), - 0, - [], - ) + this.version = callWithStackTrace(this.version.bind(this), this._rpcDebug) + this.listening = callWithStackTrace(this.listening.bind(this), this._rpcDebug) + this.peerCount = callWithStackTrace(this.peerCount.bind(this), this._rpcDebug) } /** * Returns the current network id - * @param params An empty array */ - version(_params = []) { + version() { return this._chain.config.chainCommon.chainId().toString() } /** * Returns true if client is actively listening for network connections - * @param params An empty array */ - listening(_params = []) { + listening() { return this._client.opened } /** * Returns number of peers currently connected to the client - * @param params An empty array */ - peerCount(_params = []) { + peerCount() { return addHexPrefix(this._peerPool.peers.length.toString(16)) } } diff --git a/packages/client/src/rpc/modules/txpool.ts b/packages/client/src/rpc/modules/txpool.ts index b026eea061..694ea31ea1 100644 --- a/packages/client/src/rpc/modules/txpool.ts +++ b/packages/client/src/rpc/modules/txpool.ts @@ -1,5 +1,4 @@ import { callWithStackTrace, toJSONRPCTx } from '../helpers.js' -import { middleware } from '../validation.js' import type { EthereumClient } from '../../index.js' import type { FullEthereumService } from '../../service/index.js' @@ -25,14 +24,13 @@ export class TxPool { this._vm = service.execution.vm this._rpcDebug = rpcDebug - this.content = middleware(callWithStackTrace(this.content.bind(this), this._rpcDebug), 0, []) + this.content = callWithStackTrace(this.content.bind(this), this._rpcDebug) } /** * Returns the contents of the transaction pool - * @param params An empty array */ - content(_params = []) { + content() { const pending = new Map() for (const pool of this._txpool.pool) { const pendingForAcct = new Map() diff --git a/packages/client/src/rpc/modules/web3.ts b/packages/client/src/rpc/modules/web3.ts index a67775c292..c668b3d243 100644 --- a/packages/client/src/rpc/modules/web3.ts +++ b/packages/client/src/rpc/modules/web3.ts @@ -5,9 +5,7 @@ import { getClientVersion } from '../../util/index.js' import { callWithStackTrace } from '../helpers.js' import { middleware, validators } from '../validation.js' -import type { Chain } from '../../blockchain/index.js' import type { EthereumClient } from '../../index.js' -import type { FullEthereumService } from '../../service/index.js' import type { PrefixedHexString } from '@ethereumjs/util' /** @@ -15,17 +13,13 @@ import type { PrefixedHexString } from '@ethereumjs/util' * @memberof module:rpc/modules */ export class Web3 { - private _chain?: Chain private _rpcDebug: boolean /** * Create web3_* RPC module * @param client Client to which the module binds */ constructor(client: EthereumClient, rpcDebug: boolean) { - const service = client.service as FullEthereumService - this._chain = service.chain this._rpcDebug = rpcDebug - this.clientVersion = middleware(this.clientVersion.bind(this), 0, []) this.sha3 = middleware(callWithStackTrace(this.sha3.bind(this), this._rpcDebug), 1, [ [validators.hex], @@ -36,7 +30,7 @@ export class Web3 { * Returns the current client version * @param params An empty array */ - clientVersion(_params = []) { + clientVersion() { return getClientVersion() } diff --git a/packages/client/src/rpc/validation.ts b/packages/client/src/rpc/validation.ts index aee93a65dd..a4c677c57f 100644 --- a/packages/client/src/rpc/validation.ts +++ b/packages/client/src/rpc/validation.ts @@ -1,6 +1,6 @@ import { INVALID_PARAMS } from './error-code.js' -type RPCMethod = (params?: any[]) => Promise +type RPCMethod = (params: any[]) => any /** * middleware for parameters validation