Skip to content

WriteAncientHeaderChainPoW

Source (upstream v1.17.3) Current
File core/rawdb/accessors_chain.go accessors_chain_pow.go
Symbol WriteAncientHeaderChain WriteAncientHeaderChainPoW
Ref v1.17.3 etc/v1.17.3-full-node

Based on upstream v1.17.3 WriteAncientHeaderChain; adds td *big.Int param and writes TD to ChainFreezerDifficultyTable.

Diff — vs upstream v1.17.3 (modified in-place)

--- a/core/rawdb/accessors_chain.go
+++ b/core/rawdb/accessors_chain_pow.go
@@ -1,10 +1,11 @@
-// WriteAncientHeaderChain writes the supplied headers along with nil block
-// bodies and receipts into the ancient store. It's supposed to be used for
-// storing chain segment before the chain cutoff.
-func WriteAncientHeaderChain(db ethdb.AncientWriter, headers []*types.Header) (int64, error) {
+// WriteAncientHeaderChainPoW writes headers with proper TD into ancient store.
+// td is the total difficulty of the FIRST header (including its own difficulty).
+func WriteAncientHeaderChainPoW(db ethdb.AncientWriter, headers []*types.Header, td *big.Int) (int64, error) {
+   tdSum := new(big.Int).Sub(td, headers[0].Difficulty)
    return db.ModifyAncients(func(op ethdb.AncientWriteOp) error {
        for _, header := range headers {
            num := header.Number.Uint64()
+           tdSum.Add(tdSum, header.Difficulty)
            if err := op.AppendRaw(ChainFreezerHashTable, num, header.Hash().Bytes()); err != nil {
                return fmt.Errorf("can't add block %d hash: %v", num, err)
            }
@@ -17,6 +18,9 @@
            if err := op.AppendRaw(ChainFreezerReceiptTable, num, nil); err != nil {
                return fmt.Errorf("can't append block %d receipts: %v", num, err)
            }
+           if err := op.Append(ChainFreezerDifficultyTable, num, new(big.Int).Set(tdSum)); err != nil {
+               return fmt.Errorf("can't append block %d total difficulty: %v", num, err)
+           }
        }
        return nil
    })

← Total Difficulty (TD)