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
以上方法计算困难度
时间: 2024-10-24 10:43:24