Skip to content

Commit

Permalink
channeld: wip: be able to handle the update htlc by handling the endo…
Browse files Browse the repository at this point in the history
…rsed field

Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Oct 13, 2023
1 parent 4c7f2f6 commit 2d339b5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
34 changes: 30 additions & 4 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,9 +971,32 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
peer_failed_warn(peer->pps, &peer->channel_id,
"Bad peer_add_htlc %s", tal_hex(msg, msg));
}
/* FIXME(vincenzopalazzo): We should check if the endorse value is zero or missing:
*
* BOLT 2 (0539ad868a263040087d2790684f69fb28d3fa97):
* - if `endorsed` is not provided OR `endorsed` is zero:
* - MAY choose to limit the liquidity and slots available to forward the
* corresponding outgoing HTLC in `onion_routing_packet`, if any.*/

/* FIXME(vincenzopalazzo): we should implement a 2 phase communication between
* channeld <-> (metrics/jamming plugin). Because we neeed a way to
* estimate communicate the val in some way and the get the value back
* in order to forward it.
*
* BOLT 2 (0539ad868a263040087d2790684f69fb28d3fa97):
* - otherwise:
* - if `endorsed` is present and non-zero for the corresponding incoming HTLC
* AND the incoming peer is considered to have sufficient local reputation
* (see [Local Reputation](recommendations/local-resource-conservation.md#local-reputation)):
* - SHOULD set `endorsed` to `1`
* - otherwise:
* - SHOULD set `endorsed` to `0`.
**/
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 @@ -5230,9 +5253,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 @@ -5252,22 +5277,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 2d339b5

Please sign in to comment.