diff --git a/packages/block/src/block/constructors.ts b/packages/block/src/block/constructors.ts index fdcdb023df..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, - DepositRequest, - Withdrawal, - WithdrawalRequest, bigIntToHex, bytesToHex, bytesToUtf8, + createConsolidationRequestFromJSON, + createDepositRequestFromJSON, + createWithdrawal, + createWithdrawalRequestFromJSON, 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 @@ -419,17 +419,17 @@ 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) { 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/eip4895block.spec.ts b/packages/block/test/eip4895block.spec.ts index 633b520fe9..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, - Withdrawal, + 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) => - Withdrawal.fromValuesArray(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 928e2db558..d45b295381 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, + createWithdrawalRequest, 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 { @@ -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({ @@ -109,7 +109,7 @@ describe('7685 tests', () => { }) }) -describe('fromValuesArray 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 0ccde3065f..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 -> fromValuesArray()', () => { + 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 -> fromValuesArray() -> 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/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..713c25b56e 100644 --- a/packages/tx/test/base.spec.ts +++ b/packages/tx/test/base.spec.ts @@ -184,7 +184,7 @@ describe('[BaseTransaction]', () => { } }) - it('fromValuesArray()', () => { + 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() -> fromValuesArray()`, + `${txType.name}: should do roundtrip raw() -> createWithdrawalFromBytesArray()`, ) } } diff --git a/packages/tx/test/legacy.spec.ts b/packages/tx/test/legacy.spec.ts index c9253deadb..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 fromValuesArray()', () => { + 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/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/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/src/requests.ts b/packages/util/src/request.ts similarity index 62% rename from packages/util/src/requests.ts rename to packages/util/src/request.ts index 3490e06977..66e359e435 100644 --- a/packages/util/src/requests.ts +++ b/packages/util/src/request.ts @@ -101,22 +101,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) @@ -143,19 +127,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 this.fromRequestData({ - pubkey, - withdrawalCredentials, - amount: bytesToBigInt(amount), - signature, - index: bytesToBigInt(index), - }) - } } export class WithdrawalRequest extends CLRequest { @@ -167,20 +138,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) @@ -197,19 +154,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 { @@ -221,20 +165,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]), @@ -249,30 +179,108 @@ 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 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 createDepositRequestFromRLP(bytes: Uint8Array): DepositRequest { + const [pubkey, withdrawalCredentials, amount, signature, index] = RLP.decode(bytes) as [ + Uint8Array, + Uint8Array, + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createDepositRequest({ + pubkey, + withdrawalCredentials, + amount: bytesToBigInt(amount), + signature, + 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 createWithdrawalRequestFromRLP(bytes: Uint8Array): WithdrawalRequest { + const [sourceAddress, validatorPubkey, amount] = RLP.decode(bytes) as [ + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createWithdrawalRequest({ + sourceAddress, + validatorPubkey, + 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 createConsolidationRequestFromRLP(bytes: Uint8Array): ConsolidationRequest { + const [sourceAddress, sourcePubkey, targetPubkey] = RLP.decode(bytes) as [ + Uint8Array, + Uint8Array, + Uint8Array, + ] + return createConsolidationRequest({ + sourceAddress, + sourcePubkey, + targetPubkey, + }) } export class CLRequestFactory { public static fromSerializedRequest(bytes: Uint8Array): CLRequest { switch (bytes[0]) { case CLRequestType.Deposit: - return DepositRequest.deserialize(bytes) + return createDepositRequestFromRLP(bytes.subarray(1)) case CLRequestType.Withdrawal: - return WithdrawalRequest.deserialize(bytes) + return createWithdrawalRequestFromRLP(bytes.subarray(1)) case CLRequestType.Consolidation: - return ConsolidationRequest.deserialize(bytes) + return createConsolidationRequestFromRLP(bytes.subarray(1)) default: throw Error(`Invalid request type=${bytes[0]}`) } diff --git a/packages/util/src/withdrawal.ts b/packages/util/src/withdrawal.ts index daa2e53f10..5d642acb6d 100644 --- a/packages/util/src/withdrawal.ts +++ b/packages/util/src/withdrawal.ts @@ -28,7 +28,31 @@ export interface JsonRpcWithdrawal { } export type WithdrawalBytes = [Uint8Array, Uint8Array, Uint8Array, Uint8Array] +/** + * Convert a withdrawal to a byte array + * @param withdrawal the withdrawal to convert + * @returns byte 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 */ @@ -48,57 +72,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 withdrawalToBytesArray(this) } toValue() { @@ -119,3 +94,38 @@ 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, + 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) +} + +/** + * 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}`) + } + const [index, validatorIndex, address, amount] = withdrawalArray + return createWithdrawal({ index, validatorIndex, address, amount }) +} diff --git a/packages/util/test/requests.spec.ts b/packages/util/test/requests.spec.ts index 96b4a49f57..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/requests.js' - -import type { CLRequest } from '../src/requests.js' +} 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) diff --git a/packages/util/test/withdrawal.spec.ts b/packages/util/test/withdrawal.spec.ts index 2aa7754164..740ec33ced 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, + createWithdrawalFromBytesArray, + 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('createWithdrawalFromBytesArray, toJSON and toValue', () => { + const withdrawals = (gethWithdrawalsBuffer as WithdrawalBytes[]).map( + createWithdrawalFromBytesArray, + ) 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/src/requests.ts b/packages/vm/src/requests.ts index 52baccb219..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, - DepositRequest, - 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 })) } } @@ -221,7 +219,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-4895-withdrawals.spec.ts b/packages/vm/test/api/EIPs/eip-4895-withdrawals.spec.ts index af3bbd5b82..d5f1015a9e 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, + createWithdrawalFromBytesArray, 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), + 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) => - Withdrawal.fromValuesArray(wa), + createWithdrawalFromBytesArray(wa), ) const td = await blockchain.getTotalDifficulty(genesisBlock.hash()) 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 3eb2931b39..84747031ba 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] })