18
18
from structlog import get_logger
19
19
from twisted .internet .task import LoopingCall
20
20
21
- from hathor .conf import HathorSettings
21
+ from hathor .conf . get_settings import get_settings
22
22
from hathor .indexes .height_index import HeightInfo
23
23
from hathor .p2p .messages import ProtocolMessages
24
24
from hathor .p2p .peer_id import PeerId
33
33
34
34
logger = get_logger ()
35
35
36
- settings = HathorSettings ()
37
-
38
36
39
37
class ReadyState (BaseState ):
40
38
def __init__ (self , protocol : 'HathorProtocol' ) -> None :
41
39
super ().__init__ (protocol )
40
+ self ._settings = get_settings ()
42
41
43
42
self .log = logger .new (** self .protocol .get_logger_context ())
44
43
@@ -79,7 +78,7 @@ def __init__(self, protocol: 'HathorProtocol') -> None:
79
78
80
79
# if the peer has the GET-BEST-BLOCKCHAIN capability
81
80
common_capabilities = protocol .capabilities & set (protocol .node .capabilities )
82
- if (settings .CAPABILITY_GET_BEST_BLOCKCHAIN in common_capabilities ):
81
+ if (self . _settings .CAPABILITY_GET_BEST_BLOCKCHAIN in common_capabilities ):
83
82
# set the loop to get the best blockchain from the peer
84
83
self .lc_get_best_blockchain = LoopingCall (self .send_get_best_blockchain )
85
84
self .lc_get_best_blockchain .clock = self .reactor
@@ -110,7 +109,7 @@ def on_enter(self) -> None:
110
109
self .send_get_peers ()
111
110
112
111
if self .lc_get_best_blockchain is not None :
113
- self .lc_get_best_blockchain .start (settings .BEST_BLOCKCHAIN_INTERVAL , now = False )
112
+ self .lc_get_best_blockchain .start (self . _settings .BEST_BLOCKCHAIN_INTERVAL , now = False )
114
113
115
114
self .sync_agent .start ()
116
115
@@ -209,11 +208,12 @@ def handle_pong(self, payload: str) -> None:
209
208
self .ping_start_time = None
210
209
self .log .debug ('rtt updated' , rtt = self .ping_rtt , min_rtt = self .ping_min_rtt )
211
210
212
- def send_get_best_blockchain (self , n_blocks : int = settings . DEFAULT_BEST_BLOCKCHAIN_BLOCKS ) -> None :
211
+ def send_get_best_blockchain (self , n_blocks : Optional [ int ] = None ) -> None :
213
212
""" Send a GET-BEST-BLOCKCHAIN command, requesting a list of the latest
214
213
N blocks from the best blockchain.
215
214
"""
216
- self .send_message (ProtocolMessages .GET_BEST_BLOCKCHAIN , str (n_blocks ))
215
+ actual_n_blocks : int = n_blocks if n_blocks is not None else self ._settings .DEFAULT_BEST_BLOCKCHAIN_BLOCKS
216
+ self .send_message (ProtocolMessages .GET_BEST_BLOCKCHAIN , str (actual_n_blocks ))
217
217
218
218
def handle_get_best_blockchain (self , payload : str ) -> None :
219
219
""" Executed when a GET-BEST-BLOCKCHAIN command is received.
@@ -228,9 +228,9 @@ def handle_get_best_blockchain(self, payload: str) -> None:
228
228
)
229
229
return
230
230
231
- if not (0 < n_blocks <= settings .MAX_BEST_BLOCKCHAIN_BLOCKS ):
231
+ if not (0 < n_blocks <= self . _settings .MAX_BEST_BLOCKCHAIN_BLOCKS ):
232
232
self .protocol .send_error_and_close_connection (
233
- f'N out of bounds. Valid range: [1, { settings .MAX_BEST_BLOCKCHAIN_BLOCKS } ].'
233
+ f'N out of bounds. Valid range: [1, { self . _settings .MAX_BEST_BLOCKCHAIN_BLOCKS } ].'
234
234
)
235
235
return
236
236
0 commit comments