Skip to content

Commit

Permalink
devtools/gossipwith: filter for max-messages.
Browse files Browse the repository at this point in the history
So not every message type counts: this is useful when we want to get a specific number
of a specific type.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 27, 2025
1 parent cc2be86 commit f771461
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct io_conn {
static struct secret notsosecret;
static bool no_gossip = false, all_gossip = false;
static unsigned long max_messages = -1UL;
static u16 *accept_messages = NULL;

/* Empty stubs to make us compile */
void status_peer_io(enum log_level iodir,
Expand Down Expand Up @@ -101,6 +102,18 @@ bool is_unknown_msg_discardable(const u8 *cursor)
return false;
}

static bool accept_message(const u8 *msg)
{
u16 type = fromwire_peektype(msg);
if (!accept_messages)
return true;
for (size_t i = 0; i < tal_count(accept_messages); i++) {
if (type == accept_messages[i])
return true;
}
return false;
}

static struct io_plan *simple_write(struct io_conn *conn,
const void *data, size_t len,
struct io_plan *(*next)(struct io_conn *, void *),
Expand Down Expand Up @@ -240,6 +253,10 @@ static struct io_plan *handshake_success(struct io_conn *conn,
msg = sync_crypto_read(NULL, peer_fd, cs);
if (!msg)
err(1, "Reading msg");
if (!accept_message(msg)) {
tal_free(msg);
continue;
}
if (hex) {
printf("%s\n", tal_hex(msg, msg));
} else {
Expand All @@ -248,8 +265,8 @@ static struct io_plan *handshake_success(struct io_conn *conn,
|| !write_all(STDOUT_FILENO, msg, tal_bytelen(msg)))
err(1, "Writing out msg");
}
tal_free(msg);
--max_messages;
tal_free(msg);
}
}

Expand Down Expand Up @@ -284,6 +301,15 @@ static char *opt_set_features(const char *arg, u8 **features)
return NULL;
}

static char *opt_set_filter(const char *arg, u16 **accept)
{
char **elems = tal_strsplit(tmpctx, arg, ",", STR_EMPTY_OK);
*accept = tal_arr(NULL, u16, tal_count(elems)-1);
for (size_t i = 0; elems[i]; i++)
(*accept)[i] = atoi(elems[i]);
return NULL;
}

int main(int argc, char *argv[])
{
struct io_conn *conn = tal(NULL, struct io_conn);
Expand All @@ -306,6 +332,8 @@ int main(int argc, char *argv[])
"Stream complete gossip history at start");
opt_register_noarg("--no-gossip", opt_set_bool, &no_gossip,
"Suppress all gossip at start");
opt_register_arg("--filter", opt_set_filter, NULL, &accept_messages,
"Only process these message types");
opt_register_arg("--max-messages", opt_set_ulongval, opt_show_ulongval,
&max_messages,
"Terminate after reading this many messages");
Expand Down

0 comments on commit f771461

Please sign in to comment.