diff --git a/core/blockchain.go b/core/blockchain.go index e956af0fa9..c0fd15e776 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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 diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index d06d60cc7c..baa3df7ef6 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -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 { @@ -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 @@ -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] @@ -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. diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index 89b50036c4..834cbcd16e 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -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 }