Browse Source

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
hard-fail-on-bad-server-string
ThomasV 5 years ago
parent
commit
4ec86d36a8
  1. 6
      electrum/commands.py
  2. 2
      electrum/lnpeer.py
  3. 1
      electrum/simple_config.py
  4. 20
      electrum/tests/regtest.py
  5. 112
      electrum/tests/regtest/regtest.sh

6
electrum/commands.py

@ -988,6 +988,12 @@ class Commands:
tx = chan.force_close_tx() tx = chan.force_close_tx()
return tx.serialize() 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: def eval_bool(x: str) -> bool:
if x == 'false': return False if x == 'false': return False
if x == 'true': return True if x == 'true': return True

2
electrum/lnpeer.py

@ -1357,7 +1357,7 @@ class Peer(Logger):
await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason) await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason)
return return
#self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED) #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) await self._fulfill_htlc(chan, htlc.htlc_id, preimage)
async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes): async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):

1
electrum/simple_config.py

@ -56,7 +56,6 @@ class SimpleConfig(Logger):
options = {} options = {}
Logger.__init__(self) 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 # This lock needs to be acquired for updating and reading the config in
# a thread-safe way. # a thread-safe way.

20
electrum/tests/regtest.py

@ -15,11 +15,25 @@ class TestLightning(unittest.TestCase):
assert process.returncode == 0 assert process.returncode == 0
def setUp(self): def setUp(self):
self.run_shell(['init']) test_name = self.id().split('.')[-1]
self.run_shell(['start']) 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): def tearDown(self):
self.run_shell(['stop']) for agent in self.agents:
self.run_shell(['stop', agent])
def test_breach(self): def test_breach(self):
self.run_shell(['breach']) self.run_shell(['breach'])

112
electrum/tests/regtest/regtest.sh

@ -67,48 +67,43 @@ if [[ $# -eq 0 ]]; then
exit 1 exit 1
fi fi
if [[ $1 == "init" ]]; then if [[ $1 == "new_block" ]]; 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
new_blocks 1 new_blocks 1
fi fi
if [[ $1 == "new_block" ]]; then if [[ $1 == "init" ]]; then
new_blocks 1 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 fi
# start daemons. Bob is started first because he is listening # start daemons. Bob is started first because he is listening
if [[ $1 == "start" ]]; then if [[ $1 == "start" ]]; then
$bob daemon -d agent="./run_electrum --regtest -D /tmp/$2"
$alice daemon -d $agent daemon -d
$carol daemon -d $agent load_wallet
$bob load_wallet sleep 1 # give time to synchronize
$alice load_wallet
$carol load_wallet
sleep 10 # give time to synchronize
fi fi
if [[ $1 == "stop" ]]; then if [[ $1 == "stop" ]]; then
$alice stop || true agent="./run_electrum --regtest -D /tmp/$2"
$bob stop || true $agent stop || true
$carol stop || true fi
if [[ $1 == "configure_test_forwarding" ]]; then
$bob setconfig --offline lightning_forward_payments true
fi fi
if [[ $1 == "open" ]]; then if [[ $1 == "open" ]]; then
@ -142,6 +137,7 @@ fi
# alice sends two payments, then broadcast ctx after first payment. # alice sends two payments, then broadcast ctx after first payment.
# thus, bob needs to redeem both to_local and to_remote # thus, bob needs to redeem both to_local and to_remote
if [[ $1 == "breach" ]]; then if [[ $1 == "breach" ]]; then
bob_node=$($bob nodeid) bob_node=$($bob nodeid)
channel=$($alice open_channel $bob_node 0.15) channel=$($alice open_channel $bob_node 0.15)
@ -164,11 +160,7 @@ if [[ $1 == "breach" ]]; then
fi fi
if [[ $1 == "redeem_htlcs" ]]; then if [[ $1 == "redeem_htlcs" ]]; then
$bob stop $bob setconfig lightning_settle_delay 10
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=10 $bob daemon -d
sleep 1
$bob load_wallet
sleep 1
# alice opens channel # alice opens channel
bob_node=$($bob nodeid) bob_node=$($bob nodeid)
$alice open_channel $bob_node 0.15 $alice open_channel $bob_node 0.15
@ -210,10 +202,7 @@ fi
if [[ $1 == "breach_with_unspent_htlc" ]]; then if [[ $1 == "breach_with_unspent_htlc" ]]; then
$bob stop $bob setconfig lightning_settle_delay 3
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -d
sleep 1
$bob load_wallet
wait_for_balance alice 1 wait_for_balance alice 1
echo "alice opens channel" echo "alice opens channel"
bob_node=$($bob nodeid) bob_node=$($bob nodeid)
@ -242,10 +231,7 @@ fi
if [[ $1 == "breach_with_spent_htlc" ]]; then if [[ $1 == "breach_with_spent_htlc" ]]; then
$bob stop $bob setconfig lightning_settle_delay 3
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -d
sleep 1
$bob load_wallet
wait_for_balance alice 1 wait_for_balance alice 1
echo "alice opens channel" echo "alice opens channel"
bob_node=$($bob nodeid) bob_node=$($bob nodeid)
@ -299,29 +285,35 @@ if [[ $1 == "breach_with_spent_htlc" ]]; then
$bob getbalance $bob getbalance
fi fi
if [[ $1 == "watchtower" ]]; then
# carol is a watchtower of alice if [[ $1 == "configure_test_watchtower" ]]; then
$alice stop # carol is the watchtower of bob
$carol stop $carol setconfig --offline run_watchtower true
$alice setconfig --offline watchtower_url http://127.0.0.1:12345
$carol setconfig --offline watchtower_host 127.0.0.1 $carol setconfig --offline watchtower_host 127.0.0.1
$carol setconfig --offline watchtower_port 12345 $carol setconfig --offline watchtower_port 12345
$carol daemon -d $bob setconfig --offline watchtower_url http://127.0.0.1:12345
$alice daemon -d fi
sleep 1
$alice load_wallet if [[ $1 == "watchtower" ]]; then
wait_for_balance alice 1 wait_for_balance alice 1
echo "alice opens channel" echo "alice opens channel"
bob_node=$($bob nodeid) 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 new_blocks 3
wait_until_channel_open alice wait_until_channel_open alice
echo "alice pays bob" 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 $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 $alice lnpay $invoice2
invoice3=$($bob add_lightning_request 0.05 -m "invoice3") msg="waiting until watchtower is synchronized"
$alice lnpay $invoice3 while watchtower_ctn=$($carol get_watchtower_ctn $channel) && [ $watchtower_ctn != "3" ]; do
sleep 1
msg="$msg."
printf "$msg\r"
done
printf "\n"
fi fi

Loading…
Cancel
Save