@@ -12,6 +12,7 @@ import {
12
12
commitmentsToVersionedHashes ,
13
13
getBlobs ,
14
14
hexToBytes ,
15
+ intToHex ,
15
16
randomBytes ,
16
17
} from '@ethereumjs/util'
17
18
import { createVM } from '@ethereumjs/vm'
@@ -28,7 +29,9 @@ import { mockBlockchain } from '../rpc/mockBlockchain.js'
28
29
29
30
import type { Blockchain } from '@ethereumjs/blockchain'
30
31
import type { TypedTransaction } from '@ethereumjs/tx'
32
+ import type { PrefixedHexString } from '@ethereumjs/util'
31
33
import type { VM } from '@ethereumjs/vm'
34
+
32
35
const kzg = new microEthKZG ( trustedSetup )
33
36
34
37
const A = {
@@ -353,24 +356,48 @@ describe('[PendingBlock]', async () => {
353
356
} )
354
357
355
358
const { txPool } = setup ( )
359
+ txPool [ 'config' ] . chainCommon . setHardfork ( Hardfork . Cancun )
360
+
361
+ // fill up the blobsAndProofsByHash and proofs cache before adding a blob tx
362
+ // for cache pruning check
363
+ const fillBlobs = getBlobs ( 'hello world' )
364
+ const fillCommitments = blobsToCommitments ( kzg , fillBlobs )
365
+ const fillProofs = blobsToProofs ( kzg , fillBlobs , fillCommitments )
366
+ const fillBlobAndProof = { blob : fillBlobs [ 0 ] , proof : fillProofs [ 0 ] }
367
+
368
+ const blobGasLimit = txPool [ 'config' ] . chainCommon . param ( 'maxblobGasPerBlock' )
369
+ const blobGasPerBlob = txPool [ 'config' ] . chainCommon . param ( 'blobGasPerBlob' )
370
+ const allowedBlobsPerBlock = Number ( blobGasLimit / blobGasPerBlob )
371
+ const allowedLength = allowedBlobsPerBlock * txPool [ 'config' ] . blobsAndProofsCacheBlocks
372
+
373
+ for ( let i = 0 ; i < allowedLength ; i ++ ) {
374
+ // this is space efficient as same object is inserted in dummy positions
375
+ txPool . blobsAndProofsByHash . set ( intToHex ( i ) , fillBlobAndProof )
376
+ }
377
+ assert . equal ( txPool . blobsAndProofsByHash . size , allowedLength , 'fill the cache to capacity' )
356
378
357
- const blobs = getBlobs ( 'hello world' )
358
- const commitments = blobsToCommitments ( kzg , blobs )
359
- const blobVersionedHashes = commitmentsToVersionedHashes ( commitments )
360
- const proofs = blobsToProofs ( kzg , blobs , commitments )
361
-
362
- // Create 3 txs with 2 blobs each so that only 2 of them can be included in a build
379
+ // Create 2 txs with 3 blobs each so that only 2 of them can be included in a build
380
+ let blobs : PrefixedHexString [ ] = [ ] ,
381
+ proofs : PrefixedHexString [ ] = [ ] ,
382
+ versionedHashes : PrefixedHexString [ ] = [ ]
363
383
for ( let x = 0 ; x <= 2 ; x ++ ) {
384
+ // generate unique blobs different from fillBlobs
385
+ const txBlobs = [
386
+ ...getBlobs ( `hello world-${ x } 1` ) ,
387
+ ...getBlobs ( `hello world-${ x } 2` ) ,
388
+ ...getBlobs ( `hello world-${ x } 3` ) ,
389
+ ]
390
+ assert . equal ( txBlobs . length , 3 , '3 blobs should be created' )
391
+ const txCommitments = blobsToCommitments ( kzg , txBlobs )
392
+ const txBlobVersionedHashes = commitmentsToVersionedHashes ( txCommitments )
393
+ const txProofs = blobsToProofs ( kzg , txBlobs , txCommitments )
394
+
364
395
const txA01 = createBlob4844Tx (
365
396
{
366
- blobVersionedHashes : [
367
- ...blobVersionedHashes ,
368
- ...blobVersionedHashes ,
369
- ...blobVersionedHashes ,
370
- ] ,
371
- blobs : [ ...blobs , ...blobs , ...blobs ] ,
372
- kzgCommitments : [ ...commitments , ...commitments , ...commitments ] ,
373
- kzgProofs : [ ...proofs , ...proofs , ...proofs ] ,
397
+ blobVersionedHashes : txBlobVersionedHashes ,
398
+ blobs : txBlobs ,
399
+ kzgCommitments : txCommitments ,
400
+ kzgProofs : txProofs ,
374
401
maxFeePerBlobGas : 100000000n ,
375
402
gasLimit : 0xffffffn ,
376
403
maxFeePerGas : 1000000000n ,
@@ -381,6 +408,30 @@ describe('[PendingBlock]', async () => {
381
408
{ common } ,
382
409
) . sign ( A . privateKey )
383
410
await txPool . add ( txA01 )
411
+
412
+ // accumulate for verification
413
+ blobs = [ ...blobs , ...txBlobs ]
414
+ proofs = [ ...proofs , ...txProofs ]
415
+ versionedHashes = [ ...versionedHashes , ...txBlobVersionedHashes ]
416
+ }
417
+
418
+ assert . equal (
419
+ txPool . blobsAndProofsByHash . size ,
420
+ allowedLength ,
421
+ 'cache should be prune and stay at same size' ,
422
+ )
423
+ // check if blobs and proofs are added in txpool by versioned hashes
424
+ for ( let i = 0 ; i < versionedHashes . length ; i ++ ) {
425
+ const versionedHash = versionedHashes [ i ]
426
+ const blob = blobs [ i ]
427
+ const proof = proofs [ i ]
428
+
429
+ const blobAndProof = txPool . blobsAndProofsByHash . get ( versionedHash ) ?? {
430
+ blob : '0x0' ,
431
+ proof : '0x0' ,
432
+ }
433
+ assert . equal ( blob , blobAndProof . blob , 'blob should match' )
434
+ assert . equal ( proof , blobAndProof . proof , 'proof should match' )
384
435
}
385
436
386
437
// Add one other normal tx for nonce 3 which should also be not included in the build
0 commit comments