You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.3 KiB

#! /bin/sh -e
# Wherever we are, we want to be in daemon/test dir.
cd `git rev-parse --show-toplevel`/daemon/test
. scripts/vars.sh
. scripts/helpers.sh
parse_cmdline 3 "$@"
setup_lightning 3
start_lightningd 3
# We connect 1->2->3
lcli1 connect localhost $PORT2 $FUND_INPUT_TX &
check_tx_spend
$CLI generate 1
P2SHADDR2=`$LCLI2 newaddr | sed -n 's/{ "address" : "\(.*\)" }/\1/p'`
TXID2=`$CLI sendtoaddress $P2SHADDR2 0.01`
FUND_INPUT_TX2=`$CLI getrawtransaction $TXID2`
$CLI generate 1
lcli2 connect localhost $PORT3 $FUND_INPUT_TX2 &
check_tx_spend
$CLI generate 1
DO_RECONNECT=$RECONNECT
# Make sure all in STATE_NORMAL.
check_peerstate lcli1 STATE_NORMAL
check_peerstate lcli3 STATE_NORMAL
# More than enough to cover commit fees.
HTLC_AMOUNT=100000000
# Tell node 1 about the 2->3 route.
# Add to config in case we are restaring.
echo "add-route=$ID2/$ID3/546000/10/36/36" >> $DIR1/config
lcli1 dev-add-route $ID2 $ID3 546000 10 36 36
RHASH=`lcli3 invoice $HTLC_AMOUNT RHASH | sed 's/.*"\([0-9a-f]*\)".*/\1/'`
BAD_RHASH=`echo $RHASH | tr '0-9a-f' 'a-f0-9'`
# Get route.
ROUTE=`lcli1 getroute $ID3 $HTLC_AMOUNT 1`
ROUTE=`echo $ROUTE | sed 's/^{ "route" : \(.*\) }$/\1/'`
# Try wrong hash.
if lcli1 sendpay "$ROUTE" $BAD_RHASH; then
echo Paid with wrong hash? >&2
exit 1
fi
# Try underpaying.
PAID=`echo "$ROUTE" | sed -n 's/.*"msatoshi" : \([0-9]*\),.*/\1/p'`
UNDERPAY=`echo "$ROUTE" | sed "s/: $PAID,/: $(($PAID - 1)),/"`
if lcli1 sendpay "$UNDERPAY" $RHASH; then
echo Paid with too little? >&2
exit 1
fi
# If restarting, make sure node3 remembers incoming payment.
if [ "$RECONNECT" = restart ]; then
$LCLI3 -- dev-restart $LIGHTNINGD3 >/dev/null 2>&1 || true
if ! check "$LCLI3 getpeers 2>/dev/null | tr -s '\012\011\" ' ' ' | fgrep -q 'connected : true'"; then
echo "Failed to reconnect!">&2
exit 1
fi
fi
[ "`lcli3 listinvoice RHASH | tr -s '\012\011\" ' ' '`" = "[ { label : RHASH , rhash : $RHASH , msatoshi : $HTLC_AMOUNT, complete : false } ] " ]
# Pay correctly.
lcli1 sendpay "$ROUTE" $RHASH
# Node 3 should end up with that amount (minus 1/2 tx fee)
# Note that it is delayed a little, since node2 fulfils as soon as fulfill
# starts.
check lcli3 "getpeers | $FGREP \"\\\"our_amount\\\" : $(($HTLC_AMOUNT - $NO_HTLCS_FEE / 2))\""
# If restarting, make sure node3 remembers completed payment.
if [ "$RECONNECT" = restart ]; then
echo RESTARTING NODE3
$LCLI3 -- dev-restart $LIGHTNINGD3 >/dev/null 2>&1 || true
sleep 5
$LCLI2 -- dev-restart $LIGHTNINGD2 >/dev/null 2>&1 || true
if ! check "$LCLI3 getpeers 2>/dev/null | tr -s '\012\011\" ' ' ' | fgrep -q 'connected : true'"; then
echo "Failed to reconnect!">&2
exit 1
fi
fi
[ "`lcli3 listinvoice RHASH | tr -s '\012\011\" ' ' '`" = "[ { label : RHASH , rhash : $RHASH , msatoshi : $HTLC_AMOUNT, complete : true } ] " ]
[ "`lcli3 waitinvoice | tr -s '\012\011\" ' ' '`" = "{ label : RHASH , rhash : $RHASH , msatoshi : $HTLC_AMOUNT } " ]
# Can't pay twice (try from node2)
ROUTE2=`lcli2 getroute $ID3 $HTLC_AMOUNT 1`
ROUTE2=`echo $ROUTE2 | sed 's/^{ "route" : \(.*\) }$/\1/'`
if lcli2 sendpay "$ROUTE2" $RHASH; then
echo "Paying twice worked?" >&2
exit 1
fi
lcli3 close $ID2
check_peerstate lcli3 STATE_MUTUAL_CLOSING
# Re-send should be a noop (doesn't matter that node3 is down!)
lcli1 sendpay "$ROUTE" $RHASH
# Re-send to different id or amount should complain.
SHORTROUTE=`echo "$ROUTE" | sed 's/, { "id" : .* }//' | sed 's/"msatoshi" : [0-9]*,/"msatoshi" : '$HTLC_AMOUNT,/`
lcli1 sendpay "$SHORTROUTE" $RHASH | $FGREP "already succeeded to $ID3"
lcli1 sendpay "$UNDERPAY" $RHASH | $FGREP "already succeeded with amount $HTLC_AMOUNT"
# Now node2 should fail to route.
if lcli1 sendpay "$ROUTE" $BAD_RHASH | $FGREP "failed: error code 404 node $ID2 reason Unknown peer"; then : ;
else
echo "Pay to node3 didn't give 404" >&2
exit 1
fi
# Now node1 should fail to route (route deleted)
if lcli1 getroute $ID3 $HTLC_AMOUNT 1 | $FGREP "no route found"; then : ;
else
echo "Pay to node3 didn't fail instantly second time" >&2
exit 1
fi
lcli1 close $ID2
check_peerstate lcli1 STATE_MUTUAL_CLOSING
# Make sure both txs broadcast.
check '[ `$CLI getrawmempool | egrep -c "[a-f0-9]{32}"` = 2 ]'
# Bury them in "forever" blocks.
$CLI generate 10
check_no_peers lcli1
check_no_peers lcli2
check_no_peers lcli3
lcli1 stop
lcli2 stop
lcli3 stop
all_ok