@@ -5,10 +5,9 @@ import { BlobEIP4844Transaction, Capability, TransactionFactory } from '@ethereu
5
5
import {
6
6
KECCAK256_RLP ,
7
7
Withdrawal ,
8
- arrToBufArr ,
9
8
bigIntToHex ,
10
- bufArrToArr ,
11
- bufferToHex ,
9
+ bytesToHex ,
10
+ equalsBytes ,
12
11
intToHex ,
13
12
isHexPrefixed ,
14
13
ssz ,
@@ -20,15 +19,15 @@ import { blockFromRpc } from './from-rpc'
20
19
import { BlockHeader } from './header'
21
20
import { getDataGasPrice } from './helpers'
22
21
23
- import type { BlockBuffer , BlockData , BlockOptions , JsonBlock , JsonRpcBlock } from './types'
22
+ import type { BlockBytes , BlockData , BlockOptions , JsonBlock , JsonRpcBlock } from './types'
24
23
import type { Common } from '@ethereumjs/common'
25
24
import type {
26
25
FeeMarketEIP1559Transaction ,
27
26
Transaction ,
28
27
TxOptions ,
29
28
TypedTransaction ,
30
29
} from '@ethereumjs/tx'
31
- import type { WithdrawalBuffer } from '@ethereumjs/util'
30
+ import type { WithdrawalBytes } from '@ethereumjs/util'
32
31
33
32
/**
34
33
* An object that represents the block.
@@ -49,7 +48,7 @@ export class Block {
49
48
public static async genWithdrawalsTrieRoot ( wts : Withdrawal [ ] , emptyTrie ?: Trie ) {
50
49
const trie = emptyTrie ?? new Trie ( )
51
50
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 ( ) ) )
53
52
}
54
53
return trie . root ( )
55
54
}
@@ -70,7 +69,7 @@ export class Block {
70
69
public static async genTransactionsTrieRoot ( txs : TypedTransaction [ ] , emptyTrie ?: Trie ) {
71
70
const trie = emptyTrie ?? new Trie ( )
72
71
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 ( ) )
74
73
}
75
74
return trie . root ( )
76
75
}
@@ -133,8 +132,8 @@ export class Block {
133
132
* @param serialized
134
133
* @param opts
135
134
*/
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
138
137
139
138
if ( ! Array . isArray ( values ) ) {
140
139
throw new Error ( 'Invalid serialized block input. Must be array' )
@@ -144,12 +143,12 @@ export class Block {
144
143
}
145
144
146
145
/**
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
148
147
*
149
148
* @param values
150
149
* @param opts
151
150
*/
152
- public static fromValuesArray ( values : BlockBuffer , opts ?: BlockOptions ) {
151
+ public static fromValuesArray ( values : BlockBytes , opts ?: BlockOptions ) {
153
152
if ( values . length > 4 ) {
154
153
throw new Error ( 'invalid block. More values than expected were received' )
155
154
}
@@ -163,7 +162,7 @@ export class Block {
163
162
)
164
163
}
165
164
166
- const [ headerData , txsData , uhsData , withdrawalsBuffer ] = values
165
+ const [ headerData , txsData , uhsData , withdrawalBytes ] = values
167
166
168
167
const header = BlockHeader . fromValuesArray ( headerData , opts )
169
168
@@ -199,7 +198,7 @@ export class Block {
199
198
uncleHeaders . push ( BlockHeader . fromValuesArray ( uncleHeaderData , uncleOpts ) )
200
199
}
201
200
202
- const withdrawals = ( withdrawalsBuffer as WithdrawalBuffer [ ] )
201
+ const withdrawals = ( withdrawalBytes as WithdrawalBytes [ ] )
203
202
?. map ( ( [ index , validatorIndex , address , amount ] ) => ( {
204
203
index,
205
204
validatorIndex,
@@ -312,27 +311,27 @@ export class Block {
312
311
}
313
312
314
313
/**
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.
316
315
*/
317
- raw ( ) : BlockBuffer {
318
- const bufferArray = < BlockBuffer > [
316
+ raw ( ) : BlockBytes {
317
+ const bytesArray = < BlockBytes > [
319
318
this . header . raw ( ) ,
320
319
this . transactions . map ( ( tx ) =>
321
320
tx . supports ( Capability . EIP2718TypedTransaction ) ? tx . serialize ( ) : tx . raw ( )
322
- ) as Buffer [ ] ,
321
+ ) as Uint8Array [ ] ,
323
322
this . uncleHeaders . map ( ( uh ) => uh . raw ( ) ) ,
324
323
]
325
324
const withdrawalsRaw = this . withdrawals ?. map ( ( wt ) => wt . raw ( ) )
326
325
if ( withdrawalsRaw ) {
327
- bufferArray . push ( withdrawalsRaw )
326
+ bytesArray . push ( withdrawalsRaw )
328
327
}
329
- return bufferArray
328
+ return bytesArray
330
329
}
331
330
332
331
/**
333
332
* Returns the hash of the block.
334
333
*/
335
- hash ( ) : Buffer {
334
+ hash ( ) : Uint8Array {
336
335
return this . header . hash ( )
337
336
}
338
337
@@ -346,8 +345,8 @@ export class Block {
346
345
/**
347
346
* Returns the rlp encoding of the block.
348
347
*/
349
- serialize ( ) : Buffer {
350
- return Buffer . from ( RLP . encode ( bufArrToArr ( this . raw ( ) ) ) )
348
+ serialize ( ) : Uint8Array {
349
+ return RLP . encode ( this . raw ( ) )
351
350
}
352
351
353
352
/**
@@ -365,14 +364,14 @@ export class Block {
365
364
async validateTransactionsTrie ( ) : Promise < boolean > {
366
365
let result
367
366
if ( this . transactions . length === 0 ) {
368
- result = this . header . transactionsTrie . equals ( KECCAK256_RLP )
367
+ result = equalsBytes ( this . header . transactionsTrie , KECCAK256_RLP )
369
368
return result
370
369
}
371
370
372
- if ( this . txTrie . root ( ) . equals ( KECCAK256_RLP ) ) {
371
+ if ( equalsBytes ( this . txTrie . root ( ) , KECCAK256_RLP ) === true ) {
373
372
await this . genTxTrie ( )
374
373
}
375
- result = this . txTrie . root ( ) . equals ( this . header . transactionsTrie )
374
+ result = equalsBytes ( this . txTrie . root ( ) , this . header . transactionsTrie )
376
375
return result
377
376
}
378
377
@@ -487,8 +486,8 @@ export class Block {
487
486
*/
488
487
validateUnclesHash ( ) : boolean {
489
488
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 )
492
491
}
493
492
494
493
/**
@@ -499,7 +498,7 @@ export class Block {
499
498
throw new Error ( 'EIP 4895 is not activated' )
500
499
}
501
500
const withdrawalsRoot = await Block . genWithdrawalsTrieRoot ( this . withdrawals ! )
502
- return withdrawalsRoot . equals ( this . header . withdrawalsRoot ! )
501
+ return equalsBytes ( withdrawalsRoot , this . header . withdrawalsRoot ! )
503
502
}
504
503
505
504
/**
@@ -523,7 +522,7 @@ export class Block {
523
522
}
524
523
525
524
// 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 ( ) ) )
527
526
if ( ! ( new Set ( uncleHashes ) . size === uncleHashes . length ) ) {
528
527
const msg = this . _errorMsg ( 'duplicate uncles' )
529
528
throw new Error ( msg )
@@ -572,7 +571,7 @@ export class Block {
572
571
public errorStr ( ) {
573
572
let hash = ''
574
573
try {
575
- hash = bufferToHex ( this . hash ( ) )
574
+ hash = bytesToHex ( this . hash ( ) )
576
575
} catch ( e : any ) {
577
576
hash = 'error'
578
577
}
0 commit comments