Skip to content

Commit

Permalink
lightningd: handling the endorsed field
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Oct 24, 2023
1 parent 3e1a4f5 commit 78ad13a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
13 changes: 9 additions & 4 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,8 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount,
cltv_expiry, &payment_hash,
onion_routing_packet, tlvs->blinding_point, &htlc, NULL,
/* We just forward it :) smart ah? */
tlvs->endorsed,
/* We don't immediately fail incoming htlcs,
* instead we wait and fail them after
* they've been committed */
Expand Down Expand Up @@ -5308,9 +5310,11 @@ static const u8 *get_cupdate(const struct peer *peer)
return peer->channel_update;
}

/* Offer an HTLC to the remote side. */
static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
{
u8 *msg;
bool endorsed;
u32 cltv_expiry;
struct amount_msat amount;
struct sha256 payment_hash;
Expand All @@ -5330,22 +5334,23 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
&cltv_expiry, &payment_hash,
onion_routing_packet, &blinding))
master_badmsg(WIRE_CHANNELD_OFFER_HTLC, inmsg);

if (blinding) {
tlvs = tlv_update_add_htlc_tlvs_new(tmpctx);
tlvs->blinding_point = tal_dup(tlvs, struct pubkey, blinding);
} else
tlvs = NULL;

endorsed = false;
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
amount, cltv_expiry, &payment_hash,
onion_routing_packet, take(blinding), NULL,
&htlc_fee, true);
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s",
&htlc_fee, endorsed, true);
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s endorsed=%d",
peer->htlc_id,
type_to_string(tmpctx, struct amount_msat, &amount),
cltv_expiry,
channel_add_err_name(e));
channel_add_err_name(e),
endorsed);

switch (e) {
case CHANNEL_ERR_ADD_OK:
Expand Down
1 change: 1 addition & 0 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
const struct pubkey *blinding TAKES,
struct htlc **htlcp,
struct amount_sat *htlc_fee,
const bool endorsed,
bool err_immediate_failures)
{
enum htlc_state state;
Expand Down
1 change: 1 addition & 0 deletions channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
const struct pubkey *blinding TAKES,
struct htlc **htlcp,
struct amount_sat *htlc_fee,
const bool endorsed,
bool err_immediate_failures);

/**
Expand Down
4 changes: 2 additions & 2 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static const struct htlc **include_htlcs(struct channel *channel, enum side side
memset(&preimage, i, sizeof(preimage));
sha256(&hash, &preimage, sizeof(preimage));
e = channel_add_htlc(channel, sender, i, msatoshi, 500+i, &hash,
dummy_routing, NULL, NULL, NULL, true);
dummy_routing, NULL, NULL, NULL, false, true);
assert(e == CHANNEL_ERR_ADD_OK);
htlcs[i] = channel_get_htlc(channel, sender, i);
}
Expand Down Expand Up @@ -260,7 +260,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
sha256(&rhash, &r, sizeof(r));

assert(channel_add_htlc(channel, sender, 1337, msatoshi, 900, &rhash,
dummy_routing, NULL, NULL, NULL, true)
dummy_routing, NULL, NULL, NULL, false, true)
== CHANNEL_ERR_ADD_OK);
htlc = channel_get_htlc(channel, sender, 1337);
assert(htlc);
Expand Down
16 changes: 15 additions & 1 deletion lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ send_payment_core(struct lightningd *ld,
const struct wallet_payment *old_payment;
struct channel *channel;
const u8 *failmsg;
bool endorsed;
struct htlc_out *hout;
struct routing_failure *fail;
struct command_result *ret;
Expand Down Expand Up @@ -1116,9 +1117,22 @@ send_payment_core(struct lightningd *ld,
return command_failed(cmd, data);
}


/* BOLT 2 (0539ad868a263040087d2790684f69fb28d3fa97):
* A sending node:
* - if it is the original source of the HTLC:
* - if it does not expect immediate fulfillment upon receipt by the
* final destination:
* - SHOULD set `endorsed` to `0`.
* - otherwise:
* - SHOULD set `endorsed` to `1`.
*
* We wait that someone else smarted than me will provide the way to
* calculate it for now it is just a placesolder. */
endorsed = false;
failmsg = send_onion(tmpctx, ld, packet, first_hop, msat,
rhash, NULL, partid,
group, channel, &hout);
group, channel, &hout, endorsed);

if (failmsg) {
fail = immediate_routing_failure(
Expand Down

0 comments on commit 78ad13a

Please sign in to comment.