首页 > 其他 > 详细

源码阅读 etherum-block.py

时间:2016-11-22 20:40:54      阅读:310      评论:0      收藏:0      [点我收藏+]
def calc_difficulty(parent, timestamp):
    config = parent.config
    offset = parent.difficulty // config[‘BLOCK_DIFF_FACTOR‘]
    if parent.number >= (config[‘METROPOLIS_FORK_BLKNUM‘] - 1):
        sign = max(len(parent.uncles) - ((timestamp - parent.timestamp) // config[‘METROPOLIS_DIFF_ADJUSTMENT_CUTOFF‘]), -99)
    elif parent.number >= (config[‘HOMESTEAD_FORK_BLKNUM‘] - 1):
        sign = max(1 - ((timestamp - parent.timestamp) // config[‘HOMESTEAD_DIFF_ADJUSTMENT_CUTOFF‘]), -99)
    else:
        sign = 1 if timestamp - parent.timestamp < config[‘DIFF_ADJUSTMENT_CUTOFF‘] else -1
    # If we enter a special mode where the genesis difficulty starts off below
    # the minimal difficulty, we allow low-difficulty blocks (this will never
    # happen in the official protocol)
    o = int(max(parent.difficulty + offset * sign, min(parent.difficulty, config[‘MIN_DIFF‘])))
    period_count = (parent.number + 1) // config[‘EXPDIFF_PERIOD‘]
    if period_count >= config[‘EXPDIFF_FREE_PERIODS‘]:
        o = max(o + 2 ** (period_count - config[‘EXPDIFF_FREE_PERIODS‘]), config[‘MIN_DIFF‘])
    # print(‘Calculating difficulty of block %d, timestamp difference %d, parent diff %d, child diff %d‘ % (parent.number + 1, timestamp - parent.timestamp, parent.difficulty, o))
    return o

  以上方法计算困难度

源码阅读 etherum-block.py

原文:http://www.cnblogs.com/chdxiaoming/p/6090743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!