forked from LNP-WG/lnp-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.rs
195 lines (179 loc) · 6.49 KB
/
runtime.rs
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
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020 by
// Dr. Maxim Orlovsky <[email protected]>
//
// 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 lnpbp::bitcoin::secp256k1;
use lnpbp::lnp::{message, ChannelId, Messages, TypedEnum};
use lnpbp_services::esb::{self, Handler};
use crate::rpc::{request, Request, ServiceBus};
use crate::{Config, Error, LogStyle, Service, ServiceId};
pub fn run(config: Config, channel_id: ChannelId) -> Result<(), Error> {
let runtime = Runtime {
identity: ServiceId::Channel(channel_id),
};
Service::run(config, runtime, false)
}
pub struct Runtime {
identity: ServiceId,
}
impl esb::Handler<ServiceBus> for Runtime {
type Request = Request;
type Address = ServiceId;
type Error = Error;
fn identity(&self) -> ServiceId {
self.identity.clone()
}
fn handle(
&mut self,
senders: &mut esb::SenderList<ServiceBus, ServiceId>,
bus: ServiceBus,
source: ServiceId,
request: Request,
) -> Result<(), Self::Error> {
match bus {
ServiceBus::Msg => self.handle_rpc_msg(senders, source, request),
ServiceBus::Ctl => self.handle_rpc_ctl(senders, source, request),
_ => {
Err(Error::NotSupported(ServiceBus::Bridge, request.get_type()))
}
}
}
fn handle_err(&mut self, _: esb::Error) -> Result<(), esb::Error> {
// We do nothing and do not propagate error; it's already being reported
// with `error!` macro by the controller. If we propagate error here
// this will make whole daemon panic
Ok(())
}
}
impl Runtime {
fn handle_rpc_msg(
&mut self,
senders: &mut esb::SenderList<ServiceBus, ServiceId>,
source: ServiceId,
request: Request,
) -> Result<(), Error> {
let mut notify_cli = None;
match request {
Request::SendMessage(Messages::AcceptChannel(accept_channel)) => {
info!(
"{} from the remote peer {} with temporary id {}",
"Accepting channel".promo(),
source.promoter(),
accept_channel.temporary_channel_id.promoter()
);
notify_cli = Some(Request::Progress(format!(
"{} accepted channel",
source
)));
}
Request::SendMessage(_) => {
// Ignore the rest of LN peer messages
}
_ => {
error!(
"MSG RPC can be only used for forwarding LNPWP messages"
);
return Err(Error::NotSupported(
ServiceBus::Msg,
request.get_type(),
));
}
}
if let Some(resp) = notify_cli {
senders.send_to(ServiceBus::Ctl, ServiceId::Lnpd, source, resp)?;
}
Ok(())
}
fn handle_rpc_ctl(
&mut self,
senders: &mut esb::SenderList<ServiceBus, ServiceId>,
_source: ServiceId,
request: Request,
) -> Result<(), Error> {
let mut notify_cli = None;
match request {
Request::OpenChannelWith(request::CreateChannel {
channel_req,
peerd,
report_to,
}) => {
let msg = format!(
"Requesting remote peer to {} with temp id {}",
"open a channel".ended(),
channel_req.temporary_channel_id.ender()
);
info!("{}", msg);
senders.send_to(
ServiceBus::Msg,
self.identity(),
peerd,
Request::SendMessage(Messages::OpenChannel(channel_req)),
)?;
notify_cli = Some((report_to, msg))
}
Request::AcceptChannelFrom(request::CreateChannel {
channel_req,
peerd,
..
}) => {
let dumb_key = secp256k1::PublicKey::from_secret_key(
&lnpbp::SECP256K1,
&secp256k1::key::ONE_KEY,
);
let accept_channel = message::AcceptChannel {
temporary_channel_id: channel_req.temporary_channel_id,
dust_limit_satoshis: channel_req.dust_limit_satoshis,
max_htlc_value_in_flight_msat: channel_req
.max_htlc_value_in_flight_msat,
channel_reserve_satoshis: channel_req
.channel_reserve_satoshis,
htlc_minimum_msat: channel_req.htlc_minimum_msat,
minimum_depth: 3, // TODO: take from config options
to_self_delay: channel_req.to_self_delay,
max_accepted_htlcs: channel_req.max_accepted_htlcs,
funding_pubkey: dumb_key,
revocation_basepoint: dumb_key,
payment_point: dumb_key,
delayed_payment_basepoint: dumb_key,
htlc_basepoint: dumb_key,
first_per_commitment_point: dumb_key,
shutdown_scriptpubkey: None,
unknown_tlvs: none!(),
};
senders.send_to(
ServiceBus::Msg,
self.identity(),
peerd,
Request::SendMessage(Messages::AcceptChannel(
accept_channel,
)),
)?;
}
_ => {
error!("Request is not supported by the CTL interface");
return Err(Error::NotSupported(
ServiceBus::Ctl,
request.get_type(),
));
}
}
if let Some((Some(report_to), resp)) = notify_cli {
senders.send_to(
ServiceBus::Ctl,
self.identity(),
report_to,
Request::Progress(resp),
)?;
}
Ok(())
}
}