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,20 @@ 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
+ singleton : ClassVar [Optional ['DifficultyAdjustmentAlgorithm' ]] = None
58
50
59
- def __init__ (self , * , settings : HathorSettings ) -> None :
51
+ def __init__ (self , * , settings : HathorSettings , test_mode : TestMode = TestMode . DISABLED ) -> None :
60
52
self ._settings = settings
61
53
self .AVG_TIME_BETWEEN_BLOCKS = self ._settings .AVG_TIME_BETWEEN_BLOCKS
62
54
self .MIN_BLOCK_WEIGHT = self ._settings .MIN_BLOCK_WEIGHT
55
+ self .TEST_MODE = test_mode
56
+ DifficultyAdjustmentAlgorithm .singleton = self
63
57
64
58
@cpu .profiler (key = lambda _ , block : 'calculate_block_difficulty!{}' .format (block .hash .hex ()))
65
59
def calculate_block_difficulty (self , block : 'Block' ) -> float :
66
60
""" Calculate block weight according to the ascendents of `block`, using calculate_next_weight."""
67
- if TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
61
+ if self . TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
68
62
return 1.0
69
63
70
64
if block .is_genesis :
@@ -79,7 +73,7 @@ def calculate_next_weight(self, parent_block: 'Block', timestamp: int) -> float:
79
73
80
74
The weight must not be less than `MIN_BLOCK_WEIGHT`.
81
75
"""
82
- if TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
76
+ if self . TEST_MODE & TestMode .TEST_BLOCK_WEIGHT :
83
77
return 1.0
84
78
85
79
from hathor .transaction import sum_weights
@@ -166,7 +160,7 @@ def minimum_tx_weight(self, tx: 'Transaction') -> float:
166
160
"""
167
161
# In test mode we don't validate the minimum weight for tx
168
162
# We do this to allow generating many txs for testing
169
- if TEST_MODE & TestMode .TEST_TX_WEIGHT :
163
+ if self . TEST_MODE & TestMode .TEST_TX_WEIGHT :
170
164
return 1.0
171
165
172
166
if tx .is_genesis :
0 commit comments