Browse Source

lightning: separate thread for publish transaction

regtest_lnd
Janus 7 years ago
committed by SomberNight
parent
commit
43243ee4b4
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 21
      lib/lightning.py

21
lib/lightning.py

@ -478,12 +478,21 @@ def signOutputRaw(tx, signDesc):
async def PublishTransaction(json): async def PublishTransaction(json):
req = rpc_pb2.PublishTransactionRequest() req = rpc_pb2.PublishTransactionRequest()
json_format.Parse(json, req) json_format.Parse(json, req)
global NETWORK global NETWORK, globLock
tx = transaction.Transaction(binascii.hexlify(req.tx).decode("utf-8")) tx = transaction.Transaction(binascii.hexlify(req.tx).decode("utf-8"))
suc, has = NETWORK.broadcast(tx) def target(tx, NETWORK, globLock):
globLock.acquire()
try:
res = NETWORK.broadcast(tx)
print("PUBLISH TRANSACTION IN SEPARATE THREAD PRODUCED", res)
except:
traceback.print_exc()
finally:
globLock.release()
threading.Thread(target=target, args=(tx, NETWORK, globLock)).start()
m = rpc_pb2.PublishTransactionResponse() m = rpc_pb2.PublishTransactionResponse()
m.success = suc m.success = True
m.error = str(has) if not suc else "" m.error = ""
if m.error: if m.error:
print("PublishTransaction", m.error) print("PublishTransaction", m.error)
if "Missing inputs" in m.error: if "Missing inputs" in m.error:
@ -724,7 +733,10 @@ async def readJson(reader):
except TimeoutError: except TimeoutError:
continue continue
globLock = None
async def readReqAndReply(obj, writer, netAndWalLock): async def readReqAndReply(obj, writer, netAndWalLock):
global globLock
methods = [ methods = [
# SecretKeyRing # SecretKeyRing
DerivePrivKey, DerivePrivKey,
@ -753,6 +765,7 @@ async def readReqAndReply(obj, writer, netAndWalLock):
if method.__name__ == obj["method"]: if method.__name__ == obj["method"]:
params = obj["params"][0] params = obj["params"][0]
print("calling method", obj["method"], "with", params) print("calling method", obj["method"], "with", params)
globLock = netAndWalLock
netAndWalLock.acquire() netAndWalLock.acquire()
if asyncio.iscoroutinefunction(method): if asyncio.iscoroutinefunction(method):
result = await method(params) result = await method(params)

Loading…
Cancel
Save