From 9f512f6540c53251fa65a7c4be128f1b25f7dfbf Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Aug 2016 14:25:14 +0930 Subject: [PATCH] peer: restart from the other side. Testing this revealed that we can't just reconnect when we have something to send, as we might be NATed; we should try to reconnect anyway. Signed-off-by: Rusty Russell --- daemon/peer.c | 14 +++++--------- daemon/test/test.sh | 31 +++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index 143cb6b0f..fa37911b8 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -2321,7 +2321,7 @@ static struct io_plan *reconnect_pkt_in(struct io_conn *conn, struct peer *peer) return pkt_out(conn, peer); } - /* Queue prompted us to reconnect, but we need to eliminate it now. */ + /* We need to eliminate queue now. */ clear_output_queue(peer); /* They might have missed the error, tell them before hanging up */ @@ -3832,7 +3832,8 @@ static struct io_plan *peer_reconnect(struct io_conn *conn, struct peer *peer) peer->id, crypto_on_reconnect_out, peer); } -/* Only retry when we want to send something. */ +/* We can't only retry when we want to send: they may want to send us + * something but not be able to connect (NAT). So keep retrying.. */ static void reconnect_failed(struct io_conn *conn, struct peer *peer) { /* Already otherwise connected (ie. they connected in)? */ @@ -3841,13 +3842,8 @@ static void reconnect_failed(struct io_conn *conn, struct peer *peer) return; } - if (!tal_count(peer->outpkt)) { - log_debug(peer->log, "reconnect_failed: nothing to send"); - return; - } - - log_debug(peer->log, "Have packets to send, setting timer"); - new_reltimer(peer->dstate, peer, time_from_sec(60), try_reconnect, peer); + log_debug(peer->log, "Setting timer to re-connect"); + new_reltimer(peer->dstate, peer, time_from_sec(15), try_reconnect, peer); } static struct io_plan *init_conn(struct io_conn *conn, struct peer *peer) diff --git a/daemon/test/test.sh b/daemon/test/test.sh index 7279d81d1..2c96407c0 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -354,6 +354,14 @@ EOF cp $DIR2/config $DIR3/config +if [ x"$RECONNECT" = xrestart ]; then + # Make sure node2 restarts on same port, by setting in config. + # Find a free TCP port. + port=4000 + while netstat -ntl | grep -q ":$port "; do port=$(($port + 1)); done + echo port=$port >> $DIR2/config +fi + if [ -n "$DIFFERENT_FEES" ]; then # Simply override default fee (estimatefee fails on regtest anyway) CLOSE_FEE_RATE2=50000 @@ -373,11 +381,13 @@ else $LIGHTNINGD1 > $REDIR1 2> $REDIRERR1 & fi +LIGHTNINGD2="$(readlink -f `pwd`/../lightningd) --lightning-dir=$DIR2" if [ -n "$GDB2" ]; then - echo Press return once you run: gdb --args daemon/lightningd --lightning-dir=$DIR2 >&2 + echo Press return once you run: gdb --args $LIGHTNINGD2 >&2 read REPLY else - $PREFIX ../lightningd --lightning-dir=$DIR2 > $REDIR2 2> $REDIRERR2 & + LIGHTNINGD2="$PREFIX $LIGHTNINGD2" + $LIGHTNINGD2 > $REDIR2 2> $REDIRERR2 & fi $PREFIX ../lightningd --lightning-dir=$DIR3 > $DIR3/output 2> $DIR3/errors & @@ -848,6 +858,23 @@ lcli1 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ { msatoshis : $H lcli2 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ ], their_htlcs : [ { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , state : RCVD_ADD_ACK_REVOCATION }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , state : RCVD_ADD_ACK_REVOCATION } ]" || lcli2 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ ], their_htlcs : [ { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , state : RCVD_ADD_ACK_REVOCATION }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , state : RCVD_ADD_ACK_REVOCATION } ]" +# Just for once, reconnect/restart node 2. +case "$RECONNECT" in + reconnect) + echo RECONNECTING NODE2 + $LCLI2 dev-reconnect $ID1 >/dev/null + sleep 1 + ;; + restart) + echo RESTARTING NODE2 + $LCLI2 -- dev-restart $LIGHTNINGD2 >/dev/null 2>&1 || true + if ! check "$LCLI2 getlog 2>/dev/null | fgrep -q Hello"; then + echo "Node2 dev-restart failed!">&2 + exit 1 + fi + ;; +esac + # Node2 collects the HTLCs. lcli2 fulfillhtlc $ID1 $HTLCID $SECRET lcli2 fulfillhtlc $ID1 $HTLCID2 $SECRET2