首页 > 代码库 > 源码阅读 etherum-block.py
源码阅读 etherum-block.py
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。