Skip to content

BlockChain.InsertHeadersBeforeCutoff (TD)

Source (upstream v1.17.3) Current
File core/blockchain.go blockchain.go
Symbol BlockChain.InsertHeadersBeforeCutoff BlockChain.InsertHeadersBeforeCutoff
Ref v1.17.3 etc/v1.17.3-full-node

Modified in-place on upstream v1.17.3: uses WriteAncientBlocksPoW for genesis and WriteAncientHeaderChainPoW for headers with real TD. Function only exists post-nuke (pruning mode sync PR) — diff is against upstream v1.17.3.

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

--- a/core/blockchain.go
+++ b/core/blockchain.go
@@ -22,7 +22,9 @@
        first     = headers[0].Number.Uint64()
    )
    if first == 1 && frozen == 0 {
-       _, err := rawdb.WriteAncientBlocks(bc.db, []*types.Block{bc.genesisBlock}, []rlp.RawValue{rlp.EmptyList})
+       // ETC: use PoW version to write real TD
+       td := bc.genesisBlock.Difficulty()
+       _, err := rawdb.WriteAncientBlocksPoW(bc.db, []*types.Block{bc.genesisBlock}, []rlp.RawValue{rlp.EmptyList}, td)
        if err != nil {
            log.Error("Error writing genesis to ancients", "err", err)
            return 0, err
@@ -32,9 +34,28 @@
        return 0, fmt.Errorf("headers are gapped with the ancient store, first: %d, ancient: %d", first, frozen)
    }

+   // ETC: compute parent TD for the header chain and use PoW version if
+   // available. On PoW chains a missing parent TD is corruption / unknown
+   // ancestor; the no-TD writer would leave the freezer inconsistent.
+   var td *big.Int
+   if first == 1 {
+       td = new(big.Int).Add(bc.genesisBlock.Difficulty(), headers[0].Difficulty)
+   } else {
+       parentTd := bc.GetTd(headers[0].ParentHash, first-1)
+       if parentTd != nil {
+           td = new(big.Int).Add(parentTd, headers[0].Difficulty)
+       }
+   }
    // Write headers to the ancient store, with block bodies and receipts set to nil
    // to ensure consistency across tables in the freezer.
-   _, err := rawdb.WriteAncientHeaderChain(bc.db, headers)
+   var err error
+   if td != nil {
+       _, err = rawdb.WriteAncientHeaderChainPoW(bc.db, headers, td)
+   } else if bc.chainConfig.IsPow() {
+       return 0, consensus.ErrUnknownAncestor
+   } else {
+       _, err = rawdb.WriteAncientHeaderChain(bc.db, headers)
+   }
    if err != nil {
        return 0, err
    }

← Total Difficulty (TD)