Bitcoin

ブロックチェーン、マイニング

More than 1 year has passed since last update.

ブロックを対象とする処理群

  • 不正を含まない台帳記録として、コンセンサスを確定する処理
  • プルーフ・オブ・ワークの付与によって記録の非可逆性を保証する処理
  • 分岐(フォーク)が可能な時系列的記録の中で、ベストなものをマイニングゲームによって選択する処理

ブロックの構造

サイズ フィールド名 説明
4バイト Block Size ブロックのデータサイズ
80バイト Block Header ブロックヘッダ
1〜9バイト Transaction Counter ブロックに含まれるトランザクション数
可変長 Transactions ブロックに含まれるトランザクションのリスト

ブロックの識別子(ブロックハッシュ、ブロック高)

  • 現在のブロックチェーンの先端
$ bitcoin-cli getchaintips
[
  {
    "height": 1209804, # ブロック高
    "hash": "00000000003f8e9c367da65c5dc722f26f632d21dc6ed7b3f10d43b0b4d6d26e", # ブロックハッシュ
    "branchlen": 0,
    "status": "active"
  },
  {
    "height": 1209730, # ブロック高
    "hash": "000000000000072055241f276cdbb1f125474fe478e44f7e4a330239c1d6ca4f", # ブロックハッシュ
    "branchlen": 1,
    "status": "valid-fork"
  }
]
  • 以前のブロックハッシュ
$ bitcoin-cli getblockhash 1209730
000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1

ブロックの本体

$ bitcoin-cli getblock 000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1
{
  "hash": "000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1",
  "confirmations": 75,
  "strippedsize": 41332,
  "size": 59515,
  "weight": 183511,
  "height": 1209730,
  "version": 536870912,
  "versionHex": "20000000",
  "merkleroot": "8cec087a633983350593c5613bfa1cdd8261337a997016495d3d7b9df8b9f052",
  "tx": [
    "4c82a543054837429d2fd3828c9424859235c53311f91a2131b42a3be6631960",
    "fac2f65388603baf42862036580de86b945c2402d73ec2df83b3b8da86a4e687",
    "3ffcfc32efd0dde34df55a742de0f7903ac2963fdb3366899f4b59fb7e84eb03",
    ・・・
    "84b239c645713694ca7b18207e1dec352d141da4c934fda9c0fc105e322bb3d1"
  ],
  "time": 1507421789,
  "mediantime": 1507415715,
  "nonce": 430028490,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000310c1d0e1ccf4abf7c",
  "previousblockhash": "00000000cd6f673999370ca9f570885c54f318c9f6e3f8ed135f1c73d0a7a0a6",
  "nextblockhash": "00000000001216475c736d229f28c166e8119d8f174a4a7879d22f8226636a6e"
}

ブロックヘッダの構造

バイト数 フィールド名 説明
4 version int32_t このブロックを正当化したブロック検証ルールのセットを表示するバージョン番号
32 previous block header hash char[32] 直前のヘッダのハッシュ値(SHA256(SHA256()))のInternal Byte Order(バイト単位で順序を反転)
32 merkle root hash char[32] このブロック内の全トランザクションをまとめたマークルルートハッシュ値(SHA256(SHA256()))
4 time uint32_t このブロックを作成したマイナーがマイニングを開始した時刻(Unix epoch time)
4 Difficulty Target uint32_t Proof of Work法の難易度ターゲット
4 Nonce uint32_t Proof of Work法のためのナンス
$ bitcoin-cli getblockheader 000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1
{
  "hash": "000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1",
  "confirmations": 76,
  "height": 1209730,
  "version": 536870912,
  "versionHex": "20000000",
  "merkleroot": "8cec087a633983350593c5613bfa1cdd8261337a997016495d3d7b9df8b9f052",
  "time": 1507421789,
  "mediantime": 1507415715,
  "nonce": 430028490,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000310c1d0e1ccf4abf7c",
  "previousblockhash": "00000000cd6f673999370ca9f570885c54f318c9f6e3f8ed135f1c73d0a7a0a6",
  "nextblockhash": "00000000001216475c736d229f28c166e8119d8f174a4a7879d22f8226636a6e"
}
  • 16進数表示
