def get_target(self, height, chain=None): if chain is None: chain = [] max_target = 0x00000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF if height == 0: return 0x1e0ffff0, max_target neoscrypt_target = 0x0000003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF neoscrypt_bits = 0x1d3fffff target_timespan = 3.5 * 24 * 60 * 60 # 3.5 days target_spacing = 2.5 * 60 # 2.5 minutes if height == fork_four: return neoscrypt_bits, neoscrypt_target if height >= fork_one: target_timespan = (7 * 24 * 60 * 60) / 8 # 7/8 days if height >= fork_two: target_timespan = (7 * 24 * 60 * 60) / 32 # 7/32 days if height >= fork_three: target_timespan = 60 # 1 minute timespan target_spacing = 60 # 1 minute block # 2016 initially, 504 after 1st fork, 126 after 2nd fork, 15 after 3rd fork interval = target_timespan/target_spacing is_hard_fork = height == fork_one or height == fork_two or height == fork_three or height == fork_four last = self.read_header(height - 1) if last is None: for h in chain: if h.get('block_height') == height - 1: last = h # difficulty rules regular blocks if (height % interval != 0) and (not is_hard_fork) and height < fork_three: return last.get('bits'), bits_to_target(last.get('bits')) # first retarget after genesis if interval >= height: interval = height - 1 # go back by interval first = self.read_header((height-1) - interval) if first is None: for h in chain: if h.get('block_height') == (height-1) - interval: first = h actual_timespan = last.get('timestamp') - first.get('timestamp') # additional leveraging over 4x interval window if height >= fork_two and height < fork_three: interval *= 4 first = self.read_header((height-1) - interval) if first is None: for h in chain: if h.get('block_height') == (height-1) - interval: first = h actual_timespan_long = (last.get('timestamp') - first.get('timestamp')) / 4 # average between short and long windows actual_timespan_avg = (actual_timespan + actual_timespan_long) / 2 # apply .25 damping actual_timespan = actual_timespan_avg + 3*target_timespan actual_timespan /= 4 # additional leveraging over 15, 120 and 480 block window if height >= fork_three: interval *= 480 first_short = self.read_header((height-1) - 15) if first_short is None: for h in chain: if h.get('block_height') == (height-1) - 15: first_short = h first_short_time = first_short.get('timestamp') first_medium = self.read_header((height-1) - 120) if first_medium is None: for h in chain: if h.get('block_height') == (height-1) - 120: first_medium = h first_medium_time = first_medium.get('timestamp') first_long = self.read_header((height-1) - interval) if first_long is None: for h in chain: if h.get('block_height') == (height-1) - interval: first_long = h actual_timespan_short = (last.get('timestamp') - first_short_time) / 15 actual_timespan_medium = (last.get('timestamp') - first_medium_time) / 120 actual_timespan_long = (last.get('timestamp') - first_long.get('timestamp')) / 480 actual_timespan_avg = (actual_timespan_short + actual_timespan_medium + actual_timespan_long) / 3 # apply .25 damping actual_timespan = actual_timespan_avg + 3*target_timespan actual_timespan /= 4 # initial settings (4.0 difficulty limiter) actual_timespan_max = target_timespan * 4 actual_timespan_min = target_timespan / 4 # 1st hard fork if height >= fork_one: actual_timespan_max = target_timespan*99/70 actual_timespan_min = target_timespan*70/99 # 2nd hard fork if height >= fork_two: actual_timespan_max = target_timespan*494/453 actual_timespan_min = target_timespan*453/494 actual_timespan = max(actual_timespan, actual_timespan_min) actual_timespan = min(actual_timespan, actual_timespan_max) # retarget new_diff = bits_to_target(last.get('bits')) new_diff *= actual_timespan new_diff /= target_timespan new_diff = min(new_diff, max_target) return target_to_bits(new_diff), new_diff
def get_target(self, height, chain=None): if chain is None: chain = [] max_target = 0x00000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF if height == 0: return 0x1e0ffff0, max_target neoscrypt_target = 0x0000003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF neoscrypt_bits = 0x1d3fffff target_timespan = 3.5 * 24 * 60 * 60 # 3.5 days target_spacing = 2.5 * 60 # 2.5 minutes if height == fork_four: return neoscrypt_bits, neoscrypt_target if height >= fork_one: target_timespan = (7 * 24 * 60 * 60) / 8 # 7/8 days if height >= fork_two: target_timespan = (7 * 24 * 60 * 60) / 32 # 7/32 days if height >= fork_three: target_timespan = 60 # 1 minute timespan target_spacing = 60 # 1 minute block # 2016 initially, 504 after 1st fork, 126 after 2nd fork, 15 after 3rd fork interval = target_timespan / target_spacing is_hard_fork = height == fork_one or height == fork_two or height == fork_three or height == fork_four last = self.read_header(height - 1) if last is None: for h in chain: if h.get('block_height') == height - 1: last = h # difficulty rules regular blocks if (height % interval != 0) and (not is_hard_fork) and height < fork_three: return last.get('bits'), bits_to_target(last.get('bits')) # first retarget after genesis if interval >= height: interval = height - 1 # go back by interval first = self.read_header((height - 1) - interval) if first is None: for h in chain: if h.get('block_height') == (height - 1) - interval: first = h actual_timespan = last.get('timestamp') - first.get('timestamp') # additional leveraging over 4x interval window if height >= fork_two and height < fork_three: interval *= 4 first = self.read_header((height - 1) - interval) if first is None: for h in chain: if h.get('block_height') == (height - 1) - interval: first = h actual_timespan_long = (last.get('timestamp') - first.get('timestamp')) / 4 # average between short and long windows actual_timespan_avg = (actual_timespan + actual_timespan_long) / 2 # apply .25 damping actual_timespan = actual_timespan_avg + 3 * target_timespan actual_timespan /= 4 # additional leveraging over 15, 120 and 480 block window if height >= fork_three: interval *= 480 first_short = self.read_header((height - 1) - 15) if first_short is None: for h in chain: if h.get('block_height') == (height - 1) - 15: first_short = h first_short_time = first_short.get('timestamp') first_medium = self.read_header((height - 1) - 120) if first_medium is None: for h in chain: if h.get('block_height') == (height - 1) - 120: first_medium = h first_medium_time = first_medium.get('timestamp') first_long = self.read_header((height - 1) - interval) if first_long is None: for h in chain: if h.get('block_height') == (height - 1) - interval: first_long = h actual_timespan_short = (last.get('timestamp') - first_short_time) / 15 actual_timespan_medium = (last.get('timestamp') - first_medium_time) / 120 actual_timespan_long = (last.get('timestamp') - first_long.get('timestamp')) / 480 actual_timespan_avg = (actual_timespan_short + actual_timespan_medium + actual_timespan_long) / 3 # apply .25 damping actual_timespan = actual_timespan_avg + 3 * target_timespan actual_timespan /= 4 # initial settings (4.0 difficulty limiter) actual_timespan_max = target_timespan * 4 actual_timespan_min = target_timespan / 4 # 1st hard fork if height >= fork_one: actual_timespan_max = target_timespan * 99 / 70 actual_timespan_min = target_timespan * 70 / 99 # 2nd hard fork if height >= fork_two: actual_timespan_max = target_timespan * 494 / 453 actual_timespan_min = target_timespan * 453 / 494 actual_timespan = max(actual_timespan, actual_timespan_min) actual_timespan = min(actual_timespan, actual_timespan_max) # retarget new_diff = bits_to_target(last.get('bits')) new_diff *= actual_timespan new_diff /= target_timespan new_diff = min(new_diff, max_target) return target_to_bits(new_diff), new_diff
def get_target_dgw3(self, block_height, chain=None): if chain is None: chain = [] last = self.read_header(block_height - 1) if last is None: for h in chain: if h.get('block_height') == block_height - 1: last = h # params BlockLastSolved = last BlockReading = last BlockCreating = block_height nActualTimespan = 0 LastBlockTime = 0 PastBlocksMin = 24 PastBlocksMax = 24 CountBlocks = 0 PastDifficultyAverage = 0 PastDifficultyAveragePrev = 0 bnNum = 0 max_target = 0x00000FFFF0000000000000000000000000000000000000000000000000000000 if BlockLastSolved is None or block_height - 1 < PastBlocksMin: return 0x1e0ffff0, max_target for i in range(1, PastBlocksMax + 1): CountBlocks += 1 if CountBlocks <= PastBlocksMin: if CountBlocks == 1: PastDifficultyAverage = bits_to_target( BlockReading.get('bits')) else: bnNum = bits_to_target(BlockReading.get('bits')) PastDifficultyAverage = ( (PastDifficultyAveragePrev * CountBlocks) + (bnNum)) / (CountBlocks + 1) PastDifficultyAveragePrev = PastDifficultyAverage if LastBlockTime > 0: Diff = (LastBlockTime - BlockReading.get('timestamp')) nActualTimespan += Diff LastBlockTime = BlockReading.get('timestamp') BlockReading = self.read_header((block_height - 1) - CountBlocks) if BlockReading is None: for br in chain: if br.get('block_height') == (block_height - 1) - CountBlocks: BlockReading = br bnNew = PastDifficultyAverage nTargetTimespan = CountBlocks * 120 nActualTimespan = max(nActualTimespan, nTargetTimespan / 3) nActualTimespan = min(nActualTimespan, nTargetTimespan * 3) # retarget bnNew *= nActualTimespan bnNew /= nTargetTimespan bnNew = min(bnNew, max_target) new_bits = target_to_bits(bnNew) return new_bits, bnNew
def get_target_dgw3(self, block_height, chain=None): if chain is None: chain = [] last = self.read_header(block_height-1) if last is None: for h in chain: if h.get('block_height') == block_height-1: last = h # params BlockLastSolved = last BlockReading = last BlockCreating = block_height nActualTimespan = 0 LastBlockTime = 0 PastBlocksMin = 24 PastBlocksMax = 24 CountBlocks = 0 PastDifficultyAverage = 0 PastDifficultyAveragePrev = 0 bnNum = 0 max_target = 0x00000FFFF0000000000000000000000000000000000000000000000000000000 if BlockLastSolved is None or block_height-1 < PastBlocksMin: return 0x1e0ffff0, max_target for i in range(1, PastBlocksMax + 1): CountBlocks += 1 if CountBlocks <= PastBlocksMin: if CountBlocks == 1: PastDifficultyAverage = bits_to_target(BlockReading.get('bits')) else: bnNum = bits_to_target(BlockReading.get('bits')) PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks)+(bnNum)) / (CountBlocks + 1) PastDifficultyAveragePrev = PastDifficultyAverage if LastBlockTime > 0: Diff = (LastBlockTime - BlockReading.get('timestamp')) nActualTimespan += Diff LastBlockTime = BlockReading.get('timestamp') BlockReading = self.read_header((block_height-1) - CountBlocks) if BlockReading is None: for br in chain: if br.get('block_height') == (block_height-1) - CountBlocks: BlockReading = br bnNew = PastDifficultyAverage nTargetTimespan = CountBlocks * 120 nActualTimespan = max(nActualTimespan, nTargetTimespan/3) nActualTimespan = min(nActualTimespan, nTargetTimespan*3) # retarget bnNew *= nActualTimespan bnNew /= nTargetTimespan bnNew = min(bnNew, max_target) new_bits = target_to_bits(bnNew) return new_bits, bnNew