区块链共识笔记 --- POW 难度计算(ethereum)
生活随笔
收集整理的這篇文章主要介紹了
区块链共识笔记 --- POW 难度计算(ethereum)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從前一節對區塊鏈先驅bitcoin的POW分析,挖礦的本質就是求解一個謎題,給定一個難度(difficulty),他可以動態變化,他決定了難題的題解空間,用窮舉的方式,再題解空間中找到解決謎題的解。
以太坊的難度計算公式是
翻譯一下就是:
blockDiff = parentDiff + parentDiff / 2048 * max(1 - (blockTimestamp - parentTimestamp)/9, -99) +??2^((blockNumer - 3000000) / 100000 -2)
難度計算對應到以太坊源碼:
ChainOperationParams::ChainOperationParams():m_blockReward("0x4563918244F40000"),minGasLimit(0x1388),maxGasLimit("0x7fffffffffffffff"),gasLimitBoundDivisor(0x0400),networkID(0x0),minimumDifficulty(0x020000), // 131072difficultyBoundDivisor(0x0800), //2048durationLimit(0x0d) // 13 { } u256 calculateEthashDifficulty(ChainOperationParams const& _chainParams, BlockHeader const& _bi, BlockHeader const& _parent) {const unsigned c_expDiffPeriod = 100000;if (!_bi.number())throw GenesisBlockCannotBeCalculated(); //創世區塊不計算auto const& minimumDifficulty = _chainParams.minimumDifficulty; //即創世區塊難度131072auto const& difficultyBoundDivisor = _chainParams.difficultyBoundDivisor;//2048auto const& durationLimit = _chainParams.durationLimit;//13bigint target; // stick to a bigint for the target. Don't want to risk going negative.//以太坊有不同的發展階段,不同的發展階段適用的難度調整方案不同if (_bi.number() < _chainParams.homesteadForkBlock)// Frontier-era difficulty adjustment// Frontier階段,難度調整是父區塊難度+/- 父區塊難度/2048target = _bi.timestamp() >= _parent.timestamp() + durationLimit ?_parent.difficulty() - (_parent.difficulty() / difficultyBoundDivisor) :(_parent.difficulty() + (_parent.difficulty() / difficultyBoundDivisor));else{//當前區塊與父區塊的時間差bigint const timestampDiff = bigint(_bi.timestamp()) - _parent.timestamp();// 動態平衡調節因子,Homestead階段和Byzantium階段不同,Byantium階段比配到了協議中的公式bigint const adjFactor =_bi.number() < _chainParams.byzantiumForkBlock ?max<bigint>(1 - timestampDiff / 10, -99) : // Homestead-era difficulty adjustmentmax<bigint>((_parent.hasUncles() ? 2 : 1) - timestampDiff / 9,-99); // Byzantium-era difficulty adjustmenttarget = _parent.difficulty() + _parent.difficulty() / 2048 * adjFactor;}bigint o = target;unsigned exponentialIceAgeBlockNumber = unsigned(_parent.number() + 1);// EIP-1234 Constantinople Ice Age delayif (_bi.number() >= _chainParams.constantinopleForkBlock){if (exponentialIceAgeBlockNumber >= 5000000)exponentialIceAgeBlockNumber -= 5000000;elseexponentialIceAgeBlockNumber = 0;}// EIP-649 Byzantium Ice Age delayelse if (_bi.number() >= _chainParams.byzantiumForkBlock){if (exponentialIceAgeBlockNumber >= 3000000)exponentialIceAgeBlockNumber -= 3000000;elseexponentialIceAgeBlockNumber = 0;}unsigned periodCount = exponentialIceAgeBlockNumber / c_expDiffPeriod;//難度符號加到最后的難度值上if (periodCount > 1)o += (bigint(1) << (periodCount - 2)); // latter will eventually become huge, so ensure// it's a bigint.o = max<bigint>(minimumDifficulty, o);return u256(min<bigint>(o, std::numeric_limits<u256>::max())); }對于不同的以太坊階段,有不同的難度調整算法,Byzantium階段匹配了協議中的算法
總結
以上是生活随笔為你收集整理的区块链共识笔记 --- POW 难度计算(ethereum)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PDF在线转PPT,不用下载软件网页在线
- 下一篇: 铸钢节点与Q345B焊接施工方案