2424# SOFTWARE.
2525import asyncio
2626import hashlib
27- from typing import Dict , List , TYPE_CHECKING , Tuple , Set
27+ from typing import Dict , List , TYPE_CHECKING , Tuple , Set , Optional , Sequence
2828from collections import defaultdict
2929import logging
3030
4545class SynchronizerFailure (Exception ): pass
4646
4747
48- def history_status (h ) :
48+ def history_status (h : Sequence [ tuple [ str , int ]]) -> Optional [ str ] :
4949 if not h :
5050 return None
5151 status = ''
@@ -96,7 +96,7 @@ async def _add_address(self, addr: str):
9696 finally :
9797 self ._adding_addrs .discard (addr ) # ok for addr not to be present
9898
99- async def _on_address_status (self , addr , status ):
99+ async def _on_address_status (self , addr : str , status : Optional [ str ] ):
100100 """Handle the change of the status of an address.
101101 Should remove addr from self._handling_addr_statuses when done.
102102 """
@@ -160,10 +160,19 @@ def is_up_to_date(self):
160160 and not self ._stale_histories
161161 and self .status_queue .empty ())
162162
163+ async def _maybe_request_history_for_addr (self , addr : str ) -> List [dict ]:
164+ sh = address_to_scripthash (addr )
165+ self ._requests_sent += 1
166+ async with self ._network_request_semaphore :
167+ result = await self .interface .get_history_for_scripthash (sh )
168+ self ._requests_answered += 1
169+ self .logger .info (f"receiving history { addr } { len (result )} " )
170+ return result
171+
163172 async def _on_address_status (self , addr , status ):
164173 try :
165- history = self .adb .db .get_addr_history (addr )
166- if history_status (history ) == status :
174+ old_history = self .adb .db .get_addr_history (addr )
175+ if history_status (old_history ) == status :
167176 return
168177 # No point in requesting history twice for the same announced status.
169178 # However if we got announced a new status, we should request history again:
@@ -174,12 +183,7 @@ async def _on_address_status(self, addr, status):
174183 self ._stale_histories .pop (addr , asyncio .Future ()).cancel ()
175184 finally :
176185 self ._handling_addr_statuses .discard (addr )
177- h = address_to_scripthash (addr )
178- self ._requests_sent += 1
179- async with self ._network_request_semaphore :
180- result = await self .interface .get_history_for_scripthash (h )
181- self ._requests_answered += 1
182- self .logger .info (f"receiving history { addr } { len (result )} " )
186+ result = await self ._maybe_request_history_for_addr (addr )
183187 hist = list (map (lambda item : (item ['tx_hash' ], item ['height' ]), result ))
184188 # tx_fees
185189 tx_fees = [(item ['tx_hash' ], item .get ('fee' )) for item in result ]
@@ -242,7 +246,7 @@ async def _get_transaction(self, tx_hash, *, allow_server_not_finding_tx=False):
242246 raise SynchronizerFailure (f"received tx does not match expected txid ({ tx_hash } != { tx .txid ()} )" )
243247 self .requested_tx .remove (tx_hash )
244248 self .adb .receive_tx_callback (tx )
245- self .logger .info (f"received tx { tx_hash } . bytes: { len (raw_tx )} " )
249+ self .logger .info (f"received tx { tx_hash } . bytes-len : { len (raw_tx )// 2 } " )
246250
247251 async def main (self ):
248252 self .adb .up_to_date_changed ()
0 commit comments