$ bitcoin-cli getblockheader 000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1 false
00000020a6a0a7d0731c5f13edf8e3f6c918f3545c8870f5a90c379939676fcd0000000052f0b9f89d7b3d5d491670997a336182dd1cfa3b61c59305358339637a08ec8c5d6ed959ffff001dcab6a119
  • ブロックハッシュ

SHA-256を2回作用させ、Internal Byte Orderで順序を反転したものをさらに逆順にする

irb(main):001:0> require 'digest'
=> true
irb(main):002:0> def dhash(data)
irb(main):003:1> Digest::SHA256.digest(Digest::SHA256.digest([data].pack("H*"))).unpack("h*")[0].reverse
irb(main):004:1> end
=> :dhash
irb(main):005:0> block_header = "00000020a6a0a7d0731c5f13edf8e3f6c918f3545c8870f5a90c379939676fcd0000000052f0b9f89d7b3d5d491670997a336182dd1cfa3b61c59305358339637a08ec8c5d6ed959ffff001dcab6a119"
irb(main):006:0> dhash(block_header)
=> "000000000002f85b50e40b11cbda847b9c22a1941153602bbd0dddc0b82479f1"

マークル木

  • 大規模データを効率的に要約し検証できるようにするデータ構造
  • 暗号学的なハッシュ値を含む二分木(binary tree)
  • 再帰的に葉ノードのペアから1つのハッシュ値を計算し、ハッシュ値が1つだけ残るまで続ける
  • 最後に残ったハッシュ値をマークルルートと呼ぶ
  • ハッシュアルゴリズムは、SHA256を2回適用したもので、double-SHA256
require 'digest'

def dhash(data)
  Digest::SHA256.digest(Digest::SHA256.digest([data].pack("H*").reverse)).reverse.unpack("H*")[0]
end

def merkle_root(nodes)
  if nodes.size == 1
    nodes[0]
  else
    merkle_root(nodes.each_slice(2).map{|x|
      if 
        x.size == 2 then dhash(x[1] + x[0])
      else 
        dhash(x[0] + x[0])
      end
    })
  end
end

