lnp/channel/bolt/
state.rs

1// LNP/BP Core Library implementing LNPBP specifications & standards
2// Written in 2020-2022 by
3//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the MIT License
11// along with this software.
12// If not, see <https://opensource.org/licenses/MIT>.
13
14use std::collections::BTreeMap;
15
16#[cfg(feature = "serde")]
17use amplify::ToYamlString;
18use amplify::{DumbDefault, Slice32};
19use p2p::bolt::{ActiveChannelId, TempChannelId};
20use secp256k1::ecdsa::Signature;
21use secp256k1::PublicKey;
22
23use super::{
24    CommonParams, Direction, HtlcKnown, HtlcSecret, Lifecycle, LocalKeyset,
25    PeerParams, Policy, RemoteKeyset,
26};
27use crate::channel::{Funding, State};
28
29#[derive(Clone, Debug)]
30#[derive(StrictEncode, StrictDecode)]
31#[cfg_attr(
32    feature = "serde",
33    derive(Display, Serialize, Deserialize),
34    serde(crate = "serde_crate"),
35    display(ChannelState::to_yaml_string)
36)]
37pub struct ChannelState {
38    pub funding: Funding,
39
40    /// Current channel lifecycle stage
41    pub stage: Lifecycle,
42
43    // TOO: Consider storing information about used chain at generic channel
44    // level
45    /// The chain_hash value denotes the exact blockchain that the opened
46    /// channel will reside within. This is usually the genesis hash of the
47    /// respective blockchain. The existence of the chain_hash allows nodes to
48    /// open channels across many distinct blockchains as well as have channels
49    /// within multiple blockchains opened to the same peer (if it supports the
50    /// target chains).
51    pub chain_hash: Slice32,
52
53    /// Channel id used by the channel; first temporary and later final.
54    ///
55    /// The temporary_channel_id is used to identify this channel on a per-peer
56    /// basis until the funding transaction is established, at which point it
57    /// is replaced by the channel_id, which is derived from the funding
58    /// transaction.
59    pub active_channel_id: ActiveChannelId,
60
61    /// Amount in millisatoshis
62    pub local_amount_msat: u64,
63
64    /// Amount in millisatoshis
65    pub remote_amount_msat: u64,
66
67    pub commitment_number: u64,
68
69    pub commitment_sigs: Vec<Signature>,
70
71    /// The policy for accepting remote node params
72    pub policy: Policy,
73
74    /// Common parameters applying for both nodes
75    pub common_params: CommonParams,
76
77    /// Channel parameters required to be met by the remote node when operating
78    /// towards the local one
79    pub local_params: PeerParams,
80
81    /// Channel parameters to be used towards the remote node
82    pub remote_params: PeerParams,
83
84    /// Set of locally-derived keys for creating channel transactions
85    pub local_keys: LocalKeyset,
86
87    /// Set of remote-derived keys for creating channel transactions
88    pub remote_keys: RemoteKeyset,
89
90    pub remote_per_commitment_point: PublicKey,
91
92    pub local_per_commitment_point: PublicKey,
93
94    /// Keeps information about node directionality
95    pub direction: Direction,
96
97    pub offered_htlcs: BTreeMap<u64, HtlcSecret>,
98    pub received_htlcs: BTreeMap<u64, HtlcSecret>,
99    pub resolved_htlcs: BTreeMap<u64, HtlcKnown>,
100    pub last_recieved_htlc_id: u64,
101    pub last_offered_htlc_id: u64,
102}
103
104impl State for ChannelState {
105    fn to_funding(&self) -> Funding {
106        self.funding.clone()
107    }
108
109    fn set_funding(&mut self, funding: &Funding) {
110        self.funding = funding.clone()
111    }
112}
113
114#[cfg(feature = "serde")]
115impl ToYamlString for ChannelState {}
116
117impl DumbDefault for ChannelState {
118    fn dumb_default() -> Self {
119        ChannelState {
120            funding: Funding::new(),
121            stage: Default::default(),
122            chain_hash: Default::default(),
123            active_channel_id: ActiveChannelId::Temporary(
124                TempChannelId::dumb_default(),
125            ),
126            local_amount_msat: 0,
127            remote_amount_msat: 0,
128            commitment_number: 0,
129            commitment_sigs: vec![],
130            policy: Default::default(),
131            common_params: Default::default(),
132            local_params: Default::default(),
133            remote_params: Default::default(),
134            local_keys: LocalKeyset::dumb_default(),
135            remote_keys: RemoteKeyset::dumb_default(),
136            remote_per_commitment_point: dumb_pubkey!(),
137            local_per_commitment_point: dumb_pubkey!(),
138            direction: Direction::Inbound,
139            offered_htlcs: none!(),
140            received_htlcs: none!(),
141            resolved_htlcs: none!(),
142            last_recieved_htlc_id: 0,
143            last_offered_htlc_id: 0,
144        }
145    }
146}