3535from .util import make_aiohttp_session , NetworkJobOnDefaultServer , random_shuffled_copy , OldTaskGroup , log_exceptions
3636from .bitcoin import address_to_script , script_to_scripthash , is_address , neuter_bitcoin_address
3737from .logging import Logger
38- from .interface import GracefulDisconnect , NetworkTimeout , HistoryTooLong , assert_hash256_str
38+ from .interface import (
39+ GracefulDisconnect , NetworkTimeout , HistoryTooLong , assert_hash256_str , assert_non_negative_integer ,
40+ assert_dict_contains_field , assert_dict , assert_integer ,
41+ )
3942
4043if TYPE_CHECKING :
4144 from .network import Network
@@ -120,10 +123,10 @@ async def _on_address_status(self, addr: str, status: Optional[str]):
120123 """
121124 raise NotImplementedError () # implemented by subclasses
122125
123- async def _on_outpoint_status (self , txid : str , index :int , status : dict ):
126+ async def _on_outpoint_status (self , txid : str , index : int , status : dict ):
124127 raise NotImplementedError () # implemented by subclasses
125128
126- async def _subscribe_to_address (self , addr ):
129+ async def _subscribe_to_address (self , addr : str ):
127130 spk = address_to_script (addr )
128131 h = script_to_scripthash (spk )
129132 self .scripthash_to_address [h ] = addr
@@ -153,8 +156,13 @@ async def _subscribe_to_outpoint(self, outpoint: str, *, spk_hint: str) -> None:
153156 @log_exceptions
154157 async def handle_address_status (self ):
155158 while True :
156- h , status = await self .address_status_queue .get ()
157- addr = self .scripthash_to_address [h ]
159+ sh , status = await self .address_status_queue .get ()
160+ # basic checks for response
161+ assert_hash256_str (sh )
162+ if status is not None :
163+ assert_hash256_str (status )
164+ # process status
165+ addr = self .scripthash_to_address [sh ]
158166 self ._handling_addr_statuses .add (addr )
159167 self .requested_addrs .discard (addr ) # ok for addr not to be present
160168 await self .taskgroup .spawn (self ._on_address_status , addr , status )
@@ -164,7 +172,12 @@ async def handle_address_status(self):
164172 async def handle_outpoint_status (self ):
165173 while True :
166174 txid , index , status = await self .outpoint_status_queue .get ()
167- outpoint = txid + ':%d' % index
175+ # basic checks for response. more checks done in _on_outpoint_status
176+ assert_hash256_str (txid )
177+ assert_non_negative_integer (index )
178+ assert_dict (status )
179+ # process status
180+ outpoint = f"{ txid } :{ index } "
168181 self ._handling_outpoint_statuses .add (outpoint )
169182 self .requested_outpoints .discard (outpoint ) # ok for addr not to be present
170183 await self .taskgroup .spawn (self ._on_outpoint_status , txid , index , status )
@@ -284,26 +297,28 @@ async def disconnect_if_still_stale():
284297
285298 async def _on_outpoint_status (self , txid : str , index : int , status : dict ):
286299 try :
287- assert isinstance ( status , dict )
300+ # check inputs (coming from server )
288301 assert_hash256_str (txid )
302+ assert_non_negative_integer (index )
303+ #
289304 txs = set ()
290305 txs .add (txid )
291- height = status .get ('funder_height' )
292- if height is not None :
293- assert isinstance ( height , int )
306+ funder_height = status .get ('funder_height' )
307+ if funder_height is not None :
308+ assert_integer ( funder_height )
294309 # spv the input
295- self .adb .add_unverified_or_unconfirmed_tx (txid , height )
310+ self .adb .add_unverified_or_unconfirmed_tx (txid , funder_height )
296311 # fetch the output
297312 spender_txid = status .get ('spender_txhash' )
298313 if spender_txid is not None :
299314 assert_hash256_str (spender_txid )
300- spender_height = status . get ( 'spender_height' )
301- assert isinstance (spender_height , int )
315+ spender_height = assert_dict_contains_field ( status , field_name = 'spender_height' )
316+ assert_integer (spender_height )
302317 self .adb .add_unverified_or_unconfirmed_tx (spender_txid , spender_height )
303318 txs .add (spender_txid )
304319 await self ._request_missing_txs (txs , allow_server_not_finding_tx = False )
305320 finally :
306- outpoint = txid + ':%d' % index
321+ outpoint = f" { txid } : { index } "
307322 self ._handling_outpoint_statuses .discard (outpoint )
308323
309324
0 commit comments