TXID_LIST = [
  "4c82a543054837429d2fd3828c9424859235c53311f91a2131b42a3be6631960",
  "fac2f65388603baf42862036580de86b945c2402d73ec2df83b3b8da86a4e687",
  "3ffcfc32efd0dde34df55a742de0f7903ac2963fdb3366899f4b59fb7e84eb03",
  "fe0f947c5ee948774bfae0aba2ccf34c49437445b2925b4b8a987e524f31a419",
  "ec77838716e78c943d902208e7f97d35c17f9d6e11efc6c82b8198aaab01fe1c",
  "856680d642fbd1e4570143b2bdd203f6a444dfe999969f3075dd8a7bb5da5e23",
  "f154233d38a91c52a404213787edcd25ab3f402467ec5be2f39efa6ff50f6c68",
  "b7108259badee4f161f6cbe913d53d6511194d4489656897917b1d8a7ef78a70",
  "38dfe8a9cb9714dd7bf683a972a167f65a80a1253375a450fe59e0041adf817e",
  "9396d98d8a7f58d30439cd9671a8d91e202e5970850018201cf8121e369708a0",
  "1809b4fdb72078c0bd26d395f80c68e9eaf0a6eb16b60501947790c4d8dacfa0",
  "e544a789771af16a1946b226e9248791305369f8ef733ef962c99f6bd8bce9aa",
  "83baa37215ecddde5466a2e2c982cc1eb3b0a77a5ef76c964421e3afdb9f99c1",
  "c28fbd58b49162dc108942fee2410ca7ff0bd028fbf05f8ea79306fa81e87ec4",
  "685d25e7a1e9f6a15a95a0a24b3b44e50a5691cc073ce744e46de3a8e5283ccd",
  "21bab7d58e9fd708ae879d096a174b977e8b80d2c5a143f0b33ca1e63d900c5d",
  "a445bd1299d055a025db815146b5decadb7d0d94ebd17a4f73dab400efffde07",
  "7701c3a7e76ad5cf5967c44ad053c14f68ce7ca387eada82b440732964b8dcc8",
  "e710bada658302a6c048186d10dc699222bc4c7a764a96f3c010f713eca042e7",
  "a03e4c9812b393a29e61e50450c1f4b17db4197cf9b432c201f7fe1b2d21c5ec",
  "f4714e88bc8b2de684e60345971aae2496c0bb89115c73020709b10d0c0844f6",
  "20a1cfe1752f24d9d9f77f42aa304fee52799ab0c932a202b15fc0b5c2ebb0ee",
  "07459f6a5000ba97098d464fe408a715993f8c4875963f478e56cd6878e7099a",
  "020f0f4e0b18692799cd9e4f0d83662118c33c1bf5549e65c07c5c78f3f3c9ba",
  "1ae304afe5cdddccbe42a5f31c3132c9394d74911760a3a6ccb563c0bc5c3fb4",
  "2eb625f81e4119b9a2154386c7dfdf2e998a4c44b58f622805e49aaee143e272",
  "92746ae75efb4c1c489c7cd499c354b19a4ac76ed082d1f2a87379e3dca67ae2",
  "e135475290521fbc6dbcb3133f235ab80f81328e409120ce13b9dbda6b053f1b",
  "ce971bc7c5cd299a21ebfc35f424636e60ad74504fb5768cbe07d5bd9bfb1bb3",
  "6ec4167db21312a32a9db07eda8bae6e2f50c9136b78b5bd503de77bb84cb325",
  "21dfc016c6b144676a05910557fda470720a8ce03dbc44dda6c8a693e6532b4f",
  "ee9e0b74044098e6abc7795a88daeef589b6975b8492fe08fb2a4e1c80a2437a",
  "76ae344f56d5385b14b49efa6360d74c5fb34d2504d3483491711ca5a7c523fd",
  "3602a41f29490212e1164e3e96684f0c7a58bea0df2864f42881cb58b21f13cc",
  "f5229b733ff4d87e2b50558984344b3b01a94eb37eb0e184cf600afc818c21b1",
  "15b21c7fdaa7bbb515f75e72b929753c5e292d4cc67129cc5cf101c7637c9d13",
  "2b0f91a13fa745d2f6a94633fd19a0ca1de75feaf1f979ac5eeae6d34b258d15",
  "2c3f3b8d0a8e24420f3646c89879e896d57817e3846dadef5083e4f9cfb83c20",
  "419c1b5cf3c5621bb309dba17acace45e2cacbc88e6a82b8ed6c33479894b5b6",
  "8ddf92eb6ec597f120e97746170b903e493095254d4eddc7a81105cd2ece83cd",
  "45607d33d2f7eb1ebaec1ac9a19414b91e25ff845568f5dc726ff9cfdf55d31f",
  "6f82cb5c616cc455c84bd0370235c08cc89c1f0f4523ffe37ee8a4c3bae45616",
  "f9505f0d51712921c8cefd66dde96c472e4b2ff1d0f32b9be5d1b946a0301749",
  "69b2a11bda9ee01c5f0ad459a5256de7f569b31be0570b929ec3367fc77e6348",
  "c21335ac29c0b34d394468cfd93807375e445d246d17d81f64b481a80c0571bb",
  "73b38205abb9ddbc6cea1e8b935917e7815da228edf2952e1ff9ad7ab5ffa76c",
  "faecde353498e0ca918504b2dd4794aeee40aee355766c6bdd6e3be718ff638a",
  "8f55667df44b08e245fa011bf733a5f499fdd35e71782ac938f9713afdde8c20",
  "58f0cef7ba18b1259cce3bd7950ff5c34b11b9b046415111fadbc59f3ee79ae4",
  "0156d2636396a277fa648df2d125dd6ec63389985e9fe185fed7fd1ddbd02bf8",
  "0ede729b4b0ecba01b5ddbbd6909c56d0b383fa722c5c702b817c74dc1696d46",
  "7242ab28fd176018747935718639d8207d0bf5194bb9eb8ee2a1d3918018de06",
  "ba858d13ceaffe31160cfc22938e3e14c0a6eb39ff3a0783a62d74e0ab5ddc1f",
  "564e9782aeb8c82652d705df79fd5eb895ba11ca5f7fe5b5543c1973cd07a8fb",
  "83f1086f4c4e37dad842cbd5d7618797ad1940cc82c1284629171f5e8676e25b",
  "12e756ae1a34da2ec988f32f9af083f60d5bceeeb8d228b2098ec0066ad8599e",
  "eb8d762793a5f12b9117408d71af235e5c077192452047adb77081042ad768c7",
  "5d5314be099b49a748d6b5a894f28d62d41cd343373efc949aca7568b17f7483",
  "845a4db5431e1f817bdb8d27282897e36e99d2814b77d961896db1c3f6dfa3ba",
  "32c9c10c37b7f19cc1e3932cebd6ef29643693b25fd206b489776d3ff79ece56",
  "de71f7eb9df885721200466052be890686038806919ca4c0321f217b0b810a25",
  "36e136bab04d50ece1e250dba0606b9cba064eca2e6d44097696bb58887e889a",
  "88179a91d3ea32a3796499a6866c8b79ffda3caa3e4eeca4ff6ffa72cd82f9a6",
  "24786c59bf8601357f8a68e453d1f6bf3b5046ede350fb565734b753f6e3cd94",
  "73da745192e4320be7f62b6b235cffe3d8f3f747ed4f013c7bed429e44757f21",
  "9990c4dfb826caf8b7864a126a5ca576e3e97ac62d78ce263dde55d40978b060",
  "1c387c038088aaf1a916fb8df978e9c5b211fde3ff4cd9cafa367a621a592777",
  "1bd6b46db5ff6f3fef2d816f1bf278a965a5103f5f1320edf7a80fd89717e56c",
  "151d05c87e775e2d41692aaa3275f6ba22dae57167e05d5238b32958fc56029f",
  "4bd8bdf97fb83cff907998c1b6a8853b144e27ae3ecf3584127fc6b2d64feb40",
  "d27a7ad63371062ffaeba503ee9521668ff75824db95a3c83c1cda8bef3b2dbe",
  "d030829dd960ea9ffb4f2e99c5a3a6058bee6df8f75d214e190680ac6e09b9d8",
  "3a355e6346c8c9da2b98b238ee9c8bb8d376276176e3aa90d3196c103f790bde",
  "d9e699af29abf18e9ea4cdbfa601b6f2dc91e109190d34d3d33037d3f090686e",
  "b930f7d900c8db6954bfdedafe1df2afa22134cae9671398dd951010b5156747",
  "3d18ce78080c439bd6578983e99cafd10bc1e5b81cb52384df9cbfe112f5200e",
  "134386a04f3b1c54f86acd37a20693eaae77dd6596e2733f5e03b1db710a3051",
  "66068645ed7ae2111cbe6d4d7b573e9afc0333163cb0899cdee41b3269c9ddc0",
  "99ec81b8cc977eda0897eb01962afdebd3e905d080cb3eb2be667f931c33dac8",
  "ec357afe4103fe9d6d78b1ab699593c8f0e4ca2dbd8556f4cc2fdd4f2b8e6616",
  "6eef0e73ac4e9de10f41d5feaefa67592579dee2aee3056f7a8dc0cf2083292d",
  "4eb5df06fb082cf4db2b4e571cf0c142b41cf4bd1f128b668946ada4fe2e1793",
  "b16ce60a58b58d7a11a2bdf75e070f6a5865ea8cdec3bab280fd11814d64eeb5",
  "08ebe7ec9766a5c1c4f1feac3b2f04c0a67be96924dc40cee0eced217eb17d37",
  "937264e985e700f052ae7bea49d4e593edf3d76a6a47142ec91c8da693ec609e",
  "bf98e30754b1ca1ba2a228268c54bb055bf40d4b9f273e757e5b85423316e45c",
  "8edcc5fccc98f38dac9ae647da95af11d20d26286149cafb0823aa8dda1f9c5d",
  "01433cd1639bc241e35c1615718d51f442bb6c78f417462b0623f6b72dd7f280",
  "9ce958e0a3d32954895b9200c2ee645a4d1131c49bc25fb66b18462600250586",
  "d90c5fa4b680d7096eba1dad71d90359fcf640a55a94fdbcf41743499bcc8aad",
  "8fccf665d0f9dca3eb0ecfa9777fdcc3bd22f22d9cb50b892ef8455850243d41",
  "0c68b2c912114324c6ca9df71ee0c8d35aea8e93f8fadde85c76718efe026811",
  "5d46b2bd8d5c8f80b39def048f0a4bcb6334356994ee54745602eec6748a588b",
  "58e1360d332ea2d695f17588f4dc7204db51f22ed96e0b561a9f4ed2c9a3eef6",
  "6dd09174ee1a732d82b5e7c48b6bdbea623a416e7e858f520bcbc85f52f0e136",
  "d79b86b7eedcd2d21ed205d68db6b86bd01b1c82bf2245c4578111d78b13e842",
  "cbd3ef39bb658dbff03bd19dd9da9c1bc0e1c6abf77225679711c03e39ce8d5d",
  "c2e295dadcf836ca3db06de9a7a456f354ef23893b30f26cbe3bed2aa6a91926",
  "c531b1d73b65d98ad217f0c73c51b00969e7ca65ce0fa495b24250d7a8221f5c",
  "9e6e404ccfe0b5fc468f73648948d29b266814ee8bd940c86e1f2683e2eaab65",
  "633388ff97d3e162ec289f1bcb5f7301043023fc9f57493e98ae31717a73b8e0",
  "d97fbe690e395a0505ad9823c1348722c989737d39daae339091a1f780a047f6",
  "b553776ea02abe15eb6c3327d492c0f44a16f99117a5908cc3c321f2a8d60df2",
  "4102b86adeef33bf0f7cc8b2c287ef440557083538482fdbc74523d09db58806",
  "80816ea44bf3f20123e6267a73f2d4aca343973300a701d9c10c0f86e5e6f5f1",
  "5a562542a6526b306f8caf3bd2a2331371c36774f549ae567cd8d925131cc26a",
  "a4ab75a44e4174906e41ca492ce55fbd084a563adb646fb22d6733ce45078ab3",
  "084c452473b14d1ae2e586d36f108dd7983f6da6f257bc85f1b462bab0d3e8f4",
  "aa0fa139e5651ef5380f122164544bea8c249c4be0d0360636bde6f08d4d3312",
  "0e83b27b97ed4a7de6aa8970ae2e816f8942b13c069ed57c375d818396afe244",
  "89af9562b7da62c3a606daa6cfbf009a6d685e08a65b7049dce4f0c6d4f19257",
  "546e9764679ec6ae15bad8f29878c79a368458221aed8893229a691cc82daeaf",
  "ed060a2ef65d7255c5cee79b4b2a2bfa31181eac61c46fd7c46d38e8d8e616cf",
  "340f1cdfecf87405043a8a31b24ea7532bf81a5175cc81eba2e205b933957baf",
  "af72a06998491857fc92673ddbc2f8f3da2411162cfd0196c347d8df5dff1fe5",
  "749e33b96cc0d65a8837285b9379dde90110571114e26fdcb05be3ec5f430bfb",
  "cba899da6556e8ec1a19c682e073d37cf2004e370c1659a03d651844c9fbcfe2",
  "6a95582ab4ec3846069322f07350b0543471110a6d2d4b9a6f2f4285aef73710",
  "9b0d6445ceb2b4dac0e3737c7a2d5c0d68da29ff8343700544e1df0fa7ca861e",
  "0ee923581915531b50571329a19c71144ed2431843a98fed3cbc1746fef8c7a9",
  "550f4360d1f3631e5a1529f6ea270ea385e45dc1363f9e00841ad5976b161ed9",
  "9e6fc90dc101625a19cf25fdfcd75187f538416d4bd373ed6e7568b37b4ee5f7",
  "3892b43a8cb6922c128eaec25131b57edeb9357a9e369909824972b8dd160751",
  "e254a566f954c83537d758ef448b021b8b48a5cb9a352f1b376b6e1b9e123abf",
  "a7e26b67a535059b785837e23de586aa88d06947f47f463649195ca201c5b000",
  "1f781e291cd9471f6d304960d9fb621ada39e386c1e630968492cf568dd7b924",
  "ccae3efe56b9fbf310a28355c641cef9172bdc08b338ba0c70246ab6163f3474",
  "412d7e3abaded846264c2fe0d9c4519f3cde678c95e89c1b92fa87f0185e81d7",
  "de237bfc753223c4476df9ad19bc7f441f248754d2c06f9fe178feea38a0ecd9",
  "11ffb82186ce1e313e1ee94b9f217188d32819ca7710352af7dc84122208d305",
  "b61550dd2f42dca535cd6edf2bbb0bdb015a2859403950873d626f7cf7cf4178",
  "6231fe7fcf77cf6ca202954160acf36d5ea177231fd8662b1324588a1e90f588",
  "4f42051460eb87cc3647df0d7d4e50381abacecd686f967ec24cc7f695d654ca",
  "da5336656b37817a0f8a3aaea50520abbdd7fd106caca35f7d368ac59911dc04",
  "1481781b86a04b9d03d792b905459b3098bd9f688d62ec18700b798f9d982ac6",
  "01c61cf5d115ab0413ead58bbaed41be8e3bd881d8c4a9fad1fdcadec733d0be",
  "31a36908307859b45881a0f813ae06e4a19f890f7473895a4159588fb145bb6b",
  "c09f7dbfba40a178bc46d5d1bdf89891c6d5c115d7bf24602aa3d814ff908cf3",
  "0562e1a5dd691306c0d898c579acd5a59097953e5dd544b49c9226be6075e704",
  "14b07d5a1bd5c5df0adc91eb216bbfb6dd3de4dcfe2a090a391afc99f667264c",
  "e905996f6a8dd5dd2253579c19d59aa7ec877658365ad607d4b116dffd6fca78",
  "5da3ba0aab210b866c081215ff96ecd809f84d43f3660fe2f8a833b1cc2f839b",
  "ef418d9db37505022885193aaf8876c7a30159d2c005bed134a43c67890faea8",
  "97307fd108262cb8be901b8ada38282365e5d3b5a73793ecb4362ddb1dc6f2ae",
  "195ac83205a9cb2eb87632c9a3f5ba2463bd2b903fb9becee264f7a7b0a35bbb",
  "a4b3551574a316a0435bb98451b1807f048c34b3677c9054f4a1b2a5b00aa6da",
  "1e4334577d945fc02c0e6da5f558be6e83a6c6f0333c6e84dffb4642e151a5a9",
  "f825997517964dc80eef6490c5e65da10c8be3745dc303e2c7d3878e7b3caea9",
  "9bcf20934c19053e471ff9aac86b3be330d3605f7322998b312d67ef4d7cec42",
  "53f03eec7004f5c3e5751f576d34c3c06cd0cd833191decc64c41b13be8af570",
  "e2984a7d1155ef104f98e938b3747ae1fc62d207fc216005197e878b29dea29d",
  "56e3d48278e5b7880490c8fed8e21ceb37d81e31e9f0c84437d084edad1da991",
  "9f86f83bd53b7bf8ac6783df86a8d93974ffd5eae485f8c310302b076f60393f",
  "fb8ffac63d5c784ea557596f1a1b491f220c6e252f553562df0a70f98191d7c2",
  "f47d2ed760625fb5e29feb267a5ca3d9f6ff6a8a709eb20a1a3f52bf4bbaab29",
  "84b239c645713694ca7b18207e1dec352d141da4c934fda9c0fc105e322bb3d1"
]

