Skip to content

Commit

Permalink
common: Check chain hash in gossip_timestamp_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nalinbhardwaj committed May 8, 2021
1 parent 9825f32 commit 87a9e76
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
12 changes: 11 additions & 1 deletion common/read_peer_msg.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "config.h"
#include <assert.h>
#include <bitcoin/chainparams.h>
#include <ccan/fdpass/fdpass.h>
#include <common/crypto_sync.h>
#include <common/gossip_rcvd_filter.h>
Expand Down Expand Up @@ -142,7 +143,7 @@ void handle_gossip_msg(struct per_peer_state *pps, const u8 *msg TAKES)
/* takes iff returns true */
bool handle_timestamp_filter(struct per_peer_state *pps, const u8 *msg TAKES)
{
struct bitcoin_blkid chain_hash; /* FIXME: don't ignore! */
struct bitcoin_blkid chain_hash;
u32 first_timestamp, timestamp_range;

if (!fromwire_gossip_timestamp_filter(msg, &chain_hash,
Expand All @@ -151,6 +152,15 @@ bool handle_timestamp_filter(struct per_peer_state *pps, const u8 *msg TAKES)
return false;
}

if (!bitcoin_blkid_eq(&chainparams->genesis_blockhash, &chain_hash)) {
sync_crypto_write(pps,
take(towire_warningfmt(NULL, NULL,
"gossip_timestamp_filter"
" for bad chain: %s",
tal_hex(tmpctx, msg))));
return true;
}

gossip_setup_timestamp_filter(pps, first_timestamp, timestamp_range);
return true;
}
Expand Down
18 changes: 10 additions & 8 deletions tests/test_gossip.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ def test_announce_address(node_factory, bitcoind):


@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
def test_gossip_timestamp_filter(node_factory, bitcoind):
def test_gossip_timestamp_filter(node_factory, bitcoind, chainparams):
# Updates get backdated 5 seconds with --dev-fast-gossip.
backdate = 5
l1, l2, l3, l4 = node_factory.line_graph(4, fundchannel=False)
genesis_blockhash = chainparams['chain_hash']

before_anything = int(time.time())

Expand All @@ -164,7 +165,7 @@ def test_gossip_timestamp_filter(node_factory, bitcoind):
wait_for(lambda: ['alias' in node for node in l4.rpc.listnodes()['nodes']] == [True, True, True])

msgs = l4.query_gossip('gossip_timestamp_filter',
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f',
genesis_blockhash,
'0', '0xFFFFFFFF',
filters=['0109'])

Expand All @@ -177,14 +178,14 @@ def test_gossip_timestamp_filter(node_factory, bitcoind):

# Now timestamp which doesn't overlap (gives nothing).
msgs = l4.query_gossip('gossip_timestamp_filter',
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f',
genesis_blockhash,
'0', before_anything - backdate,
filters=['0109'])
assert msgs == []

# Now choose range which will only give first update.
msgs = l4.query_gossip('gossip_timestamp_filter',
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f',
genesis_blockhash,
before_anything - backdate,
after_12 - before_anything + 1,
filters=['0109'])
Expand All @@ -198,7 +199,7 @@ def test_gossip_timestamp_filter(node_factory, bitcoind):

# Now choose range which will only give second update.
msgs = l4.query_gossip('gossip_timestamp_filter',
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f',
genesis_blockhash,
after_12 - backdate,
after_23 - after_12 + 1,
filters=['0109'])
Expand Down Expand Up @@ -1029,11 +1030,12 @@ def test_gossip_store_load_amount_truncated(node_factory):


@unittest.skipIf(not DEVELOPER, "Needs fast gossip propagation")
def test_node_reannounce(node_factory, bitcoind):
def test_node_reannounce(node_factory, bitcoind, chainparams):
"Test that we reannounce a node when parameters change"
l1, l2 = node_factory.line_graph(2, opts={'may_reconnect': True,
'log-level': 'io'})
bitcoind.generate_block(5)
genesis_blockhash = chainparams['chain_hash']

# Wait for node_announcement for l1.
l2.daemon.wait_for_log(r'\[IN\] 0101.*{}'.format(l1.info['id']))
Expand All @@ -1059,7 +1061,7 @@ def test_node_reannounce(node_factory, bitcoind):

# Get node_announcements.
msgs = l1.query_gossip('gossip_timestamp_filter',
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f',
genesis_blockhash,
'0', '0xFFFFFFFF',
# Filter out gossip_timestamp_filter,
# channel_announcement and channel_updates.
Expand All @@ -1073,7 +1075,7 @@ def test_node_reannounce(node_factory, bitcoind):
l1.restart()

msgs2 = l1.query_gossip('gossip_timestamp_filter',
'06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f',
genesis_blockhash,
'0', '0xFFFFFFFF',
# Filter out gossip_timestamp_filter,
# channel_announcement and channel_updates.
Expand Down

0 comments on commit 87a9e76

Please sign in to comment.