Skip to content

Commit abb1cee

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 2b898d6 commit abb1cee

File tree

6 files changed

+51
-17
lines changed

6 files changed

+51
-17
lines changed

channeld/channeld.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5629,7 +5629,7 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
56295629
static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
56305630
{
56315631
u8 *msg;
5632-
bool endorsed;
5632+
bool *endorsed;
56335633
u32 cltv_expiry;
56345634
struct amount_msat amount;
56355635
struct sha256 payment_hash;
@@ -5646,26 +5646,27 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
56465646
"funding not locked for offer_htlc");
56475647

56485648
if (!fromwire_channeld_offer_htlc(tmpctx, inmsg, &amount,
5649-
&cltv_expiry, &payment_hash,
5650-
onion_routing_packet, &blinding))
5649+
&cltv_expiry, &payment_hash,
5650+
onion_routing_packet, &blinding,
5651+
&endorsed))
56515652
master_badmsg(WIRE_CHANNELD_OFFER_HTLC, inmsg);
56525653
if (blinding) {
56535654
tlvs = tlv_update_add_htlc_tlvs_new(tmpctx);
56545655
tlvs->blinding_point = tal_dup(tlvs, struct pubkey, blinding);
5656+
tlvs->endorsed = (u8 *) endorsed;
56555657
} else
56565658
tlvs = NULL;
56575659

5658-
endorsed = false;
56595660
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
56605661
amount, cltv_expiry, &payment_hash,
56615662
onion_routing_packet, take(blinding), NULL,
56625663
&htlc_fee, endorsed, true);
5663-
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s endorsed=%d",
5664+
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s endorsed=%u",
56645665
peer->htlc_id,
56655666
type_to_string(tmpctx, struct amount_msat, &amount),
56665667
cltv_expiry,
56675668
channel_add_err_name(e),
5668-
endorsed);
5669+
endorsed ? *endorsed : 0);
56695670

56705671
switch (e) {
56715672
case CHANNEL_ERR_ADD_OK:

lightningd/pay.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <lightningd/pay.h>
1818
#include <lightningd/peer_control.h>
1919
#include <lightningd/peer_htlcs.h>
20+
#include <stdbool.h>
2021
#include <wallet/invoices.h>
2122

2223
/* Routing failure object */
@@ -730,7 +731,8 @@ static const u8 *send_onion(const tal_t *ctx, struct lightningd *ld,
730731
u64 partid,
731732
u64 groupid,
732733
struct channel *channel,
733-
struct htlc_out **hout)
734+
struct htlc_out **hout,
735+
const bool *endorsed)
734736
{
735737
const u8 *onion;
736738
unsigned int base_expiry;
@@ -740,7 +742,8 @@ static const u8 *send_onion(const tal_t *ctx, struct lightningd *ld,
740742
return send_htlc_out(ctx, channel, first_hop->amount,
741743
base_expiry + first_hop->delay,
742744
final_amount, payment_hash,
743-
blinding, partid, groupid, onion, NULL, hout);
745+
blinding, partid, groupid, onion, NULL, hout,
746+
endorsed);
744747
}
745748

746749
static struct command_result *check_invoice_request_usage(struct command *cmd,
@@ -1106,7 +1109,7 @@ send_payment_core(struct lightningd *ld,
11061109
fmt_amount_msat(tmpctx, first_hop->amount),
11071110
fmt_amount_msat(tmpctx, msat));
11081111

1109-
/* BOLT 2 (0539ad868a263040087d2790684f69fb28d3fa97):
1112+
/* BLIP-jamming #04:
11101113
* A sending node:
11111114
* - if it is the original source of the HTLC:
11121115
* - if it does not expect immediate fulfillment upon receipt by the

lightningd/peer_htlcs.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
610610
u64 groupid,
611611
const u8 *onion_routing_packet,
612612
struct htlc_in *in,
613-
struct htlc_out **houtp)
613+
struct htlc_out **houtp,
614+
const bool *endorsed)
614615
{
615616
u8 *msg;
616617

@@ -652,7 +653,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
652653
}
653654

654655
msg = towire_channeld_offer_htlc(out, amount, cltv, payment_hash,
655-
onion_routing_packet, blinding);
656+
onion_routing_packet, blinding,
657+
(bool *)endorsed);
656658
subd_req(out->peer->ld, out->owner, take(msg), -1, 0, rcvd_htlc_reply,
657659
*houtp);
658660

@@ -705,7 +707,8 @@ static void forward_htlc(struct htlc_in *hin,
705707
const struct short_channel_id *forward_scid,
706708
const struct channel_id *forward_to,
707709
const u8 next_onion[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)],
708-
const struct pubkey *next_blinding)
710+
const struct pubkey *next_blinding,
711+
const bool *endorsed)
709712
{
710713
const u8 *failmsg;
711714
struct lightningd *ld = hin->key.channel->peer->ld;
@@ -816,7 +819,7 @@ static void forward_htlc(struct htlc_in *hin,
816819
outgoing_cltv_value, AMOUNT_MSAT(0),
817820
&hin->payment_hash,
818821
next_blinding, 0 /* partid */, 0 /* groupid */,
819-
next_onion, hin, &hout);
822+
next_onion, hin, &hout, endorsed);
820823
if (!failmsg)
821824
return;
822825

