Skip to content

Instantly share code, notes, and snippets.

@wakiyamap
Last active October 6, 2017 07:42
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/f1a0dd0e2b160cd7f2e8c5c74b05dbb2 to your computer and use it in GitHub Desktop.
Save wakiyamap/f1a0dd0e2b160cd7f2e8c5c74b05dbb2 to your computer and use it in GitHub Desktop.
digishield_pattern1
def bits_to_target(self, bits):
MM = 256*256*256
a = bits%MM
if a < 0x8000:
a *= 256
target = (a) * pow(2, 8 * (bits//MM - 3))
return target
def target_to_bits(self, target):
MM = 256*256*256
c = ("%064X"%int(target))[2:]
i = 31
while c[0:2]=="00":
c = c[2:]
i -= 1
c = int('0x'+c[0:6],16)
if c >= 0x800000:
c //= 256
i += 1
new_bits = c + MM * i
return new_bits
def get_target_dgsld(self, height, chain=None):
if chain is None:
chain = {}
nPowTargetTimespan = 95040 #1.1 days 1.1*24*60*60
nPowTargetSpacing = 90 #1.5 minute
nPowTargetSpacingDigisheld = 90 #1.5 minute
DifficultyAdjustmentIntervalDigisheld = nPowTargetSpacingDigisheld // nPowTargetSpacing #1
AdjustmentInterval = DifficultyAdjustmentIntervalDigisheld
blockstogoback = AdjustmentInterval - 1
if (height != AdjustmentInterval):
blockstogoback = AdjustmentInterval
#latest_retarget_height = (height // AdjustmentInterval) * AdjustmentInterval
#last_height = latest_retarget_height - 1
last_height = height - 1
first_height = last_height - blockstogoback
TargetTimespan = nPowTargetSpacingDigisheld
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)
nActualTimespan = last.get('timestamp') - first.get('timestamp')
nActualTimespan = TargetTimespan + int(float(nActualTimespan - TargetTimespan) / float(8))
nActualTimespan = max(nActualTimespan, TargetTimespan - int(float(TargetTimespan) / float(4)))
nActualTimespan = min(nActualTimespan, TargetTimespan + int(float(TargetTimespan) / float(2)))
bits = last.get('bits')
bnNew = self.bits_to_target(bits)
if height % AdjustmentInterval != 0:
return bits, bnNew
# retarget
bnNew *= nActualTimespan
bnNew //= TargetTimespan
bnNew = min(bnNew, MAX_TARGET)
new_bits = self.target_to_bits(bnNew)
return new_bits, bnNew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment