美文网首页
比特币区块挖矿难度调整算法

比特币区块挖矿难度调整算法

作者: woodcol | 来源:发表于2020-03-12 13:14 被阅读0次

自己一直用来看比特币挖矿难度调整的app好像出了bug。今天花了一上午的时间研究了一下链上的挖矿难度调整的算法。现在在分享一下。
首先,先了解一下难度调整公式:

newdiff=diff*(2016*10*60)/R

newdiff:      新的难度调整值
diff:          上次难度调整结果
(2016*10*60):  每10分钟产生一个区块,两周时间产生区块数=2016,
R:             本次难度调整的2016个出快实际用时

知道了难度调整公式,那难度是怎么求出来的。

难度=创世块hash/当前难度hash

中本聪的源码src/chainparams.cpp文件220行代码的创世块里写死了第一个区块的难度hash为:0x1d00ffff。

genesis = CreateGenesisBlock(1296688602, 414098458, 0x1d00ffff, 1, 50 * COIN);

这个数的理解是,0x1d表示难度数据的数值有多少个字节。0x1d十进制是29。表示0x00ffff后边补上0后一共是29个字节的长度。所以创世块难度hash就是:
0x00000000FFFF0000000000000000000000000000000000000000000000000000
每次难度调整时都会有新的难度调整Bits值。比如当前的难度Bits值就是:386990361,这是十进制,转成十六进制就是0x17110119。用转换创世hash的方法转换后为:
0x0000000000000000001101190000000000000000000000000000000000000000
那么当前所有出快都必须小于或等于这个hash值才难算合法有效区块。
那么(当前区块高度621287)当前难度就是:

diff=0x00000000FFFF0000000000000000000000000000000000000000000000000000/0x0000000000000000001101190000000000000000000000000000000000000000
diff=16552923967337

结果等于所有网站上显示的621287区块高度的难度值16552923967337

区块数据API

btc.com的api介结:

https://btc.com/api-doc

区块数据调用例子

显示最近的区块
https://chain.api.btc.com/v3/block/latest
显示指定高度的区块
https://chain.api.btc.com/v3/block/608222
显示指定hash的区块
https://chain.api.btc.com/v3/block/0000000000000000000e94b30afc79dd32e19fe4e90618b0dc200a34036b46ec

blockchair.com的api介绍,支持btc,ltc,eth,bch,ripple,stellar,ton/testnet

https://blockchair.com/api/docs
区块数据调用例子,比特币数据单位为聪

显示最近的1个区块数据
https://api.blockchair.com/bitcoin/blocks?limit=1
显示最近的10个区块数据
https://api.blockchair.com/bitcoin/blocks
显示指定高度的区块详细数据
https://api.blockchair.com/bitcoin/blocks?q=id(608200)

LTC的使用和BTC一样,只是把上边链接中的bitcoin换成了litecoin.

数据例子

btc.com:
BTC
数据说明

{
    height: int 块高度
    version: int 块版本
    mrkl_root: string Merkle Root
    curr_max_timestamp: int 块最大时间戳
    timestamp: int 块时间戳
    bits: int bits
    nonce: int nonce
    hash: string 块哈希
    prev_block_hash: string 前向块哈希,如不存在,则为 null
    next_block_hash: string 后向块哈希,如不存在,则为 null
    size: int 块体积
    pool_difficulty: int 矿池难度
    difficulty: int 块难度
    tx_count: int 块奖励
    reward_block: int 块奖励
    reward_fees: int 块手续费
    created_at: int 该记录系统处理时间,无业务含义
    confirmations: int 确认数
    extras: {
        relayed_by: string 块播报方
    }
}

得到的数据

(https://chain.api.btc.com/v3/block/latest)

(https://chain.api.btc.com/v3/block/608226)

{
  "data": {
    "height": 608226,
    "version": 536870912,
    "mrkl_root": "0692479167e28724ec42d72f4f9d5dace1ac64dd5033aff6cb3470c3ecb4ebab",
    "timestamp": 1576417321,
    "bits": 387308498,
    "nonce": 3015452293,
    "hash": "000000000000000000009702e6fd61c4f2d6afd14ef844db8bf1b9f613906754",
    "prev_block_hash": "000000000000000000067d28ec9d70867bab2a59ea38b050b82f39f0b6f19d47",
    "next_block_hash": "0000000000000000000000000000000000000000000000000000000000000000",
    "size": 1123402,
    "pool_difficulty": 477170639938249,
    "difficulty": 12876842089682.479,
    "tx_count": 1635,
    "reward_block": 1250000000,
    "reward_fees": 5255308,
    "created_at": 1576417345,
    "confirmations": 1,
    "is_orphan": false,
    "curr_max_timestamp": 1576417321,
    "is_sw_block": true,
    "stripped_size": 956597,
    "weight": 3993193,
    "extras": {
      "pool_name": "SlushPool",
      "pool_link": "https://slushpool.com/"
    }
  },
  "err_no": 0,
  "err_msg": null
}

blockchair.com:

BTC(https://api.blockchair.com/bitcoin/blocks?limit=1)

{
  "data": [
    {
      "id": 608224,
      "hash": "000000000000000000079522cc63f43739f2de6df18c43627d63d3eb16d5fbab",
      "date": "2019-12-15",
      "time": "2019-12-15 13:39:32",
      "median_time": "2019-12-15 12:22:59",
      "size": 1169566,
      "stripped_size": 941204,
      "weight": 3993178,
      "version": 536870912,
      "version_hex": "20000000",
      "version_bits": "100000000000000000000000000000",
      "merkle_root": "d621a4b9d5e1f6c84dfd64a855449e02a7071525e8c87d6f2c5c41702cca6f0c",
      "nonce": 903910756,
      "bits": 387308498,
      "difficulty": 12876842089682,
      "chainwork": "00000000000000000000000000000000000000000ae026b93669b770616f986f",
      "coinbase_data_hex": "03e04709049537f65d652f4254432e434f4d2ffabe6d6d1d98d29707de3c8ff625db2edb68cfd51b88817d95dcf72c14dd9bc489bf68e208000000ceeed33d2220cd9c25bb010000000000",
      "transaction_count": 1853,
      "witness_count": 1228,
      "input_count": 5088,
      "output_count": 4725,
      "input_total": 715614787561,
      "input_total_usd": 50423100,
      "output_total": 716864787561,
      "output_total_usd": 50511200,
      "fee_total": 16277169,
      "fee_total_usd": 1146.91,
      "fee_per_kb": 13922.3,
      "fee_per_kb_usd": 0.980983,
      "fee_per_kwu": 4074.98,
      "fee_per_kwu_usd": 0.287128,
      "cdd_total": 2631.2267182517,
      "generation": 1250000000,
      "generation_usd": 88076.6,
      "reward": 1266277169,
      "reward_usd": 89223.5,
      "guessed_miner": "BTC.com"
    }
  ],
  "context": {
    "code": 200,
    "source": "A+T",
    "time": 0.023128986358642578,
    "limit": 1,
    "offset": 0,
    "rows": 1,
    "pre_rows": 1,
    "total_rows": 608225,
    "state": 608224,
    "cache": {
      "live": false,
      "duration": 60,
      "since": "2019-12-15 13:40:55",
      "until": "2019-12-15 13:41:55",
      "time": 4.0531158447265625e-6
    },
    "api": {
      "version": "2.0.40a",
      "last_major_update": "2019-07-19 18:07:19",
      "next_major_update": null,
      "documentation": "https:\/\/blockchair.com\/api\/docs",
      "notice": "Beginning July 19th, 2019 all applications using Blockchair API on a constant basis should apply for an API key (mailto:info@blockchair.com)"
    }
  }
}

LTC(https://api.blockchair.com/litecoin/blocks?limit=1)

{
  "data": [
    {
      "id": 1753620,
      "hash": "ac74a59df14fbde7782d7d56f06ccd601296b35022594162db45736131056346",
      "date": "2019-12-15",
      "time": "2019-12-15 13:34:21",
      "median_time": "2019-12-15 13:27:07",
      "size": 3396,
      "stripped_size": 2481,
      "weight": 10839,
      "version": 536870912,
      "version_hex": "20000000",
      "version_bits": "100000000000000000000000000000",
      "merkle_root": "f7b52a1a9f03e18616e3d1bf5723736f8ae0f766769cc1b7adc32eac3c8fa12a",
      "nonce": 864342164,
      "bits": 436417938,
      "difficulty": 5227673.9977748,
      "chainwork": "000000000000000000000000000000000000000000000380363ab1ae9aa2f42c",
      "coinbase_data_hex": "0314c21a045df6365d2cfabe6d6d6a698c3ee473c740fee60276bdb2b754abfc4dae2efbc65d83d3bccfa39b74e84000000000000000042f4c502f082200236429000000",
      "transaction_count": 11,
      "witness_count": 8,
      "input_count": 15,
      "output_count": 21,
      "input_total": 3007381084,
      "input_total_usd": 1301.9,
      "output_total": 4257381084,
      "output_total_usd": 1843.02,
      "fee_total": 356462,
      "fee_total_usd": 0.154312,
      "fee_per_kb": 115772,
      "fee_per_kb_usd": 0.0501177,
      "fee_per_kwu": 36763.8,
      "fee_per_kwu_usd": 0.0159151,
      "cdd_total": 5093.9833269198,
      "generation": 1250000000,
      "generation_usd": 541.125,
      "reward": 1250356462,
      "reward_usd": 541.279,
      "guessed_miner": "litecoinpool.org"
    }
  ],
  "context": {
    "code": 200,
    "source": "A+T",
    "time": 0.029465913772583008,
    "limit": 1,
    "offset": 0,
    "rows": 1,
    "pre_rows": 1,
    "total_rows": 1753621,
    "state": 1753620,
    "cache": {
      "live": false,
      "duration": 60,
      "since": "2019-12-15 13:37:58",
      "until": "2019-12-15 13:38:58",
      "time": 3.0994415283203125e-6
    },
    "api": {
      "version": "2.0.40a",
      "last_major_update": "2019-07-19 18:07:19",
      "next_major_update": null,
      "documentation": "https:\/\/blockchair.com\/api\/docs",
      "notice": "Beginning July 19th, 2019 all applications using Blockchair API on a constant basis should apply for an API key (mailto:info@blockchair.com)"
    }
  }
}

获取未打包数据区块信息

https://api.blockchair.com/bitcoin/stats

{
  "data": {
    "blocks": 608227,
    "transactions": 484046565,
    "outputs": 1290027617,
    "circulation": 1810280854497237,
    "blocks_24h": 127,
    "transactions_24h": 273400,
    "difficulty": 12876842089682,
    "volume_24h": 68714575466075,
    "mempool_transactions": 4644,
    "mempool_size": 4224831,
    "mempool_tps": 2.783333333333333,
    "mempool_total_fee_usd": 887.7269,
    "best_block_height": 608226,
    "best_block_hash": "000000000000000000009702e6fd61c4f2d6afd14ef844db8bf1b9f613906754",
    "best_block_time": "2019-12-15 13:42:01",
    "blockchain_size": 253752321054,
    "average_transaction_fee_24h": 5248,
    "inflation_24h": 158750000000,
    "median_transaction_fee_24h": 1857,
    "cdd_24h": 3104856.899387399,
    "largest_transaction_24h": {
      "hash": "ee04f950752be0bbed292056aae42025ad7abdf932b19e3c42a8c50e1fa7e0f1",
      "value_usd": 26808956
    },
    "nodes": 9064,
    "hashrate_24h": "81331787721971307411",
    "inflation_usd_24h": 11233340.5,
    "average_transaction_fee_usd_24h": 0.3713934472758215,
    "median_transaction_fee_usd_24h": 0.1314035484,
    "market_price_usd": 7076.12,
    "market_price_btc": 1,
    "market_price_usd_change_24h_percentage": -1.14187,
    "market_cap_usd": 128212171062,
    "market_dominance_percentage": 66.48,
    "next_retarget_time_estimate": "2019-12-19 18:14:58",
    "next_difficulty_estimate": 13035843741838,
    "countdowns": [
      {
        "event": "Reward halving",
        "time_left": 13063200
      }
    ],
    "suggested_transaction_fee_per_byte_sat": 6
  },
  "context": {
    "code": 200,
    "source": "A",
    "time": 0.6486949920654297,
    "state": 608226,
    "cache": {
      "live": false,
      "duration": "Ignore",
      "since": "2019-12-15 13:54:03",
      "until": "2020-12-14 13:54:03",
      "time": 3.0994415283203125e-6
    },
    "api": {
      "version": "2.0.40a",
      "last_major_update": "2019-07-19 18:07:19",
      "next_major_update": null,
      "documentation": "https:\/\/blockchair.com\/api\/docs",
      "notice": "Beginning July 19th, 2019 all applications using Blockchair API on a constant basis should apply for an API key (mailto:info@blockchair.com)"
    }
  }
}

相关文章

网友评论

      本文标题:比特币区块挖矿难度调整算法

      本文链接:https://www.haomeiwen.com/subject/gbdujhtx.html