From ce160d9b17a23ed51a20ec891890fe734140722a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 26 Sep 2017 14:27:31 +0930 Subject: [PATCH] lightnind: _ dev-disconnect argument to suppress commit timer. Required for catching daemon in exact state. Signed-off-by: Rusty Russell --- channeld/channel.c | 8 ++++++++ common/crypto_sync.c | 3 ++- common/cryptomsg.c | 3 ++- common/dev_disconnect.c | 7 ++++++- common/dev_disconnect.h | 24 ++++++++++++++++++------ lightningd/peer_control.c | 2 +- lightningd/test/run-cryptomsg.c | 2 +- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/channeld/channel.c b/channeld/channel.c index 966c71c08..fe16bfb62 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -618,6 +619,13 @@ static void send_commit(struct peer *peer) u8 *msg; const struct htlc **changed_htlcs; + /* Hack to suppress all commit sends if dev_disconnect says to */ + if (dev_suppress_commit) { + peer->commit_timer = NULL; + tal_free(tmpctx); + return; + } + /* FIXME: Document this requirement in BOLT 2! */ /* We can't send two commits in a row. */ if (channel_awaiting_revoke_and_ack(peer->channel)) { diff --git a/common/crypto_sync.c b/common/crypto_sync.c index 633474bc5..f5ea8b821 100644 --- a/common/crypto_sync.c +++ b/common/crypto_sync.c @@ -28,7 +28,8 @@ bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES) case DEV_DISCONNECT_BLACKHOLE: dev_blackhole_fd(fd); break; - default: + case DEV_DISCONNECT_SUPPRESS_COMMIT: + case DEV_DISCONNECT_NORMAL: break; } ret = write_all(fd, enc, tal_len(enc)); diff --git a/common/cryptomsg.c b/common/cryptomsg.c index db4288c9e..341a31d08 100644 --- a/common/cryptomsg.c +++ b/common/cryptomsg.c @@ -355,7 +355,8 @@ struct io_plan *peer_write_message(struct io_conn *conn, case DEV_DISCONNECT_BLACKHOLE: dev_blackhole_fd(io_conn_fd(conn)); break; - default: + case DEV_DISCONNECT_SUPPRESS_COMMIT: + case DEV_DISCONNECT_NORMAL: break; } diff --git a/common/dev_disconnect.c b/common/dev_disconnect.c index e7d9cec50..082f2a920 100644 --- a/common/dev_disconnect.c +++ b/common/dev_disconnect.c @@ -16,6 +16,8 @@ static int dev_disconnect_fd = -1; static char dev_disconnect_line[200]; static int dev_disconnect_count, dev_disconnect_len; +bool dev_suppress_commit; + void dev_disconnect_init(int fd) { int r; @@ -44,7 +46,7 @@ void dev_disconnect_init(int fd) dev_disconnect_fd = fd; } -char dev_disconnect(int pkt_type) +enum dev_disconnect dev_disconnect(int pkt_type) { if (!streq(wire_type_name(pkt_type), dev_disconnect_line+1)) return DEV_DISCONNECT_NORMAL; @@ -58,6 +60,9 @@ char dev_disconnect(int pkt_type) lseek(dev_disconnect_fd, dev_disconnect_len+1, SEEK_CUR); status_trace("dev_disconnect: %s", dev_disconnect_line); + + if (dev_disconnect_line[0] == DEV_DISCONNECT_SUPPRESS_COMMIT) + dev_suppress_commit = true; return dev_disconnect_line[0]; } diff --git a/common/dev_disconnect.h b/common/dev_disconnect.h index 108b9c92f..9b6a243d2 100644 --- a/common/dev_disconnect.h +++ b/common/dev_disconnect.h @@ -3,14 +3,23 @@ #include "config.h" #include -#define DEV_DISCONNECT_BEFORE '-' -#define DEV_DISCONNECT_AFTER '+' -#define DEV_DISCONNECT_DROPPKT '@' -#define DEV_DISCONNECT_BLACKHOLE '0' -#define DEV_DISCONNECT_NORMAL 0 +enum dev_disconnect { + /* Do nothing. */ + DEV_DISCONNECT_NORMAL = 0, + /* Close connection before sending packet (and fail write). */ + DEV_DISCONNECT_BEFORE = '-', + /* Close connection after sending packet. */ + DEV_DISCONNECT_AFTER = '+', + /* Close connection after dropping packet. */ + DEV_DISCONNECT_DROPPKT = '@', + /* Swallow all writes from now on, and do no more reads. */ + DEV_DISCONNECT_BLACKHOLE = '0', + /* Disable commit timer after sending this. */ + DEV_DISCONNECT_SUPPRESS_COMMIT = '_' +}; /* Force a close fd before or after a certain packet type */ -char dev_disconnect(int pkt_type); +enum dev_disconnect dev_disconnect(int pkt_type); /* Make next write on fd fail as if they'd disconnected. */ void dev_sabotage_fd(int fd); @@ -21,4 +30,7 @@ void dev_blackhole_fd(int fd); /* For debug code to set in daemon. */ void dev_disconnect_init(int fd); +/* Hack for channeld to do DEV_DISCONNECT_SUPPRESS_COMMIT. */ +extern bool dev_suppress_commit; + #endif /* LIGHTNING_COMMON_DEV_DISCONNECT_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index b25dddb4f..000690c7d 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -253,7 +253,7 @@ static void peer_start_closingd(struct peer *peer, bool reconnected); /* FIXME: Fake NOP dev_disconnect/dev_sabotage_fd for below. */ -char dev_disconnect(int pkt_type) +enum dev_disconnect dev_disconnect(int pkt_type) { return DEV_DISCONNECT_NORMAL; } diff --git a/lightningd/test/run-cryptomsg.c b/lightningd/test/run-cryptomsg.c index 1f0b4f57c..15b0de13d 100644 --- a/lightningd/test/run-cryptomsg.c +++ b/lightningd/test/run-cryptomsg.c @@ -48,7 +48,7 @@ void dev_sabotage_fd(int fd UNNEEDED) { fprintf(stderr, "dev_sabotage_fd called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ -char dev_disconnect(int pkt_type) +enum dev_disconnect dev_disconnect(int pkt_type) { return DEV_DISCONNECT_NORMAL; }