| 418 | |
| 419 | impl<A: DatastoreProvider + Lsps2OfferProvider + LightningProvider> HtlcAcceptedHookHandler<A> { |
| 420 | pub async fn handle(&self, req: HtlcAcceptedRequest) -> AnyResult<HtlcAcceptedResponse> { |
| 421 | let scid = match req.onion.short_channel_id { |
| 422 | Some(scid) => scid, |
| 423 | None => { |
| 424 | // We are the final destination of this htlc. |
| 425 | return Ok(HtlcAcceptedResponse::continue_(None, None, None)); |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | // A) Is this SCID one that we care about? |
| 430 | let ds_rec = match self.api.get_buy_request(&scid).await { |
| 431 | Ok(rec) => rec, |
| 432 | Err(_) => { |
| 433 | return Ok(HtlcAcceptedResponse::continue_(None, None, None)); |
| 434 | } |
| 435 | }; |
| 436 | |
| 437 | // Fixme: Check that we don't have a channel yet with the peer that we await to |
| 438 | // become READY to use. |
| 439 | // --- |
| 440 | |
| 441 | // Fixme: We only accept no-mpp for now, mpp and other flows will be added later on |
| 442 | // Fixme: We continue mpp for now to let the test mock handle the htlc, as we need |
| 443 | // to test the client implementation for mpp payments. |
| 444 | if ds_rec.expected_payment_size.is_some() { |
| 445 | warn!("mpp payments are not implemented yet"); |
| 446 | return Ok(HtlcAcceptedResponse::continue_(None, None, None)); |
| 447 | // return Ok(HtlcAcceptedResponse::fail( |
| 448 | // Some(UNKNOWN_NEXT_PEER.to_string()), |
| 449 | // None, |
| 450 | // )); |
| 451 | } |
| 452 | |
| 453 | // B) Is the fee option menu still valid? |
| 454 | let now = Utc::now(); |
| 455 | if now >= ds_rec.opening_fee_params.valid_until { |
| 456 | // Not valid anymore, remove from DS and fail HTLC. |
| 457 | let _ = self.api.del_buy_request(&scid).await; |
| 458 | return Ok(HtlcAcceptedResponse::fail( |
| 459 | Some(TEMPORARY_CHANNEL_FAILURE.to_string()), |
| 460 | None, |
| 461 | )); |
| 462 | } |
| 463 | |
| 464 | // C) Is the amount in the boundaries of the fee menu? |
| 465 | if req.htlc.amount_msat.msat() < ds_rec.opening_fee_params.min_fee_msat.msat() |
| 466 | || req.htlc.amount_msat.msat() > ds_rec.opening_fee_params.max_payment_size_msat.msat() |
| 467 | { |
| 468 | // No! reject the HTLC. |
| 469 | debug!("amount_msat for scid: {}, was too low or to high", scid); |
| 470 | return Ok(HtlcAcceptedResponse::fail( |
| 471 | Some(UNKNOWN_NEXT_PEER.to_string()), |
| 472 | None, |
| 473 | )); |
| 474 | } |
| 475 | |
| 476 | // D) Check that the amount_msat covers the opening fee (only for non-mpp right now) |
| 477 | let opening_fee = if let Some(opening_fee) = compute_opening_fee( |