Skip to content

Commit b8535f6

Browse files
ScottyPoiacolytec3
andauthored
Convert static only classes to functions (#782)
* run biome fix * conver static only classes to functions * Lots o lint fixes * more lint --------- Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
1 parent 0a3a8ca commit b8535f6

39 files changed

Lines changed: 592 additions & 345 deletions

packages/cli/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@
6464
"author": "acolytec3",
6565
"license": "MIT",
6666
"nodemonConfig": {
67-
"watch": [
68-
"src",
69-
"../portalnetwork/dist"
70-
]
67+
"watch": ["src", "../portalnetwork/dist"]
7168
}
72-
}
69+
}

packages/cli/src/rpc/modules/beacon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBytes, PrefixedHexString } from '@ethereumjs/util'
1+
import { type PrefixedHexString, hexToBytes } from '@ethereumjs/util'
22
import { RunStatusCode } from '@lodestar/light-client'
33
import { ssz } from '@lodestar/types'
44
import {

packages/cli/src/rpc/modules/eth.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ export class eth {
9191
async getBalance(params: [string, string]) {
9292
const [address, blockTag] = params
9393
try {
94-
const res = await this._client.ETH.getBalance(hexToBytes(address as PrefixedHexString), BigInt(blockTag))
94+
const res = await this._client.ETH.getBalance(
95+
hexToBytes(address as PrefixedHexString),
96+
BigInt(blockTag),
97+
)
9598
if (res === undefined) {
9699
throw {
97100
code: INTERNAL_ERROR,
@@ -141,7 +144,10 @@ export class eth {
141144
this._client.logger(
142145
`eth_getBlockByHash request received. blockHash: ${blockHash} includeTransactions: ${includeTransactions}`,
143146
)
144-
const block = await this._client.ETH.getBlockByHash(hexToBytes(blockHash as PrefixedHexString), includeTransactions)
147+
const block = await this._client.ETH.getBlockByHash(
148+
hexToBytes(blockHash as PrefixedHexString),
149+
includeTransactions,
150+
)
145151
//@ts-ignore @ethereumjs/block has some weird typing discrepancy
146152
if (block !== undefined) return block
147153
throw new Error('Block not found')

packages/cli/src/rpc/modules/portal.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { EntryStatus, distance } from '@chainsafe/discv5'
22
import { ENR } from '@chainsafe/enr'
3-
import { bigIntToHex, bytesToHex, hexToBytes, PrefixedHexString, short } from '@ethereumjs/util'
3+
import {
4+
type PrefixedHexString,
5+
bigIntToHex,
6+
bytesToHex,
7+
hexToBytes,
8+
short,
9+
} from '@ethereumjs/util'
410
import {
511
ContentLookup,
612
FoundContent,
@@ -16,11 +22,11 @@ import { content_params } from '../schema/index.js'
1622
import { callWithStackTrace, isValidId } from '../util.js'
1723
import { middleware, validators } from '../validators.js'
1824

25+
import { BitArray } from '@chainsafe/ssz'
1926
import { RunStatusCode } from '@lodestar/light-client'
2027
import type { Debugger } from 'debug'
2128
import type { BeaconNetwork, HistoryNetwork, PortalNetwork, StateNetwork } from 'portalnetwork'
2229
import type { GetEnrResult } from '../schema/types.js'
23-
import { BitArray } from '@chainsafe/ssz'
2430

2531
const methods = [
2632
// state
@@ -771,7 +777,9 @@ export class portal {
771777
const [contentKey] = params
772778
this.logger.extend('beaconLocalContent')(`Received request for ${contentKey}`)
773779

774-
const content = await this._beacon.findContentLocally(hexToBytes(contentKey as PrefixedHexString))
780+
const content = await this._beacon.findContentLocally(
781+
hexToBytes(contentKey as PrefixedHexString),
782+
)
775783
this.logger.extend('beaconLocalContent')(
776784
`request returned ${content !== undefined ? content.length : 'null'} bytes`,
777785
)
@@ -892,7 +900,10 @@ export class portal {
892900
this.logger.extend('findContent')(
893901
`received request to send request to ${shortId(nodeId)} for contentKey ${contentKey}`,
894902
)
895-
const res = await this._history.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey as PrefixedHexString))
903+
const res = await this._history.sendFindContent(
904+
ENR.decodeTxt(enr),
905+
hexToBytes(contentKey as PrefixedHexString),
906+
)
896907
if (res === undefined) {
897908
this.logger.extend('findContent')('request returned undefined')
898909
return undefined
@@ -924,7 +935,10 @@ export class portal {
924935
this.logger.extend('findContent')(
925936
`received request to send request to ${shortId(nodeId)} for contentKey ${contentKey}`,
926937
)
927-
const res = await this._state.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey as PrefixedHexString))
938+
const res = await this._state.sendFindContent(
939+
ENR.decodeTxt(enr),
940+
hexToBytes(contentKey as PrefixedHexString),
941+
)
928942
if (res === undefined) {
929943
this.logger.extend('findContent')('request returned type: ENRS')
930944
return { enrs: [] }
@@ -957,7 +971,10 @@ export class portal {
957971
}
958972
}
959973

960-
const res = await this._beacon.sendFindContent(ENR.decodeTxt(enr), hexToBytes(contentKey as PrefixedHexString))
974+
const res = await this._beacon.sendFindContent(
975+
ENR.decodeTxt(enr),
976+
hexToBytes(contentKey as PrefixedHexString),
977+
)
961978

962979
if (res === undefined) {
963980
this.logger.extend('findContent')('request returned type: ENRS')
@@ -1067,7 +1084,11 @@ export class portal {
10671084
async historyTraceGetContent(params: [string]) {
10681085
const [contentKey] = params
10691086
this.logger.extend('historyTraceGetContent')(`request received for ${contentKey}`)
1070-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString), true)
1087+
const lookup = new ContentLookup(
1088+
this._history,
1089+
hexToBytes(contentKey as PrefixedHexString),
1090+
true,
1091+
)
10711092
const res = await lookup.startLookup()
10721093
this.logger.extend('historyTraceGetContent')(`request returned ${JSON.stringify(res)}`)
10731094
if (!res) {
@@ -1096,7 +1117,11 @@ export class portal {
10961117
async beaconTraceGetContent(params: [string]) {
10971118
const [contentKey] = params
10981119
this.logger.extend('beaconTraceGetContent')(`request received for ${contentKey}`)
1099-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString), true)
1120+
const lookup = new ContentLookup(
1121+
this._history,
1122+
hexToBytes(contentKey as PrefixedHexString),
1123+
true,
1124+
)
11001125
const res = await lookup.startLookup()
11011126
this.logger.extend('beaconTraceGetContent')(`request returned ${JSON.stringify(res)}`)
11021127
if (!res) {
@@ -1125,7 +1150,11 @@ export class portal {
11251150
async stateTraceGetContent(params: [string]) {
11261151
const [contentKey] = params
11271152
this.logger.extend('stateTraceGetContent')(`request received for ${contentKey}`)
1128-
const lookup = new ContentLookup(this._history, hexToBytes(contentKey as PrefixedHexString), true)
1153+
const lookup = new ContentLookup(
1154+
this._history,
1155+
hexToBytes(contentKey as PrefixedHexString),
1156+
true,
1157+
)
11291158
const res = await lookup.startLookup()
11301159
this.logger.extend('stateTraceGetContent')(`request returned ${JSON.stringify(res)}`)
11311160
if (!res) {
@@ -1197,13 +1226,19 @@ export class portal {
11971226
async historyGossip(params: [string, string]) {
11981227
const [contentKey, content] = params
11991228
this.logger(`historyGossip request received for ${contentKey}`)
1200-
const res = await this._history.gossipContent(hexToBytes(contentKey as PrefixedHexString), hexToBytes(content as PrefixedHexString))
1229+
const res = await this._history.gossipContent(
1230+
hexToBytes(contentKey as PrefixedHexString),
1231+
hexToBytes(content as PrefixedHexString),
1232+
)
12011233
return res
12021234
}
12031235
async stateGossip(params: [string, string]) {
12041236
const [contentKey, content] = params
12051237
this.logger(`stateGossip request received for ${contentKey}`)
1206-
const res = await this._state.gossipContent(hexToBytes(contentKey as PrefixedHexString), hexToBytes(content as PrefixedHexString))
1238+
const res = await this._state.gossipContent(
1239+
hexToBytes(contentKey as PrefixedHexString),
1240+
hexToBytes(content as PrefixedHexString),
1241+
)
12071242
return res
12081243
}
12091244

packages/cli/src/rpc/modules/ultralight.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bytesToHex, hexToBytes, PrefixedHexString } from '@ethereumjs/util'
1+
import { type PrefixedHexString, bytesToHex, hexToBytes } from '@ethereumjs/util'
22
import { HistoryNetworkContentType, NetworkId } from 'portalnetwork'
33

44
import { INTERNAL_ERROR } from '../error-code.js'
@@ -77,7 +77,10 @@ export class ultralight {
7777
`ultralight_addContentToDB request received for ${HistoryNetworkContentType[type]} ${contentKey}`,
7878
)
7979
try {
80-
await this._history!.store(hexToBytes(contentKey as PrefixedHexString), hexToBytes(value as PrefixedHexString))
80+
await this._history!.store(
81+
hexToBytes(contentKey as PrefixedHexString),
82+
hexToBytes(value as PrefixedHexString),
83+
)
8184
this.logger(`${type} value for ${contentKey} added to content DB`)
8285
return `${type} value for ${contentKey} added to content DB`
8386
} catch (err: any) {

packages/portal-client/__tests__/unit/hooks/useJsonRpc.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ describe('useJsonRpc', () => {
4848
cleanup: vi.fn(),
4949
isNetworkReady: false,
5050
abortController: null,
51-
createAbortController: function (): AbortController {
51+
createAbortController: (): AbortController => {
5252
throw new Error('Function not implemented.')
5353
},
54-
cancelRequest: function (): void {
54+
cancelRequest: (): void => {
5555
throw new Error('Function not implemented.')
56-
}
56+
},
5757
})
5858

5959
mockFormatBlockResponse.mockImplementation((result, includeTransactions) => ({

packages/portal-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@
6464
"vite-tsconfig-paths": "^5.1.4",
6565
"vitest": "^3.0.9"
6666
}
67-
}
67+
}

packages/portal-client/src/utils/constants/methodRegistry.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ export const APPROVED_METHODS = [
77
'portal_historyFindContent',
88
'portal_historyFindNodes',
99
] as const
10-

packages/portal-client/src/utils/rpcMethods.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { hexToBytes, isHexString, isValidAddress } from '@ethereumjs/util'
2-
import { APPROVED_METHODS } from './constants/methodRegistry'
2+
import type { APPROVED_METHODS } from './constants/methodRegistry'
33

4-
import { InputValue } from './types'
5-
import { MethodConfig } from '@/services/portalNetwork/types'
4+
import type { MethodConfig } from '@/services/portalNetwork/types'
65
import { ENR } from '@chainsafe/enr'
6+
import type { InputValue } from './types'
77

88
export type MethodType = (typeof APPROVED_METHODS)[number]
99

@@ -42,7 +42,10 @@ export const methodRegistry: Record<MethodType, MethodConfig> = {
4242
eth_getTransactionCount: {
4343
name: 'Get Transanctions By An Address',
4444
paramPlaceholder: 'Enter Address',
45-
handler: (input: string, sendRequestHandle: (method: string, params?: any[]) => Promise<any>) => {
45+
handler: (
46+
input: string,
47+
sendRequestHandle: (method: string, params?: any[]) => Promise<any>,
48+
) => {
4649
const [address, blockHeight] = input.split(',')
4750
if (!isValidAddress(address)) {
4851
throw new Error('Invalid address. It should be a valid 20-byte hex string.')
@@ -53,8 +56,10 @@ export const methodRegistry: Record<MethodType, MethodConfig> = {
5356
eth_getBalance: {
5457
name: 'Get Balance Of An Address',
5558
paramPlaceholder: 'Enter Address',
56-
handler: (input: string, sendRequestHandle: (method: string, params?: any[]) => Promise<any>) => {
57-
59+
handler: (
60+
input: string,
61+
sendRequestHandle: (method: string, params?: any[]) => Promise<any>,
62+
) => {
5863
const [address, blockHeight] = input.split(',')
5964
if (!isValidAddress(address)) {
6065
throw new Error('Invalid address. It should be a valid 20-byte hex string.')
@@ -65,38 +70,45 @@ export const methodRegistry: Record<MethodType, MethodConfig> = {
6570
portal_historyPing: {
6671
name: 'Ping a node',
6772
paramPlaceholder: 'Enter node enr',
68-
handler: (input: string, sendRequestHandle: (method: string, params?: any[]) => Promise<any>) => {
69-
70-
const enr = input.split(',')
71-
.filter(enr => enr.trim())
72-
.map(enr => ENR.decodeTxt(enr))
73-
73+
handler: (
74+
input: string,
75+
sendRequestHandle: (method: string, params?: any[]) => Promise<any>,
76+
) => {
77+
const enr = input
78+
.split(',')
79+
.filter((enr) => enr.trim())
80+
.map((enr) => ENR.decodeTxt(enr))
81+
7482
return sendRequestHandle('portal_historyPing', enr)
7583
},
7684
},
7785
portal_historyFindContent: {
7886
name: 'Find Content',
7987
paramPlaceholder: 'Enter enr',
80-
handler: (input: string, sendRequestHandle: (method: string, params?: any[]) => Promise<any>) => {
81-
82-
let parts = input.split(',')
88+
handler: (
89+
input: string,
90+
sendRequestHandle: (method: string, params?: any[]) => Promise<any>,
91+
) => {
92+
const parts = input.split(',')
8393
const nodeId = parts[0]
8494
const enr = ENR.decodeTxt(nodeId)
8595
const contentKey = parts[1]
86-
96+
8797
return sendRequestHandle('portal_historyFindContent', [enr, contentKey])
8898
},
8999
},
90100
portal_historyFindNodes: {
91-
name: "Find Node",
92-
paramPlaceholder: "Enter enr",
93-
handler: (input: string, sendRequestHandle: (method: string, params?: any[]) => Promise<any>) => {
94-
let parts = input.split(',')
101+
name: 'Find Node',
102+
paramPlaceholder: 'Enter enr',
103+
handler: (
104+
input: string,
105+
sendRequestHandle: (method: string, params?: any[]) => Promise<any>,
106+
) => {
107+
const parts = input.split(',')
95108
const nodeId = parts[0]
96109
const enr = ENR.decodeTxt(nodeId)
97110
const distances = parts.slice(1)
98111
return sendRequestHandle('portal_historyFindNodes', [enr, distances])
99112
},
100113
},
101-
102-
}
114+
}

packages/portal-client/src/utils/types.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { ENR } from "@chainsafe/enr"
1+
import type { ENR } from '@chainsafe/enr'
22

33
export type NodeId = string
44

5-
export const enum ConfigId {
5+
export enum ConfigId {
66
UdpPort = 'udp-port',
77
NodeBindPort = 'node-bind-port',
88
}
@@ -25,10 +25,7 @@ export interface RPCResponse {
2525
responseType?: ResponseType
2626
}
2727

28-
export type InputValue =
29-
| string
30-
| number
31-
| `0x${string}`
28+
export type InputValue = string | number | `0x${string}`
3229

3330
export interface PeerItem {
3431
nodeId: NodeId
@@ -41,4 +38,4 @@ export interface MethodParamConfig {
4138
showBlockHeight?: boolean
4239
showDistances?: boolean
4340
showEnr?: boolean
44-
}
41+
}

0 commit comments

Comments
 (0)