Bitcoin-Core-Development

mapBlocksUnknownParent 變數是什麼?

  • July 8, 2022

我正在審查這個拉取請求。這是試圖刪除全域變數mapBlocksUnknownParent我在這裡找到了定義。在CChainState::LoadExternalBlockFile函式中。

   // Map of disk positions for blocks with unknown parent (only used for reindex)
   static std::multimap<uint256, FlatFilePos> mapBlocksUnknownParent;

問題:

  1. mapBlocksUnknownParent變數是什麼?它在哪裡有用?
  2. 函式有什麼LoadExternalBlockFile作用?(通過它的名字,我推斷它blk???.dat在啟動時讀取文件並將它們載入到記憶體中,但我不確定)
  3. 什麼時候可以有未知父母的街區?雖然 AFAIK 我們總是通過他們的祖先而不是後代到達區塊。那麼在哪種情況下我們有一個未知父級的塊?
  4. 為什麼在函式範圍內定義此變數時將其視為全域變數?這是因為變數是static嗎?
  1. mapBlocksUnknownParent變數是什麼?它在哪裡有用?

It keeps a map of blocks in the blk*.dat files whose parent is unknown, indexed by the block hash of that parent.

  1. What does LoadExternalBlockFile function do? (by its name I deduce that it reads blk???.dat files and load them into the memory at the start up. But I’m not sure)

It deals with importing blocks from external files. This means not from the network, and not blocks already in our block database. It happens in two contexts:

  • When a file is specified on the command line using -loadblock=<FILE>; this can be used to import blocks e.g. given to you on portable media (say, a USB stick).
  • When running with -reindex, in which case the block database is wiped, but the blk*.dat files are kept, and then this “importing” operation is run on these still existing blk*.dat files.
  1. When is it possible to have blocks with unknown parents? While AFAIK we always reach blocks through their ancestors, not descendants. So in which situation we have a block with unknown parent?

«««< Updated upstream

When requesting blocks from the network, we only ask for blocks whose parents are known (at least the headers must be known), so indeed, this variable isn’t useful in that context. But the blocks are not downloaded in order, and the blk*.dat files contain blocks in the order they were received. ======= 當從網路請求塊時,我們隻請求其父節點已知的塊(至少必須知道標頭),所以實際上,這個變數在這種情況下沒有用。但是這些塊不是按順序下載的,blk*.dat 文件按照接收順序包含塊。

Stashed changes

This means that when using -reindex to rebuild the database, some blocks will be encountered out-of-order, meaning blocks will be read whose parent isn’t known yet. To deal with this situation, LoadExternalBlockFile keeps track of these blocks without known parent. When the parent is encountered and processed, the children will be processed too.

  1. 為什麼在函式範圍內定義此變數時將其視為全域變數?這是因為變數是static嗎?

確切地。static函式作用域中的變數實際上是只能從該函式訪問的全域變數。只有一個變數實例存在,在該函式的所有呼叫之間共享。

引用自:https://bitcoin.stackexchange.com/questions/114391