Skip to content

Commit 68bed3e

Browse files
committed
Add hard diff removal hard fork support on mainnet
1 parent 14525de commit 68bed3e

1 file changed

Lines changed: 58 additions & 10 deletions

File tree

electrum/blockchain.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,31 @@
4444
# see https://github.com/FACT0RN/FACT0RN/blob/main/src/chainparams.cpp#L85
4545
# FACT0RN is the opposite of bitcoin in this regard, we do not have a maximum
4646
# value for a hash, but rather a minimum value for factorization measured in bits.
47-
# TODO: change MAX_TARGET variable name to MIN_TARGET everywhere it is used.
48-
MAX_TARGET = 230
47+
MIN_TARGET = 230
48+
49+
"""
50+
$ ./factorn-cli getblockchaininfo
51+
{
52+
(...)
53+
"softforks": {
54+
(...)
55+
"hard_diff_removal": {
56+
"type": "bip9",
57+
"bip9": {
58+
"status": "active",
59+
"start_time": 1743465600,
60+
"timeout": 1775001600,
61+
"since": 168672,
62+
"min_activation_height": 160000
63+
},
64+
"height": 168672,
65+
"active": true
66+
}
67+
},
68+
"warnings": ""
69+
}
70+
"""
71+
MAINNET_HARD_DIFF_REMOVAL_ACTIVATION_HEIGHT = 168672
4972

5073
class MissingHeader(Exception):
5174
pass
@@ -565,7 +588,7 @@ def get_target(self, index: int) -> int:
565588
if constants.net.TESTNET:
566589
return 0
567590
if index == -1:
568-
return MAX_TARGET
591+
return MIN_TARGET
569592

570593
# new target
571594
first = self.read_header(index * 672)
@@ -576,13 +599,38 @@ def get_target(self, index: int) -> int:
576599
nActualTimespan = last.get('timestamp') - first.get('timestamp')
577600
nTargetTimespan = 14 * 24 * 60 * 60
578601
nRatio = nActualTimespan/nTargetTimespan
579-
new_target = last.get("bits")
580-
581-
#Retarget
582-
if nRatio > 1.0333:
583-
new_target -= 1
584-
elif nRatio < 0.90:
585-
new_target += 1
602+
nBits = int(last.get("bits"))
603+
new_target = nBits
604+
605+
# Determine if hard diff removal is active
606+
firstBlockHeightThisEpoch = (index + 1) * 672
607+
hardDiffRemoved = firstBlockHeightThisEpoch >= MAINNET_HARD_DIFF_REMOVAL_ACTIVATION_HEIGHT
608+
609+
# Retarget
610+
if hardDiffRemoved:
611+
if nRatio > 1.0333:
612+
if nBits % 2 == 0:
613+
new_target -= 2
614+
else:
615+
new_target -= 1
616+
617+
if nRatio > 2.0:
618+
new_target -= 2
619+
elif nRatio < 0.90:
620+
if nBits % 2 == 0:
621+
new_target += 2
622+
else:
623+
new_target += 1
624+
625+
if nRatio < 0.5:
626+
new_target += 2
627+
elif nBits % 2 == 1:
628+
new_target -= 1
629+
else:
630+
if nRatio > 1.0333:
631+
new_target -= 1
632+
elif nRatio < 0.90:
633+
new_target += 1
586634

587635
return new_target
588636

0 commit comments

Comments
 (0)