m_root = merkle_root(TXID_LIST)
=> 8cec087a633983350593c5613bfa1cdd8261337a997016495d3d7b9df8b9f052 # merklerootに一致する

マイニング

マイニング処理

  • 入力はブロックヘッダ
  • Nonceフィールドの値を順次変更しながらブロックヘッダのSHA-256によるハッシュ値の計算を求め、そのハッシュ値がブロックヘッダのDifficulty Targetフィールドで定められた難易度ターゲットよりも小さい数値であれば成功
irb(main):001:0> block_header = "00000020a6a0a7d0731c5f13edf8e3f6c918f3545c8870f5a90c379939676fcd0000000052f0b9f89d7b3d5d491670997a336182dd1cfa3b61c59305358339637a08ec8c5d6ed959ffff001dcab6a119"
# ブロックヘッダ最後の4バイトであるNonceフィールドの値もInternal Byte Orderになっているので、これをバイト単位で順序を反転させさらに逆順にする
irb(main):003:0> nonce = [block_header[-8..-1]].pack('H*').unpack('h*')[0].reverse.to_i(16)
=> 430028490

Coinbaseトランザクション

vinというフィールドがCoinbaseトランザクションのインプット領域

$ bitcoin-cli getrawtransaction 4c82a543054837429d2fd3828c9424859235c53311f91a2131b42a3be6631960
010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff23038275120004606ed9590497971d180c869ed759397a0600000000000a636b706f6f6cffffffff02d5d78b09000000001976a9140dc69aebc1f6ca94c1455302faf7df61ad546de888ac0000000000000000266a24aa21a9ed8e5a9d7029e17ee443dd7a5f28ea8b247c6292041d79ba606f51d86198a121d60120000000000000000000000000000000000000000000000000000000000000000000000000
$ bitcoin-cli decoderawtransaction 010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff23038275120004606ed9590497971d180c869ed759397a0600000000000a636b706f6f6cffffffff02d5d78b09000000001976a9140dc69aebc1f6ca94c1455302faf7df61ad546de888ac0000000000000000266a24aa21a9ed8e5a9d7029e17ee443dd7a5f28ea8b247c6292041d79ba606f51d86198a121d60120000000000000000000000000000000000000000000000000000000000000000000000000
{
  "txid": "4c82a543054837429d2fd3828c9424859235c53311f91a2131b42a3be6631960",
  "hash": "dd2ec779d592d811bdd9a285311178180d43d0301a296e659faa40ddd2382332",
  "version": 1,
  "size": 203,
  "vsize": 176,
  "locktime": 0,
  "vin": [
    {
      "coinbase": "038275120004606ed9590497971d180c869ed759397a0600000000000a636b706f6f6c",
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 1.60159701,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 0dc69aebc1f6ca94c1455302faf7df61ad546de8 OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a9140dc69aebc1f6ca94c1455302faf7df61ad546de888ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "mgmnzsmwDnvA3PVsSEdc3dRa1wT2h21hPY"
        ]
      }
    },
    {
      "value": 0.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_RETURN aa21a9ed8e5a9d7029e17ee443dd7a5f28ea8b247c6292041d79ba606f51d86198a121d6",
        "hex": "6a24aa21a9ed8e5a9d7029e17ee443dd7a5f28ea8b247c6292041d79ba606f51d86198a121d6",
        "type": "nulldata"
      }
    }
  ]
}
  • Coinbaseフォーマット
バイト数 フィールド名 定義
32 hash(null) cahr[32] コインベースであるため前の出力がないので32バイトのnull値が入っている
4 index(UINT32_MAX) uint32_t コインベースには直前のアウトプットがないので0xffffffffが入っている
可変 script bytes compactSite uint coinbase script(最大100バイト)
可変 height script ブロック高
可変 coinbase script None コインベースのextraNonce用のフィールド
4 sequence unit32_t シーケンス番号
  • Coinbaseブロック高
irb(main):001:0> coinbase = "038275120004606ed9590497971d180c869ed759397a0600000000000a636b706f6f6c"
=> "038275120004606ed9590497971d180c869ed759397a0600000000000a636b706f6f6c"
irb(main):002:0> coinbase[2..7]
=> "827512"
irb(main):003:0> ["827512"].pack('h*').unpack('H*')[0].reverse.to_i(16)
=> 1209730

マイニングの難易度

  • ブロックヘッダのダブルハッシュと難易度ターゲットの比較
block_header = "00000020a6a0a7d0731c5f13edf8e3f6c918f3545c8870f5a90c379939676fcd0000000052f0b9f89d7b3d5d491670997a336182dd1cfa3b61c59305358339637a08ec8c5d6ed959ffff001dcab6a119"

# 難易度
difficulty_target = [block_header[-16..-9]].pack('H*').unpack('h*')[0].reverse
=> 1d00ffff

# 難易度のtarget=係数部*2**(8*(指数部-3))
def target(difficulty_target)
  exponent = difficulty_target[0..1].hex
  coefficient = difficulty_target[2..7].hex
  (coefficient*2**(8*(exponent-3)))
end

target(difficulty_target)
=> 26959535291011309493156476344723991336010898738574164086137773096960

require 'digest'

def dh(data)
  Digest::SHA256.digest(Digest::SHA256.digest([data].pack("H*"))).unpack("h*")[0].reverse
end

# ブロックヘッダのダブルハッシュと難易度ターゲットの比較
dh(block_header).hex < target(difficulty_target)
=> true