21
21
22
22
from enum import IntFlag
23
23
from math import log
24
- from typing import TYPE_CHECKING
24
+ from typing import TYPE_CHECKING , ClassVar , Optional
25
25
26
26
from structlog import get_logger
27
27
@@ -45,26 +45,21 @@ class TestMode(IntFlag):
45
45
TEST_ALL_WEIGHT = 3
46
46
47
47
48
- TEST_MODE = TestMode .DISABLED
49
-
50
-
51
- def _set_test_mode (mode : TestMode ) -> None :
52
- global TEST_MODE
53
- logger .debug ('change DAA test mode' , from_mode = TEST_MODE .name , to_mode = mode .name )
54
- TEST_MODE = mode
55
-
56
-
57
48
class DifficultyAdjustmentAlgorithm :
49
+ # TODO: This singleton is temporary, and only used in PeerId. It should be removed from there, and then from here.
50
+ singleton : ClassVar [Optional ['DifficultyAdjustmentAlgorithm' ]] = None
58
51
59
- def __init__ (self , * , settings : HathorSettings ) -> None :
52
+ def __init__ (self , * , settings : HathorSettings , test_mode : TestMode = TestMode . DISABLED ) -> None :
60
53
self ._settings = settings
61
54
self .AVG_TIME_BETWEEN_BLOCKS = self ._settings .AVG_TIME_BETWEEN_BLOCKS
62
55
self .MIN_BLOCK_WEIGHT = self ._settings .MIN_BLOCK_WEIGHT
56
+ self .TEST_MODE = test_mode
57
+ DifficultyAdjustmentAlgorithm .singleton = self
63
58
64
59
@cpu .profiler (key = lambda _ , block : 'calculate_block_difficulty!{}' .format (block .hash .hex ()))
65
60
def calculate_block_difficulty (self , block : 'Block' ) -> float :
66
61
""" Calculate block weight according to the ascendents of `block`, using calculate_next_weight."""
67
- if TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
62
+ if self . TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
68
63
return 1.0
69
64
70
65
if block .is_genesis :
@@ -79,7 +74,7 @@ def calculate_next_weight(self, parent_block: 'Block', timestamp: int) -> float:
79
74
80
75
The weight must not be less than `MIN_BLOCK_WEIGHT`.
81
76
"""
82
- if TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
77
+ if self . TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
83
78
return 1.0
84
79
85
80
from hathor .transaction import sum_weights
@@ -166,7 +161,7 @@ def minimum_tx_weight(self, tx: 'Transaction') -> float:
166
161
"""
167
162
# In test mode we don't validate the minimum weight for tx
168
163
# We do this to allow generating many txs for testing
169
- if TEST_MODE & TestMode .TEST_TX_WEIGHT :
164
+ if self . TEST_MODE & TestMode .TEST_TX_WEIGHT :
170
165
return 1.0
171
166
172
167
if tx .is_genesis :
0 commit comments