Files
purple-electrumwallet/electrum/tests/regtest/regtest.sh
T

324 lines
9.7 KiB
Bash
Raw Normal View History

2019-03-11 21:00:29 +01:00
#!/usr/bin/env bash
export HOME=~
set -eu
# alice -> bob -> carol
alice="./run_electrum --regtest -D /tmp/alice"
bob="./run_electrum --regtest -D /tmp/bob"
carol="./run_electrum --regtest -D /tmp/carol"
2019-03-11 21:00:29 +01:00
bitcoin_cli="bitcoin-cli -rpcuser=doggman -rpcpassword=donkey -rpcport=18554 -regtest"
function new_blocks()
{
$bitcoin_cli generatetoaddress $1 $($bitcoin_cli getnewaddress) > /dev/null
}
2019-08-30 15:18:04 +02:00
function wait_for_balance()
2019-07-03 08:40:18 +02:00
{
2019-08-30 15:18:04 +02:00
msg="wait until $1's balance reaches $2"
cmd="./run_electrum --regtest -D /tmp/$1"
2019-08-30 15:18:04 +02:00
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
2019-08-30 15:18:04 +02:00
msg="$msg."
printf "$msg\r"
2019-07-03 08:40:18 +02:00
done
2019-08-30 15:18:04 +02:00
printf "\n"
2019-07-03 08:40:18 +02:00
}
function wait_until_channel_open()
{
2019-08-30 15:18:04 +02:00
msg="wait until $1 sees channel open"
cmd="./run_electrum --regtest -D /tmp/$1"
2019-08-30 15:18:04 +02:00
while channel_state=$($cmd list_channels | jq '.[0] | .state' | tr -d '"') && [ $channel_state != "OPEN" ]; do
sleep 1
2019-08-30 15:18:04 +02:00
msg="$msg."
printf "$msg\r"
2019-07-03 08:40:18 +02:00
done
2019-08-30 15:18:04 +02:00
printf "\n"
}
function wait_until_channel_closed()
{
msg="wait until $1 sees channel closed"
cmd="./run_electrum --regtest -D /tmp/$1"
2019-08-30 15:18:04 +02:00
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"
2019-07-03 08:40:18 +02:00
}
2019-03-11 21:00:29 +01:00
if [[ $# -eq 0 ]]; then
echo "syntax: init|start|open|status|pay|close|stop"
exit 1
fi
if [[ $1 == "init" ]]; then
echo "initializing alice, bob and carol"
2019-03-11 21:00:29 +01:00
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
2019-03-11 21:00:29 +01:00
fi
# start daemons. Bob is started first because he is listening
if [[ $1 == "start" ]]; then
2019-09-02 19:04:08 +02:00
$bob daemon -d
$alice daemon -d
$carol daemon -d
2019-08-19 12:46:31 +02:00
$bob load_wallet
$alice load_wallet
$carol load_wallet
2019-03-11 21:00:29 +01:00
sleep 10 # give time to synchronize
fi
if [[ $1 == "stop" ]]; then
2019-08-19 12:46:31 +02:00
$alice stop || true
$bob stop || true
$carol stop || true
2019-03-11 21:00:29 +01:00
fi
if [[ $1 == "open" ]]; then
bob_node=$($bob nodeid)
channel_id1=$($alice open_channel $bob_node 0.001 --channel_push 0.001)
channel_id2=$($carol open_channel $bob_node 0.001 --channel_push 0.001)
echo "mining 3 blocks"
new_blocks 3
2019-03-11 21:00:29 +01:00
sleep 10 # time for channelDB
fi
if [[ $1 == "alice_pays_carol" ]]; then
2019-09-20 17:15:49 +02:00
request=$($carol add_lightning_request 0.0001 -m "blah")
2019-03-11 21:00:29 +01:00
$alice lnpay $request
carol_balance=$($carol list_channels | jq -r '.[0].local_balance')
echo "carol balance: $carol_balance"
if [[ $carol_balance != 110000 ]]; then
exit 1
2019-03-11 21:00:29 +01:00
fi
fi
if [[ $1 == "close" ]]; then
chan1=$($alice list_channels | jq -r ".[0].channel_point")
chan2=$($carol list_channels | jq -r ".[0].channel_point")
$alice close_channel $chan1
$carol close_channel $chan2
echo "mining 1 block"
new_blocks 1
2019-03-11 21:00:29 +01:00
fi
2019-05-28 13:11:21 +02:00
# alice sends two payments, then broadcast ctx after first payment.
# thus, bob needs to redeem both to_local and to_remote
2019-03-11 21:00:29 +01:00
if [[ $1 == "breach" ]]; then
bob_node=$($bob nodeid)
channel=$($alice open_channel $bob_node 0.15)
2019-07-03 08:40:18 +02:00
new_blocks 3
2019-08-30 15:18:04 +02:00
wait_until_channel_open alice
2019-09-20 17:15:49 +02:00
request=$($bob add_lightning_request 0.01 -m "blah")
echo "alice pays"
2019-03-11 21:00:29 +01:00
$alice lnpay $request
2019-05-28 13:11:21 +02:00
sleep 2
ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"')
2019-09-20 17:15:49 +02:00
request=$($bob add_lightning_request 0.01 -m "blah2")
2019-08-30 15:18:04 +02:00
echo "alice pays again"
2019-05-28 13:11:21 +02:00
$alice lnpay $request
echo "alice broadcasts old ctx"
$bitcoin_cli sendrawtransaction $ctx
2019-08-30 15:18:04 +02:00
wait_until_channel_closed bob
new_blocks 1
wait_for_balance bob 0.14
$bob getbalance
2019-03-11 21:00:29 +01:00
fi
if [[ $1 == "redeem_htlcs" ]]; then
2019-08-19 12:46:31 +02:00
$bob stop
2019-09-02 19:04:08 +02:00
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=10 $bob daemon -d
2019-08-30 15:57:01 +02:00
sleep 1
2019-08-19 12:46:31 +02:00
$bob load_wallet
2019-03-11 21:00:29 +01:00
sleep 1
# alice opens channel
bob_node=$($bob nodeid)
$alice open_channel $bob_node 0.15
new_blocks 6
2019-03-11 21:00:29 +01:00
sleep 10
# alice pays bob
2019-09-20 17:15:49 +02:00
invoice=$($bob add_lightning_request 0.05 -m "test")
2019-05-29 17:34:12 +02:00
$alice lnpay $invoice --timeout=1 || true
2019-03-11 21:00:29 +01:00
sleep 1
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo 'SETTLE_DELAY did not work'
2019-03-11 21:00:29 +01:00
exit 1
fi
# bob goes away
2019-08-19 12:46:31 +02:00
$bob stop
2019-03-11 21:00:29 +01:00
echo "alice balance before closing channel:" $($alice getbalance)
balance_before=$($alice getbalance | jq '[.confirmed, .unconfirmed, .lightning] | to_entries | map(select(.value != null).value) | map(tonumber) | add ')
# alice force closes the channel
chan_id=$($alice list_channels | jq -r ".[0].channel_point")
$alice close_channel $chan_id --force
new_blocks 1
sleep 3
2019-03-11 21:00:29 +01:00
echo "alice balance after closing channel:" $($alice getbalance)
new_blocks 150
2019-03-11 21:00:29 +01:00
sleep 10
new_blocks 1
sleep 3
echo "alice balance after CLTV" $($alice getbalance)
new_blocks 150
2019-03-11 21:00:29 +01:00
sleep 10
new_blocks 1
sleep 3
echo "alice balance after CSV" $($alice getbalance)
2019-08-30 15:18:04 +02:00
# fixme: add local to getbalance
wait_for_balance alice $(echo "$balance_before - 0.02" | bc -l)
$alice getbalance
2019-03-11 21:00:29 +01:00
fi
2019-05-29 17:34:12 +02:00
2019-06-24 11:13:18 +02:00
if [[ $1 == "breach_with_unspent_htlc" ]]; then
2019-08-19 12:46:31 +02:00
$bob stop
2019-09-02 19:04:08 +02:00
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -d
2019-08-30 15:57:01 +02:00
sleep 1
2019-08-19 12:46:31 +02:00
$bob load_wallet
2019-08-30 15:18:04 +02:00
wait_for_balance alice 1
echo "alice opens channel"
2019-05-29 17:34:12 +02:00
bob_node=$($bob nodeid)
channel=$($alice open_channel $bob_node 0.15)
new_blocks 3
2019-08-30 15:18:04 +02:00
wait_until_channel_open alice
2019-05-29 17:34:12 +02:00
echo "alice pays bob"
2019-09-20 17:15:49 +02:00
invoice=$($bob add_lightning_request 0.05 -m "test")
2019-05-29 17:34:12 +02:00
$alice lnpay $invoice --timeout=1 || true
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo "SETTLE_DELAY did not work, $settled != 0"
2019-05-29 17:34:12 +02:00
exit 1
fi
ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"')
sleep 5
2019-05-29 17:34:12 +02:00
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "1" ]]; then
echo "SETTLE_DELAY did not work, $settled != 1"
2019-05-29 17:34:12 +02:00
exit 1
fi
echo "alice breaches with old ctx"
$bitcoin_cli sendrawtransaction $ctx
2019-08-30 15:18:04 +02:00
wait_for_balance bob 0.14
2019-05-29 17:34:12 +02:00
fi
2019-06-24 11:13:18 +02:00
if [[ $1 == "breach_with_spent_htlc" ]]; then
2019-08-19 12:46:31 +02:00
$bob stop
2019-09-02 19:04:08 +02:00
ELECTRUM_DEBUG_LIGHTNING_SETTLE_DELAY=3 $bob daemon -d
2019-08-30 15:57:01 +02:00
sleep 1
2019-08-19 12:46:31 +02:00
$bob load_wallet
2019-08-30 15:18:04 +02:00
wait_for_balance alice 1
2019-06-24 11:13:18 +02:00
echo "alice opens channel"
bob_node=$($bob nodeid)
channel=$($alice open_channel $bob_node 0.15)
new_blocks 3
2019-08-30 15:18:04 +02:00
wait_until_channel_open alice
2019-06-24 11:13:18 +02:00
echo "alice pays bob"
2019-09-20 17:15:49 +02:00
invoice=$($bob add_lightning_request 0.05 -m "test")
2019-06-24 11:13:18 +02:00
$alice lnpay $invoice --timeout=1 || true
ctx=$($alice get_channel_ctx $channel | jq '.hex' | tr -d '"')
2019-06-24 11:13:18 +02:00
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo "SETTLE_DELAY did not work, $settled != 0"
2019-06-24 11:13:18 +02:00
exit 1
fi
cp /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/toxic_wallet
sleep 5
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "1" ]]; then
echo "SETTLE_DELAY did not work, $settled != 1"
2019-06-24 11:13:18 +02:00
exit 1
fi
echo $($bob getbalance)
echo "bob goes offline"
2019-08-19 12:46:31 +02:00
$bob stop
2019-06-24 11:13:18 +02:00
ctx_id=$($bitcoin_cli sendrawtransaction $ctx)
echo "alice breaches with old ctx:" $ctx_id
new_blocks 1
if [[ $($bitcoin_cli gettxout $ctx_id 0 | jq '.confirmations') != "1" ]]; then
echo "breach tx not confirmed"
exit 1
2019-06-24 11:13:18 +02:00
fi
echo "wait for cltv_expiry blocks"
# 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)
new_blocks 150
2019-08-19 12:46:31 +02:00
$alice stop
2019-09-02 19:04:08 +02:00
$alice daemon -d
2019-08-30 15:57:01 +02:00
sleep 1
$alice load_wallet -w /tmp/alice/regtest/wallets/toxic_wallet
2019-06-24 11:13:18 +02:00
# wait until alice has spent both ctx outputs
2019-08-30 15:18:04 +02:00
echo "alice spends to_local and htlc outputs"
wait_until_spent $ctx_id 0
wait_until_spent $ctx_id 1
2019-06-24 11:13:18 +02:00
new_blocks 1
echo "bob comes back"
2019-09-02 19:04:08 +02:00
$bob daemon -d
2019-08-30 15:57:01 +02:00
sleep 1
2019-08-19 12:46:31 +02:00
$bob load_wallet
2019-08-30 15:18:04 +02:00
wait_for_balance bob 0.049
$bob getbalance
2019-06-24 11:13:18 +02:00
fi
2019-07-05 14:42:09 +02:00
if [[ $1 == "watchtower" ]]; then
# carol is a watchtower of alice
2019-08-19 12:46:31 +02:00
$alice stop
$carol stop
$alice setconfig --offline watchtower_url http://127.0.0.1:12345
$carol setconfig --offline watchtower_host 127.0.0.1
$carol setconfig --offline watchtower_port 12345
2019-09-02 19:04:08 +02:00
$carol daemon -d
$alice daemon -d
2019-08-30 15:57:01 +02:00
sleep 1
2019-08-19 12:46:31 +02:00
$alice load_wallet
2019-08-30 15:18:04 +02:00
wait_for_balance alice 1
2019-07-05 14:42:09 +02:00
echo "alice opens channel"
bob_node=$($bob nodeid)
channel=$($alice open_channel $bob_node 0.5)
new_blocks 3
2019-08-30 15:18:04 +02:00
wait_until_channel_open alice
2019-07-05 14:42:09 +02:00
echo "alice pays bob"
2019-09-20 17:15:49 +02:00
invoice1=$($bob add_lightning_request 0.05 -m "invoice1")
2019-07-05 14:42:09 +02:00
$alice lnpay $invoice1
2019-09-20 17:15:49 +02:00
invoice2=$($bob add_lightning_request 0.05 -m "invoice2")
2019-07-05 14:42:09 +02:00
$alice lnpay $invoice2
2019-09-20 17:15:49 +02:00
invoice3=$($bob add_lightning_request 0.05 -m "invoice3")
2019-07-05 14:42:09 +02:00
$alice lnpay $invoice3
fi