Files

50 lines
1.3 KiB
Python
Raw Permalink Normal View History

2017-09-05 15:27:08 +02:00
#!/usr/bin/env python3
2012-04-30 11:48:19 +02:00
2014-07-29 10:43:04 +02:00
import sys
2018-11-02 20:14:59 +01:00
import asyncio
from electrum.network import Network
from electrum.util import print_msg, create_and_start_event_loop
from electrum.synchronizer import SynchronizerBase
from electrum.simple_config import SimpleConfig
2018-11-02 20:14:59 +01:00
2012-05-10 14:38:49 +02:00
2012-04-30 11:48:19 +02:00
try:
addr = sys.argv[1]
2013-11-10 12:30:57 -08:00
except Exception:
2017-09-05 15:27:08 +02:00
print("usage: watch_address <bitcoin_address>")
2012-06-14 21:11:37 +02:00
sys.exit(1)
2012-04-30 11:48:19 +02:00
config = SimpleConfig()
2014-07-29 10:43:04 +02:00
# start network
scripts: fix "cannot schedule new futures after interpreter shutdown" - looks like around python3.9, they changed it so that if we don't block on the main thread, it starts to shut things down - polling thread.join() makes Ctrl+C work. kind of. ``` $ ./electrum/scripts/txradar.py 6bde84a981e72573666fcc51c81ec3f8f4a813709bf16451dce3f106a114d392 Exception in run: RuntimeError('cannot schedule new futures after interpreter shutdown') Traceback (most recent call last): File "/home/user/wspace/electrum/electrum/util.py", line 1218, in wrapper return await func(*args, **kwargs) File "/home/user/wspace/electrum/electrum/interface.py", line 649, in wrapper_func return await func(self, *args, **kwargs) File "/home/user/wspace/electrum/electrum/interface.py", line 675, in run await self.open_session(ssl_context=ssl_context) File "/home/user/wspace/electrum/electrum/interface.py", line 872, in open_session async with _RSClient( File "/home/user/.local/lib/python3.10/site-packages/aiorpcx/rawsocket.py", line 167, in __aenter__ _transport, protocol = await self.create_connection() File "/home/user/wspace/electrum/electrum/interface.py", line 285, in create_connection return await super().create_connection() File "/home/user/.local/lib/python3.10/site-packages/aiorpcx/rawsocket.py", line 163, in create_connection return await connector.create_connection( File "/usr/lib/python3.10/asyncio/base_events.py", line 1036, in create_connection infos = await self._ensure_resolved( File "/usr/lib/python3.10/asyncio/base_events.py", line 1418, in _ensure_resolved return await loop.getaddrinfo(host, port, family=family, type=type, File "/usr/lib/python3.10/asyncio/base_events.py", line 863, in getaddrinfo return await self.run_in_executor( File "/usr/lib/python3.10/asyncio/base_events.py", line 821, in run_in_executor executor.submit(func, *args), loop=self) File "/usr/lib/python3.10/concurrent/futures/thread.py", line 169, in submit raise RuntimeError('cannot schedule new futures after ' RuntimeError: cannot schedule new futures after interpreter shutdown ```
2025-07-15 12:00:31 +00:00
loop, stopping_fut, loop_thread = create_and_start_event_loop()
network = Network(config)
2014-07-29 10:43:04 +02:00
network.start()
2012-04-30 11:48:19 +02:00
2014-07-29 10:43:04 +02:00
2018-11-02 20:14:59 +01:00
class Notifier(SynchronizerBase):
def __init__(self, network):
SynchronizerBase.__init__(self, network)
self.watched_addresses = set()
self.watch_queue = asyncio.Queue()
async def main(self):
# resend existing subscriptions if we were restarted
for addr in self.watched_addresses:
Revert "wallet.is_up_to_date: fix flickering during sync due to race" This reverts commit dc6c4814068809b6f6d2df9aac0fa90d827a15f2 as it introduced its own issue: while add_address was running on one thread, synchronizer._reset could be running on another, and by the time the "enqueue" coro would run, it would use a new add_queue and addr would not be in requested_addrs anymore... ``` I/w | wallet.Standard_Wallet.[test_segwit_2] | starting taskgroup. I | lnworker.LNWallet.[test_segwit_2] | starting taskgroup. E/i | interface.[testnet.qtornado.com:51002] | Exception in run: KeyError('tb1q3wmgf8n5eettnj50pzgnfrrpdpjmwn37x7nzsc5780kk4je9v4hspym8mu') Traceback (most recent call last): File ".../electrum/electrum/util.py", line 1243, in wrapper return await func(*args, **kwargs) File ".../electrum/electrum/interface.py", line 506, in wrapper_func return await func(self, *args, **kwargs) File ".../electrum/electrum/interface.py", line 529, in run await self.open_session(ssl_context) File ".../electrum/electrum/interface.py", line 679, in open_session async with self.taskgroup as group: File ".../aiorpcX/aiorpcx/curio.py", line 304, in __aexit__ await self.join() File ".../electrum/electrum/util.py", line 1339, in join task.result() File ".../electrum/electrum/synchronizer.py", line 80, in _run_tasks async with taskgroup as group: File ".../aiorpcX/aiorpcx/curio.py", line 304, in __aexit__ await self.join() File ".../electrum/electrum/util.py", line 1339, in join task.result() File ".../electrum/electrum/synchronizer.py", line 127, in subscribe_to_address self.requested_addrs.remove(addr) KeyError: 'tb1q3wmgf8n5eettnj50pzgnfrrpdpjmwn37x7nzsc5780kk4je9v4hspym8mu' ```
2022-12-20 16:15:24 +00:00
await self._add_address(addr)
2018-11-02 20:14:59 +01:00
# main loop
while True:
addr = await self.watch_queue.get()
self.watched_addresses.add(addr)
Revert "wallet.is_up_to_date: fix flickering during sync due to race" This reverts commit dc6c4814068809b6f6d2df9aac0fa90d827a15f2 as it introduced its own issue: while add_address was running on one thread, synchronizer._reset could be running on another, and by the time the "enqueue" coro would run, it would use a new add_queue and addr would not be in requested_addrs anymore... ``` I/w | wallet.Standard_Wallet.[test_segwit_2] | starting taskgroup. I | lnworker.LNWallet.[test_segwit_2] | starting taskgroup. E/i | interface.[testnet.qtornado.com:51002] | Exception in run: KeyError('tb1q3wmgf8n5eettnj50pzgnfrrpdpjmwn37x7nzsc5780kk4je9v4hspym8mu') Traceback (most recent call last): File ".../electrum/electrum/util.py", line 1243, in wrapper return await func(*args, **kwargs) File ".../electrum/electrum/interface.py", line 506, in wrapper_func return await func(self, *args, **kwargs) File ".../electrum/electrum/interface.py", line 529, in run await self.open_session(ssl_context) File ".../electrum/electrum/interface.py", line 679, in open_session async with self.taskgroup as group: File ".../aiorpcX/aiorpcx/curio.py", line 304, in __aexit__ await self.join() File ".../electrum/electrum/util.py", line 1339, in join task.result() File ".../electrum/electrum/synchronizer.py", line 80, in _run_tasks async with taskgroup as group: File ".../aiorpcX/aiorpcx/curio.py", line 304, in __aexit__ await self.join() File ".../electrum/electrum/util.py", line 1339, in join task.result() File ".../electrum/electrum/synchronizer.py", line 127, in subscribe_to_address self.requested_addrs.remove(addr) KeyError: 'tb1q3wmgf8n5eettnj50pzgnfrrpdpjmwn37x7nzsc5780kk4je9v4hspym8mu' ```
2022-12-20 16:15:24 +00:00
await self._add_address(addr)
2018-11-02 20:14:59 +01:00
async def _on_address_status(self, addr, status):
print_msg(f"addr {addr}, status {status}")
2013-10-06 12:28:45 +02:00
2018-11-02 20:14:59 +01:00
notifier = Notifier(network)
asyncio.run_coroutine_threadsafe(notifier.watch_queue.put(addr), loop)
scripts: fix "cannot schedule new futures after interpreter shutdown" - looks like around python3.9, they changed it so that if we don't block on the main thread, it starts to shut things down - polling thread.join() makes Ctrl+C work. kind of. ``` $ ./electrum/scripts/txradar.py 6bde84a981e72573666fcc51c81ec3f8f4a813709bf16451dce3f106a114d392 Exception in run: RuntimeError('cannot schedule new futures after interpreter shutdown') Traceback (most recent call last): File "/home/user/wspace/electrum/electrum/util.py", line 1218, in wrapper return await func(*args, **kwargs) File "/home/user/wspace/electrum/electrum/interface.py", line 649, in wrapper_func return await func(self, *args, **kwargs) File "/home/user/wspace/electrum/electrum/interface.py", line 675, in run await self.open_session(ssl_context=ssl_context) File "/home/user/wspace/electrum/electrum/interface.py", line 872, in open_session async with _RSClient( File "/home/user/.local/lib/python3.10/site-packages/aiorpcx/rawsocket.py", line 167, in __aenter__ _transport, protocol = await self.create_connection() File "/home/user/wspace/electrum/electrum/interface.py", line 285, in create_connection return await super().create_connection() File "/home/user/.local/lib/python3.10/site-packages/aiorpcx/rawsocket.py", line 163, in create_connection return await connector.create_connection( File "/usr/lib/python3.10/asyncio/base_events.py", line 1036, in create_connection infos = await self._ensure_resolved( File "/usr/lib/python3.10/asyncio/base_events.py", line 1418, in _ensure_resolved return await loop.getaddrinfo(host, port, family=family, type=type, File "/usr/lib/python3.10/asyncio/base_events.py", line 863, in getaddrinfo return await self.run_in_executor( File "/usr/lib/python3.10/asyncio/base_events.py", line 821, in run_in_executor executor.submit(func, *args), loop=self) File "/usr/lib/python3.10/concurrent/futures/thread.py", line 169, in submit raise RuntimeError('cannot schedule new futures after ' RuntimeError: cannot schedule new futures after interpreter shutdown ```
2025-07-15 12:00:31 +00:00
while loop_thread.is_alive():
loop_thread.join(1)