X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=modules%2Fpython_bitcoin%2F__init__.py;h=97440ce794ecd409de1c9db19fff663597cefd5f;hb=ff99a48192b6a39b3ee301c8836eb11b283f7c1a;hp=c89d3778156ea44b56aa5af94b231585ad7511ab;hpb=043aa09b70af4ab4cf99088aa358178d2b4c3e5a;p=electrum-server.git diff --git a/modules/python_bitcoin/__init__.py b/modules/python_bitcoin/__init__.py index c89d377..97440ce 100644 --- a/modules/python_bitcoin/__init__.py +++ b/modules/python_bitcoin/__init__.py @@ -1,5 +1,5 @@ import bitcoin -import stratum +from processor import Processor import threading import time @@ -96,19 +96,13 @@ class GhostValue: class NumblocksSubscribe: - def __init__(self, backend): + def __init__(self, backend, processor): self.backend = backend + self.processor = processor self.lock = threading.Lock() 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: @@ -119,53 +113,70 @@ class NumblocksSubscribe: def reorganize(self, ec, fork_point, arrivals, replaced): 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}) + self.processor.push_response({"method":"numblocks.subscribe", "result": latest}) self.backend.blockchain.subscribe_reorganize(self.reorganize) - 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): + def __init__(self, backend, processor): self.backend = backend + self.processor = processor - def get(self, session, request): + def get(self, request): address = str(request["params"]) composed.payment_history(self.backend.blockchain, address, - bitcoin.bind(self.respond, session, request, bitcoin._1)) + bitcoin.bind(self.respond, request, bitcoin._1)) - def respond(self, session, request, result): - session.push_response({"id": request["id"], "result": result}) + def respond(self, request, result): + self.processor.push_response({"id": request["id"], "method":request["method"], "params":request["params"], "result": result}) -class LibbitcoinProcessor(stratum.Processor): - def __init__(self): +class BlockchainProcessor(Processor): + + def __init__(self, config): + Processor.__init__(self) self.backend = Backend() - self.numblocks_subscribe = NumblocksSubscribe(self.backend) - self.address_get_history = AddressGetHistory(self.backend) - stratum.Processor.__init__(self) + self.numblocks_subscribe = NumblocksSubscribe(self.backend, self) + self.address_get_history = AddressGetHistory(self.backend, self) def stop(self): self.backend.stop() - def process(self, session): - request = session.pop_request() + def process(self, request): + print "New request (lib)", request if request["method"] == "numblocks.subscribe": self.numblocks_subscribe.subscribe(session, request) elif request["method"] == "address.get_history": - self.address_get_history.get(session, request) + self.address_get_history.get(request) + elif request["method"] == "server.banner": + self.push_response({"id": request["id"], "method": request["method"], "params":request["params"], + "result": "libbitcoin using python-bitcoin bindings"}) + elif request["method"] == "transaction.broadcast": + self.broadcast_transaction(request) # Execute and when ready, you call - # session.push_response(response) - -def run(stratum): - print "Warning: pre-alpha prototype. Full of bugs." - processor = LibbitcoinProcessor() - stratum.start(processor) + # self.push_response(response) + + def broadcast_transaction(self, request): + raw_tx = bitcoin.data_chunk(str(request["params"])) + exporter = bitcoin.satoshi_exporter() + try: + tx = exporter.load_transaction(raw_tx) + except RuntimeError: + response = {"id": request["id"], "method": request["method"], "params":request["params"], "result": None, + "error": {"message": + "Exception while parsing the transaction data.", + "code": -4}} + else: + self.backend.protocol.broadcast_transaction(tx) + tx_hash = str(bitcoin.hash_transaction(tx)) + response = {"id": request["id"], "method": request["method"], "params":request["params"], "result": tx_hash} + self.push_response(response) + + def run(self): + # this class is a thread. it does nothing in this example. + print "Warning: pre-alpha prototype. Full of bugs." + while not self.shared.stopped(): + time.sleep(1)