Browse Source

fix issue #1572

283
ThomasV 9 years ago
parent
commit
9ec4cbeed1
  1. 21
      lib/network.py

21
lib/network.py

@ -491,12 +491,16 @@ class Network(util.DaemonThread):
for callback in callbacks: for callback in callbacks:
callback(response) callback(response)
def get_index(self, method, params):
""" hashable index for subscriptions and cache"""
return str(method) + (':' + str(params[0]) if params else '')
def process_responses(self, interface): def process_responses(self, interface):
responses = interface.get_responses() responses = interface.get_responses()
for request, response in responses: for request, response in responses:
if request: if request:
method, params, message_id = request method, params, message_id = request
k = self.get_index(method, params)
# client requests go through self.send() with a # client requests go through self.send() with a
# callback, are only sent to the current interface, # callback, are only sent to the current interface,
# and are placed in the unanswered_requests dictionary # and are placed in the unanswered_requests dictionary
@ -520,16 +524,18 @@ class Network(util.DaemonThread):
# Rewrite response shape to match subscription request response # Rewrite response shape to match subscription request response
method = response.get('method') method = response.get('method')
params = response.get('params') params = response.get('params')
k = self.get_index(method, params)
if method == 'blockchain.headers.subscribe': if method == 'blockchain.headers.subscribe':
response['result'] = params[0] response['result'] = params[0]
response['params'] = [] response['params'] = []
elif method == 'blockchain.address.subscribe': elif method == 'blockchain.address.subscribe':
response['params'] = [params[0]] # addr response['params'] = [params[0]] # addr
response['result'] = params[1] response['result'] = params[1]
callbacks = self.subscriptions.get(repr((method, params)), []) callbacks = self.subscriptions.get(k, [])
# update cache if it's a subscription # update cache if it's a subscription
if method.endswith('.subscribe'): if method.endswith('.subscribe'):
self.sub_cache[repr((method, params))] = response self.sub_cache[k] = response
# Response is now in canonical form # Response is now in canonical form
self.process_response(interface, response, callbacks) self.process_response(interface, response, callbacks)
@ -538,7 +544,6 @@ class Network(util.DaemonThread):
with self.lock: with self.lock:
self.pending_sends.append((messages, callback)) self.pending_sends.append((messages, callback))
def process_pending_sends(self): def process_pending_sends(self):
# Requests needs connectivity. If we don't have an interface, # Requests needs connectivity. If we don't have an interface,
# we cannot process them. # we cannot process them.
@ -551,16 +556,16 @@ class Network(util.DaemonThread):
for messages, callback in sends: for messages, callback in sends:
for method, params in messages: for method, params in messages:
k = repr((method, params)) r = None
if method.endswith('.subscribe'): if method.endswith('.subscribe'):
k = self.get_index(method, params)
# add callback to list # add callback to list
l = self.subscriptions.get(k, []) l = self.subscriptions.get(k, [])
if callback not in l: if callback not in l:
l.append(callback) l.append(callback)
self.subscriptions[k] = l self.subscriptions[k] = l
# check cached response for subscriptions
# check cached response for subscriptions r = self.sub_cache.get(k)
r = self.sub_cache.get(k)
if r is not None: if r is not None:
util.print_error("cache hit", k) util.print_error("cache hit", k)
callback(r) callback(r)

Loading…
Cancel
Save