init lss connector in broker, cleanup main func

This commit is contained in:
Evan Feenstra
2023-05-24 16:38:44 -07:00
parent 77fa8c2492
commit 877c9b8a83
11 changed files with 348 additions and 160 deletions

206
broker/Cargo.lock generated
View File

@@ -247,6 +247,12 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "base64"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f1e31e207a6b8fb791a38ea3105e6cb541f55e4d029902d3039a4ad07cc4105"
[[package]] [[package]]
name = "base64-compat" name = "base64-compat"
version = "1.0.0" version = "1.0.0"
@@ -561,7 +567,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "344adc371239ef32293cb1c4fe519592fcf21206c79c02854320afcdf3ab4917" checksum = "344adc371239ef32293cb1c4fe519592fcf21206c79c02854320afcdf3ab4917"
dependencies = [ dependencies = [
"aes-gcm", "aes-gcm",
"base64", "base64 0.13.1",
"hkdf", "hkdf",
"hmac", "hmac",
"percent-encoding", "percent-encoding",
@@ -742,6 +748,40 @@ version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb" checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb"
[[package]]
name = "deadpool"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "421fe0f90f2ab22016f32a9881be5134fdd71c65298917084b0c7477cbc3856e"
dependencies = [
"async-trait",
"deadpool-runtime",
"num_cpus",
"retain_mut",
"tokio",
]
[[package]]
name = "deadpool-postgres"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "836a24a9d49deefe610b8b60c767a7412e9a931d79a89415cd2d2d71630ca8d7"
dependencies = [
"deadpool",
"log",
"tokio",
"tokio-postgres",
]
[[package]]
name = "deadpool-runtime"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaa37046cc0f6c3cc6090fbdbf73ef0b8ef4cfcc37f6befc0020f63e8cf121e1"
dependencies = [
"tokio",
]
[[package]] [[package]]
name = "delegate" name = "delegate"
version = "0.6.2" version = "0.6.2"
@@ -926,6 +966,12 @@ version = "2.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.8.0" version = "1.8.0"
@@ -1331,7 +1377,7 @@ dependencies = [
"httpdate", "httpdate",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite",
"socket2", "socket2 0.4.9",
"tokio", "tokio",
"tower-service", "tower-service",
"tracing", "tracing",
@@ -1586,6 +1632,7 @@ dependencies = [
"bitcoin_hashes 0.11.0", "bitcoin_hashes 0.11.0",
"clap 3.2.23", "clap 3.2.23",
"ctrlc", "ctrlc",
"deadpool-postgres",
"dirs", "dirs",
"fern", "fern",
"futures", "futures",
@@ -1600,6 +1647,7 @@ dependencies = [
"thiserror", "thiserror",
"time 0.3.17", "time 0.3.17",
"tokio", "tokio",
"tokio-postgres",
"tonic", "tonic",
"tonic-build", "tonic-build",
"triggered", "triggered",
@@ -1662,6 +1710,23 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
] ]
[[package]]
name = "lss-connector"
version = "0.1.0"
source = "git+https://github.com/stakwork/sphinx-rs#5623e8845fcd75c61be877d7e6285a3036bab1cb"
dependencies = [
"anyhow",
"hex",
"lightning-storage-server",
"log",
"rmp-serde",
"secp256k1",
"serde",
"tokio",
"vls-frontend",
"vls-protocol-signer",
]
[[package]] [[package]]
name = "mach" name = "mach"
version = "0.3.2" version = "0.3.2"
@@ -1698,6 +1763,15 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]]
name = "md-5"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6365506850d44bff6e2fbcb5176cf63650e48bd45ef2fe2665ae1570e0f4b9ca"
dependencies = [
"digest",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@@ -2138,6 +2212,24 @@ dependencies = [
"indexmap", "indexmap",
] ]
[[package]]
name = "phf"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "928c6535de93548188ef63bb7c4036bd415cd8f36ad25af44b9789b2ee72a48c"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_shared"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1fb5f6f826b772a8d4c0394209441e7d37cbbb967ae9c7e0e8134365c9ee676"
dependencies = [
"siphasher",
]
[[package]] [[package]]
name = "pin-project" name = "pin-project"
version = "1.0.12" version = "1.0.12"
@@ -2194,6 +2286,35 @@ version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26f6a7b87c2e435a3241addceeeff740ff8b7e76b74c13bf9acb17fa454ea00b" checksum = "26f6a7b87c2e435a3241addceeeff740ff8b7e76b74c13bf9acb17fa454ea00b"
[[package]]
name = "postgres-protocol"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b7fa9f396f51dffd61546fd8573ee20592287996568e6175ceb0f8699ad75d"
dependencies = [
"base64 0.21.1",
"byteorder",
"bytes",
"fallible-iterator",
"hmac",
"md-5",
"memchr",
"rand",
"sha2",
"stringprep",
]
[[package]]
name = "postgres-types"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f028f05971fe20f512bcc679e2c10227e57809a3af86a7606304435bc8896cd6"
dependencies = [
"bytes",
"fallible-iterator",
"postgres-protocol",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.17" version = "0.2.17"
@@ -2484,7 +2605,7 @@ version = "0.11.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c" checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
"bytes", "bytes",
"encoding_rs", "encoding_rs",
"futures-core", "futures-core",
@@ -2517,6 +2638,12 @@ dependencies = [
"winreg", "winreg",
] ]
[[package]]
name = "retain_mut"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4389f1d5789befaf6029ebd9f7dac4af7f7e3d61b69d4f30e2ac02b57e7712b0"
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.16.20" version = "0.16.20"
@@ -2642,7 +2769,7 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
"bitflags", "bitflags",
"serde", "serde",
] ]
@@ -2747,7 +2874,7 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ee86d63972a7c661d1536fefe8c3c8407321c3df668891286de28abcd087360" checksum = "1ee86d63972a7c661d1536fefe8c3c8407321c3df668891286de28abcd087360"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
] ]
[[package]] [[package]]
@@ -2756,7 +2883,7 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55" checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
] ]
[[package]] [[package]]
@@ -2899,7 +3026,7 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25bf4a5a814902cd1014dbccfa4d4560fb8432c779471e96e035602519f82eef" checksum = "25bf4a5a814902cd1014dbccfa4d4560fb8432c779471e96e035602519f82eef"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
"chrono", "chrono",
"hex", "hex",
"serde", "serde",
@@ -2962,6 +3089,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "siphasher"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de"
[[package]] [[package]]
name = "sketches-ddsketch" name = "sketches-ddsketch"
version = "0.2.0" version = "0.2.0"
@@ -3018,13 +3151,23 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "socket2"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2538b18701741680e0322a2302176d3253a35388e2e62f172f64f4f16605f877"
dependencies = [
"libc",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "sphinx-auther" name = "sphinx-auther"
version = "0.1.12" version = "0.1.12"
source = "git+https://github.com/stakwork/sphinx-rs#763533b57dd72d18bec13c54760090c7cf302f43" source = "git+https://github.com/stakwork/sphinx-rs#5623e8845fcd75c61be877d7e6285a3036bab1cb"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64", "base64 0.13.1",
"hex", "hex",
"log", "log",
"secp256k1", "secp256k1",
@@ -3033,7 +3176,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-glyph" name = "sphinx-glyph"
version = "0.1.2" version = "0.1.2"
source = "git+https://github.com/stakwork/sphinx-rs#763533b57dd72d18bec13c54760090c7cf302f43" source = "git+https://github.com/stakwork/sphinx-rs#5623e8845fcd75c61be877d7e6285a3036bab1cb"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"hex", "hex",
@@ -3057,6 +3200,7 @@ dependencies = [
"fern", "fern",
"hex", "hex",
"log", "log",
"lss-connector",
"once_cell", "once_cell",
"pretty_env_logger", "pretty_env_logger",
"rocket", "rocket",
@@ -3078,7 +3222,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-signer" name = "sphinx-signer"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/stakwork/sphinx-rs#763533b57dd72d18bec13c54760090c7cf302f43" source = "git+https://github.com/stakwork/sphinx-rs#5623e8845fcd75c61be877d7e6285a3036bab1cb"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bip39", "bip39",
@@ -3132,6 +3276,16 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "stringprep"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ee348cb74b87454fff4b551cbf727025810a004f88aeacae7f85b87f4e9a1c1"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@@ -3288,7 +3442,7 @@ dependencies = [
"parking_lot 0.12.1", "parking_lot 0.12.1",
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
"socket2", "socket2 0.4.9",
"tokio-macros", "tokio-macros",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
@@ -3314,6 +3468,30 @@ dependencies = [
"syn 2.0.16", "syn 2.0.16",
] ]
[[package]]
name = "tokio-postgres"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e89f6234aa8fd43779746012fcf53603cdb91fdd8399aa0de868c2d56b6dde1"
dependencies = [
"async-trait",
"byteorder",
"bytes",
"fallible-iterator",
"futures-channel",
"futures-util",
"log",
"parking_lot 0.12.1",
"percent-encoding",
"phf",
"pin-project-lite",
"postgres-protocol",
"postgres-types",
"socket2 0.5.3",
"tokio",
"tokio-util",
]
[[package]] [[package]]
name = "tokio-rustls" name = "tokio-rustls"
version = "0.23.4" version = "0.23.4"
@@ -3368,7 +3546,7 @@ dependencies = [
"async-stream", "async-stream",
"async-trait", "async-trait",
"axum", "axum",
"base64", "base64 0.13.1",
"bytes", "bytes",
"futures-core", "futures-core",
"futures-util", "futures-util",
@@ -4165,7 +4343,7 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64abca276c58f8341ddc13fd4bd6ae75993cc669043f5b34813c90f7dff04771" checksum = "64abca276c58f8341ddc13fd4bd6ae75993cc669043f5b34813c90f7dff04771"
dependencies = [ dependencies = [
"base64", "base64 0.13.1",
"chrono", "chrono",
"data-encoding", "data-encoding",
"der-parser", "der-parser",

View File

@@ -9,6 +9,8 @@ strip = "debuginfo"
[dependencies] [dependencies]
sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs" } sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs" }
lss-connector = { git = "https://github.com/stakwork/sphinx-rs" }
# lss-connector = { path = "../../sphinx-rs/lss-connector" }
# sphinx-key-parser = { path = "../parser" } # sphinx-key-parser = { path = "../parser" }
vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "e13c8cd994b310f598c0b2902741d89ad5472382" } vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "e13c8cd994b310f598c0b2902741d89ad5472382" }
vls-proxy = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "e13c8cd994b310f598c0b2902741d89ad5472382" } vls-proxy = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "e13c8cd994b310f598c0b2902741d89ad5472382" }

