Reward-Schedule

了解確定 nSubsidy 的按位左移邏輯

  • December 30, 2019

我試圖了解比特幣 v0.01 上的>>=操作實際上如何每 4 年將補貼減少一半。nSubsidy >>= (nBestHeight / 210000);

很清楚 210000 個區塊的平均時間是 4 年左右,以及如何劃分最後一個區塊高度將返回一個整數,該整數將為我們提供它需要減半的時間。令人困惑的是,僅僅稍微改變一下nSubsidy就會給我們想要的補貼。

我編寫了這個循環來幫助視覺化位的移位,但仍然不清楚在二進制中,一個簡單的 1 位移位如何為我們提供所需的結果。

#include <iostream>
#include <bitset>

int main()
{
   uint64_t COIN = 100000000;
   uint64_t nSubsidy; 
   std::string binary;

   //halving and non halving block height examples
   uint64_t nBestHeight[] = {-1, 0, 1, 210000, 210001, 420000, 420001, 630000, 630001, 840000, 840001, 1050000, 1050001};

   for(unsigned int a = 0; a < sizeof(nBestHeight)/sizeof(nBestHeight[0]); a = a + 1 )
   { 
       nSubsidy = 50 * COIN;

       //Shift bits
       nSubsidy >>= (nBestHeight[a] / 210000);

       //Convert to binary to visualize bit shift
       binary = std::bitset<64>(nSubsidy).to_string();

       std::cout << "nSubsidy (64 bit binary) = " << binary << "    |    (nBestHeight/210000) = " << (nBestHeight[a] / 210000) << "    |    nSubsidy = " << nSubsidy << "    |    nBestHeight = " << nBestHeight[a] << "\n";
   }

}

/*

OUTPUT

BINARY TO VISUALIZE BIT SHIFT                                                                       HALVING ERA                      MINING REWARD                 CURRENT BLOCK HEIGHT
nSubsidy (64 bit binary) = 0000000000000000000000000000000100101010000001011111001000000000    |    (nBestHeight/210000) = 0    |    nSubsidy = 5000000000    |    nBestHeight = 0
nSubsidy (64 bit binary) = 0000000000000000000000000000000100101010000001011111001000000000    |    (nBestHeight/210000) = 0    |    nSubsidy = 5000000000    |    nBestHeight = 1
nSubsidy (64 bit binary) = 0000000000000000000000000000000010010101000000101111100100000000    |    (nBestHeight/210000) = 1    |    nSubsidy = 2500000000    |    nBestHeight = 210000
nSubsidy (64 bit binary) = 0000000000000000000000000000000010010101000000101111100100000000    |    (nBestHeight/210000) = 1    |    nSubsidy = 2500000000    |    nBestHeight = 210001
nSubsidy (64 bit binary) = 0000000000000000000000000000000001001010100000010111110010000000    |    (nBestHeight/210000) = 2    |    nSubsidy = 1250000000    |    nBestHeight = 420000
nSubsidy (64 bit binary) = 0000000000000000000000000000000001001010100000010111110010000000    |    (nBestHeight/210000) = 2    |    nSubsidy = 1250000000    |    nBestHeight = 420001
nSubsidy (64 bit binary) = 0000000000000000000000000000000000100101010000001011111001000000    |    (nBestHeight/210000) = 3    |    nSubsidy = 625000000     |    nBestHeight = 630000
nSubsidy (64 bit binary) = 0000000000000000000000000000000000100101010000001011111001000000    |    (nBestHeight/210000) = 3    |    nSubsidy = 625000000     |    nBestHeight = 630001
nSubsidy (64 bit binary) = 0000000000000000000000000000000000010010101000000101111100100000    |    (nBestHeight/210000) = 4    |    nSubsidy = 312500000     |    nBestHeight = 840000
nSubsidy (64 bit binary) = 0000000000000000000000000000000000010010101000000101111100100000    |    (nBestHeight/210000) = 4    |    nSubsidy = 312500000     |    nBestHeight = 840001
nSubsidy (64 bit binary) = 0000000000000000000000000000000000001001010100000010111110010000    |    (nBestHeight/210000) = 5    |    nSubsidy = 156250000     |    nBestHeight = 1050000
nSubsidy (64 bit binary) = 0000000000000000000000000000000000001001010100000010111110010000    |    (nBestHeight/210000) = 5    |    nSubsidy = 156250000     |    nBestHeight = 1050001

*/

真的只是向右或向左移動一點,我們就複製了一半的值嗎?是否有任何資源或任何其他術語可以更好地驗證這一點?上述假設是否正確?

程式碼輸出

這不是一個真正的比特幣問題,只是一個數學問題,但無論如何我都會試一試。

讓我們看看以 10 為底的幾個數字的二進制值

0    | 0
1    | 1
2    | 10
3    | 11
4    | 100
5    | 101
6    | 110
7    | 111
8    | 1000
9    | 1001
10   | 1010

注意 2 的冪如何設置 (nth+1) 位(從右到左讀取時) - 2^0 設置第 1 位,2^1 設置第二位,2^2 設置第三位,依此類推。

由於 2 的冪只是前一個 2 的冪乘以 2,因此右移一位可實現除以 2。

例如,如果我們將 8(二進制 1000)移動 1 位:

1000 >> 1 -> 100

類似地將 1010(以 10 為基數的 10)移動一位,我們得到:

1010 >> 1 -> 101

這產生 101,即以 10 為底的 5。

自然地,這會在地板形式上產生一些精度損失。例如,如果我們嘗試使用 9(二進制為 1001):

1001 >> 1 -> 100

我們得到 100,即 4 - 來自 9/2 的 0.5 失去了。

但是,由於比特幣的內部定義是 Satoshis,而不是 BTC,我們不會損失任何精度,直到獎勵時代 11,從區塊 2,100,000 開始,此時我們將因精度損失而損失 0.5 satoshi。

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