码迷,mamicode.com
首页 > 其他好文 > 详细

源码阅读 etherum-block.py

时间:2016-11-22 20:41:47      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:app   count   python   nes   div   time   方法   prot   lock   

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

标签:app   count   python   nes   div   time   方法   prot   lock   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!