@@ -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 );
@@ -520,7 +462,12 @@ REGISTER_PAYMENT_MODIFIER(refreshgossmap, refreshgossmap_cb);
520462 * Use route hints from the invoice to update the local gossmods and uncertainty
521463 * network.
522464 */
523- // TODO check how this is done in pay.c
465+
466+ static void uncertainty_remove_channel (struct chan_extra * ce ,
467+ struct uncertainty * uncertainty )
468+ {
469+ chan_extra_map_del (uncertainty -> chan_extra_map , ce );
470+ }
524471
525472static void add_hintchan (struct payment * payment , const struct node_id * src ,
526473 const struct node_id * dst , u16 cltv_expiry_delta ,
@@ -533,7 +480,7 @@ static void add_hintchan(struct payment *payment, const struct node_id *src,
533480 assert (payment -> local_gossmods );
534481
535482 const char * errmsg ;
536- const struct chan_extra * ce =
483+ struct chan_extra * ce =
537484 uncertainty_find_channel (pay_plugin -> uncertainty , scid );
538485
539486 if (!ce ) {
@@ -580,6 +527,15 @@ static void add_hintchan(struct payment *payment, const struct node_id *src,
580527 fmt_short_channel_id (tmpctx , scid ));
581528 goto function_error ;
582529 }
530+ /* We want these channel hints destroyed when the local_gossmods
531+ * are freed. */
532+ /* FIXME: these hints are global in the uncertainty network if
533+ * two payments happen concurrently we will have race
534+ * conditions. The best way to avoid this is to use askrene and
535+ * it's layered API. */
536+ tal_steal (payment -> local_gossmods , ce );
537+ tal_add_destructor2 (ce , uncertainty_remove_channel ,
538+ pay_plugin -> uncertainty );
583539 } else {
584540 /* The channel is pubic and we already keep track of it in the
585541 * gossmap and uncertainty network. It would be wrong to assume
@@ -755,8 +711,7 @@ static struct command_result *compute_routes_cb(struct payment *payment)
755711
756712 /* Send get_routes a note that it should discard the last hop because we
757713 * are actually solving a multiple destinations problem. */
758- bool blinded_destination =
759- payment -> payment_info .blinded_paths != NULL ;
714+ bool blinded_destination = true;
760715
761716 // TODO: add an algorithm selector here
762717 /* We let this return an unlikely path, as it's better to try once than
@@ -1291,9 +1246,9 @@ REGISTER_PAYMENT_CONDITION(retry, retry_cb);
12911246// add check pre-approved invoice
12921247void * payment_virtual_program [] = {
12931248 /*0*/ OP_CALL , & previoussuccess_pay_mod ,
1294- /*2*/ OP_CALL , & selfpay_pay_mod ,
1295- /*4*/ OP_CALL , & knowledgerelax_pay_mod ,
1296- /*6*/ OP_CALL , & getmychannels_pay_mod ,
1249+ /*2*/ OP_CALL , & knowledgerelax_pay_mod ,
1250+ /*4*/ OP_CALL , & getmychannels_pay_mod ,
1251+ /*6*/ OP_CALL , & selfpay_pay_mod ,
12971252 /*8*/ OP_CALL , & refreshgossmap_pay_mod ,
12981253 /*10*/ OP_CALL , & routehints_pay_mod ,
12991254 /*12*/ OP_CALL , & blindedhints_pay_mod ,
0 commit comments