Skip to content

Commit 2f60b4c

Browse files
hook: add endorsed value in the htlc_accepted
Passing down to the plugins the endorsed value, using the htlc_accepted hook. Allow plugins to modify it, but I think we should have a way to limit what kind of plugins can modify the following value. This is left as a open question. Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent fd48db0 commit 2f60b4c

File tree

7 files changed

+51
-27
lines changed

7 files changed

+51
-27
lines changed

channeld/channeld.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5314,7 +5314,7 @@ static const u8 *get_cupdate(const struct peer *peer)
53145314
static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
53155315
{
53165316
u8 *msg;
5317-
bool endorsed;
5317+
u8 *endorsed;
53185318
u32 cltv_expiry;
53195319
struct amount_msat amount;
53205320
struct sha256 payment_hash;
@@ -5332,25 +5332,25 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
53325332

53335333
if (!fromwire_channeld_offer_htlc(tmpctx, inmsg, &amount,
53345334
&cltv_expiry, &payment_hash,
5335-
onion_routing_packet, &blinding))
5335+
onion_routing_packet, &blinding, &endorsed))
53365336
master_badmsg(WIRE_CHANNELD_OFFER_HTLC, inmsg);
53375337
if (blinding) {
53385338
tlvs = tlv_update_add_htlc_tlvs_new(tmpctx);
53395339
tlvs->blinding_point = tal_dup(tlvs, struct pubkey, blinding);
5340+
tlvs->endorsed = endorsed;
53405341
} else
53415342
tlvs = NULL;
53425343

5343-
endorsed = false;
53445344
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
53455345
amount, cltv_expiry, &payment_hash,
53465346
onion_routing_packet, take(blinding), NULL,
53475347
&htlc_fee, endorsed, true);
5348-
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s endorsed=%d",
5348+
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s endorsed=%u",
53495349
peer->htlc_id,
53505350
type_to_string(tmpctx, struct amount_msat, &amount),
53515351
cltv_expiry,
53525352
channel_add_err_name(e),
5353-
endorsed);
5353+
endorsed ? *endorsed : 0);
53545354

53555355
switch (e) {
53565356
case CHANNEL_ERR_ADD_OK:

channeld/channeld_wire.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ msgdata,channeld_offer_htlc,cltv_expiry,u32,
104104
msgdata,channeld_offer_htlc,payment_hash,sha256,
105105
msgdata,channeld_offer_htlc,onion_routing_packet,u8,1366
106106
msgdata,channeld_offer_htlc,blinding,?pubkey,
107+
msgdata,channeld_offer_htlc,endorsed,?u8,
107108

108109
# Reply; synchronous since IDs have to increment.
109110
msgtype,channeld_offer_htlc_reply,1104

lightningd/pay.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <lightningd/notification.h>
1717
#include <lightningd/pay.h>
1818
#include <lightningd/peer_control.h>
19+
#include <stdbool.h>
1920
#include <wallet/invoices.h>
2021

2122
/* Routing failure object */
@@ -768,7 +769,8 @@ static const u8 *send_onion(const tal_t *ctx, struct lightningd *ld,
768769
u64 partid,
769770
u64 groupid,
770771
struct channel *channel,
771-
struct htlc_out **hout)
772+
struct htlc_out **hout,
773+
const u8 *endorsed)
772774
{
773775
const u8 *onion;
774776
unsigned int base_expiry;
@@ -778,7 +780,8 @@ static const u8 *send_onion(const tal_t *ctx, struct lightningd *ld,
778780
return send_htlc_out(ctx, channel, first_hop->amount,
779781
base_expiry + first_hop->delay,
780782
final_amount, payment_hash,
781-
blinding, partid, groupid, onion, NULL, hout);
783+
blinding, partid, groupid, onion, NULL, hout,
784+
endorsed);
782785
}
783786

