Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util: Static Constructors for Withdrawals & Requests (Tree Shaking) #3589

Merged
merged 16 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 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,
DepositRequest,
Withdrawal,
WithdrawalRequest,
bigIntToHex,
bytesToHex,
bytesToUtf8,
createConsolidationRequestFromJSON,
createDepositRequestFromJSON,
createWithdrawal,
createWithdrawalRequestFromJSON,
equalsBytes,
fetchFromProvider,
getProvider,
Expand Down Expand Up @@ -103,7 +103,7 @@ export function createBlock(blockData: BlockData = {}, opts?: BlockOptions) {
uncleHeaders.push(uh)
}

const withdrawals = withdrawalsData?.map(Withdrawal.fromWithdrawalData)
const withdrawals = withdrawalsData?.map(createWithdrawal)
// The witness data is planned to come in rlp serialized bytes so leave this
// stub till that time
const executionWitness = executionWitnessData
Expand Down Expand Up @@ -220,7 +220,7 @@ export function createBlockFromBytesArray(values: BlockBytes, opts?: BlockOption
address,
amount,
}))
?.map(Withdrawal.fromWithdrawalData)
?.map(createWithdrawal)

let requests
if (header.common.isActivatedEIP(7685)) {
Expand Down Expand Up @@ -402,7 +402,7 @@ export async function createBlockFromExecutionPayload(
}

const transactionsTrie = await genTransactionsTrieRoot(txs, new Trie({ common: opts?.common }))
const withdrawals = withdrawalsData?.map((wData) => Withdrawal.fromWithdrawalData(wData))
const withdrawals = withdrawalsData?.map((wData) => createWithdrawal(wData))
const withdrawalsRoot = withdrawals
? await genWithdrawalsTrieRoot(withdrawals, new Trie({ common: opts?.common }))
: undefined
Expand All @@ -419,17 +419,17 @@ export async function createBlockFromExecutionPayload(

if (depositRequests !== undefined && depositRequests !== null) {
for (const dJson of depositRequests) {
requests!.push(DepositRequest.fromJSON(dJson))
requests!.push(createDepositRequestFromJSON(dJson))
}
}
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/eip4895block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RLP } from '@ethereumjs/rlp'
import {
Address,
KECCAK256_RLP,
Withdrawal,
createWithdrawalFromBytesArray,
hexToBytes,
randomBytes,
zeros,
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('EIP4895 tests', () => {
// get withdrawalsArray
const gethBlockBytesArray = RLP.decode(hexToBytes(`0x${gethWithdrawals8BlockRlp}`))
const withdrawals = (gethBlockBytesArray[3] as WithdrawalBytes[]).map((wa) =>
Withdrawal.fromValuesArray(wa),
createWithdrawalFromBytesArray(wa),
)
assert.equal(withdrawals.length, 8, '8 withdrawals should have been found')
const gethWithdrawalsRoot = (gethBlockBytesArray[0] as Uint8Array[])[16] as Uint8Array
Expand Down
10 changes: 5 additions & 5 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 {
DepositRequest,
KECCAK256_RLP,
WithdrawalRequest,
bytesToBigInt,
createDepositRequest,
createWithdrawalRequest,
randomBytes,
} from '@ethereumjs/util'
import { assert, describe, expect, it } from 'vitest'
Expand All @@ -27,7 +27,7 @@ function getRandomDepositRequest(): CLRequest<CLRequestType> {
signature: randomBytes(96),
index: bytesToBigInt(randomBytes(8)),
}
return DepositRequest.fromRequestData(depositRequestData) as CLRequest<CLRequestType>
return createDepositRequest(depositRequestData) as CLRequest<CLRequestType>
}

function getRandomWithdrawalRequest(): CLRequest<CLRequestType> {
Expand All @@ -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 Expand Up @@ -109,7 +109,7 @@ describe('7685 tests', () => {
})
})

describe('fromValuesArray tests', () => {
describe('createWithdrawalFromBytesArray tests', () => {
it('should construct a block with empty requests root', () => {
const block = createBlockFromBytesArray(
[createBlockHeader({}, { common }).raw(), [], [], [], []],
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('[Block]: Header functions', () => {
}
})

it('Initialization -> fromValuesArray()', () => {
it('Initialization -> createWithdrawalFromBytesArray()', () => {
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
const zero = new Uint8Array(0)
const headerArray = []
Expand All @@ -156,7 +156,7 @@ describe('[Block]: Header functions', () => {
)
})

it('Initialization -> fromValuesArray() -> error cases', () => {
it('Initialization -> createWithdrawalFromBytesArray() -> error cases', () => {
const headerArray = Array(22).fill(new Uint8Array(0))

// mock header data (if set to zeros(0) header throws)
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 @@ -6,8 +6,8 @@ import {
} from '@ethereumjs/block'
import { createTxFromSerializedData } from '@ethereumjs/tx'
import {
Withdrawal,
bytesToHex,
createWithdrawal,
equalsBytes,
hexToBytes,
intToBytes,
Expand Down Expand Up @@ -50,7 +50,7 @@ async function genBlockWithdrawals(blockNumber: number) {
}
})
const withdrawalsRoot = bytesToHex(
await genWithdrawalsTrieRoot(withdrawals.map(Withdrawal.fromWithdrawalData)),
await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal)),
)

return { withdrawals, withdrawalsRoot }
Expand Down
4 changes: 2 additions & 2 deletions packages/client/test/rpc/engine/withdrawals.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { genWithdrawalsTrieRoot } from '@ethereumjs/block'
import { Trie } from '@ethereumjs/trie'
import { Withdrawal, bigIntToHex, bytesToHex, intToHex } from '@ethereumjs/util'
import { bigIntToHex, bytesToHex, createWithdrawal, intToHex } from '@ethereumjs/util'
import { assert, it } from 'vitest'

import { INVALID_PARAMS } from '../../../src/rpc/error-code.js'
Expand Down Expand Up @@ -105,7 +105,7 @@ for (const { name, withdrawals, withdrawalsRoot, gethBlockRlp } of testCases) {
it(name, async () => {
// check withdrawals root computation
const computedWithdrawalsRoot = bytesToHex(
await genWithdrawalsTrieRoot(withdrawals.map(Withdrawal.fromWithdrawalData), new Trie()),
await genWithdrawalsTrieRoot(withdrawals.map(createWithdrawal), new Trie()),
)
assert.equal(
withdrawalsRoot,
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/test/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('[BaseTransaction]', () => {
}
})

it('fromValuesArray()', () => {
it('createWithdrawalFromBytesArray()', () => {
let rlpData: any = legacyTxs[0].raw()
rlpData[0] = toBytes('0x0')
try {
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('[BaseTransaction]', () => {
for (const tx of txType.txs) {
assert.ok(
txType.create.bytesArray(tx.raw() as any, { common }),
`${txType.name}: should do roundtrip raw() -> fromValuesArray()`,
`${txType.name}: should do roundtrip raw() -> createWithdrawalFromBytesArray()`,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tx/test/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('[Transaction]', () => {
)
})

it('Initialization -> decode with fromValuesArray()', () => {
it('Initialization -> decode with createWithdrawalFromBytesArray()', () => {
for (const tx of txFixtures.slice(0, 4)) {
const txData = tx.raw.map((rawTxData) => hexToBytes(rawTxData as PrefixedHexString))
const pt = createLegacyTxFromBytesArray(txData)
Expand Down
4 changes: 2 additions & 2 deletions packages/util/examples/withdrawal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Withdrawal } from '@ethereumjs/util'
import { createWithdrawal } from '@ethereumjs/util'

const withdrawal = Withdrawal.fromWithdrawalData({
const withdrawal = createWithdrawal({
index: 0n,
validatorIndex: 65535n,
address: '0x0000000000000000000000000000000000000000',
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ export * from './kzg.js'
export * from './lock.js'
export * from './mapDB.js'
export * from './provider.js'
export * from './requests.js'
export * from './request.js'
export * from './tasks.js'
export * from './verkle.js'
Loading
Loading