From 86c2463593e2f66e2927c393fe86023d2dc78d8d Mon Sep 17 00:00:00 2001 From: ScottyPoi Date: Wed, 14 Aug 2024 12:17:43 -0600 Subject: [PATCH] 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 780f3f12ce3..3181c454e6e 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 a724d51fe44..ef90625ca34 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 2c72907e600..a4e8848cbb8 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 a809485a1de..67adbafff68 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 be70abddf44..781ac015759 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 })) } }