Skip to content

Commit 2c1efa8

Browse files
committed
debug and fix block build
1 parent e12604d commit 2c1efa8

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

packages/block/src/block/block.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { keccak256 } from 'ethereum-cryptography/keccak.js'
1818
// TODO: See if there is an easier way to achieve the same result.
1919
// See: https://github.com/microsoft/TypeScript/issues/47558
2020
// (situation will eventually improve on Typescript and/or Eslint update)
21+
import { genTransactionsSszRoot, genWithdrawalsSszRoot } from '../helpers.js'
2122
import {
2223
genRequestsTrieRoot,
2324
genTransactionsTrieRoot,
@@ -43,7 +44,6 @@ import type {
4344
Withdrawal,
4445
WithdrawalRequest,
4546
} from '@ethereumjs/util'
46-
import { genTransactionsSszRoot, genWithdrawalsSszRoot } from '../helpers.js'
4747

4848
/**
4949
* Class representing a block in the Ethereum network. The {@link BlockHeader} has its own
@@ -227,7 +227,9 @@ export class Block {
227227
* Generates transaction trie for validation.
228228
*/
229229
async genTxTrie(): Promise<Uint8Array> {
230-
return this.common.isActivatedEIP(6493)? genTransactionsSszRoot(this.transactions): genTransactionsTrieRoot(this.transactions, new Trie({ common: this.common }))
230+
return this.common.isActivatedEIP(6493)
231+
? genTransactionsSszRoot(this.transactions)
232+
: genTransactionsTrieRoot(this.transactions, new Trie({ common: this.common }))
231233
}
232234

233235
/**
@@ -360,7 +362,7 @@ export class Block {
360362

361363
if (!(await this.transactionsTrieIsValid())) {
362364
const msg = this._errorMsg(
363-
`invalid transaction trie expected=${bytesToHex(this.cache.txTrieRoot!)}`
365+
`invalid transaction trie expected=${bytesToHex(this.cache.txTrieRoot!)}`,
364366
)
365367
throw new Error(msg)
366368
}
@@ -451,7 +453,9 @@ export class Block {
451453
}
452454

453455
async genWithdrawalsTrie(): Promise<Uint8Array> {
454-
return this.common.isActivatedEIP(6493)? genWithdrawalsSszRoot(this.withdrawals!): genWithdrawalsTrieRoot(this.withdrawals!, new Trie({ common: this.common }))
456+
return this.common.isActivatedEIP(6493)
457+
? genWithdrawalsSszRoot(this.withdrawals!)
458+
: genWithdrawalsTrieRoot(this.withdrawals!, new Trie({ common: this.common }))
455459
}
456460

457461
/**
@@ -535,7 +539,9 @@ export class Block {
535539
toExecutionPayload(): ExecutionPayload {
536540
const blockJSON = this.toJSON()
537541
const header = blockJSON.header!
538-
const transactions = this.common.isActivatedEIP(6493)? this.transactions.map((tx) => tx.toExecutionPayloadTx()) : this.transactions.map((tx) => bytesToHex(tx.serialize()))
542+
const transactions = this.common.isActivatedEIP(6493)
543+
? this.transactions.map((tx) => tx.toExecutionPayloadTx())
544+
: this.transactions.map((tx) => bytesToHex(tx.serialize()))
539545
const withdrawalsArr = blockJSON.withdrawals ? { withdrawals: blockJSON.withdrawals } : {}
540546

541547
const executionPayload: ExecutionPayload = {

packages/block/src/block/constructors.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { RLP } from '@ethereumjs/rlp'
22
import { Trie } from '@ethereumjs/trie'
33
import {
44
type TxOptions,
5+
createTx,
56
createTxFromBlockBodyData,
6-
createTxFromSerializedData,
7-
createTxFromTxData,
7+
createTxFromExecutionPayloadTx,
8+
createTxFromRLP,
89
normalizeTxParams,
910
} from '@ethereumjs/tx'
1011
import {
@@ -25,7 +26,13 @@ import {
2526
} from '@ethereumjs/util'
2627

2728
import { generateCliqueBlockExtraData } from '../consensus/clique.js'
28-
import { genRequestsTrieRoot, genTransactionsSszRoot, genTransactionsTrieRoot, genWithdrawalsSszRoot, genWithdrawalsTrieRoot } from '../helpers.js'
29+
import {
30+
genRequestsTrieRoot,
31+
genTransactionsSszRoot,
32+
genTransactionsTrieRoot,
33+
genWithdrawalsSszRoot,
34+
genWithdrawalsTrieRoot,
35+
} from '../helpers.js'
2936
import {
3037
Block,
3138
createBlockHeader,
@@ -46,6 +53,7 @@ import type {
4653
RequestsBytes,
4754
WithdrawalsBytes,
4855
} from '../types.js'
56+
import type { Common } from '@ethereumjs/common'
4957
import type { TypedTransaction } from '@ethereumjs/tx'
5058
import type {
5159
CLRequest,
@@ -55,7 +63,6 @@ import type {
5563
RequestBytes,
5664
WithdrawalBytes,
5765
} from '@ethereumjs/util'
58-
import type { Common } from '@ethereumjs/common'
5966

6067
/**
6168
* Static constructor to create a block from a block data dictionary
@@ -78,7 +85,7 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) {
7885
// parse transactions
7986
const transactions = []
8087
for (const txData of txsData ?? []) {
81-
const tx = createTxFromTxData(txData, {
88+
const tx = createTx(txData, {
8289
...opts,
8390
// Use header common in case of setHardfork being activated
8491
common: header.common,
@@ -289,7 +296,7 @@ export function createBlockFromRPC(
289296
const opts = { common: header.common }
290297
for (const _txParams of blockParams.transactions ?? []) {
291298
const txParams = normalizeTxParams(_txParams)
292-
const tx = createTxFromTxData(txParams, opts)
299+
const tx = createTx(txParams, opts)
293300
transactions.push(tx)
294301
}
295302

@@ -374,7 +381,7 @@ export const createBlockFromJSONRPCProvider = async (
374381
*/
375382
export async function createBlockFromExecutionPayload(
376383
payload: ExecutionPayload,
377-
opts: BlockOptions & {common: Common},
384+
opts: BlockOptions & { common: Common },
378385
): Promise<Block> {
379386
const {
380387
blockNumber: number,
@@ -392,19 +399,19 @@ export async function createBlockFromExecutionPayload(
392399
const txs = []
393400
for (const [index, serializedTxOrPayload] of transactions.entries()) {
394401
try {
395-
let tx;
402+
let tx
396403
if (opts.common.isActivatedEIP(6493)) {
397404
if (typeof serializedTxOrPayload === 'string') {
398405
throw Error('EIP 6493 activated for transaction bytes')
399406
}
400-
tx = createTxFromExecutionPayloadTx(hexToBytes(serializedTxOrPayload), {
407+
tx = createTxFromExecutionPayloadTx(serializedTxOrPayload, {
401408
common: opts?.common,
402409
})
403-
}else{
410+
} else {
404411
if (typeof serializedTxOrPayload !== 'string') {
405412
throw Error('EIP 6493 not activated for transaction payload')
406413
}
407-
tx = createTxFromSerializedData(hexToBytes(serializedTxOrPayload as PrefixedHexString), {
414+
tx = createTxFromRLP(hexToBytes(serializedTxOrPayload as PrefixedHexString), {
408415
common: opts?.common,
409416
})
410417
}
@@ -415,10 +422,14 @@ export async function createBlockFromExecutionPayload(
415422
}
416423
}
417424

418-
const transactionsTrie = opts.common.isActivatedEIP(6493)? await genTransactionsSszRoot(txs) : await genTransactionsTrieRoot(txs, new Trie({ common: opts?.common }))
425+
const transactionsTrie = opts.common.isActivatedEIP(6493)
426+
? await genTransactionsSszRoot(txs)
427+
: await genTransactionsTrieRoot(txs, new Trie({ common: opts?.common }))
419428
const withdrawals = withdrawalsData?.map((wData) => createWithdrawal(wData))
420429
const withdrawalsRoot = withdrawals
421-
? opts.common.isActivatedEIP(6493)? genWithdrawalsSszRoot(withdrawals) : await genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common }))
430+
? opts.common.isActivatedEIP(6493)
431+
? genWithdrawalsSszRoot(withdrawals)
432+
: await genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common }))
422433
: undefined
423434

424435
const hasDepositRequests = depositRequests !== undefined && depositRequests !== null

packages/block/src/from-beacon-payload.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { bigIntToHex } from '@ethereumjs/util'
22

33
import type { ExecutionPayload } from './types.js'
4-
import type { NumericString, PrefixedHexString, VerkleExecutionWitness, ssz } from '@ethereumjs/util'
4+
import type {
5+
NumericString,
6+
PrefixedHexString,
7+
VerkleExecutionWitness,
8+
ssz,
9+
} from '@ethereumjs/util'
510

611
type BeaconWithdrawal = {
712
index: PrefixedHexString

packages/block/src/helpers.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { RLP } from '@ethereumjs/rlp'
22
import { Trie } from '@ethereumjs/trie'
33
import { Blob4844Tx } from '@ethereumjs/tx'
4-
import { BIGINT_0, BIGINT_1, TypeOutput, isHexString, toType } from '@ethereumjs/util'
4+
import { BIGINT_0, BIGINT_1, TypeOutput, isHexString, ssz, toType } from '@ethereumjs/util'
55

66
import type { BlockHeaderBytes, HeaderData } from './types.js'
7+
import type { ValueOf } from '@chainsafe/ssz'
78
import type { TypedTransaction } from '@ethereumjs/tx'
8-
import type { CLRequest, CLRequestType, PrefixedHexString, Withdrawal, ssz } from '@ethereumjs/util'
9+
import type { CLRequest, CLRequestType, PrefixedHexString, Withdrawal } from '@ethereumjs/util'
910

10-
export type SszTransactionType = ValueOf<typeof ssz.Transaction>
11+
export type SSZTransactionType = ValueOf<typeof ssz.Transaction>
1112

1213
/**
1314
* Returns a 0x-prefixed hex number string from a hex string or string integer.
@@ -134,7 +135,7 @@ export async function genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: Trie
134135
return trie.root()
135136
}
136137

137-
export async function genWithdrawalsSszRoot(wts: Withdrawal[]) {
138+
export function genWithdrawalsSszRoot(wts: Withdrawal[]) {
138139
const withdrawals = wts.map((wt) => wt.toValue())
139140
return ssz.Withdrawals.hashTreeRoot(withdrawals)
140141
}
@@ -153,7 +154,7 @@ export async function genTransactionsTrieRoot(txs: TypedTransaction[], emptyTrie
153154
}
154155

155156
export async function genTransactionsSszRoot(txs: TypedTransaction[]) {
156-
const transactions = txs.map((tx) => tx.sszRaw() as unknown as SszTransactionType)
157+
const transactions = txs.map((tx) => tx.sszRaw() as unknown as SSZTransactionType)
157158
return ssz.Transactions.hashTreeRoot(transactions)
158159
}
159160

packages/tx/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './params.js'
1111
export {
1212
createTx,
1313
createTxFromBlockBodyData,
14+
createTxFromExecutionPayloadTx,
1415
createTxFromJSONRPCProvider,
1516
createTxFromRLP,
1617
createTxFromRPC,

0 commit comments

Comments
 (0)