784787
static struct command_result *check_invoice_request_usage(struct command *cmd,
@@ -1084,7 +1087,7 @@ send_payment_core(struct lightningd *ld,
10841087
const struct wallet_payment *old_payment;
10851088
struct channel *channel;
10861089
const u8 *failmsg;
1087-
bool endorsed;
1090+
const u8 *endorsed;
10881091
struct htlc_out *hout;
10891092
struct routing_failure *fail;
10901093
struct command_result *ret;
@@ -1126,10 +1129,8 @@ send_payment_core(struct lightningd *ld,
11261129
* - SHOULD set `endorsed` to `0`.
11271130
* - otherwise:
11281131
* - SHOULD set `endorsed` to `1`.
1129-
*
1130-
* We wait that someone else smarted than me will provide the way to
1131-
* calculate it for now it is just a placesolder. */
1132-
endorsed = false;
1132+
**/
1133+
endorsed = NULL;
11331134
failmsg = send_onion(tmpctx, ld, packet, first_hop, msat,
11341135
rhash, NULL, partid,
11351136
group, channel, &hout, endorsed);

lightningd/peer_htlcs.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
605605
u64 groupid,
606606
const u8 *onion_routing_packet,
607607
struct htlc_in *in,
608-
struct htlc_out **houtp)
608+
struct htlc_out **houtp,
609+
const u8 *endorsed)
609610
{
610611
u8 *msg;
611612

@@ -647,7 +648,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
647648
}
648649

649650
msg = towire_channeld_offer_htlc(out, amount, cltv, payment_hash,
650-
onion_routing_packet, blinding);
651+
onion_routing_packet, blinding,
652+
(u8 *)endorsed);
651653
subd_req(out->peer->ld, out->owner, take(msg), -1, 0, rcvd_htlc_reply,
652654
*houtp);
653655

@@ -700,7 +702,8 @@ static void forward_htlc(struct htlc_in *hin,
700702
const struct short_channel_id *forward_scid,
701703
const struct channel_id *forward_to,
702704
const u8 next_onion[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)],
703-
const struct pubkey *next_blinding)
705+
const struct pubkey *next_blinding,
706+
const u8 *endorsed)
704707
{
705708
const u8 *failmsg;
706709
struct lightningd *ld = hin->key.channel->peer->ld;
@@ -811,7 +814,7 @@ static void forward_htlc(struct htlc_in *hin,
811814
outgoing_cltv_value, AMOUNT_MSAT(0),
812815
&hin->payment_hash,
813816
next_blinding, 0 /* partid */, 0 /* groupid */,
814-
next_onion, hin, &hout);
817+
next_onion, hin, &hout, endorsed);
815818
if (!failmsg)
816819
return;
817820

@@ -839,6 +842,11 @@ struct htlc_accepted_hook_payload {
839842
u8 *next_onion;
840843
u64 failtlvtype;
841844
size_t failtlvpos;
845+
/* NULL if the jamming mitigation it is not
846+
* supported */
847+
u8 *endorsed;
848+
/* FIXME: add the possibility to encode
849+
* and decode the raw tlvs */
842850
};
843851

844852
/* We only handle the simplest cases here */
@@ -924,7 +932,8 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
924932
struct htlc_in *hin = request->hin;
925933
struct lightningd *ld = request->ld;
926934
struct preimage payment_preimage;
927-
const jsmntok_t *resulttok, *paykeytok, *payloadtok, *fwdtok;
935+
const jsmntok_t *resulttok, *paykeytok,
936+
*payloadtok, *fwdtok, *endorse_tok;
928937
u8 *failonion;
929938

930939
if (!toks || !buffer)
@@ -976,6 +985,19 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
976985
}
977986
}
978987

988+
endorse_tok = json_get_member(buffer, toks, "endorsed");
989+
if (endorse_tok) {
990+
bool internal_endorsed;
991+
tal_free(request->endorsed);
992+
request->endorsed = tal(request, u8);
993+
if (json_to_bool(buffer, endorse_tok, &internal_endorsed))
994+
fatal("Bad endorsed for htlc_accepted"
995+
" hook: %.*s",
996+
endorse_tok->end - endorse_tok->start,
997+
buffer + endorse_tok->start);
998+
*request->endorsed = internal_endorsed ? 1 : 0;
999+
}
1000+
9791001
if (json_tok_streq(buffer, resulttok, "continue")) {
9801002
return true;
9811003
}
@@ -1096,6 +1118,8 @@ static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p,
10961118
}
10971119
json_add_hex_talarr(s, "next_onion", p->next_onion);
10981120
json_add_secret(s, "shared_secret", hin->shared_secret);
1121+
if (p->endorsed != NULL)
1122+
json_add_bool(s, "endorsed", *p->endorsed == 1);
10991123
json_object_end(s);
11001124

