Skip to content

Commit

Permalink
remove the unnecessary middleware warp and add the return function fo…
Browse files Browse the repository at this point in the history
…r callWithStackTrace
  • Loading branch information
carmen0208 committed Dec 19, 2024
1 parent 73d5be3 commit f4b065d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/rpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/rpc/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
Expand Down
40 changes: 12 additions & 28 deletions packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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],
Expand All @@ -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),
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down
24 changes: 6 additions & 18 deletions packages/client/src/rpc/modules/net.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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))
}
}
6 changes: 2 additions & 4 deletions packages/client/src/rpc/modules/txpool.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<bigint, any>()
Expand Down
8 changes: 1 addition & 7 deletions packages/client/src/rpc/modules/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@ 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'

/**
* web3_* RPC module
* @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],
Expand All @@ -36,7 +30,7 @@ export class Web3 {
* Returns the current client version
* @param params An empty array
*/
clientVersion(_params = []) {
clientVersion() {
return getClientVersion()
}

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/rpc/validation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { INVALID_PARAMS } from './error-code.js'

type RPCMethod = (params?: any[]) => Promise<any>
type RPCMethod = (params: any[]) => any

/**
* middleware for parameters validation
Expand Down

0 comments on commit f4b065d

Please sign in to comment.