| 516 | } |
| 517 | |
| 518 | static 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; |
no test coverage detected