Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit e6d313b

Browse files
authored
core: add a helper function to override the default prune period (#610)
The default blob prune period is 518400-block long, which makes unit test take so much time to generate and import those blocks. This commit adds a helper function to override the default value which can be used by tests.
1 parent f230174 commit e6d313b

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

core/blockchain.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ type BlockChain struct {
230230
shouldStoreInternalTxs bool
231231
enableAdditionalChainEvent bool
232232
evmHook vm.EVMHook
233+
234+
blobPrunePeriod uint64
233235
}
234236

235237
type futureBlock struct {
@@ -284,6 +286,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
284286
shouldStoreInternalTxs: rawdb.ReadStoreInternalTransactionsEnabled(db),
285287

286288
blobSidecarsCache: blobSidecarsCache,
289+
blobPrunePeriod: params.BlobPrunePeriod,
287290
}
288291
bc.validator = NewBlockValidator(chainConfig, bc, engine)
289292
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
@@ -452,6 +455,12 @@ func (bc *BlockChain) GetHook() vm.EVMHook {
452455
return bc.evmHook
453456
}
454457

458+
// setBlobPrunePeriod is used in tests to override the default prune
459+
// period for easy testing
460+
func (bc *BlockChain) setBlobPrunePeriod(period uint64) {
461+
bc.blobPrunePeriod = period
462+
}
463+
455464
func (bc *BlockChain) loadLatestDirtyAccounts() {
456465
dirtyStateAccounts := rawdb.ReadDirtyAccounts(bc.db)
457466
for _, data := range dirtyStateAccounts {
@@ -1471,10 +1480,10 @@ func (bc *BlockChain) reorgNeeded(localBlock *types.Block, localTd *big.Int, ext
14711480

14721481
// pruneBlockSidecars prunes the sidecars of blocks that are older than the keep period
14731482
func (bc *BlockChain) pruneBlockSidecars(db ethdb.KeyValueWriter, curBlock *types.Block) {
1474-
if curBlock.NumberU64() < uint64(params.BlobPrunePeriod) {
1483+
if curBlock.NumberU64() < uint64(bc.blobPrunePeriod) {
14751484
return
14761485
}
1477-
pruneBlockNumber := curBlock.NumberU64() - uint64(params.BlobPrunePeriod)
1486+
pruneBlockNumber := curBlock.NumberU64() - uint64(bc.blobPrunePeriod)
14781487
pruneBlockHash := bc.GetCanonicalHash(pruneBlockNumber)
14791488
rawdb.DeleteBlobSidecars(db, pruneBlockHash, pruneBlockNumber)
14801489
}

core/blockchain_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,6 +4224,7 @@ func TestInsertChainWithSidecars(t *testing.T) {
42244224
}
42254225

42264226
func TestSidecarsPruning(t *testing.T) {
4227+
var prunePeriod uint64 = 1000
42274228
privateKey, _ := crypto.GenerateKey()
42284229
address := crypto.PubkeyToAddress(privateKey.PublicKey)
42294230
chainConfig := params.TestChainConfig
@@ -4243,6 +4244,7 @@ func TestSidecarsPruning(t *testing.T) {
42434244
if err != nil {
42444245
t.Fatalf("Failed to create blockchain, err %s", err)
42454246
}
4247+
chain.setBlobPrunePeriod(prunePeriod)
42464248
signer := types.NewCancunSigner(chainConfig.ChainID)
42474249

42484250
// Insert BlobPrunePeriod blocks
@@ -4257,12 +4259,12 @@ func TestSidecarsPruning(t *testing.T) {
42574259
}
42584260
// Just nSidecarsToPrune first blocks have sidecars for testing
42594261
nSidecarsToPrune := 10
4260-
sidecars := make([][]*types.BlobTxSidecar, params.BlobPrunePeriod+1)
4262+
sidecars := make([][]*types.BlobTxSidecar, prunePeriod+1)
42614263
for i := 0; i < nSidecarsToPrune; i++ {
42624264
sidecars[i] = sidecar
42634265
}
42644266

4265-
blocks, _ := GenerateChain(chainConfig, genesis, engine, db, params.BlobPrunePeriod, func(i int, bg *BlockGen) {
4267+
blocks, _ := GenerateChain(chainConfig, genesis, engine, db, int(prunePeriod), func(i int, bg *BlockGen) {
42664268
if i < nSidecarsToPrune {
42674269
tx, err := types.SignNewTx(privateKey, signer, &types.BlobTx{
42684270
ChainID: uint256.MustFromBig(chainConfig.ChainID),
@@ -4284,7 +4286,7 @@ func TestSidecarsPruning(t *testing.T) {
42844286
if err != nil {
42854287
t.Fatal(err)
42864288
}
4287-
curBlockNumber := params.BlobPrunePeriod
4289+
curBlockNumber := prunePeriod
42884290

42894291
// Check if all blobs are not pruned
42904292
for i := 1; i <= nSidecarsToPrune; i++ {
@@ -4304,11 +4306,11 @@ func TestSidecarsPruning(t *testing.T) {
43044306
}
43054307
blocks = append(blocks, newBlock...)
43064308
// Check if the oldest block's sidecars are pruned
4307-
pruneBlockNumber := curBlockNumber - params.BlobPrunePeriod
4309+
pruneBlockNumber := curBlockNumber - prunePeriod
43084310
pruneBlockHash := chain.GetBlockByNumber(uint64(pruneBlockNumber)).Hash()
43094311
sidecars := rawdb.ReadBlobSidecars(chain.db, pruneBlockHash, uint64(pruneBlockNumber))
43104312
if sidecars != nil {
4311-
t.Fatalf("Sidecars should be pruned at block %d", curBlockNumber-params.BlobPrunePeriod)
4313+
t.Fatalf("Sidecars should be pruned at block %d", curBlockNumber-prunePeriod)
43124314
}
43134315
}
43144316
}

0 commit comments

Comments
 (0)