1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// LNP/BP Core Library implementing LNPBP specifications & standards
// Written in 2019 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use amplify::DumbDefault;
use internet2::presentation::sphinx::Hop;
use p2p::legacy::{
    ChannelId, HopRealm, Messages, PaymentOnion, PaymentRequest, ShortChannelId,
};
use strict_encoding::{strict_deserialize, strict_serialize};

use super::GossipChannelInfo;
use crate::router::gossip::LocalChannelInfo;
use crate::router::Router;
use crate::{extension, router, Extension, RouterExtension};

#[derive(
    Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, Error
)]
#[display(doc_comments)]
pub enum Error {}

#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[derive(StrictEncode, StrictDecode)]
pub struct RouterState {
    remote_channels: Vec<GossipChannelInfo>,
    direct_channels: Vec<LocalChannelInfo>,
}

impl DumbDefault for RouterState {
    fn dumb_default() -> Self {
        RouterState::default()
    }
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]
#[derive(StrictEncode, StrictDecode)]
#[display(Debug)]
pub enum GossipExt {
    MainRouter = 0,
    DirectRouter = 1,
    GossipRouter = 2,
}

impl Default for GossipExt {
    fn default() -> Self {
        GossipExt::MainRouter
    }
}

impl From<GossipExt> for u16 {
    fn from(id: GossipExt) -> Self {
        let mut buf = [0u8; 2];
        buf.copy_from_slice(
            &strict_serialize(&id)
                .expect("Enum in-memory strict encoding can't fail"),
        );
        u16::from_be_bytes(buf)
    }
}

impl TryFrom<u16> for GossipExt {
    type Error = strict_encoding::Error;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        strict_deserialize(&value.to_be_bytes())
    }
}

impl extension::Nomenclature for GossipExt {
    type State = RouterState;
    type Error = Error;
    type PeerMessage = lnp2p::legacy::Messages;
    type UpdateMessage = UpdateMsg;
    type UpdateRequest = ();
}

impl router::Nomenclature for GossipExt {
    type HopPayload = PaymentOnion;

    fn default_extensions() -> Vec<Box<dyn RouterExtension<Self>>> {
        vec![
            Box::new(DirectRouter::default()) as Box<dyn RouterExtension<Self>>,
            Box::new(GossipRouter::default()) as Box<dyn RouterExtension<Self>>,
        ]
    }

    fn update_from_peer(
        _router: &mut Router<Self>,
        _message: &Messages,
    ) -> Result<(), Error> {
        // TODO: Add support for gossip queries by adding query extension if
        //       we are getting corresponding feature flag
        Ok(())
    }
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum UpdateMsg {
    DirectChannelAdd(LocalChannelInfo),
    DirectChannelRemove(ChannelId),
    DirectChannelUpdate(ChannelId),
}

/// Router for direct channels (between this node and other nodes) for
/// legacy BOLT lightning channels
#[derive(Getters, Clone, PartialEq, Eq, Debug, Default)]
pub struct DirectRouter {
    channels: Vec<LocalChannelInfo>,
}

impl DirectRouter {
    fn add_direct_channel(
        &mut self,
        info: LocalChannelInfo,
    ) -> Option<LocalChannelInfo> {
        let prev_info = self.remove_direct_channel(info.channel_id);
        self.channels.push(info);
        prev_info
    }

    fn remove_direct_channel(
        &mut self,
        channel_id: ChannelId,
    ) -> Option<LocalChannelInfo> {
        if let Some((index, _)) = self
            .channels
            .iter()
            .enumerate()
            .find(|(_, info)| info.channel_id == channel_id)
        {
            Some(self.channels.remove(index))
        } else {
            None
        }
    }
}

impl Extension<GossipExt> for DirectRouter {
    fn identity(&self) -> GossipExt {
        GossipExt::DirectRouter
    }

    fn update_from_peer(&mut self, message: &Messages) -> Result<(), Error> {
        match message {
            /*
            Messages::FundingLocked(FundingLocked { channel_id, .. }) => {}
            Messages::ChannelReestablish(_) => {}
            Messages::ClosingSigned(_) => {}
            Messages::CommitmentSigned(_) => {}
            Messages::RevokeAndAck(_) => {}
             */
            _ => {} // Nothing to do here
        }

        Ok(())
    }

    fn update_from_local(&mut self, message: &UpdateMsg) -> Result<(), Error> {
        match message {
            UpdateMsg::DirectChannelAdd(info) => {
                self.add_direct_channel(*info);
            }
            UpdateMsg::DirectChannelRemove(channel_id) => {
                self.remove_direct_channel(*channel_id);
            }
            UpdateMsg::DirectChannelUpdate(_) => {
                // TODO: implement update for channel balances
            }
        }
        Ok(())
    }

    fn load_state(&mut self, state: &RouterState) {
        self.channels = state.direct_channels.clone();
    }

    fn store_state(&self, state: &mut RouterState) {
        state.direct_channels = self.channels.clone();
    }
}

impl RouterExtension<GossipExt> for DirectRouter {
    #[inline]
    fn new() -> Box<dyn RouterExtension<GossipExt>>
    where
        Self: Sized,
    {
        Box::new(DirectRouter::default())
    }

    fn build_route(
        &mut self,
        payment: PaymentRequest,
        route: &mut Vec<Hop<PaymentOnion>>,
    ) {
        if let Some(channel) = self
            .channels
            .iter()
            .find(|info| info.remote_node == payment.node_id)
        {
            if channel.outboud_capacity_msat < payment.amount_msat {
                return; // We do not have enough funds
            }

            *route = vec![Hop::with(payment.node_id, PaymentOnion {
                // TODO: Choose realm basing on the destination configuration
                realm: HopRealm::Legacy(ShortChannelId::default()),
                amt_to_forward: payment.amount_msat,
                outgoing_cltv_value: payment.min_final_cltv_expiry,
            })];
        }
    }
}

/// BOLT-7 gossip-based router
#[derive(Getters, Clone, PartialEq, Eq, Debug, Default)]
pub struct GossipRouter {
    channels: Vec<GossipChannelInfo>,
}

impl Extension<GossipExt> for GossipRouter {
    fn identity(&self) -> GossipExt {
        GossipExt::GossipRouter
    }

    fn update_from_local(&mut self, _message: &UpdateMsg) -> Result<(), Error> {
        // Nothing to do here so far
        Ok(())
    }

    fn update_from_peer(&mut self, message: &Messages) -> Result<(), Error> {
        match message {
            // TODO: Extract routing information from gossip messages
            Messages::UpdateFee(_) => {}
            Messages::ChannelAnnouncement(_) => {}
            Messages::ChannelUpdate(_) => {}
            _ => {}
        }
        Ok(())
    }

    fn load_state(&mut self, state: &RouterState) {
        self.channels = state.remote_channels.clone()
    }

    fn store_state(&self, state: &mut RouterState) {
        state.remote_channels = self.channels.clone()
    }
}

impl RouterExtension<GossipExt> for GossipRouter {
    #[inline]
    fn new() -> Box<dyn RouterExtension<GossipExt>>
    where
        Self: Sized,
    {
        Box::new(GossipRouter::default())
    }

    fn build_route(
        &mut self,
        _payment: PaymentRequest,
        _route: &mut Vec<Hop<PaymentOnion>>,
    ) {
        // TODO: Implement route computing
    }
}