From 6298ce3b03ac6f4272fff0087245db214390458a Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 4 Jun 2018 15:27:57 +0200 Subject: [PATCH] gossip: Don't ask bitcoind for outpoints we should know Compares the `blocknum` in the `short_channel_id` with the range of blocks we store in the database and abort if we should have known about it. Avoids bombarding `bitcoind` with requests for channels that have already been spent or were invalid in the first place. Signed-off-by: Christian Decker --- lightningd/gossip_control.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 0e20dac1b..cd8800096 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -89,12 +89,15 @@ static void get_txout(struct subd *gossip, const u8 *msg) { struct short_channel_id *scid = tal(gossip, struct short_channel_id); struct outpoint *op; + u32 blockheight; + struct chain_topology *topo = gossip->ld->topology; if (!fromwire_gossip_get_txout(msg, scid)) fatal("Gossip gave bad GOSSIP_GET_TXOUT message %s", tal_hex(msg, msg)); /* FIXME: Block less than 6 deep? */ + blockheight = short_channel_id_blocknum(scid); op = wallet_outpoint_for_scid(gossip->ld->wallet, scid, scid); @@ -103,8 +106,17 @@ static void get_txout(struct subd *gossip, const u8 *msg) towire_gossip_get_txout_reply( scid, scid, op->satoshis, op->scriptpubkey)); tal_free(scid); + } else if (blockheight >= topo->min_blockheight && + blockheight <= topo->max_blockheight) { + /* We should have known about this outpoint since it is included + * in the range in the DB. The fact that we don't means that + * this is either a spent outpoint or an invalid one. Return a + * failure. */ + subd_send_msg(gossip, take(towire_gossip_get_txout_reply( + NULL, scid, 0, NULL))); + tal_free(scid); } else { - bitcoind_getoutput(gossip->ld->topology->bitcoind, + bitcoind_getoutput(topo->bitcoind, short_channel_id_blocknum(scid), short_channel_id_txnum(scid), short_channel_id_outnum(scid),