11011125
if (p->fwd_channel_id)
@@ -1144,7 +1168,7 @@ htlc_accepted_hook_final(struct htlc_accepted_hook_payload *request STEALS)
11441168
request->payload->forward_channel,
11451169
request->fwd_channel_id,
11461170
serialize_onionpacket(tmpctx, rs->next),
1147-
request->next_blinding);
1171+
request->next_blinding, request->endorsed);
11481172
} else
11491173
handle_localpay(hin,
11501174
request->payload->amt_to_forward,

lightningd/peer_htlcs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
5050
u64 groupid,
5151
const u8 *onion_routing_packet,
5252
struct htlc_in *in,
53-
struct htlc_out **houtp);
53+
struct htlc_out **houtp,
54+
const u8 *endorsed);
5455

5556
void onchain_failed_our_htlc(const struct channel *channel,
5657
const struct htlc_stub *htlc,

lightningd/test/run-invoice-select-inchan.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,6 @@ struct anchor_details *create_anchor_details(const tal_t *ctx UNNEEDED,
186186
struct channel *channel UNNEEDED,
187187
const struct bitcoin_tx *tx UNNEEDED)
188188
{ fprintf(stderr, "create_anchor_details called!\n"); abort(); }
189-
/* Generated stub for db_begin_transaction_ */
190-
void db_begin_transaction_(struct db *db UNNEEDED, const char *location UNNEEDED)
191-
{ fprintf(stderr, "db_begin_transaction_ called!\n"); abort(); }
192-
/* Generated stub for db_commit_transaction */
193-
void db_commit_transaction(struct db *db UNNEEDED)
194-
{ fprintf(stderr, "db_commit_transaction called!\n"); abort(); }
195189
/* Generated stub for delete_channel */
196190
void delete_channel(struct channel *channel STEALS UNNEEDED)
197191
{ fprintf(stderr, "delete_channel called!\n"); abort(); }
@@ -846,7 +840,7 @@ bool plugin_hook_call_(struct lightningd *ld UNNEEDED,
846840
{ fprintf(stderr, "plugin_hook_call_ called!\n"); abort(); }
847841
/* Generated stub for plugin_request_send */
848842
void plugin_request_send(struct plugin *plugin UNNEEDED,
849-
struct jsonrpc_request *req TAKES UNNEEDED)
843+
struct jsonrpc_request *req UNNEEDED)
850844
{ fprintf(stderr, "plugin_request_send called!\n"); abort(); }
851845
/* Generated stub for pubkey_from_node_id */
852846
bool pubkey_from_node_id(struct pubkey *key UNNEEDED, const struct node_id *id UNNEEDED)

wallet/test/run-wallet.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ char *json_strdup(const tal_t *ctx UNNEEDED, const char *buffer UNNEEDED, const
405405
/* Generated stub for json_stream_success */
406406
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
407407
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
408+
/* Generated stub for json_to_bool */
409+
bool json_to_bool(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool *b UNNEEDED)
410+
{ fprintf(stderr, "json_to_bool called!\n"); abort(); }
408411
/* Generated stub for json_to_channel_id */
409412
bool json_to_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
410413
struct channel_id *cid UNNEEDED)
@@ -770,7 +773,7 @@ u8 *towire_channeld_got_commitsig_reply(const tal_t *ctx UNNEEDED)
770773
u8 *towire_channeld_got_revoke_reply(const tal_t *ctx UNNEEDED)
771774
{ fprintf(stderr, "towire_channeld_got_revoke_reply called!\n"); abort(); }
772775
/* Generated stub for towire_channeld_offer_htlc */
773-
u8 *towire_channeld_offer_htlc(const tal_t *ctx UNNEEDED, struct amount_msat amount_msat UNNEEDED, u32 cltv_expiry UNNEEDED, const struct sha256 *payment_hash UNNEEDED, const u8 onion_routing_packet[1366] UNNEEDED, const struct pubkey *blinding UNNEEDED)
776+
u8 *towire_channeld_offer_htlc(const tal_t *ctx UNNEEDED, struct amount_msat amount_msat UNNEEDED, u32 cltv_expiry UNNEEDED, const struct sha256 *payment_hash UNNEEDED, const u8 onion_routing_packet[1366] UNNEEDED, const struct pubkey *blinding UNNEEDED, u8 *endorsed UNNEEDED)
774777
{ fprintf(stderr, "towire_channeld_offer_htlc called!\n"); abort(); }
775778
/* Generated stub for towire_channeld_sending_commitsig_reply */
776779
u8 *towire_channeld_sending_commitsig_reply(const tal_t *ctx UNNEEDED)

0 commit comments

Comments
 (0)