Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into util-treeshake
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Aug 29, 2024
2 parents 1c24a72 + 7df96ff commit eae4cbc
Show file tree
Hide file tree
Showing 71 changed files with 2,137 additions and 4,404 deletions.
5,678 changes: 1,678 additions & 4,000 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"@types/tape": "4.13.2",
"@typescript-eslint/eslint-plugin": "5.33.1",
"@typescript-eslint/parser": "5.33.1",
"@vitest/coverage-v8": "^v2.0.0-beta.1",
"@vitest/ui": "^v2.0.0-beta.12",
"@vitest/coverage-v8": "^v2.0.0",
"@vitest/ui": "^v2.0.0",
"c8": "7.12.0",
"cspell": "^8.13.3",
"embedme": "1.22.1",
Expand All @@ -62,10 +62,10 @@
"vite-plugin-node-polyfills": "^0.21.0",
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-wasm": "^3.3.0",
"vitest": "^v2.0.0-beta.12"
"vitest": "^v2.0.0"
},
"peerDependencies": {
"@vitest/browser": "^v2.0.0-beta.12",
"@vitest/browser": "^v2.0.0",
"webdriverio": "^8.39.0"
},
"peerDependenciesMeta": {
Expand Down
28 changes: 23 additions & 5 deletions packages/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ There are five standalone functions to instantiate a `Block`:
- `createBlockFromRPC(blockParams: JsonRpcBlock, uncles?: any[], opts?: BlockOptions)`
- `createBlockFromJsonRPCProvider(provider: string | EthersProvider, blockTag: string | bigint, opts: BlockOptions)`

For `BlockHeader` instantiation analog factory methods exists, see API docs linked below.
For `BlockHeader` instantiation standalone functions exists for instantiation, see API docs linked below.

Instantiation Example:

Expand All @@ -60,11 +60,12 @@ Properties of a `Block` or `BlockHeader` object are frozen with `Object.freeze()
API Usage Example:

```ts
// ./examples/1559.ts#L43-L47

try {
await block.validateData()
// Block data validation has passed
await blockWithMatchingBaseFee.validateData()
} catch (err) {
// handle errors appropriately
console.log(err) // block validation fails
}
```

Expand All @@ -81,6 +82,7 @@ This library supports the creation of [EIP-1559](https://eips.ethereum.org/EIPS/

import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createTxFromTxData } from '@ethereumjs/tx'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })

const block = createBlock(
Expand Down Expand Up @@ -112,6 +114,22 @@ const blockWithMatchingBaseFee = createBlock(
)

console.log(Number(blockWithMatchingBaseFee.header.baseFeePerGas)) // 11

// successful validation does not throw error
await blockWithMatchingBaseFee.validateData()

// failed validation throws error
const tx = createTxFromTxData(
{ type: 2, maxFeePerGas: BigInt(20) },
{ common: new Common({ chain: Mainnet, hardfork: Hardfork.London }) },
)
blockWithMatchingBaseFee.transactions.push(tx)
console.log(blockWithMatchingBaseFee.getTransactionsValidationErrors()) // invalid transaction added to block
try {
await blockWithMatchingBaseFee.validateData()
} catch (err) {
console.log(err) // block validation fails
}
```

EIP-1559 blocks have an extra `baseFeePerGas` field (default: `BigInt(7)`) and can encompass `FeeMarketEIP1559Transaction` txs (type `2`) (supported by `@ethereumjs/tx` `v3.2.0` or higher) as well as `LegacyTransaction` legacy txs (internal type `0`) and `AccessListEIP2930Transaction` txs (type `1`).
Expand Down Expand Up @@ -427,7 +445,7 @@ For sealing a block on instantiation you can use the `cliqueSigner` constructor

```ts
const cliqueSigner = Buffer.from('PRIVATE_KEY_HEX_STRING', 'hex')
const block = Block.fromHeaderData(headerData, { cliqueSigner })
const block = createSealedCliqueBlock(blockData, cliqueSigner)
```

Additionally there are the following utility methods for Clique/PoA related functionality in the `BlockHeader` class:
Expand Down
17 changes: 17 additions & 0 deletions packages/block/examples/1559.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { createTxFromTxData } from '@ethereumjs/tx'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })

