Skip to content

Commit

Permalink
util: extract static constructors for ConsolidationRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottyPoi committed Aug 20, 2024
1 parent 2423c9c commit 86c2463
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
8 changes: 4 additions & 4 deletions packages/block/src/block/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
} from '@ethereumjs/tx'
import {
CLRequestFactory,
ConsolidationRequest,
WithdrawalRequest,
bigIntToHex,
bytesToHex,
bytesToUtf8,
createConsolidationRequestFromJSON,
createDepositRequestFromJSON,
createWithdrawal,
createWithdrawalRequestFromJSON,
equalsBytes,
fetchFromProvider,
getProvider,
Expand Down Expand Up @@ -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))
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/eip7685block.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -36,7 +36,7 @@ function getRandomWithdrawalRequest(): CLRequest<CLRequestType> {
validatorPubkey: randomBytes(48),
amount: bytesToBigInt(randomBytes(8)),
}
return WithdrawalRequest.fromRequestData(withdrawalRequestData) as CLRequest<CLRequestType>
return createWithdrawalRequest(withdrawalRequestData) as CLRequest<CLRequestType>
}

const common = new Common({
Expand Down
4 changes: 2 additions & 2 deletions packages/client/test/rpc/engine/preimages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { createTxFromSerializedData } from '@ethereumjs/tx'
import {
bytesToHex,
createWithdrawalFromData,
createWithdrawal,
equalsBytes,
hexToBytes,
intToBytes,
Expand Down Expand Up @@ -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 }
Expand Down
60 changes: 32 additions & 28 deletions packages/util/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,6 @@ export class ConsolidationRequest extends CLRequest<CLRequestType.Consolidation>
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]),
Expand All @@ -194,19 +180,6 @@ export class ConsolidationRequest extends CLRequest<CLRequestType.Consolidation>
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 {
Expand All @@ -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]}`)
}
Expand Down Expand Up @@ -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,
})
}
10 changes: 4 additions & 6 deletions packages/vm/src/requests.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 }))
}
}

Expand Down Expand Up @@ -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 }))
}
}

Expand Down

0 comments on commit 86c2463

Please sign in to comment.