|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! Contains peer state objects that are used by `LSPS1ServiceHandler`. |
| 11 | +
|
| 12 | +use super::msgs::{LSPS1OrderId, LSPS1OrderParams, LSPS1PaymentInfo, LSPS1Request}; |
| 13 | + |
| 14 | +use crate::lsps0::ser::{LSPSDateTime, LSPSRequestId}; |
| 15 | +use crate::prelude::HashMap; |
| 16 | + |
| 17 | +use lightning::ln::msgs::{ErrorAction, LightningError}; |
| 18 | +use lightning::util::logger::Level; |
| 19 | + |
| 20 | +#[derive(Default)] |
| 21 | +pub(super) struct PeerState { |
| 22 | + pub(super) outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>, |
| 23 | + pub(super) pending_requests: HashMap<LSPSRequestId, LSPS1Request>, |
| 24 | +} |
| 25 | + |
| 26 | +impl PeerState { |
| 27 | + pub(super) fn insert_outbound_channel( |
| 28 | + &mut self, order_id: LSPS1OrderId, channel: OutboundCRChannel, |
| 29 | + ) { |
| 30 | + self.outbound_channels_by_order_id.insert(order_id, channel); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +struct ChannelStateError(String); |
| 35 | + |
| 36 | +impl From<ChannelStateError> for LightningError { |
| 37 | + fn from(value: ChannelStateError) -> Self { |
| 38 | + LightningError { err: value.0, action: ErrorAction::IgnoreAndLog(Level::Info) } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(PartialEq, Debug)] |
| 43 | +pub(super) enum OutboundRequestState { |
| 44 | + OrderCreated { order_id: LSPS1OrderId }, |
| 45 | + WaitingPayment { order_id: LSPS1OrderId }, |
| 46 | +} |
| 47 | + |
| 48 | +impl OutboundRequestState { |
| 49 | + fn awaiting_payment(&self) -> Result<Self, ChannelStateError> { |
| 50 | + match self { |
| 51 | + OutboundRequestState::OrderCreated { order_id } => { |
| 52 | + Ok(OutboundRequestState::WaitingPayment { order_id: order_id.clone() }) |
| 53 | + }, |
| 54 | + state => Err(ChannelStateError(format!("TODO. JIT Channel was in state: {:?}", state))), |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub(super) struct OutboundLSPS1Config { |
| 60 | + pub(super) order: LSPS1OrderParams, |
| 61 | + pub(super) created_at: LSPSDateTime, |
| 62 | + pub(super) payment: LSPS1PaymentInfo, |
| 63 | +} |
| 64 | + |
| 65 | +pub(super) struct OutboundCRChannel { |
| 66 | + pub(super) state: OutboundRequestState, |
| 67 | + pub(super) config: OutboundLSPS1Config, |
| 68 | +} |
| 69 | + |
| 70 | +impl OutboundCRChannel { |
| 71 | + pub(super) fn new( |
| 72 | + order: LSPS1OrderParams, created_at: LSPSDateTime, order_id: LSPS1OrderId, |
| 73 | + payment: LSPS1PaymentInfo, |
| 74 | + ) -> Self { |
| 75 | + Self { |
| 76 | + state: OutboundRequestState::OrderCreated { order_id }, |
| 77 | + config: OutboundLSPS1Config { order, created_at, payment }, |
| 78 | + } |
| 79 | + } |
| 80 | + pub(super) fn awaiting_payment(&mut self) -> Result<(), LightningError> { |
| 81 | + self.state = self.state.awaiting_payment()?; |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | +} |
0 commit comments