Skip to content

Instantly share code, notes, and snippets.

@wakiyamap

wakiyamap/from base doge

Last active Oct 6, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.