import threading
import time
+import composed
+
class Backend:
def __init__(self):
self.backend.blockchain.subscribe_reorganize(self.reorganize)
self.backend.blockchain.fetch_last_depth(self.set_last_depth)
self.latest = GhostValue()
+ self.subscribed = []
def subscribe(self, session, request):
last = self.latest.get()
session.push_response({"id": request["id"], "result": last})
+ with self.lock:
+ self.subscribed.append((session, request))
def set_last_depth(self, ec, last_depth):
if ec:
print "Error retrieving last depth", ec
else:
- with self.lock:
- self.latest.set(last_depth)
+ self.latest.set(last_depth)
def reorganize(self, ec, fork_point, arrivals, replaced):
- pass
+ latest = fork_point + len(arrivals)
+ self.latest.set(latest)
+ subscribed = self.spring_clean()
+ for session, request in subscribed:
+ session.push_response({"id": request["id"], "result": latest})
+
+ def spring_clean(self):
+ with self.lock:
+ self.subscribed = [sub for sub in self.subscribed
+ if not sub[0].stopped()]
+ return self.subscribed[:]
+
+class AddressGetHistory:
+
+ def __init__(self, backend):
+ self.backend = backend
+
+ def get(self, session, request):
+ address = str(request["params"])
+ composed.payment_history(self.backend.blockchain, address,
+ bitcoin.bind(self.respond, session, request, bitcoin._1))
+
+ def respond(self, session, request, result):
+ session.push_response({"id": request["id"], "result": result})
class LibbitcoinProcessor(stratum.Processor):
def __init__(self):
self.backend = Backend()
self.numblocks_subscribe = NumblocksSubscribe(self.backend)
+ self.address_get_history = AddressGetHistory(self.backend)
stratum.Processor.__init__(self)
def stop(self):
def process(self, session):
request = session.pop_request()
+ print "New request (lib)", request
if request["method"] == "numblocks.subscribe":
self.numblocks_subscribe.subscribe(session, request)
- print "New request (lib)", request
+ elif request["method"] == "address.get_history":
+ self.address_get_history.get(session, request)
# Execute and when ready, you call
# session.push_response(response)