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
// LNP/BP Core Library implementing LNPBP specifications & standards
// Written in 2020-2022 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 std::convert::TryFrom;
use std::fmt::{Debug, Display};
use std::hash::Hash;

use amplify::DumbDefault;
use internet2::presentation::sphinx::Hop;
use p2p::bolt::PaymentRequest;
use wallet::psbt::Psbt;

use crate::channel::tx_graph::TxGraph;
use crate::channel::Funding;
use crate::{channel, extension, router};

/// Marker trait for creating extension nomenclatures, defining order in which
/// extensions are called to process the data.
///
/// Extension nomenclature is an enum with members convertible into `u16`
/// representation
pub trait Nomenclature
where
    Self: Clone
        + Copy
        + PartialEq
        + Eq
        + PartialOrd
        + Ord
        + Hash
        + Debug
        + Display
        + Default
        + TryFrom<u16, Error = strict_encoding::Error>
        + Into<u16>,
{
    type State: DumbDefault;
    type Error: std::error::Error;
    type PeerMessage;
    type UpdateMessage;
    type UpdateRequest;
}

pub trait Extension<N: Nomenclature> {
    fn identity(&self) -> N;

    /// Perform a sate change and produce a message which should be communicated
    /// to peers notifying them about the state change
    #[allow(dead_code, unused_variables)]
    fn state_change(
        &mut self,
        request: &<N as extension::Nomenclature>::UpdateRequest,
        message: &mut <N as extension::Nomenclature>::PeerMessage,
    ) -> Result<(), <N as extension::Nomenclature>::Error> {
        // Do nothing by default
        Ok(())
    }

    /// Updates extension state from the data taken from the message received
    /// from the remote peer
    fn update_from_peer(
        &mut self,
        message: &<N as extension::Nomenclature>::PeerMessage,
    ) -> Result<(), <N as extension::Nomenclature>::Error>;

    /// Updates extension state from some local data
    fn update_from_local(
        &mut self,
        message: &<N as extension::Nomenclature>::UpdateMessage,
    ) -> Result<(), <N as extension::Nomenclature>::Error>;

    fn load_state(&mut self, state: &N::State);
    fn store_state(&self, state: &mut N::State);
}

pub trait RouterExtension<N>
where
    N: router::Nomenclature,
    Self: Extension<N>,
{
    /// Constructs boxed extension objects which can be inserted into router
    /// extension pipeline
    #[allow(clippy::new_ret_no_self)]
    fn new() -> Box<dyn RouterExtension<N>>
    where
        Self: Sized;

    fn build_route(
        &mut self,
        payment: PaymentRequest,
        route: &mut Vec<Hop<<N as router::Nomenclature>::HopPayload>>,
    );
}

pub trait ChannelExtension<N>
where
    N: channel::Nomenclature,
    N::State: channel::State,
    Self: Extension<N>,
{
    /// Constructs boxed extension objects which can be inserted into channel
    /// extension pipeline
    #[allow(clippy::new_ret_no_self)]
    fn new() -> Box<dyn ChannelExtension<N>>
    where
        Self: Sized;

    /// Applies state to the channel transaction graph
    fn build_graph(
        &self,
        tx_graph: &mut TxGraph,
        remote: bool,
    ) -> Result<(), <N as Nomenclature>::Error>;
}

/// Channel constructor specific methods
pub trait ChannelConstructor<N>
where
    N: channel::Nomenclature,
    N::State: channel::State,
    Self: ChannelExtension<N> + Default,
{
    fn enrich_funding(
        &self,
        psbt: &mut Psbt,
        funding: &Funding,
    ) -> Result<(), <N as Nomenclature>::Error>;
}