Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle reply_channel_range from LND (and enhance gossipwith) #3264

Merged
merged 4 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ def query_gossip(self, querytype, *args, filters=[]):
timeout=TIMEOUT,
stdout=subprocess.PIPE).stdout.strip()
out = subprocess.run(['devtools/gossipwith',
'--timeout-after={}'.format(int(math.sqrt(TIMEOUT) * 1000)),
'--timeout-after={}'.format(int(math.sqrt(TIMEOUT) + 1)),
'{}@localhost:{}'.format(self.info['id'],
self.port),
query],
Expand Down
44 changes: 31 additions & 13 deletions devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ccan/str/hex/hex.h>
#include <common/crypto_sync.h>
#include <common/dev_disconnect.h>
#include <common/features.h>
#include <common/peer_failed.h>
#include <common/per_peer_state.h>
#include <common/status.h>
Expand All @@ -20,7 +21,9 @@
#define io_close simple_close
static bool stream_stdin = false;
static bool no_init = false;
static bool hex = false;
static int timeout_after = -1;
static u8 *features;

static struct io_plan *simple_write(struct io_conn *conn,
const void *data, size_t len,
Expand Down Expand Up @@ -122,18 +125,15 @@ static struct io_plan *handshake_success(struct io_conn *conn,
{
u8 *msg;
struct per_peer_state *pps = new_per_peer_state(conn, orig_cs);
u8 *localfeatures;
struct pollfd pollfd[2];

pps->peer_fd = io_conn_fd(conn);
if (initial_sync) {
localfeatures = tal(conn, u8);
localfeatures[0] = (1 << 3);
} else
localfeatures = NULL;
if (initial_sync)
set_feature_bit(&features,
OPTIONAL_FEATURE(OPT_INITIAL_ROUTING_SYNC));

if (!no_init) {
msg = towire_init(NULL, NULL, localfeatures);
msg = towire_init(NULL, NULL, features);

sync_crypto_write(pps, take(msg));
/* Ignore their init message. */
Expand All @@ -160,7 +160,8 @@ static struct io_plan *handshake_success(struct io_conn *conn,
beint16_t belen;
u8 *msg;

if (poll(pollfd, ARRAY_SIZE(pollfd), timeout_after) == 0)
if (poll(pollfd, ARRAY_SIZE(pollfd),
timeout_after < 0 ? -1 : timeout_after * 1000) == 0)
return 0;

/* We always to stdin first if we can */
Expand All @@ -178,10 +179,14 @@ static struct io_plan *handshake_success(struct io_conn *conn,
msg = sync_crypto_read(NULL, pps);
if (!msg)
err(1, "Reading msg");
belen = cpu_to_be16(tal_bytelen(msg));
if (!write_all(STDOUT_FILENO, &belen, sizeof(belen))
|| !write_all(STDOUT_FILENO, msg, tal_bytelen(msg)))
err(1, "Writing out msg");
if (hex) {
printf("%s\n", tal_hex(msg, msg));
} else {
belen = cpu_to_be16(tal_bytelen(msg));
if (!write_all(STDOUT_FILENO, &belen, sizeof(belen))
|| !write_all(STDOUT_FILENO, msg, tal_bytelen(msg)))
err(1, "Writing out msg");
}
tal_free(msg);
--max_messages;
}
Expand All @@ -201,6 +206,14 @@ static void opt_show_secret(char buf[OPT_SHOW_LEN], const struct secret *s)
hex_encode(s->data, sizeof(s->data), buf, OPT_SHOW_LEN);
}

static char *opt_set_features(const char *arg, u8 **features)
{
*features = tal_hexdata(tal_parent(*features), arg, strlen(arg));
if (!*features)
return "features must be valid hex";
return NULL;
}

int main(int argc, char *argv[])
{
struct io_conn *conn = tal(NULL, struct io_conn);
Expand All @@ -216,6 +229,7 @@ int main(int argc, char *argv[])
SECP256K1_CONTEXT_SIGN);

memset(&notsosecret, 0x42, sizeof(notsosecret));
features = tal_arr(conn, u8, 0);

opt_register_noarg("--initial-sync", opt_set_bool, &initial_sync,
"Stream complete gossip history at start");
Expand All @@ -231,7 +245,11 @@ int main(int argc, char *argv[])
"Secret key to use for our identity");
opt_register_arg("--timeout-after", opt_set_intval, opt_show_intval,
&timeout_after,
"Exit (success) this many msec after no msgs rcvd");
"Exit (success) this many seconds after no msgs rcvd");
opt_register_noarg("--hex", opt_set_bool, &hex,
"Print out messages in hex");
opt_register_arg("--features=<hex>", opt_set_features, NULL,
&features, "Send these features in init");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"id@addr[:port] [hex-msg-tosend...]\n"
"Connect to a lightning peer and relay gossip messages from it",
Expand Down
43 changes: 27 additions & 16 deletions gossipd/queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,23 +721,34 @@ const u8 *handle_reply_channel_range(struct peer *peer, const u8 *msg)
if (end > peer->range_end_blocknum)
end = peer->range_end_blocknum;

/* We keep a bitmap of what blocks have been covered by replies: bit 0
* represents block peer->range_first_blocknum */
b = bitmap_ffs(peer->query_channel_blocks,
start - peer->range_first_blocknum,
end - peer->range_first_blocknum);
if (b != end - peer->range_first_blocknum) {
return towire_errorfmt(peer, NULL,
"reply_channel_range %u+%u already have block %lu",
first_blocknum, number_of_blocks,
peer->range_first_blocknum + b);
}
/* LND mis-implemented the spec. If they have multiple replies, set
* each one to the *whole* range, with complete=0 except the last.
* Try to accomodate that (pretend we make no progress until the
* end)! */
if (first_blocknum == peer->range_first_blocknum
&& first_blocknum + number_of_blocks == peer->range_end_blocknum
&& !complete
&& tal_bytelen(msg) == 64046) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this constant come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the size of the message LND sends (8000 uncompressed scids). I wanted this workaround to be as narrow as possible to avoid catching real cases.

status_debug("LND reply_channel_range detected: futzing");
} else {
/* We keep a bitmap of what blocks have been covered by replies: bit 0
* represents block peer->range_first_blocknum */
b = bitmap_ffs(peer->query_channel_blocks,
start - peer->range_first_blocknum,
end - peer->range_first_blocknum);
if (b != end - peer->range_first_blocknum) {
return towire_errorfmt(peer, NULL,
"reply_channel_range %u+%u already have block %lu",
first_blocknum, number_of_blocks,
peer->range_first_blocknum + b);
}

/* Mark that short_channel_ids for this block have been received */
bitmap_fill_range(peer->query_channel_blocks,
start - peer->range_first_blocknum,
end - peer->range_first_blocknum);
peer->range_blocks_remaining -= end - start;
/* Mark that short_channel_ids for this block have been received */
bitmap_fill_range(peer->query_channel_blocks,
start - peer->range_first_blocknum,
end - peer->range_first_blocknum);
peer->range_blocks_remaining -= end - start;
}

/* Add scids */
n = tal_count(peer->query_channel_scids);
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gossip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ def test_gossipwith(node_factory):

out = subprocess.run(['devtools/gossipwith',
'--initial-sync',
'--timeout-after={}'.format(int(math.sqrt(TIMEOUT) * 1000)),
'--timeout-after={}'.format(int(math.sqrt(TIMEOUT) + 1)),
'{}@localhost:{}'.format(l1.info['id'], l1.port)],
check=True,
timeout=TIMEOUT, stdout=subprocess.PIPE).stdout
Expand Down