Browse Source

lightning: misc patches, launch asyncio loop on separate thread

regtest_lnd
Janus 7 years ago
committed by SomberNight
parent
commit
3260b7a74f
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 1
      electrum/network.py
  2. 3
      electrum/wallet.py
  3. 17
      lib/lightning.py
  4. 21
      testserver.py

1
electrum/network.py

@ -20,6 +20,7 @@
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
import asyncio
import time import time
import queue import queue
import os import os

3
electrum/wallet.py

@ -73,6 +73,9 @@ if TYPE_CHECKING:
_logger = get_logger(__name__) _logger = get_logger(__name__)
from .lightning import LightningRPC
from .lightning import LightningWorker
TX_STATUS = [ TX_STATUS = [
_('Unconfirmed'), _('Unconfirmed'),
_('Unconfirmed parent'), _('Unconfirmed parent'),

17
lib/lightning.py

@ -18,8 +18,6 @@ from . import keystore
import queue import queue
from .util import ForeverCoroutineJob
import threading import threading
import json import json
import base64 import base64
@ -481,7 +479,7 @@ async def PublishTransaction(json):
json_format.Parse(json, req) json_format.Parse(json, req)
global NETWORK global NETWORK
tx = transaction.Transaction(binascii.hexlify(req.tx).decode("utf-8")) tx = transaction.Transaction(binascii.hexlify(req.tx).decode("utf-8"))
suc, has = await NETWORK.broadcast_async(tx) suc, has = NETWORK.broadcast(tx)
m = rpc_pb2.PublishTransactionResponse() m = rpc_pb2.PublishTransactionResponse()
m.success = suc m.success = suc
m.error = str(has) if not suc else "" m.error = str(has) if not suc else ""
@ -593,15 +591,14 @@ def computeInputScript(tx, signdesc):
from collections import namedtuple from collections import namedtuple
QueueItem = namedtuple("QueueItem", ["methodName", "args"]) QueueItem = namedtuple("QueueItem", ["methodName", "args"])
class LightningRPC(ForeverCoroutineJob): class LightningRPC:
def __init__(self): def __init__(self):
super(LightningRPC, self).__init__() super(LightningRPC, self).__init__()
self.queue = queue.Queue() self.queue = queue.Queue()
self.subscribers = [] self.subscribers = []
# overridden # overridden
async def run(self, is_running): async def run(self, netAndWalLock):
print("RPC STARTED") while True:
while is_running():
try: try:
qitem = self.queue.get(block=False) qitem = self.queue.get(block=False)
except queue.Empty: except queue.Empty:
@ -654,7 +651,7 @@ class LightningUI():
privateKeyHash = None privateKeyHash = None
class LightningWorker(ForeverCoroutineJob): class LightningWorker:
def __init__(self, wallet, network, config): def __init__(self, wallet, network, config):
global privateKeyHash global privateKeyHash
super(LightningWorker, self).__init__() super(LightningWorker, self).__init__()
@ -678,13 +675,13 @@ class LightningWorker(ForeverCoroutineJob):
assert deser[0] == "p2wpkh", deser assert deser[0] == "p2wpkh", deser
self.subscribers = [] self.subscribers = []
async def run(self, is_running): async def run(self, netAndWalLock):
global WALLET, NETWORK global WALLET, NETWORK
global CONFIG global CONFIG
wasAlreadyUpToDate = False wasAlreadyUpToDate = False
while is_running(): while True:
WALLET = self.wallet() WALLET = self.wallet()
NETWORK = self.network() NETWORK = self.network()
CONFIG = self.config() CONFIG = self.config()

21
testserver.py

@ -1,21 +0,0 @@
import asyncio
async def handler(reader, writer):
magic = await reader.read(5+6)
await asyncio.sleep(5)
print("in five sec!")
await asyncio.sleep(5)
writer.write(b'{\n "r_preimage": "6UNoNhDZ/0awtaDTM7KuCtlYcNkNljscxMLleoJv9+o=",\n "r_hash": "lQDtsJlLe8IzSRk0hrJcgglwRdtkHzX6mIwOhJrN7Ck=",\n "value": "8192",\n "settled": true,\n "creation_date": "1519994196",\n "settle_date": "1519994199",\n "payment_request": "lntb81920n1pdfj325pp5k7erq3avatceq8ca43h5uulxrhw2ma3a442a7c8fxrsw059c3m3sdqqcqzysdpwv4dn2xd74lfmea3taxj6pjfxrdl42t8w7ceptgv5ds0td0ypk47llryl6t4a48x54d7mnwremgcmljced4dhwty9g3pfywr307aqpwtkzf4",\n "expiry": "3600",\n "cltv_expiry": "144"\n}\n'.replace(b"\n",b""))
await writer.drain()
print(magic)
async def handler2(reader, writer):
while True:
data = await reader.read(2048)
if data != b'':
writer.write(b"HTTP/1.0 200 OK\r\nContent-length: 16\r\n\r\n{\"result\":\"lol\"}")
await writer.drain()
asyncio.ensure_future(asyncio.start_server(handler, "127.0.0.1", 1080))
asyncio.ensure_future(asyncio.start_server(handler2, "127.0.0.1", 8090))
asyncio.get_event_loop().run_forever()
Loading…
Cancel
Save