Skip to content

Commit

Permalink
Merge branch 'master' into eip7702
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Jul 1, 2024
2 parents 13ec300 + 2a774e5 commit aeccb99
Show file tree
Hide file tree
Showing 28 changed files with 681 additions and 246 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BIGINT_0,
CLRequestFactory,
CLRequestType,
ConsolidationRequest,
DepositRequest,
KECCAK256_RLP,
KECCAK256_RLP_ARRAY,
Expand Down Expand Up @@ -424,6 +425,7 @@ export class Block {
withdrawals: withdrawalsData,
depositRequests,
withdrawalRequests,
consolidationRequests,
executionWitness,
} = payload

Expand Down Expand Up @@ -454,8 +456,13 @@ export class Block {

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

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

if (depositRequests !== undefined && depositRequests !== null) {
for (const dJson of depositRequests) {
Expand All @@ -467,6 +474,11 @@ export class Block {
requests!.push(WithdrawalRequest.fromJSON(wJson))
}
}
if (consolidationRequests !== undefined && consolidationRequests !== null) {
for (const cJson of consolidationRequests) {
requests!.push(ConsolidationRequest.fromJSON(cJson))
}
}

const requestsRoot = requests
? await Block.genRequestsTrieRoot(requests, new Trie({ common: opts?.common }))
Expand Down Expand Up @@ -1006,6 +1018,7 @@ export class Block {
// 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) {
Expand All @@ -1018,11 +1031,16 @@ export class Block {
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.withdrawalRequests !== undefined ||
executionPayload.consolidationRequests !== undefined
) {
throw Error(`Undefined requests for activated deposit or withdrawal requests`)
}
Expand Down
18 changes: 16 additions & 2 deletions packages/block/src/from-beacon-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ type BeaconDepositRequest = {

type BeaconWithdrawalRequest = {
source_address: PrefixedHexString
validator_public_key: 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 @@ -48,6 +54,7 @@ export type BeaconPayloadJson = {
// requests data
deposit_requests?: BeaconDepositRequest[]
withdrawal_requests?: BeaconWithdrawalRequest[]
consolidation_requests?: BeaconConsolidationRequest[]

// the casing of VerkleExecutionWitness remains same camel case for now
execution_witness?: VerkleExecutionWitness
Expand Down Expand Up @@ -160,10 +167,17 @@ export function executionPayloadFromBeaconPayload(payload: BeaconPayloadJson): E
if (payload.withdrawal_requests !== undefined && payload.withdrawal_requests !== null) {
executionPayload.withdrawalRequests = payload.withdrawal_requests.map((breq) => ({
sourceAddress: breq.source_address,
validatorPublicKey: breq.validator_public_key,
validatorPubkey: breq.validator_pubkey,
amount: breq.amount,
}))
}
if (payload.consolidation_requests !== undefined && payload.consolidation_requests !== null) {
executionPayload.consolidationRequests = payload.consolidation_requests.map((breq) => ({
sourceAddress: breq.source_address,
sourcePubkey: breq.source_pubkey,
targetPubkey: breq.target_pubkey,
}))
}

if (payload.execution_witness !== undefined && payload.execution_witness !== null) {
// the casing structure in payload could be camel case or snake depending upon the CL
Expand Down
2 changes: 2 additions & 0 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
BytesLike,
CLRequest,
CLRequestType,
ConsolidationRequestV1,
DepositRequestV1,
JsonRpcWithdrawal,
PrefixedHexString,
Expand Down Expand Up @@ -269,4 +270,5 @@ export type ExecutionPayload = {
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
}
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"level": "^8.0.0",
"memory-level": "^1.0.0",
"prom-client": "^15.1.0",
"verkle-cryptography-wasm": "^0.4.2",
"verkle-cryptography-wasm": "^0.4.5",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.5",
"yargs": "^17.7.1"
Expand Down
8 changes: 7 additions & 1 deletion packages/client/src/rpc/modules/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { UNKNOWN_PAYLOAD } from '../../error-code.js'

import type { Skeleton } from '../../../service/index.js'
import type { Block, ExecutionPayload } from '@ethereumjs/block'
import type { DepositRequestV1, PrefixedHexString, WithdrawalRequestV1 } from '@ethereumjs/util'
import type {
ConsolidationRequestV1,
DepositRequestV1,
PrefixedHexString,
WithdrawalRequestV1,
} from '@ethereumjs/util'

export enum Status {
ACCEPTED = 'ACCEPTED',
Expand Down Expand Up @@ -31,6 +36,7 @@ export type ExecutionPayloadV3 = ExecutionPayloadV2 & { excessBlobGas: Uint64; b
export type ExecutionPayloadV4 = ExecutionPayloadV3 & {
depositRequests: DepositRequestV1[]
withdrawalRequests: WithdrawalRequestV1[]
consolidationRequests: ConsolidationRequestV1[]
}

export type ForkchoiceStateV1 = {
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/rpc/modules/engine/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const executionPayloadV4FieldValidators = {
...executionPayloadV3FieldValidators,
depositRequests: validators.array(validators.depositRequest()),
withdrawalRequests: validators.array(validators.withdrawalRequest()),
consolidationRequests: validators.array(validators.consolidationRequest()),
}

export const forkchoiceFieldValidators = {
Expand Down
74 changes: 61 additions & 13 deletions packages/client/src/rpc/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,10 @@ export const validators = {
}
}

const wt = params[index]
const clReq = params[index]

for (const field of requiredFields) {
if (wt[field] === undefined) {
if (clReq[field] === undefined) {
return {
code: INVALID_PARAMS,
message: `invalid argument ${index}: required field ${field}`,
Expand All @@ -458,25 +458,25 @@ export const validators = {
}

// validate pubkey
for (const field of [wt.pubkey]) {
for (const field of [clReq.pubkey]) {
const v = validate(field, this.bytes48)
if (v !== undefined) return v
}

// validate withdrawalCredentials
for (const field of [wt.withdrawalCredentials]) {
for (const field of [clReq.withdrawalCredentials]) {
const v = validate(field, this.bytes32)
if (v !== undefined) return v
}

// validate amount, index
for (const field of [wt.amount, wt.index]) {
for (const field of [clReq.amount, clReq.index]) {
const v = validate(field, this.bytes8)
if (v !== undefined) return v
}

// validate signature
for (const field of [wt.signature]) {
for (const field of [clReq.signature]) {
const v = validate(field, this.bytes96)
if (v !== undefined) return v
}
Expand All @@ -485,7 +485,7 @@ export const validators = {
},

get withdrawalRequest() {
return (requiredFields: string[] = ['sourceAddress', 'validatorPublicKey', 'amount']) => {
return (requiredFields: string[] = ['sourceAddress', 'validatorPubkey', 'amount']) => {
return (params: any[], index: number) => {
if (typeof params[index] !== 'object') {
return {
Expand All @@ -494,10 +494,10 @@ export const validators = {
}
}

const wt = params[index]
const clReq = params[index]

for (const field of requiredFields) {
if (wt[field] === undefined) {
if (clReq[field] === undefined) {
return {
code: INVALID_PARAMS,
message: `invalid argument ${index}: required field ${field}`,
Expand All @@ -512,26 +512,74 @@ export const validators = {
}

// validate sourceAddress
for (const field of [wt.sourceAddress]) {
for (const field of [clReq.sourceAddress]) {
const v = validate(field, this.address)
if (v !== undefined) return v
}

// validate validatorPublicKey
for (const field of [wt.validatorPublicKey]) {
// validate validatorPubkey
for (const field of [clReq.validatorPubkey]) {
const v = validate(field, this.bytes48)
if (v !== undefined) return v
}

// validate amount
for (const field of [wt.amount]) {
for (const field of [clReq.amount]) {
const v = validate(field, this.bytes8)
if (v !== undefined) return v
}
}
}
},

get consolidationRequest() {
return (requiredFields: string[] = ['sourceAddress', 'sourcePubkey', 'targetPubkey']) => {
return (params: any[], index: number) => {
if (typeof params[index] !== 'object') {
return {
code: INVALID_PARAMS,
message: `invalid argument ${index}: argument must be an object`,
}
}

const clReq = params[index]

for (const field of requiredFields) {
if (clReq[field] === undefined) {
return {
code: INVALID_PARAMS,
message: `invalid argument ${index}: required field ${field}`,
}
}
}

const validate = (field: any, validator: Function) => {
if (field === undefined) return
const v = validator([field], 0)
if (v !== undefined) return v
}

// validate sourceAddress
for (const field of [clReq.sourceAddress]) {
const v = validate(field, this.address)
if (v !== undefined) return v
}

// validate validatorPubkey
for (const field of [clReq.sourcePubkey]) {
const v = validate(field, this.bytes48)
if (v !== undefined) return v
}

// validate amount
for (const field of [clReq.targetPubkey]) {
const v = validate(field, this.bytes48)
if (v !== undefined) return v
}
}
}
},

/**
* object validator to check if type is object with
* required keys and expected validation of values
Expand Down
Loading

0 comments on commit aeccb99

Please sign in to comment.