Browse Source

make regtests more robust

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
ThomasV 6 years ago
parent
commit
6f333bd86d
  1. 140
      electrum/tests/regtest/regtest.sh

140
electrum/tests/regtest/regtest.sh

@ -15,20 +15,51 @@ function new_blocks()
$bitcoin_cli generatetoaddress $1 $($bitcoin_cli getnewaddress) > /dev/null $bitcoin_cli generatetoaddress $1 $($bitcoin_cli getnewaddress) > /dev/null
} }
function wait_until_funded() function wait_for_balance()
{ {
while alice_balance=$($alice getbalance | jq '.confirmed' | tr -d '"') && [ $alice_balance != "1" ]; do msg="wait until $1's balance reaches $2"
echo "waiting for alice balance" cmd="./run_electrum --regtest --lightning -D /tmp/$1"
while balance=$($cmd getbalance | jq '[.confirmed, .unconfirmed] | to_entries | map(select(.value != null).value) | map(tonumber) | add ') && (( $(echo "$balance < $2" | bc -l) )); do
sleep 1 sleep 1
msg="$msg."
printf "$msg\r"
done done
printf "\n"
} }
function wait_until_channel_open() function wait_until_channel_open()
{ {
while channel_state=$($alice list_channels | jq '.[] | .state' | tr -d '"') && [ $channel_state != "OPEN" ]; do msg="wait until $1 sees channel open"
echo "waiting for channel open" cmd="./run_electrum --regtest --lightning -D /tmp/$1"
while channel_state=$($cmd list_channels | jq '.[0] | .state' | tr -d '"') && [ $channel_state != "OPEN" ]; do
sleep 1 sleep 1
msg="$msg."
printf "$msg\r"
done done
printf "\n"
}
function wait_until_channel_closed()
{
msg="wait until $1 sees channel closed"
cmd="./run_electrum --regtest --lightning -D /tmp/$1"
while [[ $($cmd list_channels | jq '.[0].state' | tr -d '"') != "CLOSED" ]]; do
sleep 1
msg="$msg."
printf "$msg\r"
done
printf "\n"
}
function wait_until_spent()
{
msg="wait until $1:$2 is spent"
while [[ $($bitcoin_cli gettxout $1 $2) ]]; do
sleep 1
msg="$msg."
printf "$msg\r"
done
printf "\n"
} }
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
@ -61,7 +92,6 @@ if [[ $1 == "start" ]]; then
$bob daemon start $bob daemon start
$alice daemon start $alice daemon start
$carol daemon start $carol daemon start
sleep 1 # time to accept commands
$bob load_wallet $bob load_wallet
$alice load_wallet $alice load_wallet
$carol load_wallet $carol load_wallet
@ -109,32 +139,26 @@ 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)
new_blocks 3 new_blocks 3
wait_until_channel_open wait_until_channel_open alice
request=$($bob addinvoice 0.01 "blah") request=$($bob addinvoice 0.01 "blah")
echo "alice pays" echo "alice pays"
$alice lnpay $request $alice lnpay $request
sleep 2 sleep 2
ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"') ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"')
request=$($bob addinvoice 0.01 "blah2") request=$($bob addinvoice 0.01 "blah2")
echo "alice pays" echo "alice pays again"
$alice lnpay $request $alice lnpay $request
sleep 2
echo "alice broadcasts old ctx" echo "alice broadcasts old ctx"
$bitcoin_cli sendrawtransaction $ctx $bitcoin_cli sendrawtransaction $ctx
sleep 10 wait_until_channel_closed bob
new_blocks 2 new_blocks 1
sleep 10 wait_for_balance bob 0.14
balance=$($bob getbalance | jq '.confirmed | tonumber') $bob getbalance
echo "balance of bob after breach: $balance"
if (( $(echo "$balance < 0.14" | bc -l) )); then
exit 1
fi
fi fi
if [[ $1 == "redeem_htlcs" ]]; then if [[ $1 == "redeem_htlcs" ]]; then
$bob stop $bob stop
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=10 $bob daemon start ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=10 $bob daemon start
sleep 1
$bob load_wallet $bob load_wallet
sleep 1 sleep 1
# alice opens channel # alice opens channel
@ -171,25 +195,22 @@ if [[ $1 == "redeem_htlcs" ]]; then
new_blocks 1 new_blocks 1
sleep 3 sleep 3
echo "alice balance after CSV" $($alice getbalance) echo "alice balance after CSV" $($alice getbalance)
balance_after=$($alice getbalance | jq '[.confirmed, .unconfirmed] | to_entries | map(select(.value != null).value) | map(tonumber) | add ') # fixme: add local to getbalance
if (( $(echo "$balance_before - $balance_after > 0.02" | bc -l) )); then wait_for_balance alice $(echo "$balance_before - 0.02" | bc -l)
echo "htlc not redeemed." $alice getbalance
exit 1
fi
fi fi
if [[ $1 == "breach_with_unspent_htlc" ]]; then if [[ $1 == "breach_with_unspent_htlc" ]]; then
$bob stop $bob stop
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon start ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon start
sleep 1
$bob load_wallet $bob load_wallet
wait_until_funded 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.15) channel=$($alice open_channel $bob_node 0.15)
new_blocks 3 new_blocks 3
wait_until_channel_open wait_until_channel_open alice
echo "alice pays bob" echo "alice pays bob"
invoice=$($bob addinvoice 0.05 "test") invoice=$($bob addinvoice 0.05 "test")
$alice lnpay $invoice --timeout=1 || true $alice lnpay $invoice --timeout=1 || true
@ -205,48 +226,22 @@ if [[ $1 == "breach_with_unspent_htlc" ]]; then
echo "SETTLE_DELAY did not work, $settled != 1" echo "SETTLE_DELAY did not work, $settled != 1"
exit 1 exit 1
fi fi
echo $($bob getbalance)
echo "alice breaches with old ctx" echo "alice breaches with old ctx"
echo $ctx
height1=$($bob getinfo | jq '.blockchain_height')
$bitcoin_cli sendrawtransaction $ctx $bitcoin_cli sendrawtransaction $ctx
new_blocks 1 wait_for_balance bob 0.14
# wait until breach is confirmed
while height2=$($bob getinfo | jq '.blockchain_height') && [ $(($height2 - $height1)) -ne 1 ]; do
echo "waiting for block"
sleep 1
done
new_blocks 1
# wait until next block is confirmed, so that htlc tx and redeem tx are confirmed too
while height3=$($bob getinfo | jq '.blockchain_height') && [ $(($height3 - $height2)) -ne 1 ]; do
echo "waiting for block"
sleep 1
done
# wait until wallet is synchronized
while b=$($bob list_wallets | jq '.[0]|.synchronized') && [ "$b" != "true" ]; do
echo "waiting for wallet sync: $b"
sleep 1
done
echo $($bob getbalance)
balance_after=$($bob getbalance | jq '[.confirmed, .unconfirmed] | to_entries | map(select(.value != null).value) | map(tonumber) | add ')
if (( $(echo "$balance_after < 0.14" | bc -l) )); then
echo "htlc not redeemed."
exit 1
fi
fi fi
if [[ $1 == "breach_with_spent_htlc" ]]; then if [[ $1 == "breach_with_spent_htlc" ]]; then
$bob stop $bob stop
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon start ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon start
sleep 1
$bob load_wallet $bob load_wallet
wait_until_funded 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.15) channel=$($alice open_channel $bob_node 0.15)
new_blocks 3 new_blocks 3
wait_until_channel_open wait_until_channel_open alice
echo "alice pays bob" echo "alice pays bob"
invoice=$($bob addinvoice 0.05 "test") invoice=$($bob addinvoice 0.05 "test")
$alice lnpay $invoice --timeout=1 || true $alice lnpay $invoice --timeout=1 || true
@ -277,39 +272,20 @@ if [[ $1 == "breach_with_spent_htlc" ]]; then
# note: this will let alice redeem both to_local and the htlc. # note: this will let alice redeem both to_local and the htlc.
# (to_local needs to_self_delay blocks; htlc needs whatever we put in invoice) # (to_local needs to_self_delay blocks; htlc needs whatever we put in invoice)
new_blocks 150 new_blocks 150
echo "alice spends to_local and htlc outputs"
$alice stop $alice stop
cp /tmp/alice/regtest/wallets/toxic_wallet /tmp/alice/regtest/wallets/default_wallet cp /tmp/alice/regtest/wallets/toxic_wallet /tmp/alice/regtest/wallets/default_wallet
$alice daemon start $alice daemon start
sleep 1
$alice load_wallet $alice load_wallet
# wait until alice has spent both ctx outputs # wait until alice has spent both ctx outputs
while [[ $($bitcoin_cli gettxout $ctx_id 0) ]]; do echo "alice spends to_local and htlc outputs"
echo "waiting until alice spends ctx outputs" wait_until_spent $ctx_id 0
sleep 1 wait_until_spent $ctx_id 1
done
while [[ $($bitcoin_cli gettxout $ctx_id 1) ]]; do
echo "waiting until alice spends ctx outputs"
sleep 1
done
new_blocks 1 new_blocks 1
echo "bob comes back" echo "bob comes back"
$bob daemon start $bob daemon start
sleep 1
$bob load_wallet $bob load_wallet
while [[ $($bitcoin_cli getmempoolinfo | jq '.size') != "1" ]]; do wait_for_balance bob 0.049
echo "waiting for bob's transaction" $bob getbalance
sleep 1
done
echo "mempool has 1 tx"
new_blocks 1
sleep 5
balance=$($bob getbalance | jq '.confirmed | tonumber')
if (( $(echo "$balance < 0.049" | bc -l) )); then
echo "htlc not redeemed."
exit 1
fi
echo "bob balance $balance"
fi fi
if [[ $1 == "watchtower" ]]; then if [[ $1 == "watchtower" ]]; then
@ -321,15 +297,13 @@ if [[ $1 == "watchtower" ]]; then
$carol setconfig watchtower_port 12345 $carol setconfig watchtower_port 12345
$carol daemon start $carol daemon start
$alice daemon start $alice daemon start
sleep 1
$alice load_wallet $alice load_wallet
echo "waiting until alice funded" wait_for_balance alice 1
wait_until_funded
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.5)
new_blocks 3 new_blocks 3
wait_until_channel_open wait_until_channel_open alice
echo "alice pays bob" echo "alice pays bob"
invoice1=$($bob addinvoice 0.05 "invoice1") invoice1=$($bob addinvoice 0.05 "invoice1")
$alice lnpay $invoice1 $alice lnpay $invoice1

Loading…
Cancel
Save