@@ -92,6 +92,13 @@ static struct command_result *payment_rpc_failure(struct command *cmd,
9292 json_tok_full_len (toks ), json_tok_full (buffer , toks ));
9393}
9494
95+ static void add_hintchan (struct payment * payment , const struct node_id * src ,
96+ const struct node_id * dst , u16 cltv_expiry_delta ,
97+ const struct short_channel_id scid , u32 fee_base_msat ,
98+ u32 fee_proportional_millionths ,
99+ const struct amount_msat * chan_htlc_min ,
100+ const struct amount_msat * chan_htlc_max );
101+
95102/*****************************************************************************
96103 * previoussuccess
97104 *
@@ -249,88 +256,23 @@ REGISTER_PAYMENT_MODIFIER(initial_sanity_checks, initial_sanity_checks_cb);
249256
250257/*****************************************************************************
251258 * selfpay
252- *
253- * Checks if the payment destination is the sender's node and perform a self
254- * payment.
255259 */
256260
257- static struct command_result * selfpay_success (struct command * cmd ,
258- const char * method UNUSED ,
259- const char * buf ,
260- const jsmntok_t * tok ,
261- struct payment * payment )
262- {
263- struct preimage preimage ;
264- const char * err ;
265- err = json_scan (tmpctx , buf , tok , "{payment_preimage:%}" ,
266- JSON_SCAN (json_to_preimage , & preimage ));
267- if (err )
268- plugin_err (
269- cmd -> plugin , "selfpay didn't have payment_preimage: %.*s" ,
270- json_tok_full_len (tok ), json_tok_full (buf , tok ));
271-
272-
273- payment_note (payment , LOG_DBG , "Paid with self-pay." );
274- return payment_success (payment , & preimage );
275- }
276- static struct command_result * selfpay_failure (struct command * cmd ,
277- const char * method UNUSED ,
278- const char * buf ,
279- const jsmntok_t * tok ,
280- struct payment * payment )
281- {
282- struct payment_result * result =
283- tal_sendpay_result_from_json (tmpctx , buf , tok );
284- if (result == NULL ) {
285- plugin_log (pay_plugin -> plugin , LOG_UNUSUAL ,
286- "Unable to parse sendpay failure: %.*s" ,
287- json_tok_full_len (tok ), json_tok_full (buf , tok ));
288- return payment_fail (payment , LIGHTNINGD ,
289- "Self pay failed for unknown reason" );
290- }
291- return payment_fail (payment , result -> code , "%s" , result -> message );
292- }
293-
294261static struct command_result * selfpay_cb (struct payment * payment )
295262{
296- if (!node_id_eq (& pay_plugin -> my_id ,
297- & payment -> payment_info .destination )) {
298- return payment_continue (payment );
299- }
300-
301- struct command * cmd = payment_command (payment );
302- if (!cmd )
303- plugin_err (pay_plugin -> plugin ,
304- "Selfpay: cannot get a valid cmd." );
305-
306- struct payment_info * pinfo = & payment -> payment_info ;
307- struct out_req * req ;
308- req = jsonrpc_request_start (cmd , "renesendpay" , selfpay_success ,
309- selfpay_failure , payment );
310- json_add_sha256 (req -> js , "payment_hash" , & pinfo -> payment_hash );
311- json_add_u64 (req -> js , "partid" , 0 );
312- json_add_u64 (req -> js , "groupid" , payment -> groupid );
313- json_add_string (req -> js , "invoice" , pinfo -> invstr );
314- json_add_node_id (req -> js , "destination" , & pinfo -> destination );
315- json_add_amount_msat (req -> js , "amount_msat" , pinfo -> amount );
316- json_add_amount_msat (req -> js , "total_amount_msat" , pinfo -> amount );
317- json_add_u32 (req -> js , "final_cltv" , pinfo -> final_cltv );
318- if (pinfo -> label )
319- json_add_string (req -> js , "label" , pinfo -> label );
320- if (pinfo -> description )
321- json_add_string (req -> js , "description" , pinfo -> description );
322- /* An empty route means a payment to oneself, pathlen=0 */
323- json_array_start (req -> js , "route" );
324- json_array_end (req -> js );
325- if (pinfo -> payment_secret )
326- json_add_secret (req -> js , "payment_secret" ,
327- pinfo -> payment_secret );
328- else {
329- assert (pinfo -> blinded_paths );
330- const struct blinded_path * bpath = pinfo -> blinded_paths [0 ];
331- json_myadd_blinded_path (req -> js , "blinded_path" , bpath );
263+ /* A different approach to self-pay: create a fake channel from the
264+ * bolt11 destination to the routing_destination (a fake node_id). */
265+ if (!payment -> payment_info .blinded_paths ) {
266+ struct amount_msat htlc_min = AMOUNT_MSAT (0 );
267+ struct amount_msat htlc_max = AMOUNT_MSAT ((u64 )1000 * 100000000 );
268+ struct short_channel_id scid = {.u64 = 0 };
269+ add_hintchan (payment , & payment -> payment_info .destination ,
270+ payment -> routing_destination ,
271+ /* cltv delta = */ 0 , scid ,
272+ /* base fee = */ 0 ,
273+ /* ppm = */ 0 , & htlc_min , & htlc_max );
332274 }
333- return send_outreq ( req );
275+ return payment_continue ( payment );
334276}
335277
336278REGISTER_PAYMENT_MODIFIER (selfpay , selfpay_cb );
@@ -513,7 +455,12 @@ REGISTER_PAYMENT_MODIFIER(refreshgossmap, refreshgossmap_cb);
513455 * Use route hints from the invoice to update the local gossmods and uncertainty
514456 * network.
515457 */
516- // TODO check how this is done in pay.c
458+
459+ static void uncertainty_remove_channel (struct chan_extra * ce ,
460+ struct uncertainty * uncertainty )
461+ {
462+ chan_extra_map_del (uncertainty -> chan_extra_map , ce );
463+ }
517464
518465static void add_hintchan (struct payment * payment , const struct node_id * src ,
519466 const struct node_id * dst , u16 cltv_expiry_delta ,
@@ -526,7 +473,7 @@ static void add_hintchan(struct payment *payment, const struct node_id *src,
526473 assert (payment -> local_gossmods );
527474
528475 const char * errmsg ;
529- const struct chan_extra * ce =
476+ struct chan_extra * ce =
530477 uncertainty_find_channel (pay_plugin -> uncertainty , scid );
531478
532479 if (!ce ) {
@@ -573,6 +520,15 @@ static void add_hintchan(struct payment *payment, const struct node_id *src,
573520 fmt_short_channel_id (tmpctx , scid ));
574521 goto function_error ;
575522 }
523+ /* We want these channel hints destroyed when the local_gossmods
524+ * are freed. */
525+ /* FIXME: these hints are global in the uncertainty network if
526+ * two payments happen concurrently we will have race
527+ * conditions. The best way to avoid this is to use askrene and
528+ * it's layered API. */
529+ tal_steal (payment -> local_gossmods , ce );
530+ tal_add_destructor2 (ce , uncertainty_remove_channel ,
531+ pay_plugin -> uncertainty );
576532 } else {
577533 /* The channel is pubic and we already keep track of it in the
578534 * gossmap and uncertainty network. It would be wrong to assume
@@ -748,8 +704,7 @@ static struct command_result *compute_routes_cb(struct payment *payment)
748704
749705 /* Send get_routes a note that it should discard the last hop because we
750706 * are actually solving a multiple destinations problem. */
751- bool blinded_destination =
752- payment -> payment_info .blinded_paths != NULL ;
707+ bool blinded_destination = true;
753708
754709 // TODO: add an algorithm selector here
755710 /* We let this return an unlikely path, as it's better to try once than
@@ -1284,9 +1239,9 @@ REGISTER_PAYMENT_CONDITION(retry, retry_cb);
12841239// add check pre-approved invoice
12851240void * payment_virtual_program [] = {
12861241 /*0*/ OP_CALL , & previoussuccess_pay_mod ,
1287- /*2*/ OP_CALL , & selfpay_pay_mod ,
1288- /*4*/ OP_CALL , & knowledgerelax_pay_mod ,
1289- /*6*/ OP_CALL , & getmychannels_pay_mod ,
1242+ /*2*/ OP_CALL , & knowledgerelax_pay_mod ,
1243+ /*4*/ OP_CALL , & getmychannels_pay_mod ,
1244+ /*6*/ OP_CALL , & selfpay_pay_mod ,
12901245 /*8*/ OP_CALL , & refreshgossmap_pay_mod ,
12911246 /*10*/ OP_CALL , & routehints_pay_mod ,
12921247 /*12*/ OP_CALL , & blindedhints_pay_mod ,
0 commit comments