From 2abf911d383e88d12ae8e9d72c6b184e9ef5aec0 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 10:59:40 -0600 Subject: [PATCH 01/15] util: extract static withdrawal constructor functions --- packages/util/src/withdrawal.ts | 101 ++++++++++++++++---------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/packages/util/src/withdrawal.ts b/packages/util/src/withdrawal.ts index daa2e53f10..7640d1bcf8 100644 --- a/packages/util/src/withdrawal.ts +++ b/packages/util/src/withdrawal.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-use-before-define */ import { Address } from './address.js' import { bigIntToHex, bytesToHex, toBytes } from './bytes.js' import { BIGINT_0 } from './constants.js' @@ -48,57 +49,8 @@ export class Withdrawal { public readonly amount: bigint, ) {} - public static fromWithdrawalData(withdrawalData: WithdrawalData) { - const { - index: indexData, - validatorIndex: validatorIndexData, - address: addressData, - amount: amountData, - } = withdrawalData - const index = toType(indexData, TypeOutput.BigInt) - const validatorIndex = toType(validatorIndexData, TypeOutput.BigInt) - const address = addressData instanceof Address ? addressData : new Address(toBytes(addressData)) - const amount = toType(amountData, TypeOutput.BigInt) - - return new Withdrawal(index, validatorIndex, address, amount) - } - - public static fromValuesArray(withdrawalArray: WithdrawalBytes) { - if (withdrawalArray.length !== 4) { - throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`) - } - const [index, validatorIndex, address, amount] = withdrawalArray - return Withdrawal.fromWithdrawalData({ index, validatorIndex, address, amount }) - } - - /** - * Convert a withdrawal to a buffer array - * @param withdrawal the withdrawal to convert - * @returns buffer array of the withdrawal - */ - public static toBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { - const { index, validatorIndex, address, amount } = withdrawal - const indexBytes = - toType(index, TypeOutput.BigInt) === BIGINT_0 - ? new Uint8Array() - : toType(index, TypeOutput.Uint8Array) - const validatorIndexBytes = - toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0 - ? new Uint8Array() - : toType(validatorIndex, TypeOutput.Uint8Array) - const addressBytes = - address instanceof Address ? (
address).bytes : toType(address, TypeOutput.Uint8Array) - - const amountBytes = - toType(amount, TypeOutput.BigInt) === BIGINT_0 - ? new Uint8Array() - : toType(amount, TypeOutput.Uint8Array) - - return [indexBytes, validatorIndexBytes, addressBytes, amountBytes] - } - raw() { - return Withdrawal.toBytesArray(this) + return toBytesArray(this) } toValue() { @@ -119,3 +71,52 @@ export class Withdrawal { } } } + +export function fromValuesArray(withdrawalArray: WithdrawalBytes) { + if (withdrawalArray.length !== 4) { + throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`) + } + const [index, validatorIndex, address, amount] = withdrawalArray + return fromWithdrawalData({ index, validatorIndex, address, amount }) +} + +/** + * Convert a withdrawal to a buffer array + * @param withdrawal the withdrawal to convert + * @returns buffer array of the withdrawal + */ +export function toBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { + const { index, validatorIndex, address, amount } = withdrawal + const indexBytes = + toType(index, TypeOutput.BigInt) === BIGINT_0 + ? new Uint8Array() + : toType(index, TypeOutput.Uint8Array) + const validatorIndexBytes = + toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0 + ? new Uint8Array() + : toType(validatorIndex, TypeOutput.Uint8Array) + const addressBytes = + address instanceof Address ? (
address).bytes : toType(address, TypeOutput.Uint8Array) + + const amountBytes = + toType(amount, TypeOutput.BigInt) === BIGINT_0 + ? new Uint8Array() + : toType(amount, TypeOutput.Uint8Array) + + return [indexBytes, validatorIndexBytes, addressBytes, amountBytes] +} + +export function fromWithdrawalData(withdrawalData: WithdrawalData) { + const { + index: indexData, + validatorIndex: validatorIndexData, + address: addressData, + amount: amountData, + } = withdrawalData + const index = toType(indexData, TypeOutput.BigInt) + const validatorIndex = toType(validatorIndexData, TypeOutput.BigInt) + const address = addressData instanceof Address ? addressData : new Address(toBytes(addressData)) + const amount = toType(amountData, TypeOutput.BigInt) + + return new Withdrawal(index, validatorIndex, address, amount) +} From e671abe1b6ab211bc6adde37170a3cc2cae8b414 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 11:15:00 -0600 Subject: [PATCH 02/15] util: rename withdrawal constructor functions --- packages/util/src/withdrawal.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/util/src/withdrawal.ts b/packages/util/src/withdrawal.ts index 7640d1bcf8..9a4ced48f5 100644 --- a/packages/util/src/withdrawal.ts +++ b/packages/util/src/withdrawal.ts @@ -50,7 +50,7 @@ export class Withdrawal { ) {} raw() { - return toBytesArray(this) + return withdrawalToBytesArray(this) } toValue() { @@ -72,12 +72,12 @@ export class Withdrawal { } } -export function fromValuesArray(withdrawalArray: WithdrawalBytes) { +export function createWithdrawalFromValuesArray(withdrawalArray: WithdrawalBytes) { if (withdrawalArray.length !== 4) { throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`) } const [index, validatorIndex, address, amount] = withdrawalArray - return fromWithdrawalData({ index, validatorIndex, address, amount }) + return createWithdrawal({ index, validatorIndex, address, amount }) } /** @@ -85,7 +85,7 @@ export function fromValuesArray(withdrawalArray: WithdrawalBytes) { * @param withdrawal the withdrawal to convert * @returns buffer array of the withdrawal */ -export function toBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { +export function withdrawalToBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { const { index, validatorIndex, address, amount } = withdrawal const indexBytes = toType(index, TypeOutput.BigInt) === BIGINT_0 @@ -106,7 +106,7 @@ export function toBytesArray(withdrawal: Withdrawal | WithdrawalData): Withdrawa return [indexBytes, validatorIndexBytes, addressBytes, amountBytes] } -export function fromWithdrawalData(withdrawalData: WithdrawalData) { +export function createWithdrawal(withdrawalData: WithdrawalData) { const { index: indexData, validatorIndex: validatorIndexData, From 1e22867ccdc703c155a869836b08dc89764d3e11 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 11:16:32 -0600 Subject: [PATCH 03/15] update downstream --- packages/block/src/block/constructors.ts | 8 +++---- packages/block/test/eip4895block.spec.ts | 4 ++-- packages/block/test/eip7685block.spec.ts | 2 +- packages/block/test/header.spec.ts | 4 ++-- .../client/test/rpc/engine/preimages.spec.ts | 4 ++-- .../test/rpc/engine/withdrawals.spec.ts | 4 ++-- packages/tx/test/base.spec.ts | 4 ++-- packages/tx/test/legacy.spec.ts | 2 +- packages/util/examples/withdrawal.ts | 4 ++-- packages/util/test/withdrawal.spec.ts | 24 +++++++++++++------ packages/vm/src/buildBlock.ts | 5 ++-- .../api/EIPs/eip-4895-withdrawals.spec.ts | 6 ++--- 12 files changed, 41 insertions(+), 30 deletions(-) diff --git a/packages/block/src/block/constructors.ts b/packages/block/src/block/constructors.ts index fdcdb023df..f74c95ecd0 100644 --- a/packages/block/src/block/constructors.ts +++ b/packages/block/src/block/constructors.ts @@ -11,11 +11,11 @@ import { CLRequestFactory, ConsolidationRequest, DepositRequest, - Withdrawal, WithdrawalRequest, bigIntToHex, bytesToHex, bytesToUtf8, + createWithdrawal, equalsBytes, fetchFromProvider, getProvider, @@ -103,7 +103,7 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) { uncleHeaders.push(uh) } - const withdrawals = withdrawalsData?.map(Withdrawal.fromWithdrawalData) + const withdrawals = withdrawalsData?.map(createWithdrawal) // The witness data is planned to come in rlp serialized bytes so leave this // stub till that time const executionWitness = executionWitnessData @@ -220,7 +220,7 @@ export function createBlockFromBytesArray(values: BlockBytes, opts?: BlockOption address, amount, })) - ?.map(Withdrawal.fromWithdrawalData) + ?.map(createWithdrawal) let requests if (header.common.isActivatedEIP(7685)) { @@ -402,7 +402,7 @@ export async function createBlockFromExecutionPayload( } const transactionsTrie = await genTransactionsTrieRoot(txs, new Trie({ common: opts?.common })) - const withdrawals = withdrawalsData?.map((wData) => Withdrawal.fromWithdrawalData(wData)) + const withdrawals = withdrawalsData?.map((wData) => createWithdrawal(wData)) const withdrawalsRoot = withdrawals ? await genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common })) : undefined diff --git a/packages/block/test/eip4895block.spec.ts b/packages/block/test/eip4895block.spec.ts index 633b520fe9..bac23d4cc1 100644 --- a/packages/block/test/eip4895block.spec.ts +++ b/packages/block/test/eip4895block.spec.ts @@ -3,7 +3,7 @@ import { RLP } from '@ethereumjs/rlp' import { Address, KECCAK256_RLP, - Withdrawal, + createWithdrawalFromValuesArray, hexToBytes, randomBytes, zeros, @@ -40,7 +40,7 @@ describe('EIP4895 tests', () => { // get withdrawalsArray const gethBlockBytesArray = RLP.decode(hexToBytes(`0x${gethWithdrawals8BlockRlp}`)) const withdrawals = (gethBlockBytesArray[3] as WithdrawalBytes[]).map((wa) => - Withdrawal.fromValuesArray(wa), + createWithdrawalFromValuesArray(wa), ) assert.equal(withdrawals.length, 8, '8 withdrawals should have been found') const gethWithdrawalsRoot = (gethBlockBytesArray[0] as Uint8Array[])[16] as Uint8Array diff --git a/packages/block/test/eip7685block.spec.ts b/packages/block/test/eip7685block.spec.ts index 928e2db558..02dddb8bf5 100644 --- a/packages/block/test/eip7685block.spec.ts +++ b/packages/block/test/eip7685block.spec.ts @@ -109,7 +109,7 @@ describe('7685 tests', () => { }) }) -describe('fromValuesArray tests', () => { +describe('createWithdrawalFromValuesArray tests', () => { it('should construct a block with empty requests root', () => { const block = createBlockFromBytesArray( [createBlockHeader({}, { common }).raw(), [], [], [], []], diff --git a/packages/block/test/header.spec.ts b/packages/block/test/header.spec.ts index 0ccde3065f..4964ecfb8e 100644 --- a/packages/block/test/header.spec.ts +++ b/packages/block/test/header.spec.ts @@ -129,7 +129,7 @@ describe('[Block]: Header functions', () => { } }) - it('Initialization -> fromValuesArray()', () => { + it('Initialization -> createWithdrawalFromValuesArray()', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.London }) const zero = new Uint8Array(0) const headerArray = [] @@ -156,7 +156,7 @@ describe('[Block]: Header functions', () => { ) }) - it('Initialization -> fromValuesArray() -> error cases', () => { + it('Initialization -> createWithdrawalFromValuesArray() -> error cases', () => { const headerArray = Array(22).fill(new Uint8Array(0)) // mock header data (if set to zeros(0) header throws) diff --git a/packages/client/test/rpc/engine/preimages.spec.ts b/packages/client/test/rpc/engine/preimages.spec.ts index 5b6b8d19b9..a4e8848cbb 100644 --- a/packages/client/test/rpc/engine/preimages.spec.ts +++ b/packages/client/test/rpc/engine/preimages.spec.ts @@ -6,8 +6,8 @@ import { } from '@ethereumjs/block' import { createTxFromSerializedData } from '@ethereumjs/tx' import { - Withdrawal, bytesToHex, + createWithdrawal, equalsBytes, hexToBytes, intToBytes, @@ -50,7 +50,7 @@ async function genBlockWithdrawals(blockNumber: number) { } }) const withdrawalsRoot = bytesToHex( - await genWithdrawalsTrieRoot(withdrawals.map(Withdrawal.fromWithdrawalData)), + await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal)), ) return { withdrawals, withdrawalsRoot } diff --git a/packages/client/test/rpc/engine/withdrawals.spec.ts b/packages/client/test/rpc/engine/withdrawals.spec.ts index 768ca0deb4..b4a4edf502 100644 --- a/packages/client/test/rpc/engine/withdrawals.spec.ts +++ b/packages/client/test/rpc/engine/withdrawals.spec.ts @@ -1,6 +1,6 @@ import { genWithdrawalsTrieRoot } from '@ethereumjs/block' import { Trie } from '@ethereumjs/trie' -import { Withdrawal, bigIntToHex, bytesToHex, intToHex } from '@ethereumjs/util' +import { bigIntToHex, bytesToHex, createWithdrawal, intToHex } from '@ethereumjs/util' import { assert, it } from 'vitest' import { INVALID_PARAMS } from '../../../src/rpc/error-code.js' @@ -105,7 +105,7 @@ for (const { name, withdrawals, withdrawalsRoot, gethBlockRlp } of testCases) { it(name, async () => { // check withdrawals root computation const computedWithdrawalsRoot = bytesToHex( - await genWithdrawalsTrieRoot(withdrawals.map(Withdrawal.fromWithdrawalData), new Trie()), + await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal), new Trie()), ) assert.equal( withdrawalsRoot, diff --git a/packages/tx/test/base.spec.ts b/packages/tx/test/base.spec.ts index ea38403bd4..d9c4d04a7f 100644 --- a/packages/tx/test/base.spec.ts +++ b/packages/tx/test/base.spec.ts @@ -184,7 +184,7 @@ describe('[BaseTransaction]', () => { } }) - it('fromValuesArray()', () => { + it('createWithdrawalFromValuesArray()', () => { let rlpData: any = legacyTxs[0].raw() rlpData[0] = toBytes('0x0') try { @@ -270,7 +270,7 @@ describe('[BaseTransaction]', () => { for (const tx of txType.txs) { assert.ok( txType.create.bytesArray(tx.raw() as any, { common }), - `${txType.name}: should do roundtrip raw() -> fromValuesArray()`, + `${txType.name}: should do roundtrip raw() -> createWithdrawalFromValuesArray()`, ) } } diff --git a/packages/tx/test/legacy.spec.ts b/packages/tx/test/legacy.spec.ts index c9253deadb..b950b78b52 100644 --- a/packages/tx/test/legacy.spec.ts +++ b/packages/tx/test/legacy.spec.ts @@ -99,7 +99,7 @@ describe('[Transaction]', () => { ) }) - it('Initialization -> decode with fromValuesArray()', () => { + it('Initialization -> decode with createWithdrawalFromValuesArray()', () => { for (const tx of txFixtures.slice(0, 4)) { const txData = tx.raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString)) const pt = createLegacyTxFromBytesArray(txData) diff --git a/packages/util/examples/withdrawal.ts b/packages/util/examples/withdrawal.ts index cf6d4b5959..10c4e98def 100644 --- a/packages/util/examples/withdrawal.ts +++ b/packages/util/examples/withdrawal.ts @@ -1,6 +1,6 @@ -import { Withdrawal } from '@ethereumjs/util' +import { createWithdrawal } from '@ethereumjs/util' -const withdrawal = Withdrawal.fromWithdrawalData({ +const withdrawal = createWithdrawal({ index: 0n, validatorIndex: 65535n, address: '0x0000000000000000000000000000000000000000', diff --git a/packages/util/test/withdrawal.spec.ts b/packages/util/test/withdrawal.spec.ts index 2aa7754164..762872a221 100644 --- a/packages/util/test/withdrawal.spec.ts +++ b/packages/util/test/withdrawal.spec.ts @@ -1,7 +1,15 @@ import { decode, encode } from '@ethereumjs/rlp' import { assert, describe, it } from 'vitest' -import { Withdrawal, bigIntToHex, bytesToHex, hexToBytes, intToHex } from '../src/index.js' +import { + bigIntToHex, + bytesToHex, + createWithdrawal, + createWithdrawalFromValuesArray, + hexToBytes, + intToHex, + withdrawalToBytesArray, +} from '../src/index.js' import type { WithdrawalBytes, WithdrawalData } from '../src/index.js' @@ -68,25 +76,27 @@ describe('Withdrawal', () => { // gethWithdrawals8Rlp is rlp encoded block with withdrawals in the 4th element of the top array const gethWithdrawalsBuffer = decode(hexToBytes(gethWithdrawals8BlockRlp))[3]! const gethWithdrawalsRlp = bytesToHex(encode(gethWithdrawalsBuffer)) - it('fromWithdrawalData and toBytesArray', () => { + it('createWithdrawal and withdrawalToBytesArray', () => { const withdrawals = withdrawalsGethVector.map((withdrawal) => - Withdrawal.fromWithdrawalData(withdrawal as WithdrawalData), + createWithdrawal(withdrawal as WithdrawalData), ) const withdrawalstoBytesArr = withdrawals.map((wt) => wt.raw()) const withdrawalsToRlp = bytesToHex(encode(withdrawalstoBytesArr)) assert.equal(gethWithdrawalsRlp, withdrawalsToRlp, 'The withdrawals to buffer should match') }) - it('toBytesArray from withdrawalData', () => { + it('withdrawalToBytesArray from withdrawalData', () => { const withdrawalsDataToBytesArr = withdrawalsGethVector.map((withdrawal) => - Withdrawal.toBytesArray(withdrawal as WithdrawalData), + withdrawalToBytesArray(withdrawal as WithdrawalData), ) const withdrawalsDataToRlp = bytesToHex(encode(withdrawalsDataToBytesArr)) assert.equal(gethWithdrawalsRlp, withdrawalsDataToRlp, 'The withdrawals to buffer should match') }) - it('fromValuesArray, toJSON and toValue', () => { - const withdrawals = (gethWithdrawalsBuffer as WithdrawalBytes[]).map(Withdrawal.fromValuesArray) + it('createWithdrawalFromValuesArray, toJSON and toValue', () => { + const withdrawals = (gethWithdrawalsBuffer as WithdrawalBytes[]).map( + createWithdrawalFromValuesArray, + ) const withdrawalsJson = withdrawals.map((wt) => wt.toJSON()) assert.deepEqual(withdrawalsGethVector, withdrawalsJson, 'Withdrawals json should match') diff --git a/packages/vm/src/buildBlock.ts b/packages/vm/src/buildBlock.ts index 5ce32f1ddb..30013c6704 100644 --- a/packages/vm/src/buildBlock.ts +++ b/packages/vm/src/buildBlock.ts @@ -17,7 +17,7 @@ import { GWEI_TO_WEI, KECCAK256_RLP, TypeOutput, - Withdrawal, + createWithdrawal, createZeroAddress, toBytes, toType, @@ -40,6 +40,7 @@ import type { BuildBlockOpts, BuilderOpts, RunTxResult, SealBlockOpts } from './ import type { VM } from './vm.js' import type { Block, HeaderData } from '@ethereumjs/block' import type { TypedTransaction } from '@ethereumjs/tx' +import type { Withdrawal } from '@ethereumjs/util' export enum BuildStatus { Reverted = 'reverted', @@ -94,7 +95,7 @@ export class BlockBuilder { gasLimit: opts.headerData?.gasLimit ?? opts.parentBlock.header.gasLimit, timestamp: opts.headerData?.timestamp ?? Math.round(Date.now() / 1000), } - this.withdrawals = opts.withdrawals?.map(Withdrawal.fromWithdrawalData) + this.withdrawals = opts.withdrawals?.map(createWithdrawal) if ( this.vm.common.isActivatedEIP(1559) && diff --git a/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts index e37582d931..97e32500dd 100644 --- a/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts +++ b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts @@ -8,8 +8,8 @@ import { Address, GWEI_TO_WEI, KECCAK256_RLP, - Withdrawal, bytesToHex, + createWithdrawalFromValuesArray, hexToBytes, parseGethGenesisState, zeros, @@ -142,7 +142,7 @@ describe('EIP4895 tests', () => { const gethBlockBufferArray = decode(hexToBytes(gethWithdrawals8BlockRlp)) const withdrawals = (gethBlockBufferArray[3] as WithdrawalBytes[]).map((wa) => - Withdrawal.fromValuesArray(wa), + createWithdrawalFromValuesArray(wa), ) assert.equal(withdrawals[0].amount, BigInt(0), 'withdrawal 0 should have 0 amount') let block: Block @@ -215,7 +215,7 @@ describe('EIP4895 tests', () => { const gethBlockBufferArray = decode(hexToBytes(gethWithdrawals8BlockRlp)) const withdrawals = (gethBlockBufferArray[3] as WithdrawalBytes[]).map((wa) => - Withdrawal.fromValuesArray(wa), + createWithdrawalFromValuesArray(wa), ) const td = await blockchain.getTotalDifficulty(genesisBlock.hash()) From 82e3c8d3d00afd783071f57be81ac473053e0d70 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 11:25:56 -0600 Subject: [PATCH 04/15] util: rename requests.ts > request.ts --- packages/util/src/{requests.ts => request.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/util/src/{requests.ts => request.ts} (100%) diff --git a/packages/util/src/requests.ts b/packages/util/src/request.ts similarity index 100% rename from packages/util/src/requests.ts rename to packages/util/src/request.ts From 40000ccea3cb075dcf89252ad9a37c675d45e7dd Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 11:48:30 -0600 Subject: [PATCH 05/15] util: extract static DepositRequest constructors --- packages/util/src/request.ts | 52 ++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index 3490e06977..99832b3154 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-use-before-define */ import { RLP } from '@ethereumjs/rlp' import { concatBytes } from 'ethereum-cryptography/utils' @@ -101,22 +102,6 @@ export class DepositRequest extends CLRequest { super(CLRequestType.Deposit) } - public static fromRequestData(depositData: DepositRequestData): DepositRequest { - const { pubkey, withdrawalCredentials, amount, signature, index } = depositData - return new DepositRequest(pubkey, withdrawalCredentials, amount, signature, index) - } - - public static fromJSON(jsonData: DepositRequestV1): DepositRequest { - const { pubkey, withdrawalCredentials, amount, signature, index } = jsonData - return this.fromRequestData({ - pubkey: hexToBytes(pubkey), - withdrawalCredentials: hexToBytes(withdrawalCredentials), - amount: hexToBigInt(amount), - signature: hexToBytes(signature), - index: hexToBigInt(index), - }) - } - serialize() { const indexBytes = this.index === BIGINT_0 ? new Uint8Array() : bigIntToBytes(this.index) @@ -148,7 +133,7 @@ export class DepositRequest extends CLRequest { const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode( bytes.slice(1), ) as [Uint8Array, Uint8Array, Uint8Array, Uint8Array, Uint8Array] - return this.fromRequestData({ + return createDepositRequest({ pubkey, withdrawalCredentials, amount: bytesToBigInt(amount), @@ -278,3 +263,36 @@ export class CLRequestFactory { } } } + +export function createDepositRequest(depositData: DepositRequestData): DepositRequest { + const { pubkey, withdrawalCredentials, amount, signature, index } = depositData + return new DepositRequest(pubkey, withdrawalCredentials, amount, signature, index) +} + +export function createDepositRequestFromJSON(jsonData: DepositRequestV1): DepositRequest { + const { pubkey, withdrawalCredentials, amount, signature, index } = jsonData + return createDepositRequest({ + pubkey: hexToBytes(pubkey), + withdrawalCredentials: hexToBytes(withdrawalCredentials), + amount: hexToBigInt(amount), + signature: hexToBytes(signature), + index: hexToBigInt(index), + }) +} + +export function createDepositRequestFromBytes(bytes: Uint8Array): DepositRequest { + const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode(bytes.slice(1)) as [ + Uint8Array, + Uint8Array, + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createDepositRequest({ + pubkey, + withdrawalCredentials, + amount: bytesToBigInt(amount), + signature, + index: bytesToBigInt(index), + }) +} From 0ad1148dfcf6bd3abf7f754eaa47310c049c707a Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 12:06:37 -0600 Subject: [PATCH 06/15] update downstream --- packages/block/src/block/constructors.ts | 4 ++-- packages/block/test/eip7685block.spec.ts | 4 ++-- packages/client/test/rpc/engine/preimages.spec.ts | 4 ++-- packages/util/src/index.ts | 2 +- packages/util/test/requests.spec.ts | 4 ++-- packages/vm/src/requests.ts | 4 ++-- packages/vm/test/api/EIPs/eip-6110.spec.ts | 2 +- packages/vm/test/api/EIPs/eip-7685.spec.ts | 4 ++-- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/block/src/block/constructors.ts b/packages/block/src/block/constructors.ts index f74c95ecd0..78ac32ac47 100644 --- a/packages/block/src/block/constructors.ts +++ b/packages/block/src/block/constructors.ts @@ -10,11 +10,11 @@ import { import { CLRequestFactory, ConsolidationRequest, - DepositRequest, WithdrawalRequest, bigIntToHex, bytesToHex, bytesToUtf8, + createDepositRequestFromJSON, createWithdrawal, equalsBytes, fetchFromProvider, @@ -419,7 +419,7 @@ export async function createBlockFromExecutionPayload( if (depositRequests !== undefined && depositRequests !== null) { for (const dJson of depositRequests) { - requests!.push(DepositRequest.fromJSON(dJson)) + requests!.push(createDepositRequestFromJSON(dJson)) } } if (withdrawalRequests !== undefined && withdrawalRequests !== null) { diff --git a/packages/block/test/eip7685block.spec.ts b/packages/block/test/eip7685block.spec.ts index 02dddb8bf5..a724d51fe4 100644 --- a/packages/block/test/eip7685block.spec.ts +++ b/packages/block/test/eip7685block.spec.ts @@ -1,9 +1,9 @@ import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { - DepositRequest, KECCAK256_RLP, WithdrawalRequest, bytesToBigInt, + createDepositRequest, randomBytes, } from '@ethereumjs/util' import { assert, describe, expect, it } from 'vitest' @@ -27,7 +27,7 @@ function getRandomDepositRequest(): CLRequest { signature: randomBytes(96), index: bytesToBigInt(randomBytes(8)), } - return DepositRequest.fromRequestData(depositRequestData) as CLRequest + return createDepositRequest(depositRequestData) as CLRequest } function getRandomWithdrawalRequest(): CLRequest { diff --git a/packages/client/test/rpc/engine/preimages.spec.ts b/packages/client/test/rpc/engine/preimages.spec.ts index a4e8848cbb..2c72907e60 100644 --- a/packages/client/test/rpc/engine/preimages.spec.ts +++ b/packages/client/test/rpc/engine/preimages.spec.ts @@ -7,7 +7,7 @@ import { import { createTxFromSerializedData } from '@ethereumjs/tx' import { bytesToHex, - createWithdrawal, + createWithdrawalFromData, equalsBytes, hexToBytes, intToBytes, @@ -50,7 +50,7 @@ async function genBlockWithdrawals(blockNumber: number) { } }) const withdrawalsRoot = bytesToHex( - await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal)), + await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawalFromData)), ) return { withdrawals, withdrawalsRoot } diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index f1e333bd48..d1f6dd95e7 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -64,6 +64,6 @@ export * from './kzg.js' export * from './lock.js' export * from './mapDB.js' export * from './provider.js' -export * from './requests.js' +export * from './request.js' export * from './tasks.js' export * from './verkle.js' diff --git a/packages/util/test/requests.spec.ts b/packages/util/test/requests.spec.ts index 96b4a49f57..75df9dc1c0 100644 --- a/packages/util/test/requests.spec.ts +++ b/packages/util/test/requests.spec.ts @@ -7,9 +7,9 @@ import { ConsolidationRequest, DepositRequest, WithdrawalRequest, -} from '../src/requests.js' +} from '../src/request.js' -import type { CLRequest } from '../src/requests.js' +import type { CLRequest } from '../src/request.js' describe('Requests', () => { const testCases = [ diff --git a/packages/vm/src/requests.ts b/packages/vm/src/requests.ts index 52baccb219..be70abddf4 100644 --- a/packages/vm/src/requests.ts +++ b/packages/vm/src/requests.ts @@ -1,7 +1,6 @@ import { Mainnet } from '@ethereumjs/common' import { ConsolidationRequest, - DepositRequest, WithdrawalRequest, bigIntToAddressBytes, bigIntToBytes, @@ -9,6 +8,7 @@ import { bytesToHex, bytesToInt, createAddressFromString, + createDepositRequest, setLengthLeft, unpadBytes, } from '@ethereumjs/util' @@ -221,7 +221,7 @@ const accumulateDeposits = async ( ]) const index = bytesToBigInt(indexBytesBigEndian) requests.push( - DepositRequest.fromRequestData({ + createDepositRequest({ pubkey, withdrawalCredentials, amount, diff --git a/packages/vm/test/api/EIPs/eip-6110.spec.ts b/packages/vm/test/api/EIPs/eip-6110.spec.ts index 3dd7d0142a..0148859c27 100644 --- a/packages/vm/test/api/EIPs/eip-6110.spec.ts +++ b/packages/vm/test/api/EIPs/eip-6110.spec.ts @@ -15,7 +15,7 @@ import { assert, describe, it } from 'vitest' import { buildBlock, runBlock } from '../../../src/index.js' import { setupVM } from '../utils.js' -import type { DepositRequest } from '../../../../util/src/requests.js' +import type { DepositRequest } from '../../../../util/src/request.js' import type { PrefixedHexString } from '@ethereumjs/util' const depositContractByteCode = hexToBytes( diff --git a/packages/vm/test/api/EIPs/eip-7685.spec.ts b/packages/vm/test/api/EIPs/eip-7685.spec.ts index d56df5cce7..983d7c1b79 100644 --- a/packages/vm/test/api/EIPs/eip-7685.spec.ts +++ b/packages/vm/test/api/EIPs/eip-7685.spec.ts @@ -2,9 +2,9 @@ import { createBlock, genRequestsTrieRoot } from '@ethereumjs/block' import { createBlockchain } from '@ethereumjs/blockchain' import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { - DepositRequest, KECCAK256_RLP, bytesToBigInt, + createDepositRequest, hexToBytes, randomBytes, } from '@ethereumjs/util' @@ -26,7 +26,7 @@ function getRandomDepositRequest(): CLRequest { signature: randomBytes(96), index: bytesToBigInt(randomBytes(8)), } - return DepositRequest.fromRequestData(depositRequestData) as CLRequest + return createDepositRequest(depositRequestData) as CLRequest } const common = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, eips: [7685] }) From 5d97b41ec3788b0e235c61efcfe8b2568ecd8d99 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 12:10:19 -0600 Subject: [PATCH 07/15] util: extract static constructors from WithdrawalRequest --- packages/util/src/request.ts | 71 +++++++++++++++--------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index 99832b3154..a809485a1d 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -128,19 +128,6 @@ export class DepositRequest extends CLRequest { index: bigIntToHex(this.index), } } - - public static deserialize(bytes: Uint8Array): DepositRequest { - const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode( - bytes.slice(1), - ) as [Uint8Array, Uint8Array, Uint8Array, Uint8Array, Uint8Array] - return createDepositRequest({ - pubkey, - withdrawalCredentials, - amount: bytesToBigInt(amount), - signature, - index: bytesToBigInt(index), - }) - } } export class WithdrawalRequest extends CLRequest { @@ -152,20 +139,6 @@ export class WithdrawalRequest extends CLRequest { super(CLRequestType.Withdrawal) } - public static fromRequestData(withdrawalData: WithdrawalRequestData): WithdrawalRequest { - const { sourceAddress, validatorPubkey, amount } = withdrawalData - return new WithdrawalRequest(sourceAddress, validatorPubkey, amount) - } - - public static fromJSON(jsonData: WithdrawalRequestV1): WithdrawalRequest { - const { sourceAddress, validatorPubkey, amount } = jsonData - return this.fromRequestData({ - sourceAddress: hexToBytes(sourceAddress), - validatorPubkey: hexToBytes(validatorPubkey), - amount: hexToBigInt(amount), - }) - } - serialize() { const amountBytes = this.amount === BIGINT_0 ? new Uint8Array() : bigIntToBytes(this.amount) @@ -182,19 +155,6 @@ export class WithdrawalRequest extends CLRequest { amount: bigIntToHex(this.amount), } } - - public static deserialize(bytes: Uint8Array): WithdrawalRequest { - const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes.slice(1)) as [ - Uint8Array, - Uint8Array, - Uint8Array, - ] - return this.fromRequestData({ - sourceAddress, - validatorPubkey, - amount: bytesToBigInt(amount), - }) - } } export class ConsolidationRequest extends CLRequest { @@ -253,9 +213,9 @@ export class CLRequestFactory { public static fromSerializedRequest(bytes: Uint8Array): CLRequest { switch (bytes[0]) { case CLRequestType.Deposit: - return DepositRequest.deserialize(bytes) + return createDepositRequestFromBytes(bytes) case CLRequestType.Withdrawal: - return WithdrawalRequest.deserialize(bytes) + return createWithDrawalRequestFromBytes(bytes) case CLRequestType.Consolidation: return ConsolidationRequest.deserialize(bytes) default: @@ -296,3 +256,30 @@ export function createDepositRequestFromBytes(bytes: Uint8Array): DepositRequest index: bytesToBigInt(index), }) } + +export function createWithdrawalRequest(withdrawalData: WithdrawalRequestData): WithdrawalRequest { + const { sourceAddress, validatorPubkey, amount } = withdrawalData + return new WithdrawalRequest(sourceAddress, validatorPubkey, amount) +} + +export function createWithdrawalRequestFromJSON(jsonData: WithdrawalRequestV1): WithdrawalRequest { + const { sourceAddress, validatorPubkey, amount } = jsonData + return createWithdrawalRequest({ + sourceAddress: hexToBytes(sourceAddress), + validatorPubkey: hexToBytes(validatorPubkey), + amount: hexToBigInt(amount), + }) +} + +export function createWithDrawalRequestFromBytes(bytes: Uint8Array): WithdrawalRequest { + const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes.slice(1)) as [ + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createWithdrawalRequest({ + sourceAddress, + validatorPubkey, + amount: bytesToBigInt(amount), + }) +} From db3266a34234fccac995ce4020d5a59eb3d4220c Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 12:17:43 -0600 Subject: [PATCH 08/15] util: extract static constructors for ConsolidationRequest --- packages/block/src/block/constructors.ts | 8 +-- packages/block/test/eip7685block.spec.ts | 4 +- .../client/test/rpc/engine/preimages.spec.ts | 4 +- packages/util/src/request.ts | 60 ++++++++++--------- packages/vm/src/requests.ts | 10 ++-- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/packages/block/src/block/constructors.ts b/packages/block/src/block/constructors.ts index 78ac32ac47..80d1152af4 100644 --- a/packages/block/src/block/constructors.ts +++ b/packages/block/src/block/constructors.ts @@ -9,13 +9,13 @@ import { } from '@ethereumjs/tx' import { CLRequestFactory, - ConsolidationRequest, - WithdrawalRequest, bigIntToHex, bytesToHex, bytesToUtf8, + createConsolidationRequestFromJSON, createDepositRequestFromJSON, createWithdrawal, + createWithdrawalRequestFromJSON, equalsBytes, fetchFromProvider, getProvider, @@ -424,12 +424,12 @@ export async function createBlockFromExecutionPayload( } if (withdrawalRequests !== undefined && withdrawalRequests !== null) { for (const wJson of withdrawalRequests) { - requests!.push(WithdrawalRequest.fromJSON(wJson)) + requests!.push(createWithdrawalRequestFromJSON(wJson)) } } if (consolidationRequests !== undefined && consolidationRequests !== null) { for (const cJson of consolidationRequests) { - requests!.push(ConsolidationRequest.fromJSON(cJson)) + requests!.push(createConsolidationRequestFromJSON(cJson)) } } diff --git a/packages/block/test/eip7685block.spec.ts b/packages/block/test/eip7685block.spec.ts index a724d51fe4..ef90625ca3 100644 --- a/packages/block/test/eip7685block.spec.ts +++ b/packages/block/test/eip7685block.spec.ts @@ -1,9 +1,9 @@ import { Common, Hardfork, Mainnet } from '@ethereumjs/common' import { KECCAK256_RLP, - WithdrawalRequest, bytesToBigInt, createDepositRequest, + createWithdrawalRequest, randomBytes, } from '@ethereumjs/util' import { assert, describe, expect, it } from 'vitest' @@ -36,7 +36,7 @@ function getRandomWithdrawalRequest(): CLRequest { validatorPubkey: randomBytes(48), amount: bytesToBigInt(randomBytes(8)), } - return WithdrawalRequest.fromRequestData(withdrawalRequestData) as CLRequest + return createWithdrawalRequest(withdrawalRequestData) as CLRequest } const common = new Common({ diff --git a/packages/client/test/rpc/engine/preimages.spec.ts b/packages/client/test/rpc/engine/preimages.spec.ts index 2c72907e60..a4e8848cbb 100644 --- a/packages/client/test/rpc/engine/preimages.spec.ts +++ b/packages/client/test/rpc/engine/preimages.spec.ts @@ -7,7 +7,7 @@ import { import { createTxFromSerializedData } from '@ethereumjs/tx' import { bytesToHex, - createWithdrawalFromData, + createWithdrawal, equalsBytes, hexToBytes, intToBytes, @@ -50,7 +50,7 @@ async function genBlockWithdrawals(blockNumber: number) { } }) const withdrawalsRoot = bytesToHex( - await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawalFromData)), + await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal)), ) return { withdrawals, withdrawalsRoot } diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index a809485a1d..67adbafff6 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -166,20 +166,6 @@ export class ConsolidationRequest extends CLRequest super(CLRequestType.Consolidation) } - public static fromRequestData(consolidationData: ConsolidationRequestData): ConsolidationRequest { - const { sourceAddress, sourcePubkey, targetPubkey } = consolidationData - return new ConsolidationRequest(sourceAddress, sourcePubkey, targetPubkey) - } - - public static fromJSON(jsonData: ConsolidationRequestV1): ConsolidationRequest { - const { sourceAddress, sourcePubkey, targetPubkey } = jsonData - return this.fromRequestData({ - sourceAddress: hexToBytes(sourceAddress), - sourcePubkey: hexToBytes(sourcePubkey), - targetPubkey: hexToBytes(targetPubkey), - }) - } - serialize() { return concatBytes( Uint8Array.from([this.type]), @@ -194,19 +180,6 @@ export class ConsolidationRequest extends CLRequest targetPubkey: bytesToHex(this.targetPubkey), } } - - public static deserialize(bytes: Uint8Array): ConsolidationRequest { - const [sourceAddress, sourcePubkey, targetPubkey] = RLP.decode(bytes.slice(1)) as [ - Uint8Array, - Uint8Array, - Uint8Array, - ] - return this.fromRequestData({ - sourceAddress, - sourcePubkey, - targetPubkey, - }) - } } export class CLRequestFactory { @@ -217,7 +190,7 @@ export class CLRequestFactory { case CLRequestType.Withdrawal: return createWithDrawalRequestFromBytes(bytes) case CLRequestType.Consolidation: - return ConsolidationRequest.deserialize(bytes) + return createConsolidationRequestFromBytes(bytes) default: throw Error(`Invalid request type=${bytes[0]}`) } @@ -283,3 +256,34 @@ export function createWithDrawalRequestFromBytes(bytes: Uint8Array): WithdrawalR amount: bytesToBigInt(amount), }) } + +export function createConsolidationRequest( + consolidationData: ConsolidationRequestData, +): ConsolidationRequest { + const { sourceAddress, sourcePubkey, targetPubkey } = consolidationData + return new ConsolidationRequest(sourceAddress, sourcePubkey, targetPubkey) +} + +export function createConsolidationRequestFromJSON( + jsonData: ConsolidationRequestV1, +): ConsolidationRequest { + const { sourceAddress, sourcePubkey, targetPubkey } = jsonData + return createConsolidationRequest({ + sourceAddress: hexToBytes(sourceAddress), + sourcePubkey: hexToBytes(sourcePubkey), + targetPubkey: hexToBytes(targetPubkey), + }) +} + +export function createConsolidationRequestFromBytes(bytes: Uint8Array): ConsolidationRequest { + const [sourceAddress, sourcePubkey, targetPubkey] = RLP.decode(bytes.slice(1)) as [ + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createConsolidationRequest({ + sourceAddress, + sourcePubkey, + targetPubkey, + }) +} diff --git a/packages/vm/src/requests.ts b/packages/vm/src/requests.ts index be70abddf4..781ac01575 100644 --- a/packages/vm/src/requests.ts +++ b/packages/vm/src/requests.ts @@ -1,14 +1,14 @@ import { Mainnet } from '@ethereumjs/common' import { - ConsolidationRequest, - WithdrawalRequest, bigIntToAddressBytes, bigIntToBytes, bytesToBigInt, bytesToHex, bytesToInt, createAddressFromString, + createConsolidationRequest, createDepositRequest, + createWithdrawalRequest, setLengthLeft, unpadBytes, } from '@ethereumjs/util' @@ -93,7 +93,7 @@ const accumulateEIP7002Requests = async ( const sourceAddress = slicedBytes.slice(0, 20) // 20 Bytes const validatorPubkey = slicedBytes.slice(20, 68) // 48 Bytes const amount = bytesToBigInt(unpadBytes(slicedBytes.slice(68, 76))) // 8 Bytes / Uint64 - requests.push(WithdrawalRequest.fromRequestData({ sourceAddress, validatorPubkey, amount })) + requests.push(createWithdrawalRequest({ sourceAddress, validatorPubkey, amount })) } } @@ -143,9 +143,7 @@ const accumulateEIP7251Requests = async ( const sourceAddress = slicedBytes.slice(0, 20) // 20 Bytes const sourcePubkey = slicedBytes.slice(20, 68) // 48 Bytes const targetPubkey = slicedBytes.slice(68, 116) // 48 bytes - requests.push( - ConsolidationRequest.fromRequestData({ sourceAddress, sourcePubkey, targetPubkey }), - ) + requests.push(createConsolidationRequest({ sourceAddress, sourcePubkey, targetPubkey })) } } From c21feae4de561ac9ca7f3414b982c0ab15485851 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 20 Aug 2024 11:11:54 -0600 Subject: [PATCH 09/15] fix request constructor test --- packages/util/test/requests.spec.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/util/test/requests.spec.ts b/packages/util/test/requests.spec.ts index 75df9dc1c0..45832866cd 100644 --- a/packages/util/test/requests.spec.ts +++ b/packages/util/test/requests.spec.ts @@ -4,15 +4,25 @@ import { bytesToBigInt, randomBytes } from '../src/bytes.js' import { CLRequestFactory, CLRequestType, + createConsolidationRequest, + createDepositRequest, + createWithdrawalRequest, +} from '../src/request.js' + +import type { + CLRequest, ConsolidationRequest, DepositRequest, WithdrawalRequest, } from '../src/request.js' -import type { CLRequest } from '../src/request.js' - describe('Requests', () => { - const testCases = [ + const testCases: [ + string, + any, + CLRequestType, + (...args: any) => ConsolidationRequest | DepositRequest | WithdrawalRequest, + ][] = [ [ 'DepositRequest', { @@ -23,7 +33,7 @@ describe('Requests', () => { index: bytesToBigInt(randomBytes(8)), }, CLRequestType.Deposit, - DepositRequest, + createDepositRequest, ], [ 'WithdrawalRequest', @@ -33,7 +43,7 @@ describe('Requests', () => { amount: bytesToBigInt(randomBytes(8)), }, CLRequestType.Withdrawal, - WithdrawalRequest, + createWithdrawalRequest, ], [ 'ConsolidationRequest', @@ -43,14 +53,12 @@ describe('Requests', () => { targetPubkey: randomBytes(48), }, CLRequestType.Consolidation, - ConsolidationRequest, + createConsolidationRequest, ], ] - for (const [requestName, requestData, requestType, RequestInstanceType] of testCases) { + for (const [requestName, requestData, requestType, requestInstanceConstructor] of testCases) { it(`${requestName}`, () => { - const requestObject = RequestInstanceType.fromRequestData( - requestData, - ) as CLRequest + const requestObject = requestInstanceConstructor(requestData) as CLRequest const requestJSON = requestObject.toJSON() const serialized = requestObject.serialize() assert.equal(serialized[0], requestType) From 2c368ea66c67ce744b5d959d73ecc24c215c964f Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 27 Aug 2024 11:20:56 -0600 Subject: [PATCH 10/15] util: rename fromValuesArray to fromBytesArray --- packages/block/test/eip4895block.spec.ts | 4 ++-- packages/block/test/eip7685block.spec.ts | 2 +- packages/block/test/header.spec.ts | 4 ++-- packages/tx/test/base.spec.ts | 4 ++-- packages/tx/test/legacy.spec.ts | 2 +- packages/util/src/withdrawal.ts | 2 +- packages/util/test/withdrawal.spec.ts | 6 +++--- packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts | 6 +++--- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/block/test/eip4895block.spec.ts b/packages/block/test/eip4895block.spec.ts index bac23d4cc1..61c2b98200 100644 --- a/packages/block/test/eip4895block.spec.ts +++ b/packages/block/test/eip4895block.spec.ts @@ -3,7 +3,7 @@ import { RLP } from '@ethereumjs/rlp' import { Address, KECCAK256_RLP, - createWithdrawalFromValuesArray, + createWithdrawalFromBytesArray, hexToBytes, randomBytes, zeros, @@ -40,7 +40,7 @@ describe('EIP4895 tests', () => { // get withdrawalsArray const gethBlockBytesArray = RLP.decode(hexToBytes(`0x${gethWithdrawals8BlockRlp}`)) const withdrawals = (gethBlockBytesArray[3] as WithdrawalBytes[]).map((wa) => - createWithdrawalFromValuesArray(wa), + createWithdrawalFromBytesArray(wa), ) assert.equal(withdrawals.length, 8, '8 withdrawals should have been found') const gethWithdrawalsRoot = (gethBlockBytesArray[0] as Uint8Array[])[16] as Uint8Array diff --git a/packages/block/test/eip7685block.spec.ts b/packages/block/test/eip7685block.spec.ts index ef90625ca3..d45b295381 100644 --- a/packages/block/test/eip7685block.spec.ts +++ b/packages/block/test/eip7685block.spec.ts @@ -109,7 +109,7 @@ describe('7685 tests', () => { }) }) -describe('createWithdrawalFromValuesArray tests', () => { +describe('createWithdrawalFromBytesArray tests', () => { it('should construct a block with empty requests root', () => { const block = createBlockFromBytesArray( [createBlockHeader({}, { common }).raw(), [], [], [], []], diff --git a/packages/block/test/header.spec.ts b/packages/block/test/header.spec.ts index 4964ecfb8e..148bd908bc 100644 --- a/packages/block/test/header.spec.ts +++ b/packages/block/test/header.spec.ts @@ -129,7 +129,7 @@ describe('[Block]: Header functions', () => { } }) - it('Initialization -> createWithdrawalFromValuesArray()', () => { + it('Initialization -> createWithdrawalFromBytesArray()', () => { const common = new Common({ chain: Mainnet, hardfork: Hardfork.London }) const zero = new Uint8Array(0) const headerArray = [] @@ -156,7 +156,7 @@ describe('[Block]: Header functions', () => { ) }) - it('Initialization -> createWithdrawalFromValuesArray() -> error cases', () => { + it('Initialization -> createWithdrawalFromBytesArray() -> error cases', () => { const headerArray = Array(22).fill(new Uint8Array(0)) // mock header data (if set to zeros(0) header throws) diff --git a/packages/tx/test/base.spec.ts b/packages/tx/test/base.spec.ts index d9c4d04a7f..713c25b56e 100644 --- a/packages/tx/test/base.spec.ts +++ b/packages/tx/test/base.spec.ts @@ -184,7 +184,7 @@ describe('[BaseTransaction]', () => { } }) - it('createWithdrawalFromValuesArray()', () => { + it('createWithdrawalFromBytesArray()', () => { let rlpData: any = legacyTxs[0].raw() rlpData[0] = toBytes('0x0') try { @@ -270,7 +270,7 @@ describe('[BaseTransaction]', () => { for (const tx of txType.txs) { assert.ok( txType.create.bytesArray(tx.raw() as any, { common }), - `${txType.name}: should do roundtrip raw() -> createWithdrawalFromValuesArray()`, + `${txType.name}: should do roundtrip raw() -> createWithdrawalFromBytesArray()`, ) } } diff --git a/packages/tx/test/legacy.spec.ts b/packages/tx/test/legacy.spec.ts index b950b78b52..1271b948f0 100644 --- a/packages/tx/test/legacy.spec.ts +++ b/packages/tx/test/legacy.spec.ts @@ -99,7 +99,7 @@ describe('[Transaction]', () => { ) }) - it('Initialization -> decode with createWithdrawalFromValuesArray()', () => { + it('Initialization -> decode with createWithdrawalFromBytesArray()', () => { for (const tx of txFixtures.slice(0, 4)) { const txData = tx.raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString)) const pt = createLegacyTxFromBytesArray(txData) diff --git a/packages/util/src/withdrawal.ts b/packages/util/src/withdrawal.ts index 9a4ced48f5..662befbeb3 100644 --- a/packages/util/src/withdrawal.ts +++ b/packages/util/src/withdrawal.ts @@ -72,7 +72,7 @@ export class Withdrawal { } } -export function createWithdrawalFromValuesArray(withdrawalArray: WithdrawalBytes) { +export function createWithdrawalFromBytesArray(withdrawalArray: WithdrawalBytes) { if (withdrawalArray.length !== 4) { throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`) } diff --git a/packages/util/test/withdrawal.spec.ts b/packages/util/test/withdrawal.spec.ts index 762872a221..740ec33ced 100644 --- a/packages/util/test/withdrawal.spec.ts +++ b/packages/util/test/withdrawal.spec.ts @@ -5,7 +5,7 @@ import { bigIntToHex, bytesToHex, createWithdrawal, - createWithdrawalFromValuesArray, + createWithdrawalFromBytesArray, hexToBytes, intToHex, withdrawalToBytesArray, @@ -93,9 +93,9 @@ describe('Withdrawal', () => { assert.equal(gethWithdrawalsRlp, withdrawalsDataToRlp, 'The withdrawals to buffer should match') }) - it('createWithdrawalFromValuesArray, toJSON and toValue', () => { + it('createWithdrawalFromBytesArray, toJSON and toValue', () => { const withdrawals = (gethWithdrawalsBuffer as WithdrawalBytes[]).map( - createWithdrawalFromValuesArray, + createWithdrawalFromBytesArray, ) const withdrawalsJson = withdrawals.map((wt) => wt.toJSON()) assert.deepEqual(withdrawalsGethVector, withdrawalsJson, 'Withdrawals json should match') diff --git a/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts index 97e32500dd..6351909c6d 100644 --- a/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts +++ b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts @@ -9,7 +9,7 @@ import { GWEI_TO_WEI, KECCAK256_RLP, bytesToHex, - createWithdrawalFromValuesArray, + createWithdrawalFromBytesArray, hexToBytes, parseGethGenesisState, zeros, @@ -142,7 +142,7 @@ describe('EIP4895 tests', () => { const gethBlockBufferArray = decode(hexToBytes(gethWithdrawals8BlockRlp)) const withdrawals = (gethBlockBufferArray[3] as WithdrawalBytes[]).map((wa) => - createWithdrawalFromValuesArray(wa), + createWithdrawalFromBytesArray(wa), ) assert.equal(withdrawals[0].amount, BigInt(0), 'withdrawal 0 should have 0 amount') let block: Block @@ -215,7 +215,7 @@ describe('EIP4895 tests', () => { const gethBlockBufferArray = decode(hexToBytes(gethWithdrawals8BlockRlp)) const withdrawals = (gethBlockBufferArray[3] as WithdrawalBytes[]).map((wa) => - createWithdrawalFromValuesArray(wa), + createWithdrawalFromBytesArray(wa), ) const td = await blockchain.getTotalDifficulty(genesisBlock.hash()) From 61354dcf84cf7090a27b96e4e8f5573b145ddda3 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 27 Aug 2024 11:37:51 -0600 Subject: [PATCH 11/15] util: rename request "fromBytes" to "fromRLP" --- packages/util/src/request.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index 67adbafff6..20511ebbaa 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -186,11 +186,11 @@ export class CLRequestFactory { public static fromSerializedRequest(bytes: Uint8Array): CLRequest { switch (bytes[0]) { case CLRequestType.Deposit: - return createDepositRequestFromBytes(bytes) + return createDepositRequestFromRLP(bytes.subarray(1)) case CLRequestType.Withdrawal: - return createWithDrawalRequestFromBytes(bytes) + return createWithDrawalRequestFromRLP(bytes.subarray(1)) case CLRequestType.Consolidation: - return createConsolidationRequestFromBytes(bytes) + return createConsolidationRequestFromRLP(bytes.subarray(1)) default: throw Error(`Invalid request type=${bytes[0]}`) } @@ -213,8 +213,8 @@ export function createDepositRequestFromJSON(jsonData: DepositRequestV1): Deposi }) } -export function createDepositRequestFromBytes(bytes: Uint8Array): DepositRequest { - const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode(bytes.slice(1)) as [ +export function createDepositRequestFromRLP(bytes: Uint8Array): DepositRequest { + const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode(bytes) as [ Uint8Array, Uint8Array, Uint8Array, @@ -244,8 +244,8 @@ export function createWithdrawalRequestFromJSON(jsonData: WithdrawalRequestV1): }) } -export function createWithDrawalRequestFromBytes(bytes: Uint8Array): WithdrawalRequest { - const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes.slice(1)) as [ +export function createWithDrawalRequestFromRLP(bytes: Uint8Array): WithdrawalRequest { + const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes) as [ Uint8Array, Uint8Array, Uint8Array, @@ -275,8 +275,8 @@ export function createConsolidationRequestFromJSON( }) } -export function createConsolidationRequestFromBytes(bytes: Uint8Array): ConsolidationRequest { - const [sourceAddress, sourcePubkey, targetPubkey] = RLP.decode(bytes.slice(1)) as [ +export function createConsolidationRequestFromRLP(bytes: Uint8Array): ConsolidationRequest { + const [sourceAddress, sourcePubkey, targetPubkey] = RLP.decode(bytes) as [ Uint8Array, Uint8Array, Uint8Array, From a9e8b15db5e71675d5ab43accd46f157e732847d Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 27 Aug 2024 11:40:16 -0600 Subject: [PATCH 12/15] spellcheck --- packages/util/src/request.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index 20511ebbaa..d98529f8c9 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -188,7 +188,7 @@ export class CLRequestFactory { case CLRequestType.Deposit: return createDepositRequestFromRLP(bytes.subarray(1)) case CLRequestType.Withdrawal: - return createWithDrawalRequestFromRLP(bytes.subarray(1)) + return createWithdrawalRequestFromRLP(bytes.subarray(1)) case CLRequestType.Consolidation: return createConsolidationRequestFromRLP(bytes.subarray(1)) default: @@ -244,7 +244,7 @@ export function createWithdrawalRequestFromJSON(jsonData: WithdrawalRequestV1): }) } -export function createWithDrawalRequestFromRLP(bytes: Uint8Array): WithdrawalRequest { +export function createWithdrawalRequestFromRLP(bytes: Uint8Array): WithdrawalRequest { const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes) as [ Uint8Array, Uint8Array, From da121dabc923e3e9e2766e4602f24ce6fa395365 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 27 Aug 2024 11:44:31 -0600 Subject: [PATCH 13/15] remove lint exception --- packages/util/src/request.ts | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/util/src/request.ts b/packages/util/src/request.ts index d98529f8c9..66e359e435 100644 --- a/packages/util/src/request.ts +++ b/packages/util/src/request.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-use-before-define */ import { RLP } from '@ethereumjs/rlp' import { concatBytes } from 'ethereum-cryptography/utils' @@ -182,21 +181,6 @@ export class ConsolidationRequest extends CLRequest } } -export class CLRequestFactory { - public static fromSerializedRequest(bytes: Uint8Array): CLRequest { - switch (bytes[0]) { - case CLRequestType.Deposit: - return createDepositRequestFromRLP(bytes.subarray(1)) - case CLRequestType.Withdrawal: - return createWithdrawalRequestFromRLP(bytes.subarray(1)) - case CLRequestType.Consolidation: - return createConsolidationRequestFromRLP(bytes.subarray(1)) - default: - throw Error(`Invalid request type=${bytes[0]}`) - } - } -} - export function createDepositRequest(depositData: DepositRequestData): DepositRequest { const { pubkey, withdrawalCredentials, amount, signature, index } = depositData return new DepositRequest(pubkey, withdrawalCredentials, amount, signature, index) @@ -287,3 +271,18 @@ export function createConsolidationRequestFromRLP(bytes: Uint8Array): Consolidat targetPubkey, }) } + +export class CLRequestFactory { + public static fromSerializedRequest(bytes: Uint8Array): CLRequest { + switch (bytes[0]) { + case CLRequestType.Deposit: + return createDepositRequestFromRLP(bytes.subarray(1)) + case CLRequestType.Withdrawal: + return createWithdrawalRequestFromRLP(bytes.subarray(1)) + case CLRequestType.Consolidation: + return createConsolidationRequestFromRLP(bytes.subarray(1)) + default: + throw Error(`Invalid request type=${bytes[0]}`) + } + } +} From 1c24a72fd696aaa066888b288cd17624b9b113c2 Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Tue, 27 Aug 2024 11:46:07 -0600 Subject: [PATCH 14/15] remove lint exception --- packages/util/src/withdrawal.ts | 66 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/packages/util/src/withdrawal.ts b/packages/util/src/withdrawal.ts index 662befbeb3..042613436e 100644 --- a/packages/util/src/withdrawal.ts +++ b/packages/util/src/withdrawal.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-use-before-define */ import { Address } from './address.js' import { bigIntToHex, bytesToHex, toBytes } from './bytes.js' import { BIGINT_0 } from './constants.js' @@ -29,7 +28,31 @@ export interface JsonRpcWithdrawal { } export type WithdrawalBytes = [Uint8Array, Uint8Array, Uint8Array, Uint8Array] +/** + * Convert a withdrawal to a buffer array + * @param withdrawal the withdrawal to convert + * @returns buffer array of the withdrawal + */ +export function withdrawalToBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { + const { index, validatorIndex, address, amount } = withdrawal + const indexBytes = + toType(index, TypeOutput.BigInt) === BIGINT_0 + ? new Uint8Array() + : toType(index, TypeOutput.Uint8Array) + const validatorIndexBytes = + toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0 + ? new Uint8Array() + : toType(validatorIndex, TypeOutput.Uint8Array) + const addressBytes = + address instanceof Address ? (
address).bytes : toType(address, TypeOutput.Uint8Array) + const amountBytes = + toType(amount, TypeOutput.BigInt) === BIGINT_0 + ? new Uint8Array() + : toType(amount, TypeOutput.Uint8Array) + + return [indexBytes, validatorIndexBytes, addressBytes, amountBytes] +} /** * Representation of EIP-4895 withdrawal data */ @@ -72,40 +95,6 @@ export class Withdrawal { } } -export function createWithdrawalFromBytesArray(withdrawalArray: WithdrawalBytes) { - if (withdrawalArray.length !== 4) { - throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`) - } - const [index, validatorIndex, address, amount] = withdrawalArray - return createWithdrawal({ index, validatorIndex, address, amount }) -} - -/** - * Convert a withdrawal to a buffer array - * @param withdrawal the withdrawal to convert - * @returns buffer array of the withdrawal - */ -export function withdrawalToBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { - const { index, validatorIndex, address, amount } = withdrawal - const indexBytes = - toType(index, TypeOutput.BigInt) === BIGINT_0 - ? new Uint8Array() - : toType(index, TypeOutput.Uint8Array) - const validatorIndexBytes = - toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0 - ? new Uint8Array() - : toType(validatorIndex, TypeOutput.Uint8Array) - const addressBytes = - address instanceof Address ? (
address).bytes : toType(address, TypeOutput.Uint8Array) - - const amountBytes = - toType(amount, TypeOutput.BigInt) === BIGINT_0 - ? new Uint8Array() - : toType(amount, TypeOutput.Uint8Array) - - return [indexBytes, validatorIndexBytes, addressBytes, amountBytes] -} - export function createWithdrawal(withdrawalData: WithdrawalData) { const { index: indexData, @@ -120,3 +109,10 @@ export function createWithdrawal(withdrawalData: WithdrawalData) { return new Withdrawal(index, validatorIndex, address, amount) } +export function createWithdrawalFromBytesArray(withdrawalArray: WithdrawalBytes) { + if (withdrawalArray.length !== 4) { + throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`) + } + const [index, validatorIndex, address, amount] = withdrawalArray + return createWithdrawal({ index, validatorIndex, address, amount }) +} From aa01468d538bcb1665455449cf851d0d710f6955 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:45:35 -0400 Subject: [PATCH 15/15] Add docstrings --- packages/util/src/withdrawal.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/util/src/withdrawal.ts b/packages/util/src/withdrawal.ts index 042613436e..5d642acb6d 100644 --- a/packages/util/src/withdrawal.ts +++ b/packages/util/src/withdrawal.ts @@ -29,9 +29,9 @@ export interface JsonRpcWithdrawal { export type WithdrawalBytes = [Uint8Array, Uint8Array, Uint8Array, Uint8Array] /** - * Convert a withdrawal to a buffer array + * Convert a withdrawal to a byte array * @param withdrawal the withdrawal to convert - * @returns buffer array of the withdrawal + * @returns byte array of the withdrawal */ export function withdrawalToBytesArray(withdrawal: Withdrawal | WithdrawalData): WithdrawalBytes { const { index, validatorIndex, address, amount } = withdrawal @@ -95,6 +95,12 @@ export class Withdrawal { } } +/** + * Creates a validator withdrawal request to be submitted to the consensus layer + * @param withdrawalData the consensus layer index and validator index values for the + * validator requesting the withdrawal and the address and withdrawal amount of the request + * @returns a {@link Withdrawal} object + */ export function createWithdrawal(withdrawalData: WithdrawalData) { const { index: indexData, @@ -109,6 +115,13 @@ export function createWithdrawal(withdrawalData: WithdrawalData) { return new Withdrawal(index, validatorIndex, address, amount) } + +/** + * Creates a validator withdrawal request to be submitted to the consensus layer from + * an RLP list + * @param withdrawalArray decoded RLP list of withdrawal data elements + * @returns a {@link Withdrawal} object + */ export function createWithdrawalFromBytesArray(withdrawalArray: WithdrawalBytes) { if (withdrawalArray.length !== 4) { throw Error(`Invalid withdrawalArray length expected=4 actual=${withdrawalArray.length}`)