Skip to content

Instantly share code, notes, and snippets.

@wakiyamap
Last active October 5, 2017 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakiyamap/7e4beaeda30380ec15b5989a16181948 to your computer and use it in GitHub Desktop.
Save wakiyamap/7e4beaeda30380ec15b5989a16181948 to your computer and use it in GitHub Desktop.
digishield_pattern2
def get_target_dgsld(self, height, chain=None):
# Litecoin: go back the full period unless it's the first retarget
if chain is None:
chain = {}
nTargetTimespan = 95040 #1.1 days 1.1*24*60*60
nTargetSpacing = 1.5 * 60 #1.5 minute
nTargetSpacingDigisheld = 1.5 * 60 #1.5 minute
nIntervalDigisheld = nTargetSpacingDigisheld // nTargetSpacing #1
nAdjustmentInterval = nIntervalDigisheld #1
blockstogoback = nAdjustmentInterval - 1 #0
if (height != nAdjustmentInterval):
blockstogoback = nAdjustmentInterval
latest_retarget_height = (height // nAdjustmentInterval) * nAdjustmentInterval
last_height = latest_retarget_height - 1
first_height = last_height - blockstogoback #- 301
first = chain.get(first_height)
if first is None:
first = self.read_header(first_height)
last = chain.get(last_height)
if last is None:
last = self.read_header(last_height)
# bits to target
bits = last.get('bits')
bitsN = (bits >> 24) & 0xff
if not (bitsN >= 0x03 and bitsN <= 0x1e):
raise BaseException("First part of bits should be in [0x03, 0x1e]")
bitsBase = bits & 0xffffff
if not (bitsBase >= 0x8000 and bitsBase <= 0x7fffff):
raise BaseException("Second part of bits should be in [0x8000, 0x7fffff]")
target = bitsBase << (8 * (bitsN-3))
if height % nAdjustmentInterval != 0:
return bits, target
# new target
nActualTimespan = last.get('timestamp') - first.get('timestamp')
nActualTimespan = nTargetTimespan + int(float(nActualTimespan - nTargetTimespan) / float(8))
nActualTimespan = max(nActualTimespan, nTargetTimespan - int(float(nTargetTimespan) / float(4)))
nActualTimespan = min(nActualTimespan, nTargetTimespan + int(float(nTargetTimespan) / float(2)))
new_target = min(MAX_TARGET, (target*nActualTimespan) // nTargetTimespan)
# convert new target to bits
c = ("%064x" % int(new_target))[2:]
while c[:2] == '00' and len(c) > 6:
c = c[2:]
bitsN, bitsBase = len(c) // 2, int('0x' + c[:6], 16)
if bitsBase >= 0x800000:
bitsN += 1
bitsBase >>= 8
new_bits = bitsN << 24 | bitsBase
return new_bits, bitsBase << (8 * (bitsN-3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment