Skip to content

Commit 2bc5afc

Browse files
committed
renepay: refactor create_onion
Refactor create_onion function, now we could use the same function to build onions for sendonion and injectpaymentonion. Changelog-None. Signed-off-by: Lagrang3 <[email protected]>
1 parent 402370f commit 2bc5afc

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

plugins/renepay/sendpay.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ static void sphinx_append_final_hop(const tal_t *ctx,
200200
assert(ret);
201201
}
202202

203-
static const u8 *create_onion(const tal_t *ctx, struct renesendpay *renesendpay)
203+
static const u8 *create_onion(const tal_t *ctx,
204+
struct renesendpay *renesendpay,
205+
const struct node_id first_node,
206+
const size_t first_index)
204207
{
205208
bool ret;
206209
const tal_t *this_ctx = tal(ctx, tal_t);
210+
struct node_id current_node = first_node;
207211
struct pubkey node;
208212
const u8 *payload;
209213
const size_t pathlen = tal_count(renesendpay->route);
@@ -212,19 +216,20 @@ static const u8 *create_onion(const tal_t *ctx, struct renesendpay *renesendpay)
212216
sphinx_path_new(this_ctx, renesendpay->payment_hash.u.u8,
213217
sizeof(renesendpay->payment_hash.u.u8));
214218

215-
for (size_t i = 0; i < pathlen - 1; i++) {
219+
for (size_t i = first_index; i < pathlen; i++) {
216220
/* Encrypted message is for node[i] but the data is hop[i+1],
217221
* therein lays the problem with sendpay's API. */
218-
ret = pubkey_from_node_id(&node, &renesendpay->route[i].node_id);
222+
ret = pubkey_from_node_id(&node, &current_node);
219223
assert(ret);
220224

221-
struct route_hop *hop = &renesendpay->route[i + 1];
225+
struct route_hop *hop = &renesendpay->route[i];
222226
payload =
223227
onion_nonfinal_hop(this_ctx, &hop->scid, hop->amount,
224228
hop->delay + renesendpay->blockheight);
225229
// FIXME: better handle error here
226230
ret = sphinx_add_hop_has_length(sp, &node, take(payload));
227231
assert(ret);
232+
current_node = renesendpay->route[i].node_id;
228233
}
229234

230235
const u32 final_cltv = renesendpay->final_cltv + renesendpay->blockheight;
@@ -239,7 +244,7 @@ static const u8 *create_onion(const tal_t *ctx, struct renesendpay *renesendpay)
239244
sphinx_append_final_hop(this_ctx,
240245
sp,
241246
renesendpay->payment_secret,
242-
&renesendpay->destination,
247+
&current_node,
243248
renesendpay->deliver_amount,
244249
renesendpay->total_amount,
245250
final_cltv,
@@ -320,9 +325,13 @@ static struct command_result *waitblockheight_done(struct command *cmd,
320325
"renesendpay failed to read blockheight "
321326
"from waitblockheight response.");
322327

323-
const u8 *onion = create_onion(tmpctx, renesendpay);
324-
struct out_req *req = jsonrpc_request_start(
325-
cmd, "sendonion", sendonion_done, sendpay_rpc_failure, renesendpay);
328+
const u8 *onion;
329+
struct out_req *req;
330+
331+
onion =
332+
create_onion(tmpctx, renesendpay, renesendpay->route[0].node_id, 1);
333+
req = jsonrpc_request_start(cmd, "sendonion", sendonion_done,
334+
sendpay_rpc_failure, renesendpay);
326335
json_add_hex_talarr(req->js, "onion", onion);
327336
json_add_sha256(req->js, "payment_hash", &renesendpay->payment_hash);
328337
json_add_u64(req->js, "partid", renesendpay->partid);

0 commit comments

Comments
 (0)