View File

@@ -50,10 +50,11 @@
##### broker ##### broker
- client.put(muts, &client_hmac).await? - client.put(muts, &client_hmac).await?
- send some kind of confirmation back to signer - server hmac sent back to signer
##### signer ##### signer
- verify server hmac
- finally, send the VLS reply back to broker - finally, send the VLS reply back to broker
##### broker ##### broker

View File

@@ -1,4 +1,4 @@
use crate::{ChannelReply, ChannelRequest}; use crate::conn::{ChannelReply, ChannelRequest};
use async_trait::async_trait; use async_trait::async_trait;
use rocket::tokio::sync::{mpsc, oneshot}; use rocket::tokio::sync::{mpsc, oneshot};
use sphinx_signer::sphinx_glyph::topics; use sphinx_signer::sphinx_glyph::topics;

84
broker/src/conn.rs Normal file
View File

@@ -0,0 +1,84 @@
use rocket::tokio::sync::{mpsc, oneshot};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Connections {
pub pubkey: Option<String>,
pub clients: Vec<String>,
}
impl Connections {
pub fn new() -> Self {
Self {
pubkey: None,
clients: Vec::new(),
}
}
pub fn set_pubkey(&mut self, pk: &str) {
self.pubkey = Some(pk.to_string())
}
pub fn add_client(&mut self, cid: &str) {
let cids = cid.to_string();
if !self.clients.contains(&cids) {
self.clients.push(cids)
}
}
pub fn remove_client(&mut self, cid: &str) {
let cids = cid.to_string();
if self.clients.contains(&cids) {
self.clients.retain(|x| x != cid)
}
}
pub fn client_action(&mut self, cid: &str, connected: bool) {
if connected {
self.add_client(cid);
} else {
self.remove_client(cid);
}
}
}
pub struct Channel {
pub sequence: u16,
pub sender: mpsc::Sender<ChannelRequest>,
pub pubkey: [u8; 33],
}
/// Responses are received on the oneshot sender
#[derive(Debug)]
pub struct ChannelRequest {
pub topic: String,
pub message: Vec<u8>,
pub reply_tx: oneshot::Sender<ChannelReply>,
pub cid: Option<String>, // if it exists, only try the one client
}
impl ChannelRequest {
pub fn new(topic: &str, message: Vec<u8>) -> (Self, oneshot::Receiver<ChannelReply>) {
let (reply_tx, reply_rx) = oneshot::channel();
let cr = ChannelRequest {
topic: topic.to_string(),
message,
reply_tx,
cid: None,
};
(cr, reply_rx)
}
pub fn for_cid(&mut self, cid: &str) {
self.cid = Some(cid.to_string())
}
pub fn new_for(
cid: &str,
topic: &str,
message: Vec<u8>,
) -> (Self, oneshot::Receiver<ChannelReply>) {
let (mut cr, reply_rx) = ChannelRequest::new(topic, message);
cr.for_cid(cid);
(cr, reply_rx)
}
}
// mpsc reply
#[derive(Debug)]
pub struct ChannelReply {
pub reply: Vec<u8>,
}

