| 2278 | } |
| 2279 | |
| 2280 | static bool channel_added_their_htlc(struct channel *channel, |
| 2281 | const struct added_htlc *added) |
| 2282 | { |
| 2283 | struct lightningd *ld = channel->peer->ld; |
| 2284 | struct htlc_in *hin; |
| 2285 | struct secret shared_secret; |
| 2286 | struct onionpacket *op; |
| 2287 | enum onion_wire failcode; |
| 2288 | |
| 2289 | /* BOLT #2: |
| 2290 | * |
| 2291 | * - receiving an `amount_msat` equal to 0, OR less than its own `htlc_minimum_msat`: |
| 2292 | * - SHOULD send a `warning` and close the connection, or send an |
| 2293 | * `error` and fail the channel. |
| 2294 | */ |
| 2295 | if (amount_msat_is_zero(added->amount) |
| 2296 | || amount_msat_less(added->amount, channel->our_config.htlc_minimum)) { |
| 2297 | channel_internal_error(channel, |
| 2298 | "trying to add HTLC amount %s" |
| 2299 | " but minimum is %s", |
| 2300 | fmt_amount_msat(tmpctx, added->amount), |
| 2301 | fmt_amount_msat(tmpctx, |
| 2302 | channel->our_config.htlc_minimum)); |
| 2303 | return false; |
| 2304 | } |
| 2305 | |
| 2306 | /* Do the work of extracting shared secret now if possible. */ |
| 2307 | /* FIXME: We do this *again* in peer_accepted_htlc! */ |
| 2308 | op = parse_onionpacket(tmpctx, added->onion_routing_packet, |
| 2309 | sizeof(added->onion_routing_packet), |
| 2310 | &failcode); |
| 2311 | if (op) { |
| 2312 | if (!ecdh_maybe_blinding(&op->ephemeralkey, |
| 2313 | added->path_key, |
| 2314 | &shared_secret)) { |
| 2315 | log_debug(channel->log, "htlc %"PRIu64 |
| 2316 | ": can't tweak pubkey", added->id); |
| 2317 | return false; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | /* This stays around even if we fail it immediately: it *is* |
| 2322 | * part of the current commitment. */ |
| 2323 | hin = new_htlc_in(channel, channel, added->id, added->amount, |
| 2324 | added->cltv_expiry, &added->payment_hash, |
| 2325 | op ? &shared_secret : NULL, |
| 2326 | added->path_key, |
| 2327 | added->onion_routing_packet, |
| 2328 | added->extra_tlvs, |
| 2329 | added->fail_immediate); |
| 2330 | |
| 2331 | /* Save an incoming htlc to the wallet */ |
| 2332 | wallet_htlc_save_in(ld->wallet, channel, hin); |
| 2333 | /* Update channel stats */ |
| 2334 | channel_stats_incr_in_offered(channel, added->amount); |
| 2335 | |
| 2336 | log_debug(channel->log, "Adding their HTLC %"PRIu64, added->id); |
| 2337 | connect_htlc_in(channel->peer->ld->htlcs_in, hin); |
no test coverage detected