Skip to content

Commit

Permalink
freezer: refactor write ancient blocks logic;
Browse files Browse the repository at this point in the history
  • Loading branch information
galaio authored and buddh0 committed Mar 28, 2024
1 parent c6d219f commit cd1f42a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [

// Write all chain data to ancients.
td := bc.GetTd(first.Hash(), first.NumberU64())
writeSize, err := rawdb.WriteAncientBlocks(bc.db, blockChain, receiptChain, td)
writeSize, err := rawdb.WriteAncientBlocksWithBlobs(bc.db, blockChain, receiptChain, td)
if err != nil {
log.Error("Error importing chain data to ancients", "err", err)
return 0, err
Expand Down
51 changes: 23 additions & 28 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,8 @@ func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) {
WriteHeader(db, block.Header())
}

// WriteAncientBlocks writes entire block data into ancient store and returns the total written size.
func WriteAncientBlocks(db ethdb.AncientWriter, blocks []*types.Block, receipts []types.Receipts, td *big.Int) (int64, error) {
// WriteAncientBlocksWithBlobs writes entire block data with blobs into ancient store and returns the total written size.
func WriteAncientBlocksWithBlobs(db ethdb.AncientWriter, blocks []*types.Block, receipts []types.Receipts, td *big.Int) (int64, error) {
// find cancun index, it's used for new added blob ancient table
cancunIndex := -1
for i, block := range blocks {
Expand All @@ -811,34 +811,21 @@ func WriteAncientBlocks(db ethdb.AncientWriter, blocks []*types.Block, receipts
log.Info("WriteAncientBlocks", "startAt", blocks[0].Number(), "cancunIndex", cancunIndex, "len", len(blocks))

var (
tdSum = new(big.Int).Set(td)
stReceipts []*types.ReceiptForStorage
preSize int64
err error
tdSum = new(big.Int).Set(td)
preSize int64
err error
)

// handle pre-cancun blocks
if cancunIndex > 0 {
preSize, err = db.ModifyAncients(func(op ethdb.AncientWriteOp) error {
for i, block := range blocks[:cancunIndex] {
// Convert receipts to storage format and sum up total difficulty.
stReceipts = stReceipts[:0]
for _, receipt := range receipts[i] {
stReceipts = append(stReceipts, (*types.ReceiptForStorage)(receipt))
}
header := block.Header()
if i > 0 {
tdSum.Add(tdSum, header.Difficulty)
}
if err := writeAncientBlock(op, block, header, stReceipts, tdSum); err != nil {
return err
}
}
return nil
})
preSize, err = WriteAncientBlocks(db, blocks[:cancunIndex], receipts[:cancunIndex], td)
if err != nil {
return preSize, err
}
for i, block := range blocks[:cancunIndex] {
if i > 0 {
tdSum.Add(tdSum, block.Difficulty())
}
}
tdSum.Add(tdSum, blocks[cancunIndex].Difficulty())
}

// It will reset blob ancient table at cancunIndex
Expand All @@ -847,9 +834,19 @@ func WriteAncientBlocks(db ethdb.AncientWriter, blocks []*types.Block, receipts
return 0, err
}
blocks = blocks[cancunIndex:]
receipts = receipts[cancunIndex:]
}
postSize, err := WriteAncientBlocks(db, blocks, receipts, tdSum)
return preSize + postSize, err
}

postSize, err := db.ModifyAncients(func(op ethdb.AncientWriteOp) error {
// WriteAncientBlocks writes entire block data into ancient store and returns the total written size.
func WriteAncientBlocks(db ethdb.AncientWriter, blocks []*types.Block, receipts []types.Receipts, td *big.Int) (int64, error) {
var (
tdSum = new(big.Int).Set(td)
stReceipts []*types.ReceiptForStorage
)
return db.ModifyAncients(func(op ethdb.AncientWriteOp) error {
for i, block := range blocks {
// Convert receipts to storage format and sum up total difficulty.
stReceipts = stReceipts[:0]
Expand All @@ -866,8 +863,6 @@ func WriteAncientBlocks(db ethdb.AncientWriter, blocks []*types.Block, receipts
}
return nil
})

return preSize + postSize, err
}

// ReadBlobSidecarsRLP retrieves all the transaction blobs belonging to a block in RLP encoding.
Expand Down
2 changes: 1 addition & 1 deletion core/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func (p *BlockPruner) backUpOldDb(name string, cache, handles int, namespace str
blobs := rawdb.ReadBlobSidecars(chainDb, blockHash, blockNumber)
block = block.WithSidecars(blobs)
// Write into new ancient_back db.
if _, err := rawdb.WriteAncientBlocks(frdbBack, []*types.Block{block}, []types.Receipts{receipts}, td); err != nil {
if _, err := rawdb.WriteAncientBlocksWithBlobs(frdbBack, []*types.Block{block}, []types.Receipts{receipts}, td); err != nil {
log.Error("failed to write new ancient", "error", err)
return err
}
Expand Down

0 comments on commit cd1f42a

Please sign in to comment.