Skip to content

Commit

Permalink
core: add a helper function to override the default prune period (#610)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
minh-bq authored Oct 21, 2024
1 parent f230174 commit e6d313b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 11 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ type BlockChain struct {
shouldStoreInternalTxs bool
enableAdditionalChainEvent bool
evmHook vm.EVMHook

blobPrunePeriod uint64
}

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

blobSidecarsCache: blobSidecarsCache,
blobPrunePeriod: params.BlobPrunePeriod,
}
bc.validator = NewBlockValidator(chainConfig, bc, engine)
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
Expand Down Expand Up @@ -452,6 +455,12 @@ func (bc *BlockChain) GetHook() vm.EVMHook {
return bc.evmHook
}

// setBlobPrunePeriod is used in tests to override the default prune
// period for easy testing
func (bc *BlockChain) setBlobPrunePeriod(period uint64) {
bc.blobPrunePeriod = period
}

func (bc *BlockChain) loadLatestDirtyAccounts() {
dirtyStateAccounts := rawdb.ReadDirtyAccounts(bc.db)
for _, data := range dirtyStateAccounts {
Expand Down Expand Up @@ -1471,10 +1480,10 @@ func (bc *BlockChain) reorgNeeded(localBlock *types.Block, localTd *big.Int, ext

// pruneBlockSidecars prunes the sidecars of blocks that are older than the keep period
func (bc *BlockChain) pruneBlockSidecars(db ethdb.KeyValueWriter, curBlock *types.Block) {
if curBlock.NumberU64() < uint64(params.BlobPrunePeriod) {
if curBlock.NumberU64() < uint64(bc.blobPrunePeriod) {
return
}
pruneBlockNumber := curBlock.NumberU64() - uint64(params.BlobPrunePeriod)
pruneBlockNumber := curBlock.NumberU64() - uint64(bc.blobPrunePeriod)
pruneBlockHash := bc.GetCanonicalHash(pruneBlockNumber)
rawdb.DeleteBlobSidecars(db, pruneBlockHash, pruneBlockNumber)
}
Expand Down
12 changes: 7 additions & 5 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4224,6 +4224,7 @@ func TestInsertChainWithSidecars(t *testing.T) {
}

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

// Insert BlobPrunePeriod blocks
Expand All @@ -4257,12 +4259,12 @@ func TestSidecarsPruning(t *testing.T) {
}
// Just nSidecarsToPrune first blocks have sidecars for testing
nSidecarsToPrune := 10
sidecars := make([][]*types.BlobTxSidecar, params.BlobPrunePeriod+1)
sidecars := make([][]*types.BlobTxSidecar, prunePeriod+1)
for i := 0; i < nSidecarsToPrune; i++ {
sidecars[i] = sidecar
}

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

// Check if all blobs are not pruned
for i := 1; i <= nSidecarsToPrune; i++ {
Expand All @@ -4304,11 +4306,11 @@ func TestSidecarsPruning(t *testing.T) {
}
blocks = append(blocks, newBlock...)
// Check if the oldest block's sidecars are pruned
pruneBlockNumber := curBlockNumber - params.BlobPrunePeriod
pruneBlockNumber := curBlockNumber - prunePeriod
pruneBlockHash := chain.GetBlockByNumber(uint64(pruneBlockNumber)).Hash()
sidecars := rawdb.ReadBlobSidecars(chain.db, pruneBlockHash, uint64(pruneBlockNumber))
if sidecars != nil {
t.Fatalf("Sidecars should be pruned at block %d", curBlockNumber-params.BlobPrunePeriod)
t.Fatalf("Sidecars should be pruned at block %d", curBlockNumber-prunePeriod)
}
}
}

0 comments on commit e6d313b

Please sign in to comment.