Bitcoin Halving: A Technical Deep Dive

Written by bitcoin-in-action | Published 2020/12/06
Tech Story Tags: bitcoin | bitcoin-halving | mining | cryptocurrency-mining | btc | rpc | bitcoin-spotlight | bitcoin-adoption

TLDRvia the TL;DR App

Today (11 may 2020) it's a very important day for Bitcoin. Today we will have the halving!
The halving is a recurrent event, every 4 years the reward that the miner gets to resolve the Proof of Work is being cut in half.
More precisely: the reward is being cut in half every 210.000 blocks. A block is mined every 10 minutes. That's why 210.000 blocks are equivalent to 1458 days, that are equivalent to 3.99 years!
At this very moment, the blockchain has 629.992 blocks. When a miner will find a new block with the height 630.000, 12 years will pass from the genesis block, and the reward will be 6.25 bitcoins per block instead of 12.5 bitcoins.
We can reproduce the same behavior in the regtest enviroment.

Please note: in regtest the halving occurred on different height. We need to mine 449 blocks in order to have the same scenario as the mainnet. At the block with height 449, the reward is 12.5 bitcoins, when we mining another block, the reward will be 6.25 bitcoins.
In Action
First of all, we need to use the regtest environment with no blocks.
$ bitcoin-cli getblockchaininfo
{
  "chain": "regtest",
  "blocks": 0,
  "headers": 0,
  "bestblockhash": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206",
  "difficulty": 4.656542373906925e-10,
  "mediantime": 1296688602,
  "verificationprogress": 1,
  "initialblockdownload": true,
  "chainwork": "0000000000000000000000000000000000000000000000000000000000000002",
....
We can mine 449 blocks using the generatetoaddress command.
bitcoin-cli generatetoaddress 449 $(bitcoin-cli getnewaddress '' 'bech32')
We get a lot of block hash, we can explore the last one and get its coinbase. The coinbase is always the first transaction of the block. It's a particular transaction that the miner creates for itself to get the reward.
$ LAST_BLOCK_HASH=$(bitcoin-cli getbestblockhash)

$ TX=$(bitcoin-cli getblock $LAST_BLOCK_HASH | jq -r '.tx | last')

$ bitcoin-cli getrawtransaction $TX 2

{
  "txid": "7fb99af2ab497b94a6de2dcd91d3a5a97d31eaf8d0f8dd43ff4100a2f78efd8a",
  "hash": "ee38f615f26776913cf1b2a7a351c4f80e1777cbf3950fc22be42c2eafa7450b",
  "version": 2,
  "size": 170,
  "vsize": 143,
  "weight": 572,
  "locktime": 0,
  "vin": [
    {
      "coinbase": "02c1010101",
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 12.50000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 5f04362ef3474238aba0b9691c754285ad5dc735",
        "hex": "00145f04362ef3474238aba0b9691c754285ad5dc735",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1qtuzrvthngapr32aqh953ca2zskk4m3e42nqdtz"
        ]
      }
    },
    {
      "value": 0.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_RETURN aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
        "hex": "6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
        "type": "nulldata"
      }
    }
  ],
  "hex": "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0502c1010101ffffffff02807c814a000000001600145f04362ef3474238aba0b9691c754285ad5dc7350000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000",
  "blockhash": "1eb8af036a9b6613c10d1148ef64ee2c86f166085715f4d01c89907fa5086296",
  "confirmations": 1,
  "time": 1606386368,
  "blocktime": 1606386368
}
We can read "value": 12.50000000. It's the reward. Now let's go mining another block in order to replicate the same mainnet's behaviour.
$ bitcoin-cli generatetoaddress 1 $(bitcoin-cli getnewaddress '' 'bech32')

$ LAST_BLOCK_HASH=$(bitcoin-cli getbestblockhash)

$ TX=$(bitcoin-cli getblock $LAST_BLOCK_HASH | jq -r '.tx | last')

$ bitcoin-cli getrawtransaction $TX 2
{
  "txid": "2c8a9cbe986cd3a05aff1cb70183e8e9489916fd2e3dfe73a0772e99edc899c0",
  "hash": "5c94f6bdbd32032f4ae3d5c6c4dde014e7bb60a7cda9151ea253b3856c2f5b0e",
  "version": 2,
  "size": 170,
  "vsize": 143,
  "weight": 572,
  "locktime": 0,
  "vin": [
    {
      "coinbase": "02c2010101",
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 6.25000000,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 1f638364fc2154626cfeb2ffdbed23289a037043",
        "hex": "00141f638364fc2154626cfeb2ffdbed23289a037043",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1qra3cxe8uy92xym87ktlahmfr9zdqxuzryctd4h"
        ]
      }
    },
    {
      "value": 0.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_RETURN aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
        "hex": "6a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf9",
        "type": "nulldata"
      }
    }
  ],
  "hex": "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0502c2010101ffffffff0240be4025000000001600141f638364fc2154626cfeb2ffdbed23289a0370430000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000",
  "blockhash": "04eec4981e77406bb79284f89dd3afa7f028615c4e1e4fa2bed2abc8cc28f11f",
  "confirmations": 1,
  "time": 1606386591,
  "blocktime": 1606386591
}
We can read the "value": 6.25000000, and the halving is complete!
Keep in mind that: first of all, you can find the code to our repository Point number 2 do not miss this video in italian! ๐Ÿ‡ฎ๐Ÿ‡น
Stay tuned to this station and get ready for the next article.
YO ๐ŸคŸ๐Ÿป
๐Ÿ™ GitHub: https://bit.ly/2Lj3yeY
โ–บ ITA: Twitterย , Facebook, Medium, Instagram, Youtube, GitHub
โ–บ ENG: Twitterย , Facebook, Medium, Instagram, Youtube, GitHub
* Television isnโ€™t a good idea (Radio Stations)
* Email isnโ€™t a good idea (Post offices)
* Amazon isnโ€™t a good idea (Retail stores)
* Bitcoin isnโ€™t a good idea (Central banks)
* In crypto we trust

Written by bitcoin-in-action | See on https://bitcoininaction.com
Published by HackerNoon on 2020/12/06