Browse Source

wallet.is_up_to_date: fix flickering during sync due to race

AddressSynchronizer.add_address called synchronizer.add, which would only
schedule adding the addr to the Synchronizer in the next event loop iter.
If during that time, the synchronizer called adb.set_up_to_date(True),
the wallet would falsely believe and advertise itself as up_to_date
(as the wallet would see wallet.synchronize not creating new addresses,
and adb (via synchronizer) telling it is up_to_date).
Moments later, the synchronizer._add_address is finally executed and
up_to_date=False propagates out synchronizer->adb->wallet.
master
SomberNight 3 years ago
parent
commit
dc6c481406
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 2
      electrum/address_synchronizer.py
  2. 4
      electrum/scripts/watch_address.py
  3. 18
      electrum/synchronizer.py

2
electrum/address_synchronizer.py

@ -212,7 +212,7 @@ class AddressSynchronizer(Logger, EventListener):
self.db.history[address] = []
self.set_up_to_date(False)
if self.synchronizer:
self.synchronizer.add(address)
self.synchronizer.add_address(address)
def get_conflicting_transactions(self, tx_hash, tx: Transaction, include_self=False):
"""Returns a set of transaction hashes from the wallet history that are

4
electrum/scripts/watch_address.py

@ -32,12 +32,12 @@ class Notifier(SynchronizerBase):
async def main(self):
# resend existing subscriptions if we were restarted
for addr in self.watched_addresses:
await self._add_address(addr)
self.add_address(addr)
# main loop
while True:
addr = await self.watch_queue.get()
self.watched_addresses.add(addr)
await self._add_address(addr)
self.add_address(addr)
async def _on_address_status(self, addr, status):
print_msg(f"addr {addr}, status {status}")

18
electrum/synchronizer.py

@ -83,15 +83,17 @@ class SynchronizerBase(NetworkJobOnDefaultServer):
# we are being cancelled now
self.session.unsubscribe(self.status_queue)
def add(self, addr):
asyncio.run_coroutine_threadsafe(self._add_address(addr), self.asyncio_loop)
async def _add_address(self, addr: str):
# note: this method is async as add_queue.put_nowait is not thread-safe.
def add_address(self, addr: str) -> None:
"""Add an address to subscribe to.
Thread-safe. When the method returns, the Synchronizer (e.g. is_up_to_date) will know about the address.
"""
if not is_address(addr): raise ValueError(f"invalid bitcoin address {addr}")
if addr in self.requested_addrs: return
self.requested_addrs.add(addr)
async def enqueue():
# note: this method is async as add_queue.put_nowait is not thread-safe.
self.add_queue.put_nowait(addr)
asyncio.run_coroutine_threadsafe(enqueue(), self.asyncio_loop)
async def _on_address_status(self, addr, status):
"""Handle the change of the status of an address."""
@ -245,7 +247,7 @@ class Synchronizer(SynchronizerBase):
await self._request_missing_txs(history, allow_server_not_finding_tx=True)
# add addresses to bootstrap
for addr in random_shuffled_copy(self.adb.get_addresses()):
await self._add_address(addr)
self.add_address(addr)
# main loop
while True:
await asyncio.sleep(0.1)
@ -271,12 +273,12 @@ class Notifier(SynchronizerBase):
async def main(self):
# resend existing subscriptions if we were restarted
for addr in self.watched_addresses:
await self._add_address(addr)
self.add_address(addr)
# main loop
while True:
addr, url = await self._start_watching_queue.get()
self.watched_addresses[addr].append(url)
await self._add_address(addr)
self.add_address(addr)
async def start_watching_addr(self, addr: str, url: str):
await self._start_watching_queue.put((addr, url))

Loading…
Cancel
Save