electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 4ec86d36a826099f823a95d18c13b2a7aa84c6ea
parent cded582fe94c21ffffe2e037503e2f8307176e08
Author: ThomasV <thomasv@electrum.org>
Date:   Sun,  2 Feb 2020 14:50:08 +0100

faster and improved regtests
 - print the test name before each test
 - start only needed agents (alice, bob, carol)
 - set settle_delay using setconfig instead of restarting daemon
 - test the watchtower ctn in test_watchtower

Diffstat:
Melectrum/commands.py | 6++++++
Melectrum/lnpeer.py | 2+-
Melectrum/simple_config.py | 1-
Melectrum/tests/regtest.py | 20+++++++++++++++++---
Melectrum/tests/regtest/regtest.sh | 112+++++++++++++++++++++++++++++++++++++------------------------------------------
5 files changed, 76 insertions(+), 65 deletions(-)

diff --git a/electrum/commands.py b/electrum/commands.py @@ -988,6 +988,12 @@ class Commands: tx = chan.force_close_tx() return tx.serialize() + @command('wn') + async def get_watchtower_ctn(self, channel_point, wallet: Abstract_Wallet = None): + """ return the local watchtower's ctn of channel. used in regtests """ + return await self.network.local_watchtower.sweepstore.get_ctn(channel_point, None) + + def eval_bool(x: str) -> bool: if x == 'false': return False if x == 'true': return True diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py @@ -1357,7 +1357,7 @@ class Peer(Logger): await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason) return #self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED) - await asyncio.sleep(self.network.config.lightning_settle_delay) + await asyncio.sleep(self.network.config.get('lightning_settle_delay', 0)) await self._fulfill_htlc(chan, htlc.htlc_id, preimage) async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes): diff --git a/electrum/simple_config.py b/electrum/simple_config.py @@ -56,7 +56,6 @@ class SimpleConfig(Logger): options = {} Logger.__init__(self) - self.lightning_settle_delay = int(os.environ.get('ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY', 0)) # This lock needs to be acquired for updating and reading the config in # a thread-safe way. diff --git a/electrum/tests/regtest.py b/electrum/tests/regtest.py @@ -15,11 +15,25 @@ class TestLightning(unittest.TestCase): assert process.returncode == 0 def setUp(self): - self.run_shell(['init']) - self.run_shell(['start']) + test_name = self.id().split('.')[-1] + sys.stdout.write("***** %s ******\n" % test_name) + self.agents = ['alice', 'bob'] + if test_name in ['test_forwarding', 'test_watchtower']: + self.agents.append('carol') + # initialize and get funds + for agent in self.agents: + self.run_shell(['init', agent]) + # mine a block so that funds are confirmed + self.run_shell(['new_block']) + # extra configuration (optional) + self.run_shell(['configure_' + test_name]) + # start daemons + for agent in self.agents: + self.run_shell(['start', agent]) def tearDown(self): - self.run_shell(['stop']) + for agent in self.agents: + self.run_shell(['stop', agent]) def test_breach(self): self.run_shell(['breach']) diff --git a/electrum/tests/regtest/regtest.sh b/electrum/tests/regtest/regtest.sh @@ -67,48 +67,43 @@ if [[ $# -eq 0 ]]; then exit 1 fi -if [[ $1 == "init" ]]; then - echo "initializing alice, bob and carol" - rm -rf /tmp/alice/ /tmp/bob/ /tmp/carol/ - $alice create --offline > /dev/null - $bob create --offline > /dev/null - $carol create --offline > /dev/null - $alice -o init_lightning - $bob -o init_lightning - $carol -o init_lightning - $alice setconfig --offline log_to_file True - $bob setconfig --offline log_to_file True - $carol setconfig --offline log_to_file True - $alice setconfig --offline server 127.0.0.1:51001:t - $bob setconfig --offline server 127.0.0.1:51001:t - $carol setconfig --offline server 127.0.0.1:51001:t - $bob setconfig --offline lightning_listen localhost:9735 - $bob setconfig --offline lightning_forward_payments true - echo "funding alice and carol" - $bitcoin_cli sendtoaddress $($alice getunusedaddress -o) 1 - $bitcoin_cli sendtoaddress $($carol getunusedaddress -o) 1 +if [[ $1 == "new_block" ]]; then new_blocks 1 fi -if [[ $1 == "new_block" ]]; then - new_blocks 1 +if [[ $1 == "init" ]]; then + echo "initializing $2" + rm -rf /tmp/$2/ + agent="./run_electrum --regtest -D /tmp/$2" + $agent create --offline > /dev/null + $agent -o init_lightning + $agent setconfig --offline log_to_file True + $agent setconfig --offline server 127.0.0.1:51001:t + # alice is funded, bob is listening + if [[ $2 == "bob" ]]; then + $bob setconfig --offline lightning_listen localhost:9735 + else + echo "funding $2" + $bitcoin_cli sendtoaddress $($agent getunusedaddress -o) 1 + fi fi + # start daemons. Bob is started first because he is listening if [[ $1 == "start" ]]; then - $bob daemon -d - $alice daemon -d - $carol daemon -d - $bob load_wallet - $alice load_wallet - $carol load_wallet - sleep 10 # give time to synchronize + agent="./run_electrum --regtest -D /tmp/$2" + $agent daemon -d + $agent load_wallet + sleep 1 # give time to synchronize fi if [[ $1 == "stop" ]]; then - $alice stop || true - $bob stop || true - $carol stop || true + agent="./run_electrum --regtest -D /tmp/$2" + $agent stop || true +fi + +if [[ $1 == "configure_test_forwarding" ]]; then + $bob setconfig --offline lightning_forward_payments true fi if [[ $1 == "open" ]]; then @@ -142,6 +137,7 @@ fi # alice sends two payments, then broadcast ctx after first payment. # thus, bob needs to redeem both to_local and to_remote + if [[ $1 == "breach" ]]; then bob_node=$($bob nodeid) channel=$($alice open_channel $bob_node 0.15) @@ -164,11 +160,7 @@ if [[ $1 == "breach" ]]; then fi if [[ $1 == "redeem_htlcs" ]]; then - $bob stop - ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=10 $bob daemon -d - sleep 1 - $bob load_wallet - sleep 1 + $bob setconfig lightning_settle_delay 10 # alice opens channel bob_node=$($bob nodeid) $alice open_channel $bob_node 0.15 @@ -210,10 +202,7 @@ fi if [[ $1 == "breach_with_unspent_htlc" ]]; then - $bob stop - ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -d - sleep 1 - $bob load_wallet + $bob setconfig lightning_settle_delay 3 wait_for_balance alice 1 echo "alice opens channel" bob_node=$($bob nodeid) @@ -242,10 +231,7 @@ fi if [[ $1 == "breach_with_spent_htlc" ]]; then - $bob stop - ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -d - sleep 1 - $bob load_wallet + $bob setconfig lightning_settle_delay 3 wait_for_balance alice 1 echo "alice opens channel" bob_node=$($bob nodeid) @@ -299,29 +285,35 @@ if [[ $1 == "breach_with_spent_htlc" ]]; then $bob getbalance fi -if [[ $1 == "watchtower" ]]; then - # carol is a watchtower of alice - $alice stop - $carol stop - $alice setconfig --offline watchtower_url http://127.0.0.1:12345 + +if [[ $1 == "configure_test_watchtower" ]]; then + # carol is the watchtower of bob + $carol setconfig --offline run_watchtower true $carol setconfig --offline watchtower_host 127.0.0.1 $carol setconfig --offline watchtower_port 12345 - $carol daemon -d - $alice daemon -d - sleep 1 - $alice load_wallet + $bob setconfig --offline watchtower_url http://127.0.0.1:12345 +fi + +if [[ $1 == "watchtower" ]]; then wait_for_balance alice 1 echo "alice opens channel" bob_node=$($bob nodeid) - channel=$($alice open_channel $bob_node 0.5) + channel=$($alice open_channel $bob_node 0.15) + echo "channel outpoint: $channel" new_blocks 3 wait_until_channel_open alice echo "alice pays bob" - invoice1=$($bob add_lightning_request 0.05 -m "invoice1") + invoice1=$($bob add_lightning_request 0.01 -m "invoice1") $alice lnpay $invoice1 - invoice2=$($bob add_lightning_request 0.05 -m "invoice2") + ctx=$($alice get_channel_ctx $channel) + echo "alice pays bob again" + invoice2=$($bob add_lightning_request 0.01 -m "invoice2") $alice lnpay $invoice2 - invoice3=$($bob add_lightning_request 0.05 -m "invoice3") - $alice lnpay $invoice3 - + msg="waiting until watchtower is synchronized" + while watchtower_ctn=$($carol get_watchtower_ctn $channel) && [ $watchtower_ctn != "3" ]; do + sleep 1 + msg="$msg." + printf "$msg\r" + done + printf "\n" fi