Skip to content

Commit 657cdad

Browse files
authored
monorepo: remove redundant fills and zeros function (#3709)
1 parent 58e68b9 commit 657cdad

40 files changed

+116
-183
lines changed

packages/block/src/header/header.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
equalsBytes,
1818
hexToBytes,
1919
toType,
20-
zeros,
2120
} from '@ethereumjs/util'
2221
import { keccak256 } from 'ethereum-cryptography/keccak.js'
2322

@@ -106,21 +105,21 @@ export class BlockHeader {
106105
const skipValidateConsensusFormat = opts.skipConsensusFormatValidation ?? false
107106

108107
const defaults = {
109-
parentHash: zeros(32),
108+
parentHash: new Uint8Array(32),
110109
uncleHash: KECCAK256_RLP_ARRAY,
111110
coinbase: createZeroAddress(),
112-
stateRoot: zeros(32),
111+
stateRoot: new Uint8Array(32),
113112
transactionsTrie: KECCAK256_RLP,
114113
receiptTrie: KECCAK256_RLP,
115-
logsBloom: zeros(256),
114+
logsBloom: new Uint8Array(256),
116115
difficulty: BIGINT_0,
117116
number: BIGINT_0,
118117
gasLimit: DEFAULT_GAS_LIMIT,
119118
gasUsed: BIGINT_0,
120119
timestamp: BIGINT_0,
121120
extraData: new Uint8Array(0),
122-
mixHash: zeros(32),
123-
nonce: zeros(8),
121+
mixHash: new Uint8Array(32),
122+
nonce: new Uint8Array(8),
124123
}
125124

126125
const parentHash = toType(headerData.parentHash, TypeOutput.Uint8Array) ?? defaults.parentHash
@@ -161,7 +160,7 @@ export class BlockHeader {
161160
withdrawalsRoot: this.common.isActivatedEIP(4895) ? KECCAK256_RLP : undefined,
162161
blobGasUsed: this.common.isActivatedEIP(4844) ? BIGINT_0 : undefined,
163162
excessBlobGas: this.common.isActivatedEIP(4844) ? BIGINT_0 : undefined,
164-
parentBeaconBlockRoot: this.common.isActivatedEIP(4788) ? zeros(32) : undefined,
163+
parentBeaconBlockRoot: this.common.isActivatedEIP(4788) ? new Uint8Array(32) : undefined,
165164
requestsRoot: this.common.isActivatedEIP(7685) ? KECCAK256_RLP : undefined,
166165
}
167166

@@ -424,8 +423,8 @@ export class BlockHeader {
424423
)} (cannot exceed 32 bytes length, received ${extraData.length} bytes)`
425424
error = true
426425
}
427-
if (!equalsBytes(nonce, zeros(8))) {
428-
errorMsg += `, nonce: ${bytesToHex(nonce)} (expected: ${bytesToHex(zeros(8))})`
426+
if (!equalsBytes(nonce, new Uint8Array(8))) {
427+
errorMsg += `, nonce: ${bytesToHex(nonce)} (expected: ${bytesToHex(new Uint8Array(8))})`
429428
error = true
430429
}
431430
}

packages/block/test/block.spec.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { Common, Goerli, Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common'
22
import { RLP } from '@ethereumjs/rlp'
33
import { createLegacyTx } from '@ethereumjs/tx'
4-
import {
5-
KECCAK256_RLP_ARRAY,
6-
bytesToHex,
7-
equalsBytes,
8-
hexToBytes,
9-
toBytes,
10-
zeros,
11-
} from '@ethereumjs/util'
4+
import { KECCAK256_RLP_ARRAY, bytesToHex, equalsBytes, hexToBytes, toBytes } from '@ethereumjs/util'
125
import { assert, describe, it } from 'vitest'
136

147
import { genTransactionsTrieRoot } from '../src/helpers.js'
@@ -76,14 +69,14 @@ describe('[Block]: block functions', () => {
7669
headerArray.push(zero)
7770
}
7871

79-
// mock header data (if set to zeros(0) header throws)
80-
headerArray[0] = zeros(32) // parentHash
81-
headerArray[2] = zeros(20) // coinbase
82-
headerArray[3] = zeros(32) // stateRoot
83-
headerArray[4] = zeros(32) // transactionsTrie
84-
headerArray[5] = zeros(32) // receiptTrie
85-
headerArray[13] = zeros(32) // mixHash
86-
headerArray[14] = zeros(8) // nonce
72+
// mock header data (if set to new Uint8Array() header throws)
73+
headerArray[0] = new Uint8Array(32) // parentHash
74+
headerArray[2] = new Uint8Array(20) // coinbase
75+
headerArray[3] = new Uint8Array(32) // stateRoot
76+
headerArray[4] = new Uint8Array(32) // transactionsTrie
77+
headerArray[5] = new Uint8Array(32) // receiptTrie
78+
headerArray[13] = new Uint8Array(32) // mixHash
79+
headerArray[14] = new Uint8Array(8) // nonce
8780

8881
const valuesArray = <BlockBytes>[headerArray, [], []]
8982

@@ -265,7 +258,7 @@ describe('[Block]: block functions', () => {
265258
}
266259
}
267260

268-
const zeroRoot = zeros(32)
261+
const zeroRoot = new Uint8Array(32)
269262

270263
// Tx root
271264
block = createBlock({

packages/block/test/eip4788block.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
2-
import { bytesToHex, zeros } from '@ethereumjs/util'
2+
import { bytesToHex } from '@ethereumjs/util'
33
import { assert, describe, it } from 'vitest'
44

55
import { createBlock, createBlockHeader } from '../src/index.js'
@@ -13,7 +13,7 @@ describe('EIP4788 header tests', () => {
1313
() => {
1414
createBlockHeader(
1515
{
16-
parentBeaconBlockRoot: zeros(32),
16+
parentBeaconBlockRoot: new Uint8Array(32),
1717
},
1818
{
1919
common: earlyCommon,
@@ -45,7 +45,7 @@ describe('EIP4788 header tests', () => {
4545
{
4646
excessBlobGas: 0n,
4747
blobGasUsed: 0n,
48-
parentBeaconBlockRoot: zeros(32),
48+
parentBeaconBlockRoot: new Uint8Array(32),
4949
},
5050
{
5151
common,
@@ -62,7 +62,7 @@ describe('EIP4788 header tests', () => {
6262
)
6363
assert.equal(
6464
block.toJSON().header?.parentBeaconBlockRoot,
65-
bytesToHex(zeros(32)),
65+
bytesToHex(new Uint8Array(32)),
6666
'JSON output includes excessBlobGas',
6767
)
6868
})

packages/block/test/eip4895block.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
createWithdrawalFromBytesArray,
77
hexToBytes,
88
randomBytes,
9-
zeros,
109
} from '@ethereumjs/util'
1110
import { assert, describe, it } from 'vitest'
1211

@@ -57,7 +56,7 @@ describe('EIP4895 tests', () => {
5756
() => {
5857
createBlockHeader(
5958
{
60-
withdrawalsRoot: zeros(32),
59+
withdrawalsRoot: new Uint8Array(32),
6160
},
6261
{
6362
common: earlyCommon,
@@ -79,7 +78,7 @@ describe('EIP4895 tests', () => {
7978
assert.doesNotThrow(() => {
8079
createBlockHeader(
8180
{
82-
withdrawalsRoot: zeros(32),
81+
withdrawalsRoot: new Uint8Array(32),
8382
},
8483
{
8584
common,
@@ -116,7 +115,7 @@ describe('EIP4895 tests', () => {
116115
createBlock(
117116
{
118117
header: {
119-
withdrawalsRoot: zeros(32),
118+
withdrawalsRoot: new Uint8Array(32),
120119
},
121120
withdrawals: [],
122121
},
@@ -128,7 +127,7 @@ describe('EIP4895 tests', () => {
128127
const block = createBlock(
129128
{
130129
header: {
131-
withdrawalsRoot: zeros(32),
130+
withdrawalsRoot: new Uint8Array(32),
132131
},
133132
withdrawals: [],
134133
},

packages/block/test/header.spec.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
createZeroAddress,
99
equalsBytes,
1010
hexToBytes,
11-
zeros,
1211
} from '@ethereumjs/util'
1312
import { assert, describe, it } from 'vitest'
1413

@@ -32,21 +31,21 @@ import type { PrefixedHexString } from '@ethereumjs/util'
3231
describe('[Block]: Header functions', () => {
3332
it('should create with default constructor', () => {
3433
function compareDefaultHeader(header: BlockHeader) {
35-
assert.ok(equalsBytes(header.parentHash, zeros(32)))
34+
assert.ok(equalsBytes(header.parentHash, new Uint8Array(32)))
3635
assert.ok(equalsBytes(header.uncleHash, KECCAK256_RLP_ARRAY))
3736
assert.ok(header.coinbase.equals(createZeroAddress()))
38-
assert.ok(equalsBytes(header.stateRoot, zeros(32)))
37+
assert.ok(equalsBytes(header.stateRoot, new Uint8Array(32)))
3938
assert.ok(equalsBytes(header.transactionsTrie, KECCAK256_RLP))
4039
assert.ok(equalsBytes(header.receiptTrie, KECCAK256_RLP))
41-
assert.ok(equalsBytes(header.logsBloom, zeros(256)))
40+
assert.ok(equalsBytes(header.logsBloom, new Uint8Array(256)))
4241
assert.equal(header.difficulty, BigInt(0))
4342
assert.equal(header.number, BigInt(0))
4443
assert.equal(header.gasLimit, BigInt('0xffffffffffffff'))
4544
assert.equal(header.gasUsed, BigInt(0))
4645
assert.equal(header.timestamp, BigInt(0))
4746
assert.ok(equalsBytes(header.extraData, new Uint8Array(0)))
48-
assert.ok(equalsBytes(header.mixHash, zeros(32)))
49-
assert.ok(equalsBytes(header.nonce, zeros(8)))
47+
assert.ok(equalsBytes(header.mixHash, new Uint8Array(32)))
48+
assert.ok(equalsBytes(header.nonce, new Uint8Array(8)))
5049
}
5150

5251
const header = createBlockHeader()
@@ -137,14 +136,14 @@ describe('[Block]: Header functions', () => {
137136
headerArray.push(zero)
138137
}
139138

140-
// mock header data (if set to zeros(0) header throws)
141-
headerArray[0] = zeros(32) //parentHash
142-
headerArray[2] = zeros(20) //coinbase
143-
headerArray[3] = zeros(32) //stateRoot
144-
headerArray[4] = zeros(32) //transactionsTrie
145-
headerArray[5] = zeros(32) //receiptTrie
146-
headerArray[13] = zeros(32) // mixHash
147-
headerArray[14] = zeros(8) // nonce
139+
// mock header data (if set to new Uint8Array() header throws)
140+
headerArray[0] = new Uint8Array(32) //parentHash
141+
headerArray[2] = new Uint8Array(20) //coinbase
142+
headerArray[3] = new Uint8Array(32) //stateRoot
143+
headerArray[4] = new Uint8Array(32) //transactionsTrie
144+
headerArray[5] = new Uint8Array(32) //receiptTrie
145+
headerArray[13] = new Uint8Array(32) // mixHash
146+
headerArray[14] = new Uint8Array(8) // nonce
148147

149148
let header = createBlockHeaderFromBytesArray(headerArray, { common })
150149
assert.ok(Object.isFrozen(header), 'block should be frozen by default')
@@ -159,15 +158,15 @@ describe('[Block]: Header functions', () => {
159158
it('Initialization -> createWithdrawalFromBytesArray() -> error cases', () => {
160159
const headerArray = Array(22).fill(new Uint8Array(0))
161160

162-
// mock header data (if set to zeros(0) header throws)
163-
headerArray[0] = zeros(32) //parentHash
164-
headerArray[2] = zeros(20) //coinbase
165-
headerArray[3] = zeros(32) //stateRoot
166-
headerArray[4] = zeros(32) //transactionsTrie
167-
headerArray[5] = zeros(32) //receiptTrie
168-
headerArray[13] = zeros(32) // mixHash
169-
headerArray[14] = zeros(8) // nonce
170-
headerArray[15] = zeros(4) // bad data
161+
// mock header data (if set to new Uint8Array() header throws)
162+
headerArray[0] = new Uint8Array(32) //parentHash
163+
headerArray[2] = new Uint8Array(20) //coinbase
164+
headerArray[3] = new Uint8Array(32) //stateRoot
165+
headerArray[4] = new Uint8Array(32) //transactionsTrie
166+
headerArray[5] = new Uint8Array(32) //receiptTrie
167+
headerArray[13] = new Uint8Array(32) // mixHash
168+
headerArray[14] = new Uint8Array(8) // nonce
169+
headerArray[15] = new Uint8Array(4) // bad data
171170
try {
172171
createBlockHeaderFromBytesArray(headerArray)
173172
} catch (e: any) {

packages/block/test/mergeBlock.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
createZeroAddress,
66
equalsBytes,
77
hexToBytes,
8-
zeros,
98
} from '@ethereumjs/util'
109
import { assert, describe, it } from 'vitest'
1110

@@ -19,21 +18,21 @@ const common = new Common({
1918
})
2019

2120
function validateMergeHeader(header: BlockHeader) {
22-
assert.ok(equalsBytes(header.parentHash, zeros(32)), 'parentHash')
21+
assert.ok(equalsBytes(header.parentHash, new Uint8Array(32)), 'parentHash')
2322
assert.ok(equalsBytes(header.uncleHash, KECCAK256_RLP_ARRAY), 'uncleHash')
2423
assert.ok(header.coinbase.equals(createZeroAddress()), 'coinbase')
25-
assert.ok(equalsBytes(header.stateRoot, zeros(32)), 'stateRoot')
24+
assert.ok(equalsBytes(header.stateRoot, new Uint8Array(32)), 'stateRoot')
2625
assert.ok(equalsBytes(header.transactionsTrie, KECCAK256_RLP), 'transactionsTrie')
2726
assert.ok(equalsBytes(header.receiptTrie, KECCAK256_RLP), 'receiptTrie')
28-
assert.ok(equalsBytes(header.logsBloom, zeros(256)), 'logsBloom')
27+
assert.ok(equalsBytes(header.logsBloom, new Uint8Array(256)), 'logsBloom')
2928
assert.equal(header.difficulty, BigInt(0), 'difficulty')
3029
assert.equal(header.number, BigInt(0), 'number')
3130
assert.equal(header.gasLimit, BigInt('0xffffffffffffff'), 'gasLimit')
3231
assert.equal(header.gasUsed, BigInt(0), 'gasUsed')
3332
assert.equal(header.timestamp, BigInt(0), 'timestamp')
3433
assert.ok(header.extraData.length <= 32, 'extraData')
3534
assert.equal(header.mixHash.length, 32, 'mixHash')
36-
assert.ok(equalsBytes(header.nonce, zeros(8)), 'nonce')
35+
assert.ok(equalsBytes(header.nonce, new Uint8Array(8)), 'nonce')
3736
}
3837

3938
describe('[Header]: Casper PoS / The Merge Functionality', () => {

packages/client/src/miner/pendingBlock.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
equalsBytes,
1212
toBytes,
1313
toType,
14-
zeros,
1514
} from '@ethereumjs/util'
1615
import { BuildStatus, buildBlock } from '@ethereumjs/vm'
1716
import { keccak256 } from 'ethereum-cryptography/keccak'
@@ -121,12 +120,12 @@ export class PendingBlock {
121120
// potentially included in the fcU in future and can be safely added in uniqueness calc
122121
const timestampBuf = bigIntToUnpaddedBytes(toType(timestamp ?? 0, TypeOutput.BigInt))
123122
const gasLimitBuf = bigIntToUnpaddedBytes(gasLimit)
124-
const mixHashBuf = toType(mixHash!, TypeOutput.Uint8Array) ?? zeros(32)
123+
const mixHashBuf = toType(mixHash!, TypeOutput.Uint8Array) ?? new Uint8Array(32)
125124
const parentBeaconBlockRootBuf =
126-
toType(parentBeaconBlockRoot!, TypeOutput.Uint8Array) ?? zeros(32)
127-
const coinbaseBuf = toType(coinbase ?? zeros(20), TypeOutput.Uint8Array)
125+
toType(parentBeaconBlockRoot!, TypeOutput.Uint8Array) ?? new Uint8Array(32)
126+
const coinbaseBuf = toType(coinbase ?? new Uint8Array(20), TypeOutput.Uint8Array)
128127

129-
let withdrawalsBuf = zeros(0)
128+
let withdrawalsBuf = new Uint8Array()
130129

131130
if (withdrawals !== undefined && withdrawals !== null) {
132131
const withdrawalsBufTemp: Uint8Array[] = []

packages/client/src/rpc/modules/engine/engine.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
equalsBytes,
77
hexToBytes,
88
toBytes,
9-
zeros,
109
} from '@ethereumjs/util'
1110

1211
import { ExecStatus } from '../../../execution/index.js'
@@ -71,7 +70,7 @@ import type { Block, ExecutionPayload } from '@ethereumjs/block'
7170
import type { PrefixedHexString } from '@ethereumjs/util'
7271
import type { VM } from '@ethereumjs/vm'
7372

74-
const zeroBlockHash = zeros(32)
73+
const zeroBlockHash = new Uint8Array(32)
7574

7675
/**
7776
* engine_* RPC module
@@ -486,7 +485,7 @@ export class Engine {
486485
const latestValidHash =
487486
this.chain.blocks.latest !== null
488487
? await validHash(this.chain.blocks.latest.hash(), this.chain, this.chainCache)
489-
: bytesToHex(zeros(32))
488+
: bytesToHex(new Uint8Array(32))
490489
const response = {
491490
status: Status.INVALID,
492491
validationError: this.skeleton.fillStatus.validationError ?? '',
@@ -555,7 +554,7 @@ export class Engine {
555554
const latestValidHash =
556555
this.chain.blocks.latest !== null
557556
? await validHash(this.chain.blocks.latest.hash(), this.chain, this.chainCache)
558-
: bytesToHex(zeros(32))
557+
: bytesToHex(new Uint8Array(32))
559558
const response = {
560559
status: Status.INVALID,
561560
validationError: this.skeleton.fillStatus.validationError ?? '',
@@ -978,7 +977,7 @@ export class Engine {
978977
const latestValidHash =
979978
this.chain.blocks.latest !== null
980979
? await validHash(this.chain.blocks.latest.hash(), this.chain, this.chainCache)
981-
: bytesToHex(zeros(32))
980+
: bytesToHex(new Uint8Array(32))
982981
const response = {
983982
payloadStatus: {
984983
status: Status.INVALID,

packages/client/src/service/skeleton.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
formatBigDecimal,
1414
intToBytes,
1515
utf8ToBytes,
16-
zeros,
1716
} from '@ethereumjs/util'
1817

1918
import { short, timeDuration } from '../util/index.js'
@@ -89,7 +88,7 @@ export const errReorgDenied = new Error('non-forced head reorg denied')
8988
*/
9089
export const errSyncMerged = new Error('sync merged')
9190

92-
const zeroBlockHash = zeros(32)
91+
const zeroBlockHash = new Uint8Array(32)
9392
/**
9493
* The Skeleton chain class helps support beacon sync by accepting head blocks
9594
* while backfill syncing the rest of the chain.

packages/common/test/hardforks.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBytes, zeros } from '@ethereumjs/util'
1+
import { hexToBytes } from '@ethereumjs/util'
22
import { assert, describe, it } from 'vitest'
33

44
import {
@@ -312,7 +312,7 @@ describe('[Common]: Hardfork logic', () => {
312312
hardfork: Hardfork.Cancun,
313313
mergeForkIdPostMerge: true,
314314
}
315-
const genesisHash = zeros(32)
315+
const genesisHash = new Uint8Array(32)
316316
const zeroCommon = createCommonFromGethGenesis(defaultConfig, gethConfig)
317317

318318
const zeroCommonShanghaiFork = zeroCommon.forkHash(Hardfork.Shanghai, genesisHash)

0 commit comments

Comments
 (0)