Browse Source

commands: "notify" cmd: stop watching addr if called with empty URL

closes #5881
master
SomberNight 5 years ago
parent
commit
e2ae44beb9
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 11
      electrum/commands.py
  2. 13
      electrum/synchronizer.py

11
electrum/commands.py

@ -898,11 +898,16 @@ class Commands:
return True return True
@command('n') @command('n')
async def notify(self, address: str, URL: str): async def notify(self, address: str, URL: Optional[str]):
"""Watch an address. Every time the address changes, a http POST is sent to the URL.""" """Watch an address. Every time the address changes, a http POST is sent to the URL.
Call with an empty URL to stop watching an address.
"""
if not hasattr(self, "_notifier"): if not hasattr(self, "_notifier"):
self._notifier = Notifier(self.network) self._notifier = Notifier(self.network)
await self._notifier.start_watching_queue.put((address, URL)) if URL:
await self._notifier.start_watching_addr(address, URL)
else:
await self._notifier.stop_watching_addr(address)
return True return True
@command('wn') @command('wn')

13
electrum/synchronizer.py

@ -263,7 +263,7 @@ class Notifier(SynchronizerBase):
def __init__(self, network): def __init__(self, network):
SynchronizerBase.__init__(self, network) SynchronizerBase.__init__(self, network)
self.watched_addresses = defaultdict(list) # type: Dict[str, List[str]] self.watched_addresses = defaultdict(list) # type: Dict[str, List[str]]
self.start_watching_queue = asyncio.Queue() self._start_watching_queue = asyncio.Queue() # type: asyncio.Queue[Tuple[str, str]]
async def main(self): async def main(self):
# resend existing subscriptions if we were restarted # resend existing subscriptions if we were restarted
@ -271,11 +271,20 @@ class Notifier(SynchronizerBase):
await self._add_address(addr) await self._add_address(addr)
# main loop # main loop
while True: while True:
addr, url = await self.start_watching_queue.get() addr, url = await self._start_watching_queue.get()
self.watched_addresses[addr].append(url) self.watched_addresses[addr].append(url)
await self._add_address(addr) await self._add_address(addr)
async def start_watching_addr(self, addr: str, url: str):
await self._start_watching_queue.put((addr, url))
async def stop_watching_addr(self, addr: str):
self.watched_addresses.pop(addr, None)
# TODO blockchain.scripthash.unsubscribe
async def _on_address_status(self, addr, status): async def _on_address_status(self, addr, status):
if addr not in self.watched_addresses:
return
self.logger.info(f'new status for addr {addr}') self.logger.info(f'new status for addr {addr}')
headers = {'content-type': 'application/json'} headers = {'content-type': 'application/json'}
data = {'address': addr, 'status': status} data = {'address': addr, 'status': status}

Loading…
Cancel
Save