lnbase: add lnbase_test
This commit is contained in:
parent
1c90479f55
commit
c0a8fd811f
@ -698,6 +698,12 @@ class Commands:
|
|||||||
lightning.lightningCall(self.wallet.network.lightningrpc, lcmd)(*lightningargs)
|
lightning.lightningCall(self.wallet.network.lightningrpc, lcmd)(*lightningargs)
|
||||||
return q.get(block=True, timeout=600)
|
return q.get(block=True, timeout=600)
|
||||||
|
|
||||||
|
@command("wn")
|
||||||
|
def lnbase_test(self):
|
||||||
|
if not self.network.config.get("lnbase", False):
|
||||||
|
return "You need to pass --lnbase to the daemon starting command to run the lnbase test"
|
||||||
|
return self.wallet.lnworker.blocking_test_run()
|
||||||
|
|
||||||
param_descriptions = {
|
param_descriptions = {
|
||||||
'privkey': 'Private key. Type \'?\' to get a prompt.',
|
'privkey': 'Private key. Type \'?\' to get a prompt.',
|
||||||
'destination': 'Bitcoin address, contact or alias',
|
'destination': 'Bitcoin address, contact or alias',
|
||||||
|
|||||||
@ -269,6 +269,7 @@ class Network(util.DaemonThread):
|
|||||||
self.socket_queue = queue.Queue()
|
self.socket_queue = queue.Queue()
|
||||||
self.start_network(deserialize_server(self.default_server)[2],
|
self.start_network(deserialize_server(self.default_server)[2],
|
||||||
deserialize_proxy(self.config.get('proxy')))
|
deserialize_proxy(self.config.get('proxy')))
|
||||||
|
self.asyncio_loop = loop = asyncio.new_event_loop()
|
||||||
|
|
||||||
def with_interface_lock(func):
|
def with_interface_lock(func):
|
||||||
def func_wrapper(self, *args, **kwargs):
|
def func_wrapper(self, *args, **kwargs):
|
||||||
@ -1089,17 +1090,16 @@ class Network(util.DaemonThread):
|
|||||||
b.update_size()
|
b.update_size()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
asyncio.set_event_loop(self.asyncio_loop)
|
||||||
self.init_headers_file()
|
self.init_headers_file()
|
||||||
self.futures = []
|
self.futures = []
|
||||||
self.asyncio_loop = loop = asyncio.new_event_loop()
|
|
||||||
networkAndWalletLock = QLock()
|
networkAndWalletLock = QLock()
|
||||||
|
self.lightninglock.acquire()
|
||||||
def asyncioThread():
|
def asyncioThread():
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
if self.config.get("lightning", False):
|
if self.config.get("lightning", False):
|
||||||
self.lightninglock.acquire()
|
|
||||||
if self.lightningrpc is not None and self.lightningworker is not None:
|
if self.lightningrpc is not None and self.lightningworker is not None:
|
||||||
task = asyncio.ensure_future(asyncio.gather(self.lightningrpc.run(networkAndWalletLock), self.lightningworker.run(networkAndWalletLock)))
|
task = asyncio.ensure_future(asyncio.gather(self.lightningrpc.run(networkAndWalletLock), self.lightningworker.run(networkAndWalletLock)))
|
||||||
loop.run_forever()
|
self.asyncio_loop.run_forever()
|
||||||
|
|
||||||
threading.Thread(target=asyncioThread).start()
|
threading.Thread(target=asyncioThread).start()
|
||||||
networkAndWalletLock.acquire()
|
networkAndWalletLock.acquire()
|
||||||
@ -1113,10 +1113,10 @@ class Network(util.DaemonThread):
|
|||||||
networkAndWalletLock.acquire()
|
networkAndWalletLock.acquire()
|
||||||
# cancel tasks
|
# cancel tasks
|
||||||
[f.cancel() for f in self.futures]
|
[f.cancel() for f in self.futures]
|
||||||
loop.stop()
|
self.asyncio_loop.stop()
|
||||||
if loop.is_running(): time.sleep(0.1)
|
if self.asyncio_loop.is_running(): time.sleep(0.1)
|
||||||
try:
|
try:
|
||||||
loop.close()
|
self.asyncio_loop.close()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
self.stop_network()
|
self.stop_network()
|
||||||
|
|||||||
@ -26,7 +26,7 @@
|
|||||||
# - Standard_Wallet: one keystore, P2PKH
|
# - Standard_Wallet: one keystore, P2PKH
|
||||||
# - Multisig_Wallet: several keystores, P2SH
|
# - Multisig_Wallet: several keystores, P2SH
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
import random
|
import random
|
||||||
@ -1306,8 +1306,9 @@ class Abstract_Wallet(PrintError):
|
|||||||
if network.config.get("lightning", False):
|
if network.config.get("lightning", False):
|
||||||
network.lightningworker = LightningWorker(self, network, network.config)
|
network.lightningworker = LightningWorker(self, network, network.config)
|
||||||
network.lightningrpc = LightningRPC()
|
network.lightningrpc = LightningRPC()
|
||||||
network.lightninglock.release()
|
network.lightninglock.release()
|
||||||
if network.config.get("lnbase", False):
|
if network.config.get("lnbase", False):
|
||||||
|
asyncio.set_event_loop(network.asyncio_loop)
|
||||||
from .lnbase import LNWorker
|
from .lnbase import LNWorker
|
||||||
self.lnworker = LNWorker(self, network)
|
self.lnworker = LNWorker(self, network)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -331,8 +331,8 @@ class Peer(PrintError):
|
|||||||
#def open_channel(self, funding_sat, push_msat):
|
#def open_channel(self, funding_sat, push_msat):
|
||||||
# self.send_message(gen_msg('open_channel', funding_satoshis=funding_sat, push_msat=push_msat))
|
# self.send_message(gen_msg('open_channel', funding_satoshis=funding_sat, push_msat=push_msat))
|
||||||
|
|
||||||
async def main_loop(self, loop):
|
async def main_loop(self):
|
||||||
self.reader, self.writer = await asyncio.open_connection(self.host, self.port, loop=loop)
|
self.reader, self.writer = await asyncio.open_connection(self.host, self.port)
|
||||||
await self.handshake()
|
await self.handshake()
|
||||||
# send init
|
# send init
|
||||||
self.send_message(gen_msg("init", gflen=0, lflen=0))
|
self.send_message(gen_msg("init", gflen=0, lflen=0))
|
||||||
@ -350,7 +350,7 @@ class Peer(PrintError):
|
|||||||
self.print_error('closing lnbase')
|
self.print_error('closing lnbase')
|
||||||
self.writer.close()
|
self.writer.close()
|
||||||
|
|
||||||
async def channel_establishment_flow(self):
|
async def channel_establishment_flow(self, wallet):
|
||||||
await self.init_message_received_future
|
await self.init_message_received_future
|
||||||
pubkeys = get_unused_public_keys()
|
pubkeys = get_unused_public_keys()
|
||||||
temp_channel_id = os.urandom(32)
|
temp_channel_id = os.urandom(32)
|
||||||
@ -359,11 +359,11 @@ class Peer(PrintError):
|
|||||||
self.send_message(msg)
|
self.send_message(msg)
|
||||||
try:
|
try:
|
||||||
accept_channel = await self.temporary_channel_id_to_incoming_accept_channel[temp_channel_id]
|
accept_channel = await self.temporary_channel_id_to_incoming_accept_channel[temp_channel_id]
|
||||||
except LightningError:
|
|
||||||
return
|
|
||||||
finally:
|
finally:
|
||||||
del self.temporary_channel_id_to_incoming_accept_channel[temp_channel_id]
|
del self.temporary_channel_id_to_incoming_accept_channel[temp_channel_id]
|
||||||
|
|
||||||
|
raise Exception("TODO: create funding transaction using wallet")
|
||||||
|
|
||||||
|
|
||||||
# replacement for lightningCall
|
# replacement for lightningCall
|
||||||
class LNWorker:
|
class LNWorker:
|
||||||
@ -371,19 +371,29 @@ class LNWorker:
|
|||||||
def __init__(self, wallet, network):
|
def __init__(self, wallet, network):
|
||||||
self.wallet = wallet
|
self.wallet = wallet
|
||||||
self.network = network
|
self.network = network
|
||||||
self.loop = network.asyncio_loop
|
|
||||||
host, port, pubkey = ('ecdsa.net', '9735', '038370f0e7a03eded3e1d41dc081084a87f0afa1c5b22090b4f3abb391eb15d8ff')
|
host, port, pubkey = ('ecdsa.net', '9735', '038370f0e7a03eded3e1d41dc081084a87f0afa1c5b22090b4f3abb391eb15d8ff')
|
||||||
pubkey = binascii.unhexlify(pubkey)
|
pubkey = binascii.unhexlify(pubkey)
|
||||||
port = int(port)
|
port = int(port)
|
||||||
privkey = b"\x21"*32 + b"\x01"
|
privkey = b"\x21"*32 + b"\x01"
|
||||||
self.peer = Peer(privkey, host, port, pubkey)
|
self.peer = Peer(privkey, host, port, pubkey)
|
||||||
self.network.futures.append(asyncio.run_coroutine_threadsafe(self.peer.main_loop(self.loop), self.loop))
|
self.network.futures.append(asyncio.run_coroutine_threadsafe(self.peer.main_loop(), asyncio.get_event_loop()))
|
||||||
|
|
||||||
def openchannel(self):
|
def openchannel(self):
|
||||||
# todo: get utxo from wallet
|
# todo: get utxo from wallet
|
||||||
# submit coro to asyncio main loop
|
# submit coro to asyncio main loop
|
||||||
self.peer.open_channel()
|
self.peer.open_channel()
|
||||||
|
|
||||||
|
def blocking_test_run(self):
|
||||||
|
start = time.time()
|
||||||
|
fut = asyncio.ensure_future(self._test())
|
||||||
|
asyncio.get_event_loop().run_until_complete(fut)
|
||||||
|
fut.exception()
|
||||||
|
return "blocking test run took: " + str(time.time() - start)
|
||||||
|
|
||||||
|
async def _test(self):
|
||||||
|
await self.peer.channel_establishment_flow(self.wallet)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
node_list = [
|
node_list = [
|
||||||
('ecdsa.net', '9735', '038370f0e7a03eded3e1d41dc081084a87f0afa1c5b22090b4f3abb391eb15d8ff'),
|
('ecdsa.net', '9735', '038370f0e7a03eded3e1d41dc081084a87f0afa1c5b22090b4f3abb391eb15d8ff'),
|
||||||
@ -405,8 +415,5 @@ if __name__ == "__main__":
|
|||||||
port = int(port)
|
port = int(port)
|
||||||
privkey = b"\x21"*32 + b"\x01"
|
privkey = b"\x21"*32 + b"\x01"
|
||||||
peer = Peer(privkey, host, port, pubkey)
|
peer = Peer(privkey, host, port, pubkey)
|
||||||
loop = asyncio.get_event_loop()
|
loop.run_until_complete(peer.main_loop())
|
||||||
async def asynctest():
|
|
||||||
await peer.channel_establishment_flow()
|
|
||||||
loop.run_until_complete(asyncio.gather(asynctest(), peer.main_loop(loop)))
|
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user