Skip to content

Commit

Permalink
Integrate Simplified kzg-wasm Version / Deprecate Util.initKZG() (#3321)
Browse files Browse the repository at this point in the history
* Remove all Util.initKZG() calls

* Remove all Util initKZG imports

* Temporarily reference PR kzg-wasm version from GitHub in package.json files

* More Util.initKZG() and import removals

* Rename kzg-wasm createKZG() -> initKZG() (last step to avoid naming collisions)

* various cleanup

* Add finalized kzg-wasm version

* Rebuild package-lock.json

* Remove kzg-wasm dependency from root package.json

* Rebuild package-lock.json

* Rework Util blobsToCommitments() to not rely on a global kzg object (easy cases)

* Rework Util blobsToCommitments() to not rely on a global kzg object (somewhat harder cases)

* Fix blobsToCommitments() usage in the tx library 4844 tx initialization

* Test fixes

* Rename Util.initKZG() to original method name, some clean-ups

---------

Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
  • Loading branch information
holgerd77 and acolytec3 committed Mar 15, 2024
1 parent 906b362 commit a70312d
Show file tree
Hide file tree
Showing 35 changed files with 152 additions and 186 deletions.
21 changes: 10 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-simple-import-sort": "7.0.0",
"eslint-plugin-sonarjs": "0.19.0",
"kzg-wasm": "^0.2.0",
"lint-staged": "13.0.3",
"lockfile-lint-api": "^5.5.1",
"prettier": "2.7.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/block/examples/4844.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Common, Chain, Hardfork } from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import { BlobEIP4844Transaction } from '@ethereumjs/tx'
import { Address, initKZG } from '@ethereumjs/util'
import { createKZG } from 'kzg-wasm'
import { Address } from '@ethereumjs/util'
import { loadKZG } from 'kzg-wasm'
import { randomBytes } from 'crypto'

const main = async () => {
const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()

const common = new Common({
chain: Chain.Mainnet,
hardfork: Hardfork.Cancun,
Expand Down
2 changes: 1 addition & 1 deletion packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"ethereum-cryptography": "^2.1.3"
},
"devDependencies": {
"kzg-wasm": "^0.2.0"
"kzg-wasm": "^0.3.1"
},
"engines": {
"node": ">=18"
Expand Down
17 changes: 8 additions & 9 deletions packages/block/test/eip4844block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import {
blobsToCommitments,
commitmentsToVersionedHashes,
getBlobs,
initKZG,
randomBytes,
} from '@ethereumjs/util'
import { createKZG } from 'kzg-wasm'
import { loadKZG } from 'kzg-wasm'
import { assert, beforeAll, describe, it } from 'vitest'

import { BlockHeader } from '../src/header.js'
Expand All @@ -17,13 +16,14 @@ import { Block } from '../src/index.js'
import gethGenesis from './testdata/4844-hardfork.json'

import type { TypedTransaction } from '@ethereumjs/tx'
import type { Kzg } from '@ethereumjs/util'

describe('EIP4844 header tests', () => {
let common: Common

beforeAll(async () => {
const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()

common = Common.fromGethGenesis(gethGenesis, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
Expand Down Expand Up @@ -101,8 +101,7 @@ describe('blob gas tests', () => {
let common: Common
let blobGasPerBlob: bigint
beforeAll(async () => {
const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()
common = Common.fromGethGenesis(gethGenesis, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
Expand Down Expand Up @@ -155,11 +154,11 @@ describe('blob gas tests', () => {
})

describe('transaction validation tests', () => {
let kzg: Kzg
let common: Common
let blobGasPerBlob: bigint
beforeAll(async () => {
const kzg = await createKZG()
initKZG(kzg)
kzg = await loadKZG()
common = Common.fromGethGenesis(gethGenesis, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
Expand All @@ -169,7 +168,7 @@ describe('transaction validation tests', () => {
})
it('should work', () => {
const blobs = getBlobs('hello world')
const commitments = blobsToCommitments(blobs)
const commitments = blobsToCommitments(kzg, blobs)
const blobVersionedHashes = commitmentsToVersionedHashes(commitments)

const tx1 = BlobEIP4844Transaction.fromTxData(
Expand Down
13 changes: 3 additions & 10 deletions packages/block/test/from-beacon-payload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Common, Hardfork } from '@ethereumjs/common'
import { initKZG } from '@ethereumjs/util'
import { createKZG } from 'kzg-wasm'
import { loadKZG } from 'kzg-wasm'
import { assert, beforeAll, describe, it } from 'vitest'

import * as shardingJson from '../../client/test/sim/configs/4844-devnet.json'
Expand All @@ -12,11 +11,10 @@ import * as payload87475 from './testdata/payload-slot-87475.json'
import * as testnetVerkleKaustinen from './testdata/testnetVerkleKaustinen.json'

describe('[fromExecutionPayloadJson]: 4844 devnet 5', () => {
let kzg
let common: Common
beforeAll(async () => {
kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()

const commonJson = { ...shardingJson }
commonJson.config = { ...commonJson.config, chainId: 4844001005 }
const network = 'sharding'
Expand Down Expand Up @@ -77,11 +75,6 @@ describe('[fromExecutionPayloadJson]: 4844 devnet 5', () => {
})

describe('[fromExecutionPayloadJson]: kaustinen', () => {
let kzg
beforeAll(async () => {
kzg = await createKZG()
initKZG(kzg)
})
const network = 'kaustinen'

// safely change chainId without modifying undelying json
Expand Down
6 changes: 2 additions & 4 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
ecrecover,
ecsign,
hexToBytes,
initKZG,
parseGethGenesisState,
randomBytes,
setLengthLeft,
Expand All @@ -33,7 +32,7 @@ import { ecdsaRecover, ecdsaSign } from 'ethereum-cryptography/secp256k1-compat'
import { sha256 } from 'ethereum-cryptography/sha256'
import { existsSync, writeFileSync } from 'fs'
import { ensureDirSync, readFileSync, removeSync } from 'fs-extra'
import { createKZG } from 'kzg-wasm'
import { loadKZG } from 'kzg-wasm'
import { Level } from 'level'
import { homedir } from 'os'
import * as path from 'path'
Expand Down Expand Up @@ -808,8 +807,7 @@ async function run() {
// Give network id precedence over network name
const chain = args.networkId ?? args.network ?? Chain.Mainnet
const cryptoFunctions: CustomCrypto = {}
const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()

// Initialize WASM crypto if JS crypto is not specified
if (args.useJsCrypto === false) {
Expand Down
8 changes: 3 additions & 5 deletions packages/client/devnets/4844-interop/tools/txGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
getBlobs,
bytesToHex,
hexToBytes,
initKZG,
} from '@ethereumjs/util'

import { randomBytes } from '@ethereumjs/util'
import { Client } from 'jayson/promise'
import { createKZG } from 'kzg-wasm'
import { loadKZG } from 'kzg-wasm'

// CLI Args
const clientPort = parseInt(process.argv[2]) // EL client port number
Expand All @@ -28,8 +27,7 @@ async function getNonce(client: Client, account: string) {
}

async function run(data: any) {
const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()

const common = Common.fromGethGenesis(genesisJson, {
chain: genesisJson.ChainName ?? 'devnet',
Expand All @@ -40,7 +38,7 @@ async function run(data: any) {
const client = Client.http({ port: clientPort })

const blobs = getBlobs(data)
const commitments = blobsToCommitments(blobs)
const commitments = blobsToCommitments(kzg, blobs)
const hashes = commitmentsToVersionedHashes(commitments)

const account = Address.fromPrivateKey(randomBytes(32))
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@polkadot/wasm-crypto": "^7.3.2",
"abstract-level": "^1.0.3",
"body-parser": "^1.19.2",
"kzg-wasm": "^0.2.0",
"kzg-wasm": "^0.3.1",
"chalk": "^4.1.2",
"connect": "^3.7.0",
"cors": "^2.8.5",
Expand Down
16 changes: 7 additions & 9 deletions packages/client/test/miner/pendingBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import {
equalsBytes,
getBlobs,
hexToBytes,
initKZG,
randomBytes,
} from '@ethereumjs/util'
import { VM } from '@ethereumjs/vm'
import { createKZG } from 'kzg-wasm'
import { loadKZG } from 'kzg-wasm'
import { assert, describe, it, vi } from 'vitest'

import gethGenesis from '../../../block/test/testdata/4844-hardfork.json'
Expand Down Expand Up @@ -353,8 +352,7 @@ describe('[PendingBlock]', async () => {
})

it('construct blob bundles', async () => {
const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()
const common = Common.fromGethGenesis(gethGenesis, {
chain: 'customChain',
hardfork: Hardfork.Cancun,
Expand All @@ -366,9 +364,9 @@ describe('[PendingBlock]', async () => {
const { txPool } = setup()

const blobs = getBlobs('hello world')
const commitments = blobsToCommitments(blobs)
const commitments = blobsToCommitments(kzg, blobs)
const blobVersionedHashes = commitmentsToVersionedHashes(commitments)
const proofs = blobsToProofs(blobs, commitments)
const proofs = blobsToProofs(kzg, blobs, commitments)

// Create 3 txs with 2 blobs each so that only 2 of them can be included in a build
for (let x = 0; x <= 2; x++) {
Expand Down Expand Up @@ -434,7 +432,7 @@ describe('[PendingBlock]', async () => {

it('should exclude missingBlobTx', async () => {
const gethGenesis = require('../../../block/test/testdata/4844-hardfork.json')
const kzg = await createKZG()
const kzg = await loadKZG()

const common = Common.fromGethGenesis(gethGenesis, {
chain: 'customChain',
Expand All @@ -445,9 +443,9 @@ describe('[PendingBlock]', async () => {
const { txPool } = setup()

const blobs = getBlobs('hello world')
const commitments = blobsToCommitments(blobs)
const commitments = blobsToCommitments(kzg, blobs)
const blobVersionedHashes = commitmentsToVersionedHashes(commitments)
const proofs = blobsToProofs(blobs, commitments)
const proofs = blobsToProofs(kzg, blobs, commitments)

// create a tx with missing blob data which should be excluded from the build
const missingBlobTx = BlobEIP4844Transaction.fromTxData(
Expand Down
5 changes: 5 additions & 0 deletions packages/client/test/net/protocol/ethprotocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Block } from '@ethereumjs/block'
import { Common, Chain as CommonChain, Hardfork } from '@ethereumjs/common'
import { FeeMarketEIP1559Transaction, TransactionFactory, TransactionType } from '@ethereumjs/tx'
import { Address, bigIntToBytes, bytesToBigInt, hexToBytes, randomBytes } from '@ethereumjs/util'
import { loadKZG } from 'kzg-wasm'
import { assert, describe, it } from 'vitest'

import { Chain } from '../../../src/blockchain/chain'
Expand Down Expand Up @@ -202,11 +203,15 @@ describe('[EthProtocol]', () => {
})

it('verify that Transactions handler encodes/decodes correctly', async () => {
const kzg = await loadKZG()
const config = new Config({
common: new Common({
chain: CommonChain.Holesky,
hardfork: Hardfork.Paris,
eips: [4895, 4844],
customCrypto: {
kzg,
},
}),
accountCache: 10000,
storageCache: 1000,
Expand Down
10 changes: 4 additions & 6 deletions packages/client/test/rpc/engine/getPayloadV3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
commitmentsToVersionedHashes,
getBlobs,
hexToBytes,
initKZG,
} from '@ethereumjs/util'
import { createKZG } from 'kzg-wasm'
import { loadKZG } from 'kzg-wasm'
import { assert, describe, it } from 'vitest'

import { INVALID_PARAMS } from '../../../src/rpc/error-code.js'
Expand Down Expand Up @@ -68,8 +67,7 @@ describe(method, () => {
return this
}

const kzg = await createKZG()
initKZG(kzg)
const kzg = await loadKZG()

const { service, server, common } = await setupChain(genesisJSON, 'post-merge', {
engine: true,
Expand All @@ -91,9 +89,9 @@ describe(method, () => {
assert.ok(payloadId !== undefined && payloadId !== null, 'valid payloadId should be received')

const txBlobs = getBlobs('hello world')
const txCommitments = blobsToCommitments(txBlobs)
const txCommitments = blobsToCommitments(kzg, txBlobs)
const txVersionedHashes = commitmentsToVersionedHashes(txCommitments)
const txProofs = blobsToProofs(txBlobs, txCommitments)
const txProofs = blobsToProofs(kzg, txBlobs, txCommitments)

const tx = TransactionFactory.fromTxData(
{
Expand Down
Loading

0 comments on commit a70312d

Please sign in to comment.