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

Buffer to Uint8Array cleanup #2607

Merged
merged 19 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
48 changes: 16 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 29 additions & 30 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { BlobEIP4844Transaction, Capability, TransactionFactory } from '@ethereu
import {
KECCAK256_RLP,
Withdrawal,
arrToBufArr,
bigIntToHex,
bufArrToArr,
bufferToHex,
bytesToHex,
equalsBytes,
intToHex,
isHexPrefixed,
ssz,
Expand All @@ -20,15 +19,15 @@ import { blockFromRpc } from './from-rpc'
import { BlockHeader } from './header'
import { getDataGasPrice } from './helpers'

import type { BlockBuffer, BlockData, BlockOptions, JsonBlock, JsonRpcBlock } from './types'
import type { BlockBytes, BlockData, BlockOptions, JsonBlock, JsonRpcBlock } from './types'
import type { Common } from '@ethereumjs/common'
import type {
FeeMarketEIP1559Transaction,
Transaction,
TxOptions,
TypedTransaction,
} from '@ethereumjs/tx'
import type { WithdrawalBuffer } from '@ethereumjs/util'
import type { WithdrawalBytes } from '@ethereumjs/util'

/**
* An object that represents the block.
Expand All @@ -49,7 +48,7 @@ export class Block {
public static async genWithdrawalsTrieRoot(wts: Withdrawal[], emptyTrie?: Trie) {
const trie = emptyTrie ?? new Trie()
for (const [i, wt] of wts.entries()) {
await trie.put(Buffer.from(RLP.encode(i)), arrToBufArr(RLP.encode(wt.raw())))
await trie.put(RLP.encode(i), RLP.encode(wt.raw()))
}
return trie.root()
}
Expand All @@ -70,7 +69,7 @@ export class Block {
public static async genTransactionsTrieRoot(txs: TypedTransaction[], emptyTrie?: Trie) {
const trie = emptyTrie ?? new Trie()
for (const [i, tx] of txs.entries()) {
await trie.put(Buffer.from(RLP.encode(i)), tx.serialize())
await trie.put(RLP.encode(i), tx.serialize())
}
return trie.root()
}
Expand Down Expand Up @@ -133,8 +132,8 @@ export class Block {
* @param serialized
* @param opts
*/
public static fromRLPSerializedBlock(serialized: Buffer, opts?: BlockOptions) {
const values = arrToBufArr(RLP.decode(Uint8Array.from(serialized))) as BlockBuffer
public static fromRLPSerializedBlock(serialized: Uint8Array, opts?: BlockOptions) {
const values = RLP.decode(Uint8Array.from(serialized)) as BlockBytes

if (!Array.isArray(values)) {
throw new Error('Invalid serialized block input. Must be array')
Expand All @@ -144,16 +143,16 @@ export class Block {
}

/**
* Static constructor to create a block from an array of Buffer values
* Static constructor to create a block from an array of Bytes values
*
* @param values
* @param opts
*/
public static fromValuesArray(values: BlockBuffer, opts?: BlockOptions) {
public static fromValuesArray(values: BlockBytes, opts?: BlockOptions) {
if (values.length > 4) {
throw new Error('invalid block. More values than expected were received')
}
const [headerData, txsData, uhsData, withdrawalsBuffer] = values
const [headerData, txsData, uhsData, withdrawalBytes] = values

const header = BlockHeader.fromValuesArray(headerData, opts)

Expand Down Expand Up @@ -189,7 +188,7 @@ export class Block {
uncleHeaders.push(BlockHeader.fromValuesArray(uncleHeaderData, uncleOpts))
}

const withdrawals = (withdrawalsBuffer as WithdrawalBuffer[])
const withdrawals = (withdrawalBytes as WithdrawalBytes[])
?.map(([index, validatorIndex, address, amount]) => ({
index,
validatorIndex,
Expand Down Expand Up @@ -302,27 +301,27 @@ export class Block {
}

/**
* Returns a Buffer Array of the raw Buffers of this block, in order.
* Returns a Array of the raw Bytes Arays of this block, in order.
*/
raw(): BlockBuffer {
const bufferArray = <BlockBuffer>[
raw(): BlockBytes {
const bytesArray = <BlockBytes>[
this.header.raw(),
this.transactions.map((tx) =>
tx.supports(Capability.EIP2718TypedTransaction) ? tx.serialize() : tx.raw()
) as Buffer[],
) as Uint8Array[],
this.uncleHeaders.map((uh) => uh.raw()),
]
const withdrawalsRaw = this.withdrawals?.map((wt) => wt.raw())
if (withdrawalsRaw) {
bufferArray.push(withdrawalsRaw)
bytesArray.push(withdrawalsRaw)
}
return bufferArray
return bytesArray
}

/**
* Returns the hash of the block.
*/
hash(): Buffer {
hash(): Uint8Array {
return this.header.hash()
}

Expand All @@ -336,8 +335,8 @@ export class Block {
/**
* Returns the rlp encoding of the block.
*/
serialize(): Buffer {
return Buffer.from(RLP.encode(bufArrToArr(this.raw())))
serialize(): Uint8Array {
return RLP.encode(this.raw())
}

/**
Expand All @@ -355,14 +354,14 @@ export class Block {
async validateTransactionsTrie(): Promise<boolean> {
let result
if (this.transactions.length === 0) {
result = this.header.transactionsTrie.equals(KECCAK256_RLP)
result = equalsBytes(this.header.transactionsTrie, KECCAK256_RLP)
return result
}

if (this.txTrie.root().equals(KECCAK256_RLP)) {
if (equalsBytes(this.txTrie.root(), KECCAK256_RLP)) {
await this.genTxTrie()
}
result = this.txTrie.root().equals(this.header.transactionsTrie)
result = equalsBytes(this.txTrie.root(), this.header.transactionsTrie)
return result
}

Expand Down Expand Up @@ -477,8 +476,8 @@ export class Block {
*/
validateUnclesHash(): boolean {
const uncles = this.uncleHeaders.map((uh) => uh.raw())
const raw = RLP.encode(bufArrToArr(uncles))
return Buffer.from(keccak256(raw)).equals(this.header.uncleHash)
const raw = RLP.encode(uncles)
return equalsBytes(keccak256(raw), this.header.uncleHash)
}

/**
Expand All @@ -489,7 +488,7 @@ export class Block {
throw new Error('EIP 4895 is not activated')
}
const withdrawalsRoot = await Block.genWithdrawalsTrieRoot(this.withdrawals!)
return withdrawalsRoot.equals(this.header.withdrawalsRoot!)
return equalsBytes(withdrawalsRoot, this.header.withdrawalsRoot!)
}

/**
Expand All @@ -513,7 +512,7 @@ export class Block {
}

// Header does not count an uncle twice.
const uncleHashes = this.uncleHeaders.map((header) => header.hash().toString('hex'))
const uncleHashes = this.uncleHeaders.map((header) => bytesToHex(header.hash()))
if (!(new Set(uncleHashes).size === uncleHashes.length)) {
const msg = this._errorMsg('duplicate uncles')
throw new Error(msg)
Expand Down Expand Up @@ -562,7 +561,7 @@ export class Block {
public errorStr() {
let hash = ''
try {
hash = bufferToHex(this.hash())
hash = bytesToHex(this.hash())
} catch (e: any) {
hash = 'error'
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/from-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransactionFactory } from '@ethereumjs/tx'
import { TypeOutput, setLengthLeft, toBuffer, toType } from '@ethereumjs/util'
import { TypeOutput, setLengthLeft, toBytes, toType } from '@ethereumjs/util'

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

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

txParams.v = toType(txParams.v, TypeOutput.BigInt)
Expand Down
Loading