Browse Source

Tweak notify handling

master
Neil Booth 8 years ago
parent
commit
a1eb446af4
  1. 63
      server/protocol.py

63
server/protocol.py

@ -108,9 +108,10 @@ class ServerManager(LoggedClass):
def notify(self, height, touched): def notify(self, height, touched):
'''Notify sessions about height changes and touched addresses.''' '''Notify sessions about height changes and touched addresses.'''
sessions = [session for session in self.sessions cache = {}
if isinstance(session, ElectrumX)] for session in self.sessions:
ElectrumX.notify(sessions, height, touched) if isinstance(session, ElectrumX):
session.notify(height, touched, cache)
def stop(self): def stop(self):
'''Close listening servers.''' '''Close listening servers.'''
@ -324,36 +325,36 @@ class ElectrumX(Session):
for prefix, suffixes in rpcs for prefix, suffixes in rpcs
for suffix in suffixes.split()} for suffix in suffixes.split()}
@classmethod def notify(self, height, touched, cache):
def notify(cls, sessions, height, touched): '''Notify the client about changes in height and touched addresses.
headers_payload = height_payload = None
for session in sessions: Cache is a shared cache for this update.
if height != session.notified_height: '''
session.notified_height = height if height != self.notified_height:
if session.subscribe_headers: self.notified_height = height
if headers_payload is None: if self.subscribe_headers:
headers_payload = json_notification_payload( key = 'headers_payload'
'blockchain.headers.subscribe', if key not in cache:
(session.electrum_header(height), ), cache[key] = json_notification_payload(
) 'blockchain.headers.subscribe',
session.send_json(headers_payload) (self.electrum_header(height), ),
)
if session.subscribe_height: self.send_json(cache[key])
if height_payload is None:
height_payload = json_notification_payload( if self.subscribe_height:
'blockchain.numblocks.subscribe',
(height, ),
)
session.send_json(height_payload)
hash168_to_address = session.coin.hash168_to_address
for hash168 in session.hash168s.intersection(touched):
address = hash168_to_address(hash168)
status = session.address_status(hash168)
payload = json_notification_payload( payload = json_notification_payload(
'blockchain.address.subscribe', (address, status)) 'blockchain.numblocks.subscribe',
session.send_json(payload) (height, ),
)
self.send_json(payload)
hash168_to_address = self.coin.hash168_to_address
for hash168 in self.hash168s.intersection(touched):
address = hash168_to_address(hash168)
status = self.address_status(hash168)
payload = json_notification_payload(
'blockchain.address.subscribe', (address, status))
self.send_json(payload)
def height(self): def height(self):
'''Return the block processor's current height.''' '''Return the block processor's current height.'''

Loading…
Cancel
Save