Skip to content

Commit d207116

Browse files
acolytec3holgerd77g11techjochem-brouwergabrocheleau
committed
Buffer to Uint8Array conversion (#2566)
* V7 update to master 1 (#2593) * Added v7 release reference in main README table (#2562) * common: Schedule shanghai on goerli (#2563) * common: Schedule shanghai on goerli * update timestamp * util/tx: Shift ssz back to case dependency free ES2019 compatible version (#2564) * util/tx: Shift ssz back to case dependency free ES2019 compatible version * update package lock * update karma ecma version * VM: some optimization on the bnadd/bnmul precompiles to only copy over the necessary 128 bytes as input for the WASM call (#2568) * EVM: Memory Fix & Other Optimizations (#2570) * EVM: Rename evm debug logger to evm:evm (one for package, one for class), consistency, also, logger will otherwise be left out when run with evm:* * VM: Rename message checkpoint to state checkpoint in debug message (there is a dedicated message checkpoint msg along msg logging) * EVM: CALL/CREATE debug exit msg differentiation * EVM: avoid buffer copy in memory read (performance) * EVM: Rewrite runCall() checkpoint/revert conditional for readability/simplification * EVM: Added EIP check for transient storage checkpointing * EVM: Precompile Debug Logger Improvements (#2572) * EVM: Added general precompile debug logger setup, first ECRECOVER exemplary debug logging * EVM: Added remaining precompile debug loggers * EVM: added error cases to BLS precompile debug log * EVM: Added missing precompile return value debug logs * Small fixes * tx: ensure eip3860 txs can have more than max_initcode_size data if to field is non-empty (#2575) * EVM: Avoid memory.read() Memory Copy (#2573) * EVM: added avoidCopy parameter to memory.read() function, first test on CREATE opcode * EVM: Add direct memory read to all calling opcodes * EVM: Copy over memory on IDENTITY precompile * EVM: remove length checks and return buffer 0-filling in Memory.read() (memory is uncoditionally being extended properly anyhow) * Some optimizations * blockchain: fix merge->clique transition (#2571) * Client: ensure safe/finalized blocks are part of the canonical chain on forkchoiceUpdated (#2577) * client/engine: ensure finalized/safe blocks are in canonical chain * client: engine-api: fix finalized block check * client/tests: fix forkchoice updated test * client: add fcu tests to check if blocks are part of canonical chain * client/engine: ensure payload has a valid timestamp forkchoiceUpdated (#2579) * client/engine: ensure invalid blockhash response matches spec (#2583) * client/engine: delete invalid skeleton blocks (#2584) Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * Setup to dev/test snapsync with sim architecture (#2574) * Setup to dev/test snapsync with sim architecture * modfiy single-run to setup a lodestar<>geth node to snapsync from * setup an ethereumjs inline client and get it to peer with geth * cleanup setup a bit * snapsync run spec * get the snap testdev sim working * finalize the test infra and update usage doc * enhance coverage * Use geth RPC to connect to ethJS * refac wait for snap sync completion --------- Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * client: Add safe and finalized blockoptions to the chain (#2585) * client: Add safe and finalized blockoptions to the chain * fix tests * fix more tests * fix remaining * cleanup * enhance coverage * unset scheduled goerli timestamp based hfs colliding with test * Client: Small Debug Helpers and CLI Improvements (#2586) * Client: new constant MAX_TOLERATED_BLOCK_TIME for execution, added warning for slowly executed blocks * Client -> Execution: NumBlocksPerIteration (default: 50) as an option * Client: only restart RLPx server or log peer stats if max peers is set to be greater than 0 * Apply suggestions from code review Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * Apply suggestions from code review --------- Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * common: Schedule Shanghai on mainnet! (#2591) * common: Schedule Shanghai on mainnet! * clear hf timestamp for test * VM: Diff-based Touched Accounts Checkpointing (#2581) * VM: Switched to a more efficient diff-based way of touched account checkpointing * VM: move accessed storage inefficient checkpointing problem to berlin, haha * EVM: avoid memory copy in MLOAD opcode function * Remove console.log() in EVM * vmState: ensure touched accounts delete stack gets properly updated on commit * vm/eei: save touched height * vm/vmState: new possible fix for touched accounts * vm/vmState: another attempt to fix touched accounts journaling * vm: add journaling * Check correct journal height on revert --------- Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com> Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> --------- Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com> Co-authored-by: g11tech <gajinder@g11.in> Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com> * First pass - most util changes * Fix most account stuff * Fix account test * Many byte fixes * util: fix constants tests * remaining fixes * Turn off ci jobs * monorepo: bigIntToUnpaddedBuffer -> bigIntToUnpaddedBytes * util: update description of bytes exporT * util: remove unused import * common: use bytesToHex helper instead of toString('hex') * trie: refactor non-test files to Uint8Array * util: add binary string utils * util: remove extra Uint8Array.from * util: remove arrToBufArray util * trie: adjust tests and fix outstanding issues * util: remove binarystring utils and add compareBytes and randomBytes util * common: refactor common with Uint8Array * util: accept 0x-prefixed and non-prefixed hex strings in toBytes * tx: refactor Buffer -> Uint8Array * tx: remove unused import * util: revert toBytes update * block: refactor Buffer -> uint8array * block: adjust import * trie: refactor remaining buffer instances * move devp2p to uint8Array * statemanager: refactor buffer -> uint8array * util: simplify zeros * util: add concatBytesUnsafe * ethash: partial migration * ethash: update examples * ethash: wip fixes * ethash: more WIP * ethash: ensure fnv input is read from mix, not mixView * blockchain: migrate to uint8array * ethash: renable all tests * ethash: fix bytesReverse * Fix miner tests * many hexToBytes moves * most of evm/vm moves * evm: Fix all tests * vm: more fixes * More fixes * vm: fix receipts encoding * vm: fix tester * client: refactor buffer to uint8array * client: additional uint8 adjustments * client: fix most tests * fix remaining client unit tests * reactivate most CI * client: fix les test/protocol * turn client CI on * util: fix name typo * lint * Fix withdrawals * remove buffarraytoarr * Remove bufArrtoArr references * Lint * fix examples * Fix difficulty test * lint * block: update randomBytes import * replace randombytes import * client: fix sim test util * vm: fix example * devp2p: update snappy typing and fix tests * Fix tests * Remove additional buffer references * rustbn fixes * add 0x prefix to precompile address * Remove `node-ip` dependency and buffer references in devp2p * Switch slice to subarray * evm: fix blake2f * Merge fixes * more merge commit fixes * more test fixes * Address all the feedback * fix dns test * Update packages/util/src/bytes.ts Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com> * Fix return type for baToJSON * util: instantiate hexByByte array * Remove baToJson * rebase fixes * Fix event typing * Revert outdated initcode changes * lint --------- Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com> Co-authored-by: g11tech <gajinder@g11.in> Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com> Co-authored-by: Gabriel Rocheleau <contact@rockwaterweb.com>
1 parent abe741e commit d207116

File tree

363 files changed

+6527
-6325
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

363 files changed

+6527
-6325
lines changed

package-lock.json

Lines changed: 16 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/block/src/block.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { BlobEIP4844Transaction, Capability, TransactionFactory } from '@ethereu
55
import {
66
KECCAK256_RLP,
77
Withdrawal,
8-
arrToBufArr,
98
bigIntToHex,
10-
bufArrToArr,
11-
bufferToHex,
9+
bytesToHex,
10+
equalsBytes,
1211
intToHex,
1312
isHexPrefixed,
1413
ssz,
@@ -20,15 +19,15 @@ import { blockFromRpc } from './from-rpc'
2019
import { BlockHeader } from './header'
2120
import { getDataGasPrice } from './helpers'
2221

23-
import type { BlockBuffer, BlockData, BlockOptions, JsonBlock, JsonRpcBlock } from './types'
22+
import type { BlockBytes, BlockData, BlockOptions, JsonBlock, JsonRpcBlock } from './types'
2423
import type { Common } from '@ethereumjs/common'
2524
import type {
2625
FeeMarketEIP1559Transaction,
2726
Transaction,
2827
TxOptions,
2928
TypedTransaction,
3029
} from '@ethereumjs/tx'
31-
import type { WithdrawalBuffer } from '@ethereumjs/util'
30+
import type { WithdrawalBytes } from '@ethereumjs/util'
3231

3332
/**
3433
* An object that represents the block.
@@ -49,7 +48,7 @@ export class Block {
4948
public static async genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: Trie) {
5049
const trie = emptyTrie ?? new Trie()
5150
for (const [i, wt] of wts.entries()) {
52-
await trie.put(Buffer.from(RLP.encode(i)), arrToBufArr(RLP.encode(wt.raw())))
51+
await trie.put(RLP.encode(i), RLP.encode(wt.raw()))
5352
}
5453
return trie.root()
5554
}
@@ -70,7 +69,7 @@ export class Block {
7069
public static async genTransactionsTrieRoot(txs: TypedTransaction[], emptyTrie?: Trie) {
7170
const trie = emptyTrie ?? new Trie()
7271
for (const [i, tx] of txs.entries()) {
73-
await trie.put(Buffer.from(RLP.encode(i)), tx.serialize())
72+
await trie.put(RLP.encode(i), tx.serialize())
7473
}
7574
return trie.root()
7675
}
@@ -133,8 +132,8 @@ export class Block {
133132
* @param serialized
134133
* @param opts
135134
*/
136-
public static fromRLPSerializedBlock(serialized: Buffer, opts?: BlockOptions) {
137-
const values = arrToBufArr(RLP.decode(Uint8Array.from(serialized))) as BlockBuffer
135+
public static fromRLPSerializedBlock(serialized: Uint8Array, opts?: BlockOptions) {
136+
const values = RLP.decode(Uint8Array.from(serialized)) as BlockBytes
138137

139138
if (!Array.isArray(values)) {
140139
throw new Error('Invalid serialized block input. Must be array')
@@ -144,12 +143,12 @@ export class Block {
144143
}
145144

146145
/**
147-
* Static constructor to create a block from an array of Buffer values
146+
* Static constructor to create a block from an array of Bytes values
148147
*
149148
* @param values
150149
* @param opts
151150
*/
152-
public static fromValuesArray(values: BlockBuffer, opts?: BlockOptions) {
151+
public static fromValuesArray(values: BlockBytes, opts?: BlockOptions) {
153152
if (values.length > 4) {
154153
throw new Error('invalid block. More values than expected were received')
155154
}
@@ -163,7 +162,7 @@ export class Block {
163162
)
164163
}
165164

166-
const [headerData, txsData, uhsData, withdrawalsBuffer] = values
165+
const [headerData, txsData, uhsData, withdrawalBytes] = values
167166

168167
const header = BlockHeader.fromValuesArray(headerData, opts)
169168

@@ -199,7 +198,7 @@ export class Block {
199198
uncleHeaders.push(BlockHeader.fromValuesArray(uncleHeaderData, uncleOpts))
200199
}
201200

202-
const withdrawals = (withdrawalsBuffer as WithdrawalBuffer[])
201+
const withdrawals = (withdrawalBytes as WithdrawalBytes[])
203202
?.map(([index, validatorIndex, address, amount]) => ({
204203
index,
205204
validatorIndex,
@@ -312,27 +311,27 @@ export class Block {
312311
}
313312

314313
/**
315-
* Returns a Buffer Array of the raw Buffers of this block, in order.
314+
* Returns a Array of the raw Bytes Arays of this block, in order.
316315
*/
317-
raw(): BlockBuffer {
318-
const bufferArray = <BlockBuffer>[
316+
raw(): BlockBytes {
317+
const bytesArray = <BlockBytes>[
319318
this.header.raw(),
320319
this.transactions.map((tx) =>
321320
tx.supports(Capability.EIP2718TypedTransaction) ? tx.serialize() : tx.raw()
322-
) as Buffer[],
321+
) as Uint8Array[],
323322
this.uncleHeaders.map((uh) => uh.raw()),
324323
]
325324
const withdrawalsRaw = this.withdrawals?.map((wt) => wt.raw())
326325
if (withdrawalsRaw) {
327-
bufferArray.push(withdrawalsRaw)
326+
bytesArray.push(withdrawalsRaw)
328327
}
329-
return bufferArray
328+
return bytesArray
330329
}
331330

332331
/**
333332
* Returns the hash of the block.
334333
*/
335-
hash(): Buffer {
334+
hash(): Uint8Array {
336335
return this.header.hash()
337336
}
338337

@@ -346,8 +345,8 @@ export class Block {
346345
/**
347346
* Returns the rlp encoding of the block.
348347
*/
349-
serialize(): Buffer {
350-
return Buffer.from(RLP.encode(bufArrToArr(this.raw())))
348+
serialize(): Uint8Array {
349+
return RLP.encode(this.raw())
351350
}
352351

353352
/**
@@ -365,14 +364,14 @@ export class Block {
365364
async validateTransactionsTrie(): Promise<boolean> {
366365
let result
367366
if (this.transactions.length === 0) {
368-
result = this.header.transactionsTrie.equals(KECCAK256_RLP)
367+
result = equalsBytes(this.header.transactionsTrie, KECCAK256_RLP)
369368
return result
370369
}
371370

372-
if (this.txTrie.root().equals(KECCAK256_RLP)) {
371+
if (equalsBytes(this.txTrie.root(), KECCAK256_RLP) === true) {
373372
await this.genTxTrie()
374373
}
375-
result = this.txTrie.root().equals(this.header.transactionsTrie)
374+
result = equalsBytes(this.txTrie.root(), this.header.transactionsTrie)
376375
return result
377376
}
378377

@@ -487,8 +486,8 @@ export class Block {
487486
*/
488487
validateUnclesHash(): boolean {
489488
const uncles = this.uncleHeaders.map((uh) => uh.raw())
490-
const raw = RLP.encode(bufArrToArr(uncles))
491-
return Buffer.from(keccak256(raw)).equals(this.header.uncleHash)
489+
const raw = RLP.encode(uncles)
490+
return equalsBytes(keccak256(raw), this.header.uncleHash)
492491
}
493492

494493
/**
@@ -499,7 +498,7 @@ export class Block {
499498
throw new Error('EIP 4895 is not activated')
500499
}
501500
const withdrawalsRoot = await Block.genWithdrawalsTrieRoot(this.withdrawals!)
502-
return withdrawalsRoot.equals(this.header.withdrawalsRoot!)
501+
return equalsBytes(withdrawalsRoot, this.header.withdrawalsRoot!)
503502
}
504503

505504
/**
@@ -523,7 +522,7 @@ export class Block {
523522
}
524523

525524
// Header does not count an uncle twice.
526-
const uncleHashes = this.uncleHeaders.map((header) => header.hash().toString('hex'))
525+
const uncleHashes = this.uncleHeaders.map((header) => bytesToHex(header.hash()))
527526
if (!(new Set(uncleHashes).size === uncleHashes.length)) {
528527
const msg = this._errorMsg('duplicate uncles')
529528
throw new Error(msg)
@@ -572,7 +571,7 @@ export class Block {
572571
public errorStr() {
573572
let hash = ''
574573
try {
575-
hash = bufferToHex(this.hash())
574+
hash = bytesToHex(this.hash())
576575
} catch (e: any) {
577576
hash = 'error'
578577
}

packages/block/src/from-rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TransactionFactory } from '@ethereumjs/tx'
2-
import { TypeOutput, setLengthLeft, toBuffer, toType } from '@ethereumjs/util'
2+
import { TypeOutput, setLengthLeft, toBytes, toType } from '@ethereumjs/util'
33

44
import { blockHeaderFromRpc } from './header-from-rpc'
55

@@ -21,7 +21,7 @@ function normalizeTxParams(_txParams: any) {
2121
// strict byte length checking
2222
txParams.to =
2323
txParams.to !== null && txParams.to !== undefined
24-
? setLengthLeft(toBuffer(txParams.to), 20)
24+
? setLengthLeft(toBytes(txParams.to), 20)
2525
: null
2626

2727
txParams.v = toType(txParams.v, TypeOutput.BigInt)

0 commit comments

Comments
 (0)