Skip to content

Commit

Permalink
client/util/vm: simplify CLRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Oct 31, 2024
1 parent 46ec57d commit 5446fb8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
20 changes: 9 additions & 11 deletions packages/client/src/rpc/modules/engine/util/newPayload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { createBlockFromExecutionPayload, genRequestsRoot } from '@ethereumjs/block'

Check warning on line 1 in packages/client/src/rpc/modules/engine/util/newPayload.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/engine/util/newPayload.ts#L1

Added line #L1 was not covered by tests
import { Blob4844Tx } from '@ethereumjs/tx'
import {
ConsolidationRequest,
DepositRequest,
WithdrawalRequest,
bytesToHex,
hexToBytes,
} from '@ethereumjs/util'
import { CLRequest, CLRequestType, bytesToHex, hexToBytes } from '@ethereumjs/util'
import { sha256 } from 'ethereum-cryptography/sha256'

Check warning on line 4 in packages/client/src/rpc/modules/engine/util/newPayload.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/engine/util/newPayload.ts#L3-L4

Added lines #L3 - L4 were not covered by tests

import { short } from '../../../../util/index.js'
Expand All @@ -18,7 +12,7 @@ import type { Chain } from '../../../../blockchain/index.js'
import type { ChainCache, PayloadStatusV1 } from '../types.js'
import type { Block, ExecutionPayload } from '@ethereumjs/block'
import type { Common } from '@ethereumjs/common'

Check warning on line 14 in packages/client/src/rpc/modules/engine/util/newPayload.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/rpc/modules/engine/util/newPayload.ts#L14

Added line #L14 was not covered by tests
import type { CLRequest, CLRequestType, PrefixedHexString } from '@ethereumjs/util'
import type { PrefixedHexString } from '@ethereumjs/util'

type CLData = {
parentBeaconBlockRoot?: PrefixedHexString
Expand Down Expand Up @@ -68,15 +62,19 @@ export const validateAndGen7685RequestsHash = (
const requests: CLRequest<CLRequestType>[] = []
let requestIndex = 0
if (common.isActivatedEIP(6110)) {
requests.push(new DepositRequest(hexToBytes(executionRequests[requestIndex])))
requests.push(new CLRequest(CLRequestType.Deposit, hexToBytes(executionRequests[requestIndex])))
requestIndex++
}
if (common.isActivatedEIP(7002)) {
requests.push(new WithdrawalRequest(hexToBytes(executionRequests[requestIndex])))
requests.push(
new CLRequest(CLRequestType.Withdrawal, hexToBytes(executionRequests[requestIndex])),
)
requestIndex++
}
if (common.isActivatedEIP(7251)) {
requests.push(new ConsolidationRequest(hexToBytes(executionRequests[requestIndex])))
requests.push(
new CLRequest(CLRequestType.Consolidation, hexToBytes(executionRequests[requestIndex])),
)
requestIndex++
}

Expand Down
26 changes: 4 additions & 22 deletions packages/util/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,14 @@ export class CLRequest<T extends CLRequestType> {
}
}

export class DepositRequest extends CLRequest<CLRequestType.Deposit> {
constructor(requestData: Uint8Array) {
super(CLRequestType.Deposit, requestData)
}
}

export class WithdrawalRequest extends CLRequest<CLRequestType.Withdrawal> {
constructor(requestData: Uint8Array) {
super(CLRequestType.Withdrawal, requestData)
}
}

export class ConsolidationRequest extends CLRequest<CLRequestType.Consolidation> {
constructor(requestData: Uint8Array) {
super(CLRequestType.Consolidation, requestData)
}
}

export function createCLRequest<T extends CLRequestType>(bytes: Uint8Array): CLRequest<T> {
export function createCLRequest(bytes: Uint8Array): CLRequest<CLRequestType> {
switch (bytes[0]) {
case CLRequestType.Deposit:
return new DepositRequest(bytes.subarray(1)) as CLRequest<T>
return new CLRequest(CLRequestType.Deposit, bytes.subarray(1))
case CLRequestType.Withdrawal:
return new WithdrawalRequest(bytes.subarray(1)) as CLRequest<T>
return new CLRequest(CLRequestType.Withdrawal, bytes.subarray(1))
case CLRequestType.Consolidation:
return new ConsolidationRequest(bytes.subarray(1)) as CLRequest<T>
return new CLRequest(CLRequestType.Consolidation, bytes.subarray(1))
default:
throw Error(`Invalid request type=${bytes[0]}`)
}
Expand Down
16 changes: 7 additions & 9 deletions packages/vm/src/requests.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Mainnet } from '@ethereumjs/common'
import {
ConsolidationRequest,
DepositRequest,
WithdrawalRequest,
CLRequest,
CLRequestType,
bigIntToAddressBytes,
bigIntToBytes,
bytesToHex,
Expand All @@ -14,7 +13,6 @@ import {

import type { RunTxResult } from './types.js'
import type { VM } from './vm.js'
import type { CLRequest, CLRequestType } from '@ethereumjs/util'

/**
* This helper method generates a list of all CL requests that can be included in a pending block
Expand Down Expand Up @@ -69,7 +67,7 @@ const accumulateWithdrawalsRequest = async (
const originalAccount = await vm.stateManager.getAccount(withdrawalsAddress)

if (originalAccount === undefined) {
return new WithdrawalRequest(new Uint8Array())
return new CLRequest(CLRequestType.Withdrawal, new Uint8Array())
}

const results = await vm.evm.runCall({
Expand All @@ -85,7 +83,7 @@ const accumulateWithdrawalsRequest = async (
}

const resultsBytes = results.execResult.returnValue
return new WithdrawalRequest(resultsBytes)
return new CLRequest(CLRequestType.Withdrawal, resultsBytes)
}

const accumulateConsolidationsRequest = async (
Expand All @@ -105,7 +103,7 @@ const accumulateConsolidationsRequest = async (
const originalAccount = await vm.stateManager.getAccount(consolidationsAddress)

if (originalAccount === undefined) {
return new ConsolidationRequest(new Uint8Array(0))
return new CLRequest(CLRequestType.Consolidation, new Uint8Array(0))

Check warning on line 106 in packages/vm/src/requests.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/requests.ts#L106

Added line #L106 was not covered by tests
}

const results = await vm.evm.runCall({
Expand All @@ -121,7 +119,7 @@ const accumulateConsolidationsRequest = async (
}

const resultsBytes = results.execResult.returnValue
return new ConsolidationRequest(resultsBytes)
return new CLRequest(CLRequestType.Consolidation, resultsBytes)

Check warning on line 122 in packages/vm/src/requests.ts

View check run for this annotation

Codecov / codecov/patch

packages/vm/src/requests.ts#L122

Added line #L122 was not covered by tests
}

const accumulateDepositsRequest = (
Expand All @@ -148,7 +146,7 @@ const accumulateDepositsRequest = (
}
}

return new DepositRequest(resultsBytes)
return new CLRequest(CLRequestType.Deposit, resultsBytes)
}

function parseDepositLog(requestData: Uint8Array) {
Expand Down

0 comments on commit 5446fb8

Please sign in to comment.