View File

@@ -6,7 +6,9 @@ mod routes;
mod run_test; mod run_test;
mod unix_fd; mod unix_fd;
mod util; mod util;
mod conn;
use crate::conn::{Connections, ChannelRequest};
use crate::chain_tracker::MqttSignerPort; use crate::chain_tracker::MqttSignerPort;
use crate::mqtt::{check_auth, start_broker}; use crate::mqtt::{check_auth, start_broker};
use crate::unix_fd::SignerLoop; use crate::unix_fd::SignerLoop;
@@ -14,10 +16,9 @@ use crate::util::{read_broker_config, Settings};
use clap::{arg, App}; use clap::{arg, App};
use rocket::tokio::{ use rocket::tokio::{
self, self,
sync::{broadcast, mpsc, oneshot}, sync::{broadcast, mpsc},
}; };
use rumqttd::{oneshot as std_oneshot, AuthMsg}; use rumqttd::{oneshot as std_oneshot, AuthMsg};
use serde::{Deserialize, Serialize};
use std::env; use std::env;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use url::Url; use url::Url;
@@ -26,91 +27,7 @@ use vls_proxy::client::UnixClient;
use vls_proxy::connection::{open_parent_fd, UnixConnection}; use vls_proxy::connection::{open_parent_fd, UnixConnection};
use vls_proxy::portfront::SignerPortFront; use vls_proxy::portfront::SignerPortFront;
use vls_proxy::util::{add_hsmd_args, handle_hsmd_version}; use vls_proxy::util::{add_hsmd_args, handle_hsmd_version};
use lss_connector::LssBroker;
#[derive(Debug, Serialize, Deserialize)]
pub struct Connections {
pub pubkey: Option<String>,
pub clients: Vec<String>,
}
impl Connections {
pub fn new() -> Self {
Self {
pubkey: None,
clients: Vec::new(),
}
}
pub fn set_pubkey(&mut self, pk: &str) {
self.pubkey = Some(pk.to_string())
}
pub fn add_client(&mut self, cid: &str) {
let cids = cid.to_string();
if !self.clients.contains(&cids) {
self.clients.push(cids)
}
}
pub fn remove_client(&mut self, cid: &str) {
let cids = cid.to_string();
if self.clients.contains(&cids) {
self.clients.retain(|x| x != cid)
}
}
pub fn client_action(&mut self, cid: &str, connected: bool) {
if connected {
self.add_client(cid);
} else {
self.remove_client(cid);
}
}
}
pub struct Channel {
pub sequence: u16,
pub sender: mpsc::Sender<ChannelRequest>,
pub pubkey: [u8; 33],
}
/// Responses are received on the oneshot sender
#[derive(Debug)]
pub struct ChannelRequest {
pub topic: String,
pub message: Vec<u8>,
pub reply_tx: oneshot::Sender<ChannelReply>,
pub cid: Option<String>, // if it exists, only try the one client
}
impl ChannelRequest {
pub fn new(topic: &str, message: Vec<u8>) -> (Self, oneshot::Receiver<ChannelReply>) {
let (reply_tx, reply_rx) = oneshot::channel();
let cr = ChannelRequest {
topic: topic.to_string(),
message,
reply_tx,
cid: None,
};
(cr, reply_rx)
}
pub fn for_cid(&mut self, cid: &str) {
self.cid = Some(cid.to_string())
}
pub fn new_for(
cid: &str,
topic: &str,
message: Vec<u8>,
) -> (Self, oneshot::Receiver<ChannelReply>) {
let (mut cr, reply_rx) = ChannelRequest::new(topic, message);
cr.for_cid(cid);
(cr, reply_rx)
}
}
// mpsc reply
#[derive(Debug)]
pub struct ChannelReply {
pub reply: Vec<u8>,
}
// const CLIENT_ID: &str = "sphinx-1";
const BROKER_CONFIG_PATH: &str = "../broker.conf";
#[rocket::launch] #[rocket::launch]
async fn rocket() -> _ { async fn rocket() -> _ {
@@ -135,9 +52,9 @@ async fn rocket() -> _ {
panic!("end") panic!("end")
} else { } else {
if matches.is_present("test") { if matches.is_present("test") {
run_test::run_test() run_test::run_test().await
} else { } else {
run_main(parent_fd) run_main(parent_fd).await
} }
} }
} }
@@ -149,8 +66,43 @@ fn make_clap_app() -> App<'static> {
add_hsmd_args(app) add_hsmd_args(app)
} }
async fn run_main(parent_fd: i32) -> rocket::Rocket<rocket::Build> {
let settings = read_broker_config();
let (mqtt_tx, mqtt_rx) = mpsc::channel(10000);
let (error_tx, error_rx) = broadcast::channel(10000);
error_log::log_errors(error_rx);
let conns = broker_setup(settings, mqtt_rx, error_tx.clone()).await;
if let Ok(btc_url) = env::var("BITCOIND_RPC_URL") {
let signer_port = Box::new(MqttSignerPort::new(mqtt_tx.clone()));
let port_front = SignerPortFront::new(signer_port, settings.network);
let source_factory = Arc::new(SourceFactory::new(".", settings.network));
let frontend = Frontend::new(
Arc::new(port_front),
source_factory,
Url::parse(&btc_url).expect("malformed btc rpc url"),
);
tokio::spawn(async move {
frontend.start();
});
} else {
log::warn!("Running without a frontend")
}
let cln_client = UnixClient::new(UnixConnection::new(parent_fd));
// TODO pass status_rx into SignerLoop?
let mut signer_loop = SignerLoop::new(cln_client, mqtt_tx.clone());
// spawn CLN listener
std::thread::spawn(move || {
signer_loop.start(Some(settings));
});
routes::launch_rocket(mqtt_tx, error_tx, settings, conns)
}
// blocks until a connection received // blocks until a connection received
pub fn main_setup( pub async fn broker_setup(
settings: Settings, settings: Settings,
mqtt_rx: mpsc::Receiver<ChannelRequest>, mqtt_rx: mpsc::Receiver<ChannelRequest>,
error_tx: broadcast::Sender<Vec<u8>>, error_tx: broadcast::Sender<Vec<u8>>,
@@ -171,6 +123,14 @@ pub fn main_setup(
} }
}); });
// LSS
let lss_client = if let Ok(uri) = env::var("VLS_LSS") {
let lss_conn = LssBroker::new(uri.clone()).await.unwrap();
Some(lss_conn)
} else {
None
};
// broker // broker
log::info!("=> start broker on network: {}", settings.network); log::info!("=> start broker on network: {}", settings.network);
start_broker( start_broker(
@@ -206,47 +166,3 @@ pub fn main_setup(
conns conns
} }
fn run_main(parent_fd: i32) -> rocket::Rocket<rocket::Build> {
let settings = read_broker_config(BROKER_CONFIG_PATH);
let (mqtt_tx, mqtt_rx) = mpsc::channel(10000);
let (error_tx, error_rx) = broadcast::channel(10000);
error_log::log_errors(error_rx);
let conns = main_setup(settings, mqtt_rx, error_tx.clone());
// let mqtt_tx_ = mqtt_tx.clone();
// tokio::spawn(async move {
// while let Some(msg) = unix_rx.recv().await {
// // update LSS here?
// if let Err(e) = mqtt_tx_.send(msg).await {
// log::error!("failed to send on mqtt_tx {:?}", e);
// }
// }
// });
if let Ok(btc_url) = env::var("BITCOIND_RPC_URL") {
let signer_port = Box::new(MqttSignerPort::new(mqtt_tx.clone()));
let port_front = SignerPortFront::new(signer_port, settings.network);
let source_factory = Arc::new(SourceFactory::new(".", settings.network));
let frontend = Frontend::new(
Arc::new(port_front),
source_factory,
Url::parse(&btc_url).expect("malformed btc rpc url"),
);
tokio::spawn(async move {
frontend.start();
});
} else {
log::warn!("Running without a frontend")
}
let cln_client = UnixClient::new(UnixConnection::new(parent_fd));
// TODO pass status_rx into SignerLoop?
let mut signer_loop = SignerLoop::new(cln_client, mqtt_tx.clone());
// spawn CLN listener
std::thread::spawn(move || {
signer_loop.start(Some(settings));
});
routes::launch_rocket(mqtt_tx, error_tx, settings, conns)
}

View File

@@ -1,6 +1,6 @@
use crate::conn::Connections;
use crate::conn::{ChannelReply, ChannelRequest};
use crate::util::Settings; use crate::util::Settings;
use crate::Connections;
use crate::{ChannelReply, ChannelRequest};
use rocket::tokio::{sync::broadcast, sync::mpsc}; use rocket::tokio::{sync::broadcast, sync::mpsc};
use rumqttd::{local::LinkTx, Alert, AlertEvent, AuthMsg, Broker, Config, Notification}; use rumqttd::{local::LinkTx, Alert, AlertEvent, AuthMsg, Broker, Config, Notification};
use sphinx_signer::sphinx_glyph::sphinx_auther::token::Token; use sphinx_signer::sphinx_glyph::sphinx_auther::token::Token;
@@ -76,6 +76,7 @@ pub fn start_broker(
// String is the client id // String is the client id
let (msg_tx, msg_rx) = std::sync::mpsc::channel::<(String, Vec<u8>)>(); let (msg_tx, msg_rx) = std::sync::mpsc::channel::<(String, Vec<u8>)>();
let (lss_tx, lss_rx) = std::sync::mpsc::channel::<Vec<u8>>();
// receive from CLN, Frontend, or Controller // receive from CLN, Frontend, or Controller
let conns_ = connections.clone(); let conns_ = connections.clone();
@@ -143,9 +144,12 @@ pub fn start_broker(
let topic = topic_res.unwrap(); let topic = topic_res.unwrap();
if topic.ends_with(topics::ERROR) { if topic.ends_with(topics::ERROR) {
let _ = error_sender.send(f.publish.payload.to_vec()); let _ = error_sender.send(f.publish.payload.to_vec());
} else if topics.ends_with(topics::LSS_PUB) { } else if topic.ends_with(topics::LSS_PUB) {
// send to LSS client here // send to LSS client here
// get the hmac back, pub to the device // get the hmac back, pub to the device
if let Err(e) = lss_tx.send(f.publish.payload.to_vec()) {
log::error!("failed to pub to lss_tx! {:?}", e);
}
} else { } else {
let ts: Vec<&str> = topic.split("/").collect(); let ts: Vec<&str> = topic.split("/").collect();
if ts.len() != 2 { if ts.len() != 2 {

View File

@@ -1,6 +1,6 @@
use crate::util::Settings; use crate::util::Settings;
use crate::ChannelRequest; use crate::conn::ChannelRequest;
use crate::Connections; use crate::conn::Connections;
use rocket::fairing::{Fairing, Info, Kind}; use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header; use rocket::http::Header;
use rocket::response::stream::{Event, EventStream}; use rocket::response::stream::{Event, EventStream};

View File

@@ -1,6 +1,6 @@
use crate::routes::launch_rocket; use crate::routes::launch_rocket;
use crate::util::Settings; use crate::util::Settings;
use crate::ChannelRequest; use crate::conn::ChannelRequest;
use rocket::tokio::{self, sync::broadcast, sync::mpsc}; use rocket::tokio::{self, sync::broadcast, sync::mpsc};
use sphinx_signer::{parser, sphinx_glyph::topics}; use sphinx_signer::{parser, sphinx_glyph::topics};
use vls_protocol::serde_bolt::WireString; use vls_protocol::serde_bolt::WireString;
@@ -8,7 +8,7 @@ use vls_protocol::{msgs, msgs::Message};
// const CLIENT_ID: &str = "test-1"; // const CLIENT_ID: &str = "test-1";
pub fn run_test() -> rocket::Rocket<rocket::Build> { pub async fn run_test() -> rocket::Rocket<rocket::Build> {
log::info!("TEST..."); log::info!("TEST...");
// let mut id = 0u16; // let mut id = 0u16;
@@ -20,7 +20,7 @@ pub fn run_test() -> rocket::Rocket<rocket::Build> {
crate::error_log::log_errors(error_rx); crate::error_log::log_errors(error_rx);
// block until connection // block until connection
let conns = crate::main_setup(settings, mqtt_rx, error_tx.clone()); let conns = crate::broker_setup(settings, mqtt_rx, error_tx.clone()).await;
log::info!("=> off to the races!"); log::info!("=> off to the races!");
let tx_ = mqtt_tx.clone(); let tx_ = mqtt_tx.clone();

View File

@@ -1,5 +1,5 @@
use crate::conn::{Channel, ChannelReply, ChannelRequest};
use crate::util::Settings; use crate::util::Settings;
use crate::{Channel, ChannelReply, ChannelRequest};
use bitcoin::blockdata::constants::ChainHash; use bitcoin::blockdata::constants::ChainHash;
use log::*; use log::*;
use rocket::tokio::sync::{mpsc, oneshot}; use rocket::tokio::sync::{mpsc, oneshot};

View File

@@ -22,7 +22,10 @@ impl Default for Settings {
} }
} }
pub fn read_broker_config(config_path: &str) -> Settings { const BROKER_CONFIG_PATH: &str = "../broker.conf";
pub fn read_broker_config() -> Settings {
let config_path = BROKER_CONFIG_PATH;
let mut settings = Settings::default(); let mut settings = Settings::default();
if let Ok(set) = fs::read_to_string(config_path) { if let Ok(set) = fs::read_to_string(config_path) {
let table = Value::from_str(&set) let table = Value::from_str(&set)