def lwma_next_work_required(block_list):

    # T
    target_solvetime = 10 * 60
    # N For T=600, 300, 150 use approximately N=60, 90, 120
    average_window = 60
    # Define a k that will be used to get a proper average after weighting the solvetimes.
    k = int(average_window * (average_window + 1) * target_solvetime // 2)

    height = block_list[-1]['height']
    pow_limit = net['pow_limit']

    # New coins should just give away first N blocks before using this algorithm.
    if height < average_window:
        return ser.compact_from_uint256(pow_limit)

    average_target = 0
    prev_timestamp = block_list[-average_window - 1]['time']
    sum_weighted_solvetimes = 0
    solvetime_weight = 0

    # Loop through N most recent blocks.
    for i in range(height + 1 - average_window, height + 1):
        block = block_list[i - height - 1]
        # Prevent solvetimes from being negative in a safe way. It must be done like this.
        # In particular, do not attempt anything like  if(solvetime < 0) {solvetime=0;}
        # The +1 ensures new coins do not calculate nextTarget = 0.
        this_timestamp = block[
            'time'] if block['time'] > prev_timestamp else prev_timestamp + 1

        # A 6*T limit will prevent large drops in difficulty from long solvetimes.
        solve_time = min(6 * target_solvetime, this_timestamp - prev_timestamp)

        # The following is part of "preventing negative solvetimes".
        prev_timestamp = this_timestamp

        # Give linearly higher weight to more recent solvetimes.
        solvetime_weight = solvetime_weight + 1
        sum_weighted_solvetimes += solve_time * solvetime_weight

        target = ser.uint256_from_compact(block['nbits'])
        average_target += target // (
            average_window * k
        )  # Dividing by k here prevents an overflow below.

    # Desired equation in next line was nextTarget = avgTarget * sumWeightSolvetimes / k
    # but 1/k was moved to line above to prevent overflow in new coins
    next_target = sum_weighted_solvetimes * average_target

    if next_target > pow_limit:
        print('exceed pow limit')
        next_target = pow_limit

    next_nbits = ser.compact_from_uint256(next_target)
    print('lwma nbits:', hex(next_nbits))
    print('difficulty:', difficulty(next_nbits))

    return next_nbits
Example #2
0
 def _check_difficulty_target(self, header):
     parent = b2lx(header.hashPrevBlock)
     target = self.get_difficulty_target(parent)
     if (self.get_height() + 1) % 2016 == 0:
         end = self.get_timestamp(parent)
         min, max = (302400, 4838400)
         for i in range(2015):
             parent = self._get_parent(parent)
         start = self.get_timestamp(parent)
         difference = end - start
         if difference < min:
             difference = min
         elif difference > max:
             difference = max
         target = compact_from_uint256(long(uint256_from_compact(target) * (float(difference) / (60 * 60 * 24 * 14))))
     if self.testnet and header.nTime - self.get_timestamp(parent) >= 1200:
         return target
     if uint256_from_compact(header.nBits) > uint256_from_compact(target):
         raise CheckBlockHeaderError("Target difficutly is incorrect")
     return target
Example #3
0
 def _check_difficulty_target(self, header):
     parent = b2lx(header.hashPrevBlock)
     target = self.get_difficulty_target(parent)
     if (self.get_height() + 1) % 2016 == 0:
         end = self.get_timestamp(parent)
         min, max = (302400, 4838400)
         for i in range(2015):
             parent = self._get_parent(parent)
         start = self.get_timestamp(parent)
         difference = end - start
         if difference < min:
             difference = min
         elif difference > max:
             difference = max
         target = compact_from_uint256(
             long(
                 uint256_from_compact(target) * (float(difference) /
                                                 (60 * 60 * 24 * 14))))
     if self.testnet and header.nTime - self.get_timestamp(parent) >= 1200:
         return target
     if uint256_from_compact(header.nBits) > uint256_from_compact(target):
         raise CheckBlockHeaderError("Target difficutly is incorrect")
     return target
def get_next_work_required(block_list):
    global net
    fork_height = net['fork_height']
    adjust_interval = 72
    last_height = block_list[-1]['height']
    height_span = last_height + 1 - fork_height

    if height_span % adjust_interval != 0:
        print('Not time to adjust target.')
        return block_list[-1]['nbits']

    target_time_span = 72 * 10 * 60
    retarget_factor = 2
    real_time_span = block_list[-1]['time'] - block_list[-adjust_interval][
        'time']
    adjusted_time_span = real_time_span
    if adjusted_time_span < target_time_span // retarget_factor:
        adjusted_time_span = target_time_span // retarget_factor
    if adjusted_time_span > target_time_span * retarget_factor:
        adjusted_time_span = target_time_span * retarget_factor

    print('adjusted time span: %s, real time span: %s' %
          (adjusted_time_span, real_time_span))

    pow_limit = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    last_target = ser.uint256_from_compact(block_list[-1]['nbits'])
    next_target = last_target * adjusted_time_span // target_time_span

    if next_target > pow_limit:
        print('exceed pow limit')
        next_target = pow_limit

    next_nbits = ser.compact_from_uint256(next_target)
    print('original nbits:', hex(next_nbits))
    print('difficulty:', difficulty(next_nbits))

    return next_nbits