@@ -844,6 +847,11 @@ struct htlc_accepted_hook_payload {
844847
u8 *next_onion;
845848
u64 failtlvtype;
846849
size_t failtlvpos;
850+
/* NULL if the jamming mitigation it is not
851+
* supported */
852+
bool *endorsed;
853+
/* FIXME: add the possibility to encode
854+
* and decode the raw tlvs */
847855
};
848856

849857
/* We only handle the simplest cases here */
@@ -929,7 +937,8 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
929937
struct htlc_in *hin = request->hin;
930938
struct lightningd *ld = request->ld;
931939
struct preimage payment_preimage;
932-
const jsmntok_t *resulttok, *paykeytok, *payloadtok, *fwdtok;
940+
const jsmntok_t *resulttok, *paykeytok,
941+
*payloadtok, *fwdtok, *endorse_tok;
933942
u8 *failonion;
934943

935944
if (!toks || !buffer)
@@ -981,6 +990,19 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
981990
}
982991
}
983992

993+
endorse_tok = json_get_member(buffer, toks, "endorsed");
994+
if (endorse_tok) {
995+
bool internal_endorsed;
996+
tal_free(request->endorsed);
997+
request->endorsed = tal(request, bool);
998+
if (json_to_bool(buffer, endorse_tok, &internal_endorsed))
999+
fatal("Bad endorsed for htlc_accepted"
1000+
" hook: %.*s",
1001+
endorse_tok->end - endorse_tok->start,
1002+
buffer + endorse_tok->start);
1003+
*request->endorsed = internal_endorsed ? 1 : 0;
1004+
}
1005+
9841006
if (json_tok_streq(buffer, resulttok, "continue")) {
9851007
return true;
9861008
}
@@ -1103,6 +1125,8 @@ static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p,
11031125
}
11041126
json_add_hex_talarr(s, "next_onion", p->next_onion);
11051127
json_add_secret(s, "shared_secret", hin->shared_secret);
1128+
if (p->endorsed != NULL)
1129+
json_add_bool(s, "endorsed", *p->endorsed == 1);
11061130
json_object_end(s);
11071131

11081132
if (p->fwd_channel_id)
@@ -1151,7 +1175,7 @@ htlc_accepted_hook_final(struct htlc_accepted_hook_payload *request STEALS)
11511175
request->payload->forward_channel,
11521176
request->fwd_channel_id,
11531177
serialize_onionpacket(tmpctx, rs->next),
1154-
request->next_blinding);
1178+
request->next_blinding, request->endorsed);
11551179
} else
11561180
handle_localpay(hin,
11571181
request->payload->amt_to_forward,

lightningd/peer_htlcs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
3737
u64 groupid,
3838
const u8 *onion_routing_packet,
3939
struct htlc_in *in,
40-
struct htlc_out **houtp);
40+
struct htlc_out **houtp,
41+
const bool *endorsed);
4142

4243
void onchain_failed_our_htlc(const struct channel *channel,
4344
const struct htlc_stub *htlc,

wallet/test/run-wallet.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ struct json_stream *json_stream_fail(struct command *cmd UNNEEDED,
512512
/* Generated stub for json_stream_success */
513513
struct json_stream *json_stream_success(struct command *cmd UNNEEDED)
514514
{ fprintf(stderr, "json_stream_success called!\n"); abort(); }
515+
/* Generated stub for json_to_bool */
516+
bool json_to_bool(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool *b UNNEEDED)
517+
{ fprintf(stderr, "json_to_bool called!\n"); abort(); }
515518
/* Generated stub for json_to_channel_id */
516519
bool json_to_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
517520
struct channel_id *cid UNNEEDED)
@@ -982,7 +985,7 @@ u8 *towire_channeld_got_commitsig_reply(const tal_t *ctx UNNEEDED)
982985
u8 *towire_channeld_got_revoke_reply(const tal_t *ctx UNNEEDED)
983986
{ fprintf(stderr, "towire_channeld_got_revoke_reply called!\n"); abort(); }
984987
/* Generated stub for towire_channeld_offer_htlc */
985-
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)
988+
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, bool *endorsed UNNEEDED)
986989
{ fprintf(stderr, "towire_channeld_offer_htlc called!\n"); abort(); }
987990
/* Generated stub for towire_channeld_sending_commitsig_reply */
988991
u8 *towire_channeld_sending_commitsig_reply(const tal_t *ctx UNNEEDED)

wire/peer_wire.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ msgdata,update_add_htlc,cltv_expiry,u32,
245245
msgdata,update_add_htlc,onion_routing_packet,byte,1366
246246
tlvtype,update_add_htlc_tlvs,blinding_point,0
247247
tlvdata,update_add_htlc_tlvs,blinding_point,blinding,point,
248+
tlvtype,update_add_htlc_tlvs,endorsed,1
249+
tlvdata,update_add_htlc_tlvs,endorsed,endorsed,byte,
248250
msgtype,update_fulfill_htlc,130
249251
msgdata,update_fulfill_htlc,channel_id,channel_id,
250252
msgdata,update_fulfill_htlc,id,u64,

0 commit comments

Comments
 (0)