Skip to content

Commit a3441ff

Browse files
committed
offers: make find_best_peer take a feature bitmap.
This means we can ask for more than one required feature at a time. Signed-off-by: Rusty Russell <[email protected]>
1 parent 13852b7 commit a3441ff

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

plugins/offers.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* This plugin covers both sending and receiving offers */
22
#include "config.h"
33
#include <ccan/array_size/array_size.h>
4+
#include <ccan/bitops/bitops.h>
45
#include <ccan/rune/rune.h>
56
#include <ccan/tal/str/str.h>
67
#include <common/bech32.h>
@@ -314,7 +315,7 @@ struct find_best_peer_data {
314315
struct command_result *(*cb)(struct command *,
315316
const struct chaninfo *,
316317
void *);
317-
int needed_feature;
318+
u64 needed_features;
318319
void *arg;
319320
};
320321

@@ -333,6 +334,7 @@ static struct command_result *listincoming_done(struct command *cmd,
333334
struct chaninfo ci;
334335
const jsmntok_t *pftok;
335336
u8 *features;
337+
u64 feature_bits;
336338
const char *err;
337339
struct amount_msat feebase;
338340
bool enabled;
@@ -373,11 +375,20 @@ static struct command_result *listincoming_done(struct command *cmd,
373375
if (!pftok)
374376
continue;
375377
features = json_tok_bin_from_hex(tmpctx, buf, pftok);
376-
if (!feature_offered(features, data->needed_feature))
377-
continue;
378+
379+
/* It must have all the features we need */
380+
feature_bits = data->needed_features;
381+
while (feature_bits) {
382+
int feature = bitops_ls64(feature_bits);
383+
if (!feature_offered(features, feature))
384+
goto next;
385+
feature_bits &= ~(1ULL << feature);
386+
}
378387

379388
if (!best || amount_msat_greater(ci.capacity, best->capacity))
380389
best = tal_dup(tmpctx, struct chaninfo, &ci);
390+
391+
next:;
381392
}
382393

383394
/* Free data if they don't */
@@ -386,7 +397,7 @@ static struct command_result *listincoming_done(struct command *cmd,
386397
}
387398

388399
struct command_result *find_best_peer_(struct command *cmd,
389-
int needed_feature,
400+
u64 needed_features,
390401
struct command_result *(*cb)(struct command *,
391402
const struct chaninfo *,
392403
void *),
@@ -396,7 +407,7 @@ struct command_result *find_best_peer_(struct command *cmd,
396407
struct find_best_peer_data *data = tal(cmd, struct find_best_peer_data);
397408
data->cb = cb;
398409
data->arg = arg;
399-
data->needed_feature = needed_feature;
410+
data->needed_features = needed_features;
400411
req = jsonrpc_request_start(cmd, "listincoming",
401412
listincoming_done, forward_error, data);
402413
return send_outreq(req);

plugins/offers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ struct chaninfo {
7878

7979
/* Calls listpeerchannels, then cb with best peer (if any!) which has needed_feature */
8080
struct command_result *find_best_peer_(struct command *cmd,
81-
int needed_feature,
81+
u64 needed_features,
8282
struct command_result *(*cb)(struct command *,
8383
const struct chaninfo *,
8484
void *),
8585
void *arg);
8686

87-
#define find_best_peer(cmd, needed_feature, cb, arg) \
88-
find_best_peer_((cmd), (needed_feature), \
87+
#define find_best_peer(cmd, needed_features, cb, arg) \
88+
find_best_peer_((cmd), (needed_features), \
8989
typesafe_cb_preargs(struct command_result *, void *, \
9090
(cb), (arg), \
9191
struct command *, \

plugins/offers_invreq_hook.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ static struct command_result *add_blindedpaths(struct command *cmd,
381381
if (!we_want_blinded_path(cmd->plugin, true))
382382
return create_invoicereq(cmd, ir);
383383

384-
return find_best_peer(cmd, OPT_ROUTE_BLINDING,
384+
return find_best_peer(cmd, 1ULL << OPT_ROUTE_BLINDING,
385385
found_best_peer, ir);
386386
}
387387

plugins/offers_offer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static struct command_result *maybe_add_path(struct command *cmd,
303303
*/
304304
if (!offinfo->offer->offer_paths) {
305305
if (we_want_blinded_path(cmd->plugin, false))
306-
return find_best_peer(cmd, OPT_ONION_MESSAGES,
306+
return find_best_peer(cmd, 1ULL << OPT_ONION_MESSAGES,
307307
found_best_peer, offinfo);
308308
}
309309
return create_offer(cmd, offinfo);
@@ -729,7 +729,7 @@ struct command_result *json_invoicerequest(struct command *cmd,
729729
idata->invreq = invreq;
730730
idata->single_use = *single_use;
731731
idata->label = label;
732-
return find_best_peer(cmd, OPT_ONION_MESSAGES,
732+
return find_best_peer(cmd, 1ULL << OPT_ONION_MESSAGES,
733733
found_best_peer_invrequest, idata);
734734
}
735735

0 commit comments

Comments
 (0)