/
daemons.rs
189 lines (168 loc) · 6.21 KB
/
daemons.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
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020-2022 by
// Dr. Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// 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::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::Command;
use amplify::hex::ToHex;
use bitcoin::secp256k1::{SecretKey, SECP256K1};
use internet2::addr::LocalNode;
use internet2::session::noise::FramingProtocol;
use lnp::p2p;
use lnp::p2p::bolt::ActiveChannelId;
use microservices::cli::LogStyle;
use microservices::peer::{supervisor, PeerSocket};
use microservices::{DaemonHandle, Launcher, LauncherError};
use crate::lnpd::runtime::Runtime;
use crate::{channeld, peerd, routed, signd, watchd, Config, Error};
pub fn read_node_key_file(key_file: &Path) -> LocalNode {
let mut file = fs::File::open(key_file).unwrap_or_else(|_| {
panic!(
"Unable to open key file '{}';\nplease check that the file exists and the daemon has \
access rights to it. If you have not created a key file before, run \"lnpd init\". \
for more info you can run \"lnpd --help\"",
key_file.display()
)
});
let mut node_secret = [0u8; 32];
file.read_exact(&mut node_secret).expect("incorrect format of the node key file");
let node_secret =
SecretKey::from_slice(&node_secret).expect("incorrect format of the node key file");
let local_node = LocalNode::with(&SECP256K1, node_secret);
let local_id = local_node.node_id();
info!("{}: {}", "Local node id".ended(), local_id.addr());
local_node
}
/// Daemons that can be launched by lnpd
#[derive(Clone, Eq, PartialEq, Debug, Display)]
pub enum Daemon {
#[display("signd")]
Signd,
#[display("peerd --bolt")]
PeerdBolt(PeerSocket, PathBuf),
#[display("peerd --bifrost")]
PeerdBifrost(PeerSocket, PathBuf),
#[display("channeld")]
Channeld(ActiveChannelId),
#[display("routed")]
Routed,
#[display("watchd")]
Watchd,
}
impl Daemon {
pub fn protocol(&self) -> Option<p2p::Protocol> {
match self {
Daemon::PeerdBolt(..) => Some(p2p::Protocol::Bolt),
Daemon::PeerdBifrost(..) => Some(p2p::Protocol::Bifrost),
// TODO: Add support for bifrost channels
_ => None,
}
}
}
impl Launcher for Daemon {
type RunError = Error;
type Config = Config;
fn bin_name(&self) -> &'static str {
match self {
Daemon::Signd => "signd",
Daemon::PeerdBolt(..) => "peerd",
Daemon::PeerdBifrost(..) => "peerd",
Daemon::Channeld(..) => "channeld",
Daemon::Routed => "routed",
Daemon::Watchd => "watchd",
}
}
fn cmd_args(&self, cmd: &mut Command) -> Result<(), LauncherError<Self>> {
cmd.args(std::env::args().skip(1).filter(|arg| {
!["--listen", "--bolt", "--bifrost"].iter().any(|pat| arg.starts_with(pat))
}));
match self.protocol() {
Some(p2p::Protocol::Bolt) => cmd.args(["--bolt"]),
Some(p2p::Protocol::Bifrost) => cmd.args(["--bifrost"]),
None => cmd,
};
match self {
Daemon::PeerdBolt(PeerSocket::Listen(socket_addr), _)
| Daemon::PeerdBifrost(PeerSocket::Listen(socket_addr), _) => {
cmd.args([
"--listen",
&socket_addr.ip().to_string(),
"--port",
&socket_addr.port().to_string(),
]);
}
Daemon::PeerdBolt(PeerSocket::Connect(node_addr), _) => {
cmd.args(["--connect", &format!("bolt://{}", node_addr)]);
}
Daemon::PeerdBifrost(PeerSocket::Connect(node_addr), _) => {
cmd.args(["--connect", &format!("bifrost://{}", node_addr)]);
}
Daemon::Channeld(channel_id, ..) => {
cmd.args(&[channel_id.as_slice32().to_hex()]);
if channel_id.channel_id().is_some() {
cmd.args(["--reestablish"]);
}
}
_ => { /* No additional configuration is required here */ }
}
Ok(())
}
fn run_impl(self, config: Config) -> Result<(), Error> {
match self {
Daemon::Signd => signd::run(config),
Daemon::PeerdBolt(socket, key_file) => {
let local_node = read_node_key_file(&key_file);
let threaded = config.threaded;
let config = Config::with(config, peerd::Config { protocol: p2p::Protocol::Bolt });
supervisor::run(
config,
threaded,
FramingProtocol::Brontide,
local_node,
socket,
peerd::runtime::run,
)
}
Daemon::PeerdBifrost(socket, key_file) => {
let threaded = config.threaded;
let local_node = read_node_key_file(&key_file);
let config =
Config::with(config, peerd::Config { protocol: p2p::Protocol::Bifrost });
supervisor::run(
config,
threaded,
FramingProtocol::Brontozaur,
local_node,
socket,
peerd::runtime::run,
)
}
Daemon::Channeld(channel_id) => channeld::run(config, channel_id),
Daemon::Routed => routed::run(config),
Daemon::Watchd => watchd::run(config),
}
}
}
impl Runtime {
pub(super) fn launch_daemon(
&self,
daemon: Daemon,
config: Config,
) -> Result<DaemonHandle<Daemon>, LauncherError<Daemon>> {
if self.config.threaded {
daemon.thread_daemon(config)
} else {
daemon.exec_daemon()
}
}
}