const block = createBlock(
Expand Down Expand Up @@ -31,3 +32,19 @@ const blockWithMatchingBaseFee = createBlock(
)

console.log(Number(blockWithMatchingBaseFee.header.baseFeePerGas)) // 11

// successful validation does not throw error
await blockWithMatchingBaseFee.validateData()

// failed validation throws error
const tx = createTxFromTxData(
{ type: 2, maxFeePerGas: BigInt(20) },
{ common: new Common({ chain: Mainnet, hardfork: Hardfork.London }) },
)
blockWithMatchingBaseFee.transactions.push(tx)
console.log(blockWithMatchingBaseFee.getTransactionsValidationErrors()) // invalid transaction added to block
try {
await blockWithMatchingBaseFee.validateData()
} catch (err) {
console.log(err) // block validation fails
}
6 changes: 2 additions & 4 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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 { ChainConfig } from '@ethereumjs/common'
import type { NestedUint8Array, PrefixedHexString } from '@ethereumjs/util'

describe('[Block]: block functions', () => {
Expand Down Expand Up @@ -99,10 +100,7 @@ describe('[Block]: block functions', () => {
})

it('initialization -> setHardfork option', () => {
// @ts-ignore type is too strict in this case
const common = createCustomCommon(testnetMerge.default, Mainnet, {
name: 'testnetMerge',
})
const common = createCustomCommon(testnetMerge.default as ChainConfig, Mainnet)

let block = createBlock(
{
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
equalsBytes,
hexToBytes,
} from '@ethereumjs/util'
import { VM, runBlock, runTx } from '@ethereumjs/vm'
import { createVM, runBlock, runTx } from '@ethereumjs/vm'
import { writeFileSync } from 'fs'
import * as mcl from 'mcl-wasm'
import { initRustBN } from 'rustbn-wasm'
Expand All @@ -41,7 +41,7 @@ import { ReceiptsManager } from './receipt.js'
import type { ExecutionOptions } from './execution.js'
import type { Block } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'
import type { RunBlockOpts, TxReceipt } from '@ethereumjs/vm'
import type { RunBlockOpts, TxReceipt, VM } from '@ethereumjs/vm'

export enum ExecStatus {
VALID = 'VALID',
Expand Down Expand Up @@ -183,7 +183,7 @@ export class VMExecution extends Execution {

await mcl.init(mcl.BLS12_381)
const rustBN = await initRustBN()
this.merkleVM = await VM.create({
this.merkleVM = await createVM({
common: this.config.execCommon,
blockchain: this.chain.blockchain,
stateManager,
Expand All @@ -208,7 +208,7 @@ export class VMExecution extends Execution {
})
await mcl.init(mcl.BLS12_381)
const rustBN = await initRustBN()
this.verkleVM = await VM.create({
this.verkleVM = await createVM({
common: this.config.execCommon,
blockchain: this.chain.blockchain,
stateManager,
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/util/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function debugCodeReplayBlock(execution: VMExecution, block: Block)
import { Level } from 'level';
import { Common } from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import { VM, runBlock } from './src'
import { VM, runBlock, createVM } from './src'
import { Trie } from '@ethereumjs/trie'
import { DefaultStateManager } from './src/state'
import { Blockchain } from '@ethereumjs/blockchain'
Expand All @@ -55,7 +55,7 @@ const main = async () => {
validateBlocks: true,
validateConsensus: false,
})
const vm = await VM.create({ stateManager, blockchain, common })
const vm = await createVM({ stateManager, blockchain, common })
await runBlock({ block })
}
Expand Down
2 changes: 0 additions & 2 deletions packages/client/src/util/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ export function createRPCServer(
server.on('response', onBatchResponse)
const namespaces = [...new Set(Object.keys(methods).map((m) => m.split('_')[0]))].join(',')

//@ts-ignore
return { server, methods, namespaces }
}

Expand Down Expand Up @@ -207,7 +206,6 @@ export function createRPCServerListener(opts: CreateRPCServerListenerOpts): Http
}
})
}
//@ts-ignore
app.use(server.middleware())
const httpServer = createServer(app)
return httpServer
Expand Down
8 changes: 4 additions & 4 deletions packages/client/test/execution/vmexecution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createBlockFromExecutionPayload } from '@ethereumjs/block'
import { createBlockchain, createBlockchainFromBlocksData } from '@ethereumjs/blockchain'
import { Common, Goerli, Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common'
import { bytesToHex } from '@ethereumjs/util'
import { VM } from '@ethereumjs/vm'
import { createVM } from '@ethereumjs/vm'
import { assert, describe, it } from 'vitest'

import { Chain } from '../../src/blockchain/index.js'
Expand All @@ -16,6 +16,7 @@ import shanghaiJSON from '../testdata/geth-genesis/withdrawals.json'

import type { BlockData, ExecutionPayload } from '@ethereumjs/block'
import type { Blockchain } from '@ethereumjs/blockchain'
import type { ChainConfig } from '@ethereumjs/common'

const shanghaiPayload = {
blockNumber: '0x1',
Expand Down Expand Up @@ -87,7 +88,7 @@ const shanghaiPayload = {

describe('[VMExecution]', () => {
it('Initialization', async () => {
const vm = await VM.create()
const vm = await createVM()
const config = new Config({ vm, accountCache: 10000, storageCache: 1000 })
const chain = await Chain.create({ config })
const exec = new VMExecution({ config, chain })
Expand Down Expand Up @@ -123,8 +124,7 @@ describe('[VMExecution]', () => {
newHead = await exec.vm.blockchain.getIteratorHead!()
assert.equal(newHead.header.number, BigInt(5), 'should run all blocks')

// @ts-ignore PrefixedHexString type is too strict
const common = createCustomCommon(testnet, Mainnet)
const common = createCustomCommon(testnet as ChainConfig, Mainnet)
exec = await testSetup(blockchain, common)
await exec.run()
assert.equal(exec.hardfork, 'byzantium', 'should update HF on block run')
Expand Down
19 changes: 10 additions & 9 deletions packages/client/test/miner/pendingBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
hexToBytes,
randomBytes,
} from '@ethereumjs/util'
import { VM } from '@ethereumjs/vm'
import { createVM } from '@ethereumjs/vm'
import { loadKZG } from 'kzg-wasm'
import { assert, describe, it, vi } from 'vitest'

Expand All @@ -28,6 +28,7 @@ import { mockBlockchain } from '../rpc/mockBlockchain.js'

import type { Blockchain } from '@ethereumjs/blockchain'
import type { TypedTransaction } from '@ethereumjs/tx'
import type { VM } from '@ethereumjs/vm'

const A = {
address: new Address(hexToBytes('0x0b90087d864e82a284dca15923f3776de6bb016f')),
Expand Down Expand Up @@ -127,7 +128,7 @@ describe('[PendingBlock]', async () => {
it('should start and build', async () => {
const { txPool } = setup()
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(5000000000000000))
await setBalance(vm, B.address, BigInt(5000000000000000))
await txPool.add(txA01)
Expand Down Expand Up @@ -155,7 +156,7 @@ describe('[PendingBlock]', async () => {
it('should include txs with mismatching hardforks that can still be executed', async () => {
const { txPool } = setup()
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(5000000000000000))
await setBalance(vm, B.address, BigInt(5000000000000000))

Expand Down Expand Up @@ -204,7 +205,7 @@ describe('[PendingBlock]', async () => {
await txPool.add(txA01)
const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true })
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(5000000000000000))
const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!()
const payloadId = await pendingBlock.start(vm, parentBlock)
Expand All @@ -225,7 +226,7 @@ describe('[PendingBlock]', async () => {
common['_chainParams'].genesis.gasLimit = 50000

const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(5000000000000000))

// create alternate transactions with custom gas limits to
Expand Down Expand Up @@ -277,7 +278,7 @@ describe('[PendingBlock]', async () => {
it('should skip adding txs when tx too big to fit', async () => {
const { txPool } = setup()
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(5000000000000000))
await txPool.add(txA01)
await txPool.add(txA02)
Expand Down Expand Up @@ -319,7 +320,7 @@ describe('[PendingBlock]', async () => {
await txPool.add(txA01)
const pendingBlock = new PendingBlock({ config, txPool, skipHardForkValidation: true })
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!()
const payloadId = await pendingBlock.start(vm, parentBlock)
assert.equal(pendingBlock.pendingPayloads.size, 1, 'should set the pending payload')
Expand Down Expand Up @@ -399,7 +400,7 @@ describe('[PendingBlock]', async () => {

const pendingBlock = new PendingBlock({ config, txPool })
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(500000000000000000))
const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!()
// stub the vm's common set hf to do nothing but stay in cancun
Expand Down Expand Up @@ -459,7 +460,7 @@ describe('[PendingBlock]', async () => {

const pendingBlock = new PendingBlock({ config, txPool })
const blockchain = await createBlockchain({ common })
const vm = await VM.create({ common, blockchain })
const vm = await createVM({ common, blockchain })
await setBalance(vm, A.address, BigInt(500000000000000000))
const parentBlock = await (vm.blockchain as Blockchain).getCanonicalHeadBlock!()
// stub the vm's common set hf to do nothing but stay in cancun
Expand Down
4 changes: 2 additions & 2 deletions packages/client/test/sync/skeleton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import mergeGenesisParams from '../testdata/common/mergeTestnet.json'
import genesisJSON from '../testdata/geth-genesis/post-merge.json'

import type { Block } from '@ethereumjs/block'
import type { ChainConfig } from '@ethereumjs/common'
type Subchain = {
head: bigint
tail: bigint
Expand Down Expand Up @@ -810,8 +811,7 @@ describe('[Skeleton] / setHead', async () => {
})

it('should abort filling the canonical chain if the terminal block is invalid', async () => {
// @ts-ignore PrefixedHexString type is too strict
const common = createCustomCommon(mergeGenesisParams, Mainnet, { name: 'post-merge' })
const common = createCustomCommon(mergeGenesisParams as ChainConfig, Mainnet)
common.setHardforkBy({ blockNumber: BigInt(0) })
const config = new Config({
common,
Expand Down
8 changes: 4 additions & 4 deletions packages/evm/test/eips/eip-4200.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { createEVM } from '../../src/index.js'

import { getCommon } from './eof-utils.js'

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

async function getEVM() {
const common = getCommon()
const evm = createEVM({
Expand All @@ -19,12 +21,10 @@ describe('EIP 4200 tests', async () => {
const evm = await getEVM()
for (const key in testData.validInvalid.vectors) {
it(`Container validation tests ${key}`, () => {
//@ts-ignore
const input = testData.validInvalid.vectors[key]
const code = hexToBytes(input.code)
const input = testData.validInvalid.vectors[key as keyof typeof testData.validInvalid.vectors]
const code = hexToBytes(input.code as PrefixedHexString)

const expected = input.results.Prague.result
const _exception = input.results.Prague.exception

if (expected === true) {
validateEOF(code, evm)
Expand Down
Loading

0 comments on commit eae4cbc

Please sign in to comment.