Skip to content

Commit f2b8a17

Browse files
monorepo: type improvements (#3947)
* monorepo: remove some anys * monorepo: improve types and remove some anys * monorepo: fix more <any> typecastings * monorepo: more fixes * evm: fix opcode type issues with typeguard * chore: fix formatting --------- Co-authored-by: Holger Drewes <[email protected]>
1 parent 8512301 commit f2b8a17

34 files changed

+140
-131
lines changed

packages/binarytree/src/db/checkpoint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class CheckpointDB implements DB {
188188
} else {
189189
const valuePut =
190190
this.valueEncoding === ValueEncoding.Bytes ? value : bytesToUnprefixedHex(value)
191-
await this.db.put(keyHex, <any>valuePut, {
191+
await this.db.put(keyHex, valuePut, {
192192
keyEncoding: KeyEncoding.String,
193193
valueEncoding: this.valueEncoding,
194194
})
@@ -254,7 +254,7 @@ export class CheckpointDB implements DB {
254254
}
255255
return convertedOp
256256
})
257-
await this.db.batch(<any>convertedOps)
257+
await this.db.batch(convertedOps as any)
258258
}
259259
}
260260

packages/blockchain/src/consensus/clique.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
BIGINT_1,
1313
BIGINT_2,
1414
EthereumJSErrorWithoutCode,
15+
type NestedUint8Array,
1516
TypeOutput,
1617
bigIntToBytes,
1718
bytesToBigInt,
@@ -556,10 +557,13 @@ export class CliqueConsensus implements Consensus {
556557
private async getCliqueLatestSignerStates(): Promise<CliqueLatestSignerStates> {
557558
const signerStates = await this.blockchain!.db.get(CLIQUE_SIGNERS_KEY)
558559
if (signerStates === undefined) return []
559-
const states = RLP.decode(signerStates as Uint8Array) as [Uint8Array, Uint8Array[]]
560+
const states = RLP.decode(signerStates as Uint8Array) as NestedUint8Array
560561
return states.map((state) => {
561562
const blockNum = bytesToBigInt(state[0] as Uint8Array)
562-
const addresses = (<any>state[1]).map((bytes: Uint8Array) => new Address(bytes))
563+
const addresses: Address[] = (state[1] as Uint8Array[]).map((bytes: Uint8Array): Address => {
564+
const address = new Address(bytes)
565+
return address
566+
})
563567
return [blockNum, addresses]
564568
}) as CliqueLatestSignerStates
565569
}

packages/blockchain/test/index.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,7 @@ describe('blockchain test', () => {
609609
const hash = await blockchain.dbManager.numberToHash(BigInt(0))
610610
assert.deepEqual(genesis.hash(), hash, 'should perform _numberToHash correctly')
611611

612-
// cast the blockchain as <any> in order to get access to the private getTotalDifficulty
613-
const td = await (<any>blockchain).getTotalDifficulty(genesis.hash(), BigInt(0))
612+
const td = await blockchain.getTotalDifficulty(genesis.hash(), BigInt(0))
614613
assert.equal(td, genesis.header.difficulty, 'should perform getTotalDifficulty correctly')
615614
})
616615

packages/client/src/sync/fetcher/accountfetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
414414
null,
415415
[],
416416
[],
417-
<any>rangeResult.proof,
417+
rangeResult.proof,
418418
)
419419
// if proof is false, reject corrupt peer
420420
if (isMissingRightRange !== false) return undefined

packages/client/src/sync/fetcher/storagefetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat
282282
null,
283283
[],
284284
[],
285-
<any>rangeResult.proof,
285+
rangeResult.proof,
286286
)
287287

288288
// if proof is false, reject corrupt peer

packages/client/test/sync/fullsync.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('[FullSynchronizer]', async () => {
114114
;(sync as any).chain = { blocks: { td: BigInt(1) } }
115115
;(sync as any).pool = { peers }
116116
;(sync as any).forceSync = true
117-
assert.equal(await sync.best(), <any>peers[1], 'found best')
117+
assert.equal(await sync.best(), peers[1] as any, 'found best')
118118
await sync.stop()
119119
await sync.close()
120120
})

packages/devp2p/test/dns.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { testData } from './testdata.ts'
77

88
describe('DNS', () => {
99
const mockData = testData.dns
10-
const mockDns = td.replace<any>('dns')
10+
const mockDns = td.replace('dns') as any
1111

1212
let dns: DNS
1313
function initializeDns() {

packages/devp2p/test/integration/eth-simulator.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ describe('ETH simulator tests', () => {
231231
switch (code) {
232232
case EthMessageCodes.STATUS:
233233
protocol.sendStatus(status)
234-
assert.throws(() => protocol.sendMessage(<any>0x55, []), /Unknown code 85/)
234+
// @ts-expect-error -- Testing unknown code
235+
assert.throws(() => protocol.sendMessage(0x55, []), /Unknown code 85/)
235236
util.destroyRLPXs(rlpxs)
236237
resolve(undefined)
237238
}

packages/evm/src/interpreter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export class Interpreter {
438438
* Get info for an opcode from EVM's list of opcodes.
439439
*/
440440
lookupOpInfo(op: number): OpcodeMapEntry {
441-
return (<any>this._evm)._opcodeMap[op]
441+
return this._evm['_opcodeMap'][op]
442442
}
443443

444444
async _runStepHook(dynamicFee: bigint, gasLeft: bigint): Promise<void> {

packages/evm/src/opcodes/codes.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { dynamicGasHandlers } from './gas.ts'
66
import { getFullname } from './util.ts'
77

88
import type { Common } from '@ethereumjs/common'
9-
import type { CustomOpcode } from '../types.ts'
9+
import { type CustomOpcode, isAddOpcode } from '../types.ts'
1010
import type { OpHandler } from './functions.ts'
1111
import type { AsyncDynamicGasHandler, SyncDynamicGasHandler } from './gas.ts'
1212

@@ -50,7 +50,9 @@ export class Opcode {
5050
}
5151

5252
export type OpcodeList = Map<number, Opcode>
53-
type OpcodeEntry = { [key: number]: { name: string; isAsync: boolean; dynamicGas: boolean } }
53+
type OpcodeEntry = {
54+
[key: number]: { name: string; isAsync: boolean; dynamicGas: boolean }
55+
}
5456
type OpcodeEntryFee = OpcodeEntry & { [key: number]: { fee: number } }
5557

5658
// Default: sync and no dynamic gas
@@ -447,18 +449,13 @@ export function getOpcodesForHF(common: Common, customOpcodes?: CustomOpcode[]):
447449

448450
if (customOpcodes) {
449451
for (const _code of customOpcodes) {
450-
const code = <any>_code
451-
if (code.logicFunction === undefined) {
452+
const code = _code
453+
454+
if (!isAddOpcode(code)) {
452455
delete opcodeBuilder[code.opcode]
453456
continue
454457
}
455458

456-
// Sanity checks
457-
if (code.opcodeName === undefined || code.baseFee === undefined) {
458-
throw EthereumJSErrorWithoutCode(
459-
`Custom opcode ${code.opcode} does not have the required values: opcodeName and baseFee are required`,
460-
)
461-
}
462459
const entry = {
463460
[code.opcode]: {
464461
name: code.opcodeName,

0 commit comments

Comments
 (0)