Skip to content

Commit

Permalink
monorepo: cleanup ts ignores (#3361)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau authored Apr 22, 2024
1 parent c93d510 commit 9631d98
Show file tree
Hide file tree
Showing 31 changed files with 145 additions and 223 deletions.
49 changes: 29 additions & 20 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import * as testDataPreLondon2 from './testdata/testdata_pre-london-2.json'
import * as testDataPreLondon from './testdata/testdata_pre-london.json'
import * as testnetMerge from './testdata/testnetMerge.json'

import type { BlockBytes } from '../src/index.js'
import type { NestedUint8Array } from '@ethereumjs/util'
import type { BlockBytes, JsonRpcBlock } from '../src/index.js'
import type { ChainConfig } from '@ethereumjs/common'
import type { NestedUint8Array, PrefixedHexString } from '@ethereumjs/util'

describe('[Block]: block functions', () => {
it('should test block initialization', () => {
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('[Block]: block functions', () => {
)

const zero = new Uint8Array(0)
const headerArray = []
const headerArray: Uint8Array[] = []
for (let item = 0; item < 15; item++) {
headerArray.push(zero)
}
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('[Block]: block functions', () => {
const common = new Common({
chain: 'testnetMerge',
hardfork: Hardfork.Istanbul,
customChains,
customChains: customChains as ChainConfig[],
})

let block = Block.fromBlockData(
Expand Down Expand Up @@ -153,7 +154,7 @@ describe('[Block]: block functions', () => {

it('should test block validation on pow chain', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp)
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
try {
Block.fromRLPSerializedBlock(blockRlp, { common })
assert.ok(true, 'should pass')
Expand All @@ -166,7 +167,7 @@ describe('[Block]: block functions', () => {
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })

try {
blockFromRpc(testDataFromRpcGoerli, [], { common })
blockFromRpc(testDataFromRpcGoerli as JsonRpcBlock, [], { common })
assert.ok(true, 'does not throw')
} catch (error: any) {
assert.fail('error thrown')
Expand All @@ -179,7 +180,7 @@ describe('[Block]: block functions', () => {
}

it('should test transaction validation - invalid tx trie', async () => {
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp)
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
await testTransactionValidation(block)
Expand Down Expand Up @@ -220,7 +221,7 @@ describe('[Block]: block functions', () => {

it('should test transaction validation with legacy tx in london', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp)
const blockRlp = toBytes(testDataPreLondon.blocks[0].rlp as PrefixedHexString)
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
await testTransactionValidation(block)
;(block.transactions[0] as any).gasPrice = BigInt(0)
Expand All @@ -233,7 +234,7 @@ describe('[Block]: block functions', () => {

it('should test uncles hash validation', async () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const blockRlp = toBytes(testDataPreLondon2.blocks[2].rlp)
const blockRlp = toBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString)
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
assert.equal(block.uncleHashIsValid(), true)
;(block.header as any).uncleHash = new Uint8Array(32)
Expand Down Expand Up @@ -322,16 +323,16 @@ describe('[Block]: block functions', () => {

it('should test genesis hashes (mainnet default)', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes('0x' + testDataGenesis.test.genesis_rlp_hex)
const hash = hexToBytes('0x' + testDataGenesis.test.genesis_hash)
const rlp = hexToBytes(`0x${testDataGenesis.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.test.genesis_hash}`)
const block = Block.fromRLPSerializedBlock(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')
})

it('should test hash() method (mainnet default)', () => {
let common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexToBytes('0x' + testDataGenesis.test.genesis_rlp_hex)
const hash = hexToBytes('0x' + testDataGenesis.test.genesis_hash)
const rlp = hexToBytes(`0x${testDataGenesis.test.genesis_rlp_hex}`)
const hash = hexToBytes(`0x${testDataGenesis.test.genesis_hash}`)
let block = Block.fromRLPSerializedBlock(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')

Expand Down Expand Up @@ -369,23 +370,31 @@ describe('[Block]: block functions', () => {

it('should return the same block data from raw()', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const block = Block.fromRLPSerializedBlock(toBytes(testDataPreLondon2.blocks[2].rlp), {
common,
})
const block = Block.fromRLPSerializedBlock(
toBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString),
{
common,
}
)
const blockFromRaw = Block.fromValuesArray(block.raw(), { common })
assert.ok(equalsBytes(block.hash(), blockFromRaw.hash()))
})

it('should test toJSON', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const block = Block.fromRLPSerializedBlock(toBytes(testDataPreLondon2.blocks[2].rlp), {
common,
})
const block = Block.fromRLPSerializedBlock(
toBytes(testDataPreLondon2.blocks[2].rlp as PrefixedHexString),
{
common,
}
)
assert.equal(typeof block.toJSON(), 'object')
})

it('DAO hardfork', () => {
const blockData = RLP.decode(testDataPreLondon2.blocks[0].rlp) as NestedUint8Array
const blockData = RLP.decode(
testDataPreLondon2.blocks[0].rlp as PrefixedHexString
) as NestedUint8Array
// Set block number from test block to mainnet DAO fork block 1920000
blockData[0][8] = hexToBytes('0x1D4C00')

Expand Down
8 changes: 4 additions & 4 deletions packages/blockchain/test/customConsensus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ describe('consensus transition checks', () => {
const blockchain = await Blockchain.create({ common, consensus })

try {
await (blockchain as any).checkAndTransitionHardForkByNumber(5n)
await blockchain.checkAndTransitionHardForkByNumber(5n)
assert.ok('checkAndTransitionHardForkByNumber does not throw with custom consensus')
} catch (err: any) {
assert.fail(
`checkAndTransitionHardForkByNumber should not throw with custom consensus, error=${err.message}`
)
}

;(blockchain as any).consensus = new EthashConsensus()
;(blockchain.common as any).consensusAlgorithm = () => 'fibonacci'
blockchain.consensus = new EthashConsensus()
blockchain.common.consensusAlgorithm = () => 'fibonacci'

try {
await (blockchain as any).checkAndTransitionHardForkByNumber(5n)
await blockchain.checkAndTransitionHardForkByNumber(5n)
assert.fail(
'checkAndTransitionHardForkByNumber should throw when using standard consensus (ethash, clique, casper) but consensus algorithm defined in common is different'
)
Expand Down
3 changes: 1 addition & 2 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,7 @@ async function run() {
chain: chainName,
mergeForkIdPostMerge: args.mergeForkIdPostMerge,
})
//@ts-ignore
common.customCrypto = cryptoFunctions
;(common.customCrypto as any) = cryptoFunctions
customGenesisState = parseGethGenesisState(genesisFile)
}

Expand Down
10 changes: 3 additions & 7 deletions packages/client/src/net/server/rlpxserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,10 @@ export class RlpxServer extends Server {
let peer: RlpxPeer | null = new RlpxPeer({
config: this.config,
id: bytesToUnprefixedHex(rlpxPeer.getId()!),
// @ts-ignore
host: rlpxPeer._socket.remoteAddress!,
// @ts-ignore
port: rlpxPeer._socket.remotePort!,
host: rlpxPeer['_socket'].remoteAddress!,
port: rlpxPeer['_socket'].remotePort!,
protocols: Array.from(this.protocols),
// @ts-ignore: Property 'server' does not exist on type 'Socket'.
// TODO: check this error
inbound: rlpxPeer._socket.server !== undefined,
inbound: (rlpxPeer['_socket'] as any).server !== undefined,
})
try {
await peer.accept(rlpxPeer, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type PayloadToPayloadStats = {
}

const logCLStatus = (logger: winston.Logger, logMsg: string, logLevel: logLevel) => {
//@ts-ignore
logger[logLevel](enginePrefix + logMsg)
}
export class CLConnectionManager {
Expand Down
6 changes: 1 addition & 5 deletions packages/client/test/net/server/rlpxserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ describe('should return rlpx server info with ip4 as default', async () => {
}
;(server as any).rlpx = { destroy: vi.fn() }

// @ts-ignore
server.rlpx!.id = hexToBytes('0x' + mockId)
await server.start()
const nodeInfo = server.getRlpxInfo()
Expand Down Expand Up @@ -209,7 +208,6 @@ describe('should return rlpx server info with ip6', async () => {
}
;(server as any).rlpx = { destroy: vi.fn() }

// @ts-ignore
server.rlpx!.id = hexToBytes('0x' + mockId)

config.events.on(Event.SERVER_ERROR, (err) => {
Expand Down Expand Up @@ -346,8 +344,6 @@ describe('should init rlpx', async () => {
;(server as any).peers.set('01', { id: '01' } as any)
server.rlpx!.events.emit('peer:removed', rlpxPeer)
server.rlpx!.events.emit('peer:error', rlpxPeer, new Error('err0'))

// @ts-ignore
server.rlpx!.id = hexToBytes('0xff')
;(server.rlpx!.id as any) = hexToBytes('0xff')
server.rlpx!.events.emit('listening')
})
3 changes: 1 addition & 2 deletions packages/client/test/rpc/engine/kaustinen6.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ describe(`valid verkle network setup`, async () => {
testData = JSON.parse(readFileSync(fileName, 'utf8'))[testCase]
isBeaconData = false
} else {
// @ts-expect-error -- Typescript complains that `testCase` can't index the `blocks` object
testData = blocks[testCase]
testData = blocks[testCase as keyof typeof blocks]
isBeaconData = true
}
if (testData === undefined) {
Expand Down
6 changes: 2 additions & 4 deletions packages/client/test/rpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ export async function createClient(clientOpts: Partial<createClientArgs> = {}) {
savePreimages: clientOpts.savePreimages,
logger: getLogger({}),
})
const blockchain = clientOpts.blockchain ?? mockBlockchain()
const blockchain = clientOpts.blockchain ?? (mockBlockchain() as unknown as Blockchain)

const chain =
// @ts-ignore TODO Move to async Chain.create() initialization
clientOpts.chain ?? new Chain({ config, blockchain: blockchain as any, genesisState })
const chain = clientOpts.chain ?? (await Chain.create({ config, blockchain, genesisState }))
chain.opened = true

// if blockchain has not been bundled with chain, add the mock blockchain
Expand Down
7 changes: 4 additions & 3 deletions packages/client/test/sync/fetcher/headerfetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ describe('[HeaderFetcher]', async () => {
const fetcher = new HeaderFetcher({ config, pool, flow })
const headers = [{ number: 1 }, { number: 2 }]
assert.deepEqual(
//@ts-ignore
fetcher.process({ task: { count: 2 }, peer: 'peer0' }, { headers, bv: BigInt(1) }),
fetcher.process(
{ task: { count: 2 }, peer: 'peer0' } as any,
{ headers, bv: BigInt(1) } as any
),
headers as any,
'got results'
)
//@ts-ignore
assert.notOk(
fetcher.process({ task: { count: 2 } } as any, { headers: [], bv: BigInt(1) } as any),
'bad results'
Expand Down
2 changes: 0 additions & 2 deletions packages/client/test/sync/fetcher/storagefetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ describe('[StorageFetcher]', async () => {
JSON.stringify(utf8ToBytes(highestReceivedhash)),
'should set new highest known hash'
)

// @ts-ignore
;(job.task.storageRequests[0] as any).first = BigInt(3)
;(job.task.storageRequests[0] as any).count = BigInt(4)
const result = (await fetcher.request(job as any)) as any
Expand Down
5 changes: 3 additions & 2 deletions packages/common/test/customChains.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as testnet from './data/testnet.json'
import * as testnet2 from './data/testnet2.json'
import * as testnet3 from './data/testnet3.json'

import type { HardforkTransitionConfig } from '../src/index.js'

describe('[Common]: Custom chains', () => {
it('chain -> object: should provide correct access to private network chain parameters', () => {
const c = new Common({ chain: testnet, hardfork: Hardfork.Byzantium })
Expand Down Expand Up @@ -219,8 +221,7 @@ describe('custom chain setup with hardforks with undefined/null block numbers',
]

assert.throws(
//@ts-expect-error -- Disabling type check to verify that error is thrown
() => Common.custom({ hardforks: undefinedHardforks }),
() => Common.custom({ hardforks: undefinedHardforks as HardforkTransitionConfig[] }),
undefined,
undefined,
'throws when a hardfork with an undefined block number is passed'
Expand Down
9 changes: 3 additions & 6 deletions packages/devp2p/examples/peer-communication-les.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const REMOTE_CLIENTID_FILTER = [
'prichain',
]

// @ts-ignore
const getPeerAddr = (peer: Peer) => `${peer._socket.remoteAddress}:${peer._socket.remotePort}`
const getPeerAddr = (peer: Peer) => `${peer['_socket'].remoteAddress}:${peer['_socket'].remotePort}`

// DPT
const dpt = new devp2p.DPT(PRIVATE_KEY, {
Expand Down Expand Up @@ -224,11 +223,9 @@ setInterval(() => {
const peersCount = dpt.getPeers().length
const openSlots = rlpx._getOpenSlots()

// @ts-ignore
const queueLength = rlpx._peersQueue.length
const queueLength = rlpx['_peersQueue'].length

// @ts-ignore
const queueLength2 = rlpx._peersQueue.filter((o) => o.ts <= Date.now()).length
const queueLength2 = rlpx['_peersQueue'].filter((o) => o.ts <= Date.now()).length

console.log(
chalk.yellow(
Expand Down
9 changes: 3 additions & 6 deletions packages/devp2p/examples/peer-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ const CHECK_BLOCK_HEADER = RLP.decode(
'0xf90219a0d44a4d33e28d7ea9edd12b69bd32b394587eee498b0e2543ce2bad1877ffbeaca01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347941ad91ee08f21be3de0ba2ba6918e714da6b45836a0fdec060ee45e55da9e36060fc95dddd0bdc47e447224666a895d9f0dc9adaa0ca0092d9fcc02ca9b372daec726704ce720d3aa366739868f4820ecaabadb9ac309a0974fee017515a46303f467b6fd50872994db1b0ea64d3455bad93ff9678aced9b90100356050004c5c89691add79838a01d4c302419252a4d3c96e9273908b7ee84660886c070607b4928c416a1800746a0d1dbb442d0baf06eea321422263726748600cc200e82aec08336863514d12d665718016989189c116bc0947046cc6718110586c11464a189000a11a41cc96991970153d88840768170244197e164c6204249b9091a0052ac85088c8108a4418dd2903690a036722623888ea14e90458a390a305a2342cb02766094f68c4100036330719848b48411614686717ab6068a46318204232429dc42020608802ceecd66c3c33a3a1fc6e82522049470328a4a81ba07c6604228ba94f008476005087a6804463696b41002650c0fdf548448a90408717ca31b6d618e883bad42083be153b83bdfbb1846078104798307834383639373636353666366532303530366636663663a0ae1de0acd35a98e211c7e276ad7524bb84a5e1b8d33dd7d1c052b095b564e8b888cca66773148b6e12'
)

// @ts-ignore
const getPeerAddr = (peer: Peer) => `${peer._socket.remoteAddress}:${peer._socket.remotePort}`
const getPeerAddr = (peer: Peer) => `${peer['_socket'].remoteAddress}:${peer['_socket'].remotePort}`

// DPT
const dpt = new devp2p.DPT(PRIVATE_KEY, {
Expand Down Expand Up @@ -373,11 +372,9 @@ setInterval(() => {
const peersCount = dpt.getPeers().length
const openSlots = rlpx._getOpenSlots()

// @ts-ignore
const queueLength = rlpx._peersQueue.length
const queueLength = rlpx['_peersQueue'].length

// @ts-ignore
const queueLength2 = rlpx._peersQueue.filter((o) => o.ts <= Date.now()).length
const queueLength2 = rlpx['_peersQueue'].filter((o) => o.ts <= Date.now()).length

console.log(
chalk.yellow(
Expand Down
Loading

0 comments on commit 9631d98

Please sign in to comment.