MCPcopy Create free account
hub / github.com/ElementsProject/lightning / add_htlc

Function add_htlc

channeld/full_channel.c:518–843  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

516}
517
518static enum channel_add_err add_htlc(struct channel *channel,
519 enum htlc_state state,
520 u64 id,
521 struct amount_msat amount,
522 u32 cltv_expiry,
523 const struct sha256 *payment_hash,
524 const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)],
525 const struct pubkey *blinding TAKES,
526 struct htlc **htlcp,
527 bool enforce_aggregate_limits,
528 struct amount_sat *htlc_fee,
529 bool err_immediate_failures)
530{
531 struct htlc *htlc, *old;
532 struct amount_msat msat_in_htlcs, committed_msat,
533 adding_msat, removing_msat, htlc_dust_amt;
534 enum side sender = htlc_state_owner(state), recipient = !sender;
535 const struct htlc **committed, **adding, **removing;
536 const struct channel_view *view;
537 size_t htlc_count;
538 bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS);
539 u32 feerate, feerate_ceil;
540
541 htlc = tal(tmpctx, struct htlc);
542
543 htlc->id = id;
544 htlc->amount = amount;
545 htlc->state = state;
546 htlc->fail_immediate = false;
547
548 htlc->rhash = *payment_hash;
549 htlc->blinding = tal_dup_or_null(htlc, struct pubkey, blinding);
550 htlc->failed = NULL;
551 htlc->r = NULL;
552 htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE), 0);
553
554 /* FIXME: Change expiry to simple u32 */
555
556 /* BOLT #2:
557 *
558 * A receiving node:
559 *...
560 * - if sending node sets `cltv_expiry` to greater or equal to
561 * 500000000:
562 * - SHOULD send a `warning` and close the connection, or send an
563 * `error` and fail the channel.
564 */
565 if (!blocks_to_abs_locktime(cltv_expiry, &htlc->expiry)) {
566 return CHANNEL_ERR_INVALID_EXPIRY;
567 }
568
569 old = htlc_get(channel->htlcs, htlc->id, htlc_owner(htlc));
570 if (old) {
571 if (old->state != htlc->state
572 || !amount_msat_eq(old->amount, htlc->amount)
573 || old->expiry.locktime != htlc->expiry.locktime
574 || !sha256_eq(&old->rhash, &htlc->rhash))
575 return CHANNEL_ERR_DUPLICATE_ID_DIFFERENT;

Callers 2

channel_add_htlcFunction · 0.85
channel_force_htlcsFunction · 0.85

Calls 15

htlc_state_ownerFunction · 0.85
blocks_to_abs_locktimeFunction · 0.85
htlc_getFunction · 0.85
htlc_ownerFunction · 0.85
amount_msat_lessFunction · 0.85
amount_msat_greaterFunction · 0.85
gather_htlcsFunction · 0.85
htlc_arr_appendFunction · 0.85
sum_offered_msatoshisFunction · 0.85
amount_msat_addFunction · 0.85
amount_msat_subFunction · 0.85
fee_for_htlcsFunction · 0.85

Tested by

no test coverage detected