From c99906a9a91aaa482dc54ab6a49f197aa0ff7f57 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 6 Sep 2019 15:39:36 +0930 Subject: [PATCH] per-peer-daemons: tie in gossip filter. Signed-off-by: Rusty Russell --- channeld/Makefile | 1 + closingd/Makefile | 2 ++ common/gossip_store.c | 7 +++++++ common/per_peer_state.c | 4 ++++ common/per_peer_state.h | 2 ++ common/read_peer_msg.c | 2 ++ connectd/Makefile | 1 + devtools/Makefile | 2 ++ gossipd/Makefile | 1 + lightningd/Makefile | 1 + openingd/Makefile | 1 + openingd/openingd.c | 2 ++ tests/test_gossip.py | 10 +++++++--- 13 files changed, 33 insertions(+), 3 deletions(-) diff --git a/channeld/Makefile b/channeld/Makefile index 7e31a9d11..9fe4d9bd0 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -48,6 +48,7 @@ CHANNELD_COMMON_OBJS := \ common/features.o \ common/gen_status_wire.o \ common/gen_peer_status_wire.o \ + common/gossip_rcvd_filter.o \ common/gossip_store.o \ common/htlc_state.o \ common/htlc_trim.o \ diff --git a/closingd/Makefile b/closingd/Makefile index 26cc3fbde..15462a2ec 100644 --- a/closingd/Makefile +++ b/closingd/Makefile @@ -57,6 +57,7 @@ CLOSINGD_COMMON_OBJS := \ common/features.o \ common/gen_peer_status_wire.o \ common/gen_status_wire.o \ + common/gossip_rcvd_filter.o \ common/gossip_store.o \ common/htlc_wire.o \ common/key_derive.o \ @@ -66,6 +67,7 @@ CLOSINGD_COMMON_OBJS := \ common/peer_failed.o \ common/per_peer_state.o \ common/permute_tx.o \ + common/pseudorand.o \ common/read_peer_msg.o \ common/socket_close.o \ common/status.o \ diff --git a/common/gossip_store.c b/common/gossip_store.c index 63bad8a91..2edca39c2 100644 --- a/common/gossip_store.c +++ b/common/gossip_store.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -122,6 +123,12 @@ u8 *gossip_store_next(const tal_t *ctx, struct per_peer_state *pps) 0, SEEK_CUR) - msglen, tal_hex(tmpctx, msg)); + /* Don't send back gossip they sent to us! */ + if (gossip_rcvd_filter_del(pps->grf, msg)) { + msg = tal_free(msg); + continue; + } + /* Ignore gossipd internal messages. */ type = fromwire_peektype(msg); if (type != WIRE_CHANNEL_ANNOUNCEMENT diff --git a/common/per_peer_state.c b/common/per_peer_state.c index 7653f9b1f..0166e7b35 100644 --- a/common/per_peer_state.c +++ b/common/per_peer_state.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -22,6 +23,7 @@ struct per_peer_state *new_per_peer_state(const tal_t *ctx, pps->cs = *cs; pps->gs = NULL; pps->peer_fd = pps->gossip_fd = pps->gossip_store_fd = -1; + pps->grf = new_gossip_rcvd_filter(pps); tal_add_destructor(pps, destroy_per_peer_state); return pps; } @@ -70,6 +72,7 @@ void towire_per_peer_state(u8 **pptr, const struct per_peer_state *pps) towire_bool(pptr, pps->gs != NULL); if (pps->gs) towire_gossip_state(pptr, pps->gs); + /* We don't pass the gossip_rcvd_filter: it's merely an optimization */ } void per_peer_state_fdpass_send(int fd, const struct per_peer_state *pps) @@ -138,4 +141,5 @@ void per_peer_state_reset_gossip_timer(struct per_peer_state *pps) t = time_from_msec(pps->dev_gossip_broadcast_msec); #endif pps->gs->next_gossip = timemono_add(time_mono(), t); + gossip_rcvd_filter_age(pps->grf); } diff --git a/common/per_peer_state.h b/common/per_peer_state.h index ba9e50c4f..0979e57b0 100644 --- a/common/per_peer_state.h +++ b/common/per_peer_state.h @@ -20,6 +20,8 @@ struct per_peer_state { struct crypto_state cs; /* NULL if it's not initialized yet */ struct gossip_state *gs; + /* Cache of msgs we have received, to avoid re-xmitting from store */ + struct gossip_rcvd_filter *grf; #if DEVELOPER /* Normally 60000, but adjustable for dev mode */ u32 dev_gossip_broadcast_msec; diff --git a/common/read_peer_msg.c b/common/read_peer_msg.c index 53355ff77..dbbbd1f07 100644 --- a/common/read_peer_msg.c +++ b/common/read_peer_msg.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -166,6 +167,7 @@ bool handle_peer_gossip_or_error(struct per_peer_state *pps, if (handle_timestamp_filter(pps, msg)) return true; else if (is_msg_for_gossipd(msg)) { + gossip_rcvd_filter_add(pps->grf, msg); wire_sync_write(pps->gossip_fd, msg); /* wire_sync_write takes, so don't take again. */ return true; diff --git a/connectd/Makefile b/connectd/Makefile index 6f76ebfd2..7e0b5b39b 100644 --- a/connectd/Makefile +++ b/connectd/Makefile @@ -51,6 +51,7 @@ CONNECTD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/gossip_rcvd_filter.o \ common/key_derive.o \ common/memleak.o \ common/msg_queue.o \ diff --git a/devtools/Makefile b/devtools/Makefile index cdde6a17f..4f56bcb20 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -16,10 +16,12 @@ DEVTOOLS_COMMON_OBJS := \ common/crypto_state.o \ common/decode_short_channel_ids.o \ common/features.o \ + common/gossip_rcvd_filter.o \ common/hash_u5.o \ common/memleak.o \ common/node_id.o \ common/per_peer_state.o \ + common/pseudorand.o \ common/json.o \ common/json_helpers.o \ common/type_to_string.o \ diff --git a/gossipd/Makefile b/gossipd/Makefile index 72704512e..834ef400e 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -51,6 +51,7 @@ GOSSIPD_COMMON_OBJS := \ common/dev_disconnect.o \ common/features.o \ common/gen_status_wire.o \ + common/gossip_rcvd_filter.o \ common/key_derive.o \ common/memleak.o \ common/msg_queue.o \ diff --git a/lightningd/Makefile b/lightningd/Makefile index 25064c8de..b99b3fd82 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -31,6 +31,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/funding_tx.o \ common/gen_peer_status_wire.o \ common/gen_status_wire.o \ + common/gossip_rcvd_filter.o \ common/hash_u5.o \ common/htlc_state.o \ common/htlc_trim.o \ diff --git a/openingd/Makefile b/openingd/Makefile index c9b734cb6..31308c705 100644 --- a/openingd/Makefile +++ b/openingd/Makefile @@ -51,6 +51,7 @@ OPENINGD_COMMON_OBJS := \ common/funding_tx.o \ common/gen_status_wire.o \ common/gen_peer_status_wire.o \ + common/gossip_rcvd_filter.o \ common/gossip_store.o \ common/htlc_wire.o \ common/initial_channel.o \ diff --git a/openingd/openingd.c b/openingd/openingd.c index 42b94b215..a2fac1435 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -377,6 +378,7 @@ static u8 *opening_negotiate_msg(const tal_t *ctx, struct state *state, /* Some messages go straight to gossipd. */ if (is_msg_for_gossipd(msg)) { + gossip_rcvd_filter_add(state->pps->grf, msg); wire_sync_write(state->pps->gossip_fd, take(msg)); continue; } diff --git a/tests/test_gossip.py b/tests/test_gossip.py index 6d3e9eb4a..7e15b484d 100644 --- a/tests/test_gossip.py +++ b/tests/test_gossip.py @@ -978,8 +978,13 @@ def test_node_reannounce(node_factory, bitcoind): nannouncement = l2.daemon.wait_for_log(r'{}.*\[IN\] 0101.*{}'.format(l1.info['id'], l1.info['id'])).split('[IN] ')[1] wait_for(lambda: only_one(l2.rpc.listnodes(l1.info['id'])['nodes'])['alias'] == 'SENIORBEAM') - # Restart should re-xmit exact same update on reconnect. - l1.restart() + # Restart should re-xmit exact same update on reconnect, but make sure + # l2 doesn't send it first! + l1.stop() + l2.stop() + os.remove(os.path.join(l2.daemon.lightning_dir, 'gossip_store')) + l2.start() + l1.start() # l1 should retransmit it exactly the same (no timestamp change!) l2.daemon.wait_for_log(r'{}.*\[IN\] {}'.format(l1.info['id'], nannouncement)) @@ -1377,7 +1382,6 @@ def test_gossip_announce_unknown_block(node_factory, bitcoind): sync_blockheight(bitcoind, [l1]) -@pytest.mark.xfail(strict=True) @unittest.skipIf(not DEVELOPER, "gossip without DEVELOPER=1 is slow") def test_gossip_no_backtalk(node_factory): l1, l2 = node_factory.line_graph(2, wait_for_announce=True)