Skip to content

Commit

Permalink
convert requests to flat type across util,block and vm
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Sep 29, 2024
1 parent 1c30c2a commit 73003e7
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 233 deletions.
4 changes: 2 additions & 2 deletions packages/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ const main = async () => {
const depositRequestData = {
pubkey: randomBytes(48),
withdrawalCredentials: randomBytes(32),
amount: bytesToBigInt(randomBytes(8)),
amount: (randomBytes(8)),
signature: randomBytes(96),
index: bytesToBigInt(randomBytes(8)),
index: (randomBytes(8)),
}
const request = createDepositRequest(depositRequestData) as CLRequest<CLRequestType>
const requests = [request]
Expand Down
4 changes: 2 additions & 2 deletions packages/block/examples/6110Requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const main = async () => {
const depositRequestData = {
pubkey: randomBytes(48),
withdrawalCredentials: randomBytes(32),
amount: bytesToBigInt(randomBytes(8)),
amount: randomBytes(8),
signature: randomBytes(96),
index: bytesToBigInt(randomBytes(8)),
index: randomBytes(8),
}
const request = createDepositRequest(depositRequestData) as CLRequest<CLRequestType>
const requests = [request]
Expand Down
44 changes: 6 additions & 38 deletions packages/block/src/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Trie } from '@ethereumjs/trie'
import { Blob4844Tx, Capability } from '@ethereumjs/tx'
import {
BIGINT_0,
CLRequestType,
KECCAK256_RLP,
KECCAK256_RLP_ARRAY,
bytesToHex,
Expand Down Expand Up @@ -35,14 +34,7 @@ import {
import type { BlockBytes, BlockOptions, ExecutionPayload, JSONBlock } from '../types.js'
import type { Common } from '@ethereumjs/common'
import type { FeeMarket1559Tx, LegacyTx, TypedTransaction } from '@ethereumjs/tx'
import type {
CLRequest,
ConsolidationRequest,
DepositRequest,
VerkleExecutionWitness,
Withdrawal,
WithdrawalRequest,
} from '@ethereumjs/util'
import type { CLRequest, CLRequestType, VerkleExecutionWitness, Withdrawal } from '@ethereumjs/util'

/**
* Class representing a block in the Ethereum network. The {@link BlockHeader} has its own
Expand Down Expand Up @@ -545,6 +537,10 @@ export class Block {
const header = blockJSON.header!
const transactions = this.transactions.map((tx) => bytesToHex(tx.serialize())) ?? []
const withdrawalsArr = blockJSON.withdrawals ? { withdrawals: blockJSON.withdrawals } : {}
const executionRequestsArr =
this.requests !== undefined
? { executionRequests: this.requests.map((req) => bytesToHex(req.serialize())) }
: undefined

const executionPayload: ExecutionPayload = {
blockNumber: header.number!,
Expand All @@ -566,35 +562,7 @@ export class Block {
...withdrawalsArr,
parentBeaconBlockRoot: header.parentBeaconBlockRoot,
executionWitness: this.executionWitness,

// lets add the request fields first and then iterate over requests to fill them up
depositRequests: this.common.isActivatedEIP(6110) ? [] : undefined,
withdrawalRequests: this.common.isActivatedEIP(7002) ? [] : undefined,
consolidationRequests: this.common.isActivatedEIP(7251) ? [] : undefined,
}

if (this.requests !== undefined) {
for (const request of this.requests) {
switch (request.type) {
case CLRequestType.Deposit:
executionPayload.depositRequests!.push((request as DepositRequest).toJSON())
continue

case CLRequestType.Withdrawal:
executionPayload.withdrawalRequests!.push((request as WithdrawalRequest).toJSON())
continue

case CLRequestType.Consolidation:
executionPayload.consolidationRequests!.push((request as ConsolidationRequest).toJSON())
continue
}
}
} else if (
executionPayload.depositRequests !== undefined ||
executionPayload.withdrawalRequests !== undefined ||
executionPayload.consolidationRequests !== undefined
) {
throw Error(`Undefined requests for activated deposit or withdrawal requests`)
...executionRequestsArr,
}

return executionPayload
Expand Down
38 changes: 4 additions & 34 deletions packages/block/src/block/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
bigIntToHex,
bytesToHex,
bytesToUtf8,
createConsolidationRequestFromJSON,
createDepositRequestFromJSON,
createWithdrawal,
createWithdrawalRequestFromJSON,
equalsBytes,
fetchFromProvider,
getProvider,
Expand Down Expand Up @@ -49,8 +46,6 @@ import type {
} from '../types.js'
import type { TypedTransaction } from '@ethereumjs/tx'
import type {
CLRequest,
CLRequestType,
EthersProvider,
PrefixedHexString,
RequestBytes,
Expand Down Expand Up @@ -383,9 +378,7 @@ export async function createBlockFromExecutionPayload(
feeRecipient: coinbase,
transactions,
withdrawals: withdrawalsData,
depositRequests,
withdrawalRequests,
consolidationRequests,
executionRequests,
executionWitness,
} = payload

Expand All @@ -408,32 +401,9 @@ export async function createBlockFromExecutionPayload(
? await genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common }))
: undefined

const hasDepositRequests = depositRequests !== undefined && depositRequests !== null
const hasWithdrawalRequests = withdrawalRequests !== undefined && withdrawalRequests !== null
const hasConsolidationRequests =
consolidationRequests !== undefined && consolidationRequests !== null

const requests =
hasDepositRequests || hasWithdrawalRequests || hasConsolidationRequests
? ([] as CLRequest<CLRequestType>[])
: undefined

if (depositRequests !== undefined && depositRequests !== null) {
for (const dJSON of depositRequests) {
requests!.push(createDepositRequestFromJSON(dJSON))
}
}
if (withdrawalRequests !== undefined && withdrawalRequests !== null) {
for (const wJSON of withdrawalRequests) {
requests!.push(createWithdrawalRequestFromJSON(wJSON))
}
}
if (consolidationRequests !== undefined && consolidationRequests !== null) {
for (const cJSON of consolidationRequests) {
requests!.push(createConsolidationRequestFromJSON(cJSON))
}
}

const requests = executionRequests?.map((req) =>
CLRequestFactory.fromSerializedRequest(hexToBytes(req)),
)
const keccakFunction = opts?.common?.customCrypto.keccak256 ?? keccak256
const requestsRoot = requests ? await genRequestsRoot(requests, keccakFunction) : undefined

Expand Down
51 changes: 3 additions & 48 deletions packages/block/src/from-beacon-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ type BeaconWithdrawal = {
amount: PrefixedHexString
}

type BeaconDepositRequest = {
pubkey: PrefixedHexString
withdrawal_credentials: PrefixedHexString
amount: PrefixedHexString
signature: PrefixedHexString
index: PrefixedHexString
}

type BeaconWithdrawalRequest = {
source_address: PrefixedHexString
validator_pubkey: PrefixedHexString
amount: PrefixedHexString
}

type BeaconConsolidationRequest = {
source_address: PrefixedHexString
source_pubkey: PrefixedHexString
target_pubkey: PrefixedHexString
}

// Payload JSON that one gets using the beacon apis
// curl localhost:5052/eth/v2/beacon/blocks/56610 | jq .data.message.body.execution_payload
export type BeaconPayloadJSON = {
Expand All @@ -52,10 +32,7 @@ export type BeaconPayloadJSON = {
excess_blob_gas?: NumericString
parent_beacon_block_root?: PrefixedHexString
// requests data
deposit_requests?: BeaconDepositRequest[]
withdrawal_requests?: BeaconWithdrawalRequest[]
consolidation_requests?: BeaconConsolidationRequest[]

execution_requests?: PrefixedHexString[]
// the casing of VerkleExecutionWitness remains same camel case for now
execution_witness?: VerkleExecutionWitness
}
Expand Down Expand Up @@ -158,30 +135,8 @@ export function executionPayloadFromBeaconPayload(payload: BeaconPayloadJSON): E
}

// requests
if (payload.deposit_requests !== undefined && payload.deposit_requests !== null) {
executionPayload.depositRequests = payload.deposit_requests.map((beaconRequest) => ({
pubkey: beaconRequest.pubkey,
withdrawalCredentials: beaconRequest.withdrawal_credentials,
amount: beaconRequest.amount,
signature: beaconRequest.signature,
index: beaconRequest.index,
}))
}
if (payload.withdrawal_requests !== undefined && payload.withdrawal_requests !== null) {
executionPayload.withdrawalRequests = payload.withdrawal_requests.map((beaconRequest) => ({
sourceAddress: beaconRequest.source_address,
validatorPubkey: beaconRequest.validator_pubkey,
amount: beaconRequest.amount,
}))
}
if (payload.consolidation_requests !== undefined && payload.consolidation_requests !== null) {
executionPayload.consolidationRequests = payload.consolidation_requests.map(
(beaconRequest) => ({
sourceAddress: beaconRequest.source_address,
sourcePubkey: beaconRequest.source_pubkey,
targetPubkey: beaconRequest.target_pubkey,
}),
)
if (payload.execution_requests !== undefined && payload.execution_requests !== null) {
executionPayload.executionRequests = payload.execution_requests
}

if (payload.execution_witness !== undefined && payload.execution_witness !== null) {
Expand Down
7 changes: 1 addition & 6 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import type {
BytesLike,
CLRequest,
CLRequestType,
ConsolidationRequestV1,
DepositRequestV1,
JSONRPCWithdrawal,
NumericString,
PrefixedHexString,
RequestBytes,
VerkleExecutionWitness,
WithdrawalBytes,
WithdrawalData,
WithdrawalRequestV1,
} from '@ethereumjs/util'

/**
Expand Down Expand Up @@ -274,7 +271,5 @@ export type ExecutionPayload = {
parentBeaconBlockRoot?: PrefixedHexString // QUANTITY, 64 Bits
// VerkleExecutionWitness is already a hex serialized object
executionWitness?: VerkleExecutionWitness | null // QUANTITY, 64 Bits, null implies not available
depositRequests?: DepositRequestV1[] // Array of 6110 deposit requests
withdrawalRequests?: WithdrawalRequestV1[] // Array of 7002 withdrawal requests
consolidationRequests?: ConsolidationRequestV1[] // Array of 7251 consolidation requests
executionRequests?: PrefixedHexString[] | null // null implies not available
}
4 changes: 2 additions & 2 deletions packages/block/test/eip7685block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function getRandomDepositRequest(): CLRequest<CLRequestType> {
const depositRequestData = {
pubkey: randomBytes(48),
withdrawalCredentials: randomBytes(32),
amount: bytesToBigInt(randomBytes(8)),
amount: randomBytes(8),
signature: randomBytes(96),
index: bytesToBigInt(randomBytes(8)),
index: randomBytes(8),
}
return createDepositRequest(depositRequestData) as CLRequest<CLRequestType>
}
Expand Down
Loading

0 comments on commit 73003e7

Please sign in to comment.