Skip to content

Commit

Permalink
monorepo: more type improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrocheleau committed Apr 20, 2024
1 parent 13e936c commit 0817150
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 65 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
44 changes: 25 additions & 19 deletions packages/block/test/from-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ import * as uncleBlockData from './testdata/testdata-from-rpc-with-uncles_uncle-
import * as blockDataWithWithdrawals from './testdata/testdata-from-rpc-with-withdrawals.json'
import * as blockData from './testdata/testdata-from-rpc.json'

import type { JsonRpcBlock } from '../src/index.js'
import type { LegacyTransaction } from '@ethereumjs/tx'
import type { PrefixedHexString } from '@ethereumjs/util'

describe('[fromRPC]: block #2924874', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })

it('should create a block with transactions with valid signatures', () => {
const block = blockFromRpc(blockData, [], { common })
const block = blockFromRpc(blockData as JsonRpcBlock, [], { common })
const allValid = block.transactions.every((tx) => tx.verifySignature())
assert.equal(allValid, true, 'all transaction signatures are valid')
})

it('should create a block header with the correct hash', () => {
const block = blockHeaderFromRpc(blockData, { common })
const hash = hexToBytes(blockData.hash)
const block = blockHeaderFromRpc(blockData as JsonRpcBlock, { common })
const hash = hexToBytes(blockData.hash as PrefixedHexString)
assert.ok(equalsBytes(block.hash(), hash))
})
})
Expand All @@ -44,7 +46,7 @@ describe('[fromRPC]:', () => {
const blockDataTransactionValueAsInteger = blockData
blockDataTransactionValueAsInteger.transactions[0].value = valueAsIntegerString
const blockFromTransactionValueAsInteger = blockFromRpc(
blockDataTransactionValueAsInteger,
blockDataTransactionValueAsInteger as JsonRpcBlock,
undefined,
{ common }
)
Expand All @@ -60,7 +62,7 @@ describe('[fromRPC]:', () => {
const blockDataTransactionGasPriceAsInteger = blockData
blockDataTransactionGasPriceAsInteger.transactions[0].gasPrice = gasPriceAsIntegerString
const blockFromTransactionGasPriceAsInteger = blockFromRpc(
blockDataTransactionGasPriceAsInteger,
blockDataTransactionGasPriceAsInteger as JsonRpcBlock,
undefined,
{ common }
)
Expand All @@ -74,9 +76,13 @@ describe('[fromRPC]:', () => {

it('should create a block given json data that includes a difficulty parameter of type integer string', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const blockDifficultyAsInteger = blockFromRpc(blockDataDifficultyAsInteger, undefined, {
common,
})
const blockDifficultyAsInteger = blockFromRpc(
blockDataDifficultyAsInteger as JsonRpcBlock,
undefined,
{
common,
}
)
assert.equal(
blockDifficultyAsInteger.header.difficulty.toString(),
blockDataDifficultyAsInteger.difficulty
Expand All @@ -85,7 +91,7 @@ describe('[fromRPC]:', () => {

it('should create a block from london hardfork', () => {
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.London })
const block = blockFromRpc(testDataFromRpcGoerliLondon, [], { common })
const block = blockFromRpc(testDataFromRpcGoerliLondon as JsonRpcBlock, [], { common })
assert.equal(
`0x${block.header.baseFeePerGas?.toString(16)}`,
testDataFromRpcGoerliLondon.baseFeePerGas
Expand All @@ -95,19 +101,19 @@ describe('[fromRPC]:', () => {

it('should create a block with uncles', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
const block = blockFromRpc(blockDataWithUncles, [uncleBlockData], { common })
const block = blockFromRpc(blockDataWithUncles as JsonRpcBlock, [uncleBlockData], { common })
assert.ok(block.uncleHashIsValid())
})

it('should create a block with EIP-4896 withdrawals', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Shanghai })
const block = blockFromRpc(blockDataWithWithdrawals, [], { common })
const block = blockFromRpc(blockDataWithWithdrawals as JsonRpcBlock, [], { common })
assert.ok(block.withdrawalsTrieIsValid())
})

it('should create a block header with the correct hash when EIP-4896 withdrawals are present', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Shanghai })
const block = blockHeaderFromRpc(blockDataWithWithdrawals, { common })
const block = blockHeaderFromRpc(blockDataWithWithdrawals as JsonRpcBlock, { common })
const hash = blockDataWithWithdrawals.hash
assert.equal(bytesToHex(block.hash()), hash)
})
Expand All @@ -116,25 +122,25 @@ describe('[fromRPC]:', () => {
describe('[fromRPC] - Alchemy/Infura API block responses', () => {
it('should create pre merge block from Alchemy API response to eth_getBlockByHash', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const block = blockFromRpc(alchemy14151203, [], { common })
const block = blockFromRpc(alchemy14151203 as JsonRpcBlock, [], { common })
assert.equal(bytesToHex(block.hash()), alchemy14151203.hash)
})

it('should create pre and post merge blocks from Infura API responses to eth_getBlockByHash and eth_getBlockByNumber', () => {
const common = new Common({ chain: Chain.Mainnet })
let block = blockFromRpc(infura2000004woTxs, [], { common, setHardfork: true })
let block = blockFromRpc(infura2000004woTxs as JsonRpcBlock, [], { common, setHardfork: true })
assert.equal(
bytesToHex(block.hash()),
infura2000004woTxs.hash,
'created premerge block w/o txns'
)
block = blockFromRpc(infura2000004wTxs, [], { common, setHardfork: true })
block = blockFromRpc(infura2000004wTxs as JsonRpcBlock, [], { common, setHardfork: true })
assert.equal(
bytesToHex(block.hash()),
infura2000004wTxs.hash,
'created premerge block with txns'
)
block = blockFromRpc(infura15571241woTxs, [], {
block = blockFromRpc(infura15571241woTxs as JsonRpcBlock, [], {
common,
setHardfork: 58750000000000000000000n,
})
Expand All @@ -144,7 +150,7 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => {
'created post merge block without txns'
)

block = blockFromRpc(infura15571241wTxs, [], {
block = blockFromRpc(infura15571241wTxs as JsonRpcBlock, [], {
common,
setHardfork: 58750000000000000000000n,
})
Expand All @@ -157,8 +163,8 @@ describe('[fromRPC] - Alchemy/Infura API block responses', () => {

it('should correctly parse a cancun block over rpc', () => {
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Cancun })
const block = blockHeaderFromRpc(infuraGoerliBlock10536893, { common })
const hash = hexToBytes(infuraGoerliBlock10536893.hash)
const block = blockHeaderFromRpc(infuraGoerliBlock10536893 as JsonRpcBlock, { common })
const hash = hexToBytes(infuraGoerliBlock10536893.hash as PrefixedHexString)
assert.ok(equalsBytes(block.hash(), hash))
})
})
Expand Down
12 changes: 7 additions & 5 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
concatBytes,
equalsBytes,
hexToBytes,
toBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'
Expand All @@ -21,6 +20,7 @@ import * as blocksGoerli from './testdata/blocks_goerli.json'
import * as blocksMainnet from './testdata/blocks_mainnet.json'

import type { CliqueConfig } from '@ethereumjs/common'
import type { PrefixedHexString } from '@ethereumjs/util'

describe('[Block]: Header functions', () => {
it('should create with default constructor', () => {
Expand Down Expand Up @@ -457,12 +457,14 @@ describe('[Block]: Header functions', () => {
const bcBlockGasLimitTestData = testData.tests.BlockGasLimit2p63m1

for (const key of Object.keys(bcBlockGasLimitTestData)) {
const genesisRlp = toBytes(
bcBlockGasLimitTestData[key as keyof typeof bcBlockGasLimitTestData].genesisRLP
const genesisRlp = hexToBytes(
bcBlockGasLimitTestData[key as keyof typeof bcBlockGasLimitTestData]
.genesisRLP as PrefixedHexString
)
const parentBlock = Block.fromRLPSerializedBlock(genesisRlp, { common })
const blockRlp = toBytes(
bcBlockGasLimitTestData[key as keyof typeof bcBlockGasLimitTestData].blocks[0].rlp
const blockRlp = hexToBytes(
bcBlockGasLimitTestData[key as keyof typeof bcBlockGasLimitTestData].blocks[0]
.rlp as PrefixedHexString
)
const block = Block.fromRLPSerializedBlock(blockRlp, { common })
assert.doesNotThrow(() => block.validateGasLimit(parentBlock))
Expand Down
10 changes: 5 additions & 5 deletions packages/client/test/rpc/engine/preimages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe(`valid verkle network setup`, async () => {
{
name: 'block 1 no txs',
blockData: {
transactions: [] as string[],
transactions: [] as PrefixedHexString[],
blockNumber: '0x01',
stateRoot: '0x78026f1e4f2ff57c340634f844f47cb241beef4c965be86a483c855793e4b07d',
receiptTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
Expand All @@ -154,7 +154,7 @@ describe(`valid verkle network setup`, async () => {
{
name: 'block 2 having kaustinen2 block 12 txs',
blockData: {
transactions: blocks.block12.execute.transactions,
transactions: blocks.block12.execute.transactions as PrefixedHexString[],
blockNumber: '0x02',
stateRoot: '0xa86d54279c8faebed72e112310b29115d3600e8cc6ff2a2e4466a788b8776ad9',
receiptTrie: '0xd95b673818fa493deec414e01e610d97ee287c9421c8eff4102b1647c1a184e4',
Expand Down Expand Up @@ -183,7 +183,7 @@ describe(`valid verkle network setup`, async () => {
{
name: 'block 3 no txs with just withdrawals but zero coinbase',
blockData: {
transactions: [] as string[],
transactions: [] as PrefixedHexString[],
blockNumber: '0x03',
stateRoot: '0xe4538f9d7531eb76e82edf7480e4578bc2be5f454ab02db4d9db6187dfa1f9ca',
receiptTrie: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
Expand All @@ -208,7 +208,7 @@ describe(`valid verkle network setup`, async () => {
{
name: 'block 4 with kaustinen block13 txs and withdrawals',
blockData: {
transactions: blocks.block13.execute.transactions,
transactions: blocks.block13.execute.transactions as PrefixedHexString[],
blockNumber: '0x04',
stateRoot: '0x57e675e1d6b2ab5d65601e81658de1468afad77752a271a48364dcefda856614',
receiptTrie: '0x6a0be0e8208f625225e43681258eb9901ed753e2656f0cd6c0a3971fada5f190',
Expand Down Expand Up @@ -238,7 +238,7 @@ describe(`valid verkle network setup`, async () => {
},
] as const

let parentHash = genesisBlockHash
let parentHash: PrefixedHexString = genesisBlockHash
for (const testCase of testCases) {
const { name, blockData, preimages } = testCase
it(`run ${name}`, async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/client/test/rpc/engine/withdrawals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import genesisJSON from '../../testdata/geth-genesis/withdrawals.json'
import { getRpcClient, setupChain } from '../helpers.js'

import type { ExecutionPayload } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'

// Testvectors picked from static testcase generated by the geth team for api unit tests
// see: https://hackmd.io/PqZgMpnkSWCWv5joJoFymQ
Expand Down Expand Up @@ -73,7 +74,7 @@ const gethWithdrawals8BlockRlp =
const withdrawalsGethVector = withdrawalsVector.map((testVec) => ({
index: intToHex(testVec.Index),
validatorIndex: intToHex(testVec.Validator),
address: testVec.Recipient,
address: testVec.Recipient as PrefixedHexString,
amount: bigIntToHex(BigInt(testVec.Amount)),
}))

Expand Down
3 changes: 2 additions & 1 deletion packages/client/test/rpc/eth/call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { INVALID_PARAMS } from '../../../src/rpc/error-code'
import { createClient, createManager, getRpcClient, startRPC } from '../helpers.js'

import type { FullEthereumService } from '../../../src/service'
import type { PrefixedHexString } from '@ethereumjs/util'

const method = 'eth_call'

Expand Down Expand Up @@ -77,7 +78,7 @@ describe(method, () => {
const estimateTxData = {
to: createdAddress!.toString(),
from: address.toString(),
data: `0x${funcHash}`,
data: `0x${funcHash}` as PrefixedHexString,
gasLimit: bigIntToHex(BigInt(53000)),
}
const estimateTx = LegacyTransaction.fromTxData(estimateTxData, { freeze: false })
Expand Down
Loading

0 comments on commit 0817150

Please sign in to comment.