Skip to content

Commit c6deb4e

Browse files
authored
util: replace unnecessary toBytes usage (#4014)
* util: replace some toBytes usage * client: more hexToBytes * chore: remove more toBytes * chore: remove toBytes usage * lint: remove unused imports * chore: remove unused import * chore: remove unused import
1 parent 2908c04 commit c6deb4e

File tree

23 files changed

+161
-156
lines changed

23 files changed

+161
-156
lines changed

packages/block/test/header.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ describe('[Block]: Header functions', () => {
320320
const common = new Common({ chain: goerliChainConfig, hardfork: Hardfork.Istanbul })
321321
const blockchain = new Mockchain()
322322
323-
const genesisRlp = toBytes(testDataPreLondon.genesisRLP)
323+
const genesisRlp = hexToBytes(testDataPreLondon.genesisRLP)
324324
const block = createBlockFromRLP(genesisRlp, { common })
325325
await blockchain.putBlock(block)
326326

packages/client/src/miner/pendingBlock.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
concatBytes,
1010
createZeroAddress,
1111
equalsBytes,
12-
toBytes,
1312
toType,
1413
} from '@ethereumjs/util'
1514
import { BuildStatus, buildBlock } from '@ethereumjs/vm'
@@ -145,19 +144,18 @@ export class PendingBlock {
145144

146145
const keccakFunction = this.config.chainCommon.customCrypto.keccak256 ?? keccak256
147146

148-
const payloadIdBytes = toBytes(
149-
keccakFunction(
150-
concatBytes(
151-
parentBlock.hash(),
152-
mixHashBuf,
153-
timestampBuf,
154-
gasLimitBuf,
155-
parentBeaconBlockRootBuf,
156-
coinbaseBuf,
157-
withdrawalsBuf,
158-
),
159-
).subarray(0, 8),
160-
)
147+
const payloadIdBytes = keccakFunction(
148+
concatBytes(
149+
parentBlock.hash(),
150+
mixHashBuf,
151+
timestampBuf,
152+
gasLimitBuf,
153+
parentBeaconBlockRootBuf,
154+
coinbaseBuf,
155+
withdrawalsBuf,
156+
),
157+
).subarray(0, 8)
158+
161159
const payloadId = bytesToHex(payloadIdBytes)
162160

163161
// If payload has already been triggered, then return the payloadid

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

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

1211
import { ExecStatus } from '../../../execution/index.ts'
@@ -911,7 +910,7 @@ export class Engine {
911910
*/
912911
let headBlock: Block | undefined
913912
try {
914-
const head = toBytes(headBlockHash)
913+
const head = hexToBytes(headBlockHash)
915914
headBlock =
916915
this.remoteBlocks.get(headBlockHash.slice(2)) ??
917916
(await this.skeleton.getBlockByHash(head, true)) ??

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bytesToHex, hexToBytes, toBytes } from '@ethereumjs/util'
1+
import { bytesToHex, hexToBytes } from '@ethereumjs/util'
22
import { keccak256 } from 'ethereum-cryptography/keccak.js'
33

44
import { getClientVersion } from '../../util/index.ts'
@@ -45,7 +45,7 @@ export class Web3 {
4545
* @param params The data to convert into a SHA3 hash
4646
*/
4747
sha3(params: PrefixedHexString[]): PrefixedHexString {
48-
const hexEncodedDigest = bytesToHex(keccak256(toBytes(hexToBytes(params[0]))))
48+
const hexEncodedDigest = bytesToHex(keccak256(hexToBytes(params[0])))
4949
return hexEncodedDigest
5050
}
5151
}

packages/client/test/rpc/mockBlockchain.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { createBlock } from '@ethereumjs/block'
22
import { createLegacyTx } from '@ethereumjs/tx'
3-
import { equalsBytes, toBytes } from '@ethereumjs/util'
3+
import { type PrefixedHexString, equalsBytes, hexToBytes } from '@ethereumjs/util'
44

55
import { dummy } from './helpers.ts'
66

7-
import type { LegacyTx } from '@ethereumjs/tx'
7+
import type { JSONTx, LegacyTx, TypedTransaction } from '@ethereumjs/tx'
88

9-
export function mockBlockchain(options: any = {}) {
10-
const number = options.number ?? '0x444444'
11-
const blockHash =
9+
export function mockBlockchain(
10+
options: {
11+
number?: PrefixedHexString
12+
hash?: PrefixedHexString
13+
transactions?: TypedTransaction[] | JSONTx[]
14+
} = {},
15+
) {
16+
const number: PrefixedHexString = options.number ?? '0x444444'
17+
const blockHash: PrefixedHexString =
1218
options.hash ?? '0x910abca1728c53e8d6df870dd7af5352e974357dc58205dea1676be17ba6becf'
1319
const transactions = options.transactions ?? [createLegacyTx({}).sign(dummy.privKey)]
1420
const block = {
15-
hash: () => toBytes(blockHash),
21+
hash: () => hexToBytes(blockHash),
1622
serialize: () => createBlock({ header: { number }, transactions }).serialize(),
1723
header: {
1824
number: BigInt(number),
19-
hash: () => toBytes(blockHash),
25+
hash: () => hexToBytes(blockHash),
2026
},
2127
toJSON: () => ({
2228
...createBlock({ header: { number } }).toJSON(),
2329
hash: options.hash ?? blockHash,
24-
transactions: transactions.map((t: LegacyTx) => t.toJSON()),
30+
transactions: transactions.map((t) => (t as LegacyTx).toJSON()),
2531
}),
2632
transactions,
2733
uncleHeaders: [],

packages/mpt/src/util/encoding.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { concatBytes, hexToBytes, toBytes, unprefixedHexToBytes } from '@ethereumjs/util'
1+
import { concatBytes, hexToBytes, unprefixedHexToBytes } from '@ethereumjs/util'
22

33
import { nibblesTypeToPackedBytes } from './nibbles.ts'
44

@@ -129,12 +129,11 @@ export const nibbleTypeToByteType = (arr: Nibbles): Uint8Array => {
129129
* @returns Nibble typed nibble array
130130
*/
131131
export const byteTypeToNibbleType = (key: Uint8Array): Nibbles => {
132-
const bKey = toBytes(key)
133132
const nibbles = [] as Nibbles
134133

135-
for (let i = 0; i < bKey.length; i++) {
134+
for (let i = 0; i < key.length; i++) {
136135
const q = i
137-
nibbles[q] = bKey[i] % 16
136+
nibbles[q] = key[i] % 16
138137
}
139138

140139
return nibbles

packages/mpt/src/util/nibbles.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { toBytes } from '@ethereumjs/util'
2-
31
import type { Nibbles } from '../types.ts'
42

53
/**
@@ -8,14 +6,13 @@ import type { Nibbles } from '../types.ts'
86
* @param key
97
*/
108
export function bytesToNibbles(key: Uint8Array): Nibbles {
11-
const bKey = toBytes(key)
129
const nibbles = [] as Nibbles
1310

14-
for (let i = 0; i < bKey.length; i++) {
11+
for (let i = 0; i < key.length; i++) {
1512
let q = i * 2
16-
nibbles[q] = bKey[i] >> 4
13+
nibbles[q] = key[i] >> 4
1714
++q
18-
nibbles[q] = bKey[i] % 16
15+
nibbles[q] = key[i] % 16
1916
}
2017

2118
return nibbles

packages/mpt/test/proof/range.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
compareBytes,
44
concatBytes,
55
hexToBytes,
6+
intToBytes,
67
randomBytes,
78
setLengthLeft,
8-
toBytes,
99
} from '@ethereumjs/util'
1010
import { assert, describe, it } from 'vitest'
1111

@@ -28,8 +28,9 @@ async function randomTrie(db: DB<string, string>, addKey: boolean = true) {
2828

2929
if (addKey) {
3030
for (let i = 0; i < 100; i++) {
31-
const key = setLengthLeft(toBytes(i), 32)
32-
const val = toBytes(i)
31+
const indexBytes = intToBytes(i)
32+
const key = setLengthLeft(indexBytes, 32)
33+
const val = indexBytes
3334
await trie.put(key, val)
3435
entries.push([key, val])
3536
}
@@ -62,15 +63,15 @@ function getRandomIntInclusive(min: number, max: number): number {
6263
function decreaseKey(key: Uint8Array) {
6364
for (let i = key.length - 1; i >= 0; i--) {
6465
if (key[i] > 0) {
65-
return concatBytes(key.slice(0, i), toBytes(key[i] - 1), key.slice(i + 1))
66+
return concatBytes(key.slice(0, i), intToBytes(key[i] - 1), key.slice(i + 1))
6667
}
6768
}
6869
}
6970

7071
function increaseKey(key: Uint8Array) {
7172
for (let i = key.length - 1; i >= 0; i--) {
7273
if (key[i] < 255) {
73-
return concatBytes(key.slice(0, i), toBytes(key[i] + 1), key.slice(i + 1))
74+
return concatBytes(key.slice(0, i), intToBytes(key[i] + 1), key.slice(i + 1))
7475
}
7576
}
7677
}
@@ -323,8 +324,9 @@ describe('simple merkle range proofs generation and verification', () => {
323324
const trie = new MerklePatriciaTrie()
324325
const entries: [Uint8Array, Uint8Array][] = []
325326
for (let i = 0; i < 10; i++) {
326-
const key = setLengthLeft(toBytes(i), 32)
327-
const val = toBytes(i)
327+
const indexBytes = intToBytes(i)
328+
const key = setLengthLeft(indexBytes, 32)
329+
const val = indexBytes
328330
await trie.put(key, val)
329331
entries.push([key, val])
330332
}

packages/statemanager/src/statelessVerkleStateManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
setLengthLeft,
2222
setLengthRight,
2323
short,
24-
toBytes,
2524
} from '@ethereumjs/util'
2625
import debugDefault from 'debug'
2726
import { keccak256 } from 'ethereum-cryptography/keccak.js'
@@ -351,7 +350,8 @@ export class StatelessVerkleStateManager implements StateManagerInterface {
351350
BigInt(bytesToHex(key)),
352351
this.verkleCrypto,
353352
)
354-
const storageValue = toBytes(this._state[bytesToHex(storageKey)])
353+
const rawStorageValue = this._state[bytesToHex(storageKey)]
354+
const storageValue = rawStorageValue === null ? new Uint8Array() : hexToBytes(rawStorageValue)
355355

356356
this._caches?.storage?.put(address, key, storageValue ?? hexToBytes('0x80'))
357357

packages/tx/examples/custom-chain-id-tx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Hardfork, Mainnet, createCustomCommon } from '@ethereumjs/common'
22
import { createLegacyTxFromRLP } from '@ethereumjs/tx'
3-
import { toBytes } from '@ethereumjs/util'
3+
import { hexToBytes, toBytes } from '@ethereumjs/util'
44

5-
const txData = toBytes(
5+
const txData = hexToBytes(
66
'0xf9010b82930284d09dc30083419ce0942d18de92e0f9aee1a29770c3b15c6cf8ac5498e580b8a42f43f4fb0000000000000000000000000000000000000000000000000000016b78998da900000000000000000000000000000000000000000000000000000000000cb1b70000000000000000000000000000000000000000000000000000000000000fa00000000000000000000000000000000000000000000000000000000001363e4f00000000000000000000000000000000000000000000000000000000000186a029a0fac36e66d329af0e831b2e61179b3ec8d7c7a8a2179e303cfed3364aff2bc3e4a07cb73d56e561ccbd838818dd3dea5fa0b5158577ffc61c0e6ec1f0ed55716891',
77
)
88

0 commit comments

Comments
 (0)