This commit is contained in:
decentclock
2022-08-26 17:35:55 +00:00
parent 53ea8e365b
commit cd465bf084
4 changed files with 47 additions and 43 deletions

View File

@@ -10,10 +10,8 @@ use crate::chain_tracker::MqttSignerPort;
use crate::mqtt::start_broker; use crate::mqtt::start_broker;
use crate::unix_fd::SignerLoop; use crate::unix_fd::SignerLoop;
use crate::util::read_broker_config; use crate::util::read_broker_config;
use bitcoin::Network;
use clap::{App, AppSettings, Arg}; use clap::{App, AppSettings, Arg};
use std::env; use std::env;
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use url::Url; use url::Url;
@@ -75,11 +73,10 @@ fn main() -> anyhow::Result<()> {
} }
let settings = read_broker_config(BROKER_CONFIG_PATH); let settings = read_broker_config(BROKER_CONFIG_PATH);
let network = Network::from_str(settings["network"].as_str().unwrap()).unwrap();
let (tx, rx) = mpsc::channel(1000); let (tx, rx) = mpsc::channel(1000);
let (status_tx, mut status_rx) = mpsc::channel(1000); let (status_tx, mut status_rx) = mpsc::channel(1000);
log::info!("=> start broker on network: {}", network); log::info!("=> start broker on network: {}", settings.network);
let runtime = start_broker(rx, status_tx, "sphinx-1", &settings); let runtime = start_broker(rx, status_tx, "sphinx-1", &settings);
log::info!("=> wait for connected status"); log::info!("=> wait for connected status");
// wait for connection = true // wait for connection = true
@@ -93,7 +90,7 @@ fn main() -> anyhow::Result<()> {
let frontend = Frontend::new( let frontend = Frontend::new(
Arc::new(SignerPortFront { Arc::new(SignerPortFront {
signer_port: Box::new(signer_port), signer_port: Box::new(signer_port),
network, network: settings.network,
}), }),
Url::parse(&btc_url).expect("malformed btc rpc url"), Url::parse(&btc_url).expect("malformed btc rpc url"),
); );

View File

@@ -1,3 +1,4 @@
use crate::util::Settings;
use crate::{ChannelReply, ChannelRequest}; use crate::{ChannelReply, ChannelRequest};
use librumqttd::{ use librumqttd::{
async_locallink, async_locallink,
@@ -11,7 +12,6 @@ use std::thread;
use std::time::Duration; use std::time::Duration;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::time::timeout; use tokio::time::timeout;
use toml::Value;
const SUB_TOPIC: &str = "sphinx-return"; const SUB_TOPIC: &str = "sphinx-return";
const PUB_TOPIC: &str = "sphinx"; const PUB_TOPIC: &str = "sphinx";
@@ -32,7 +32,7 @@ pub fn start_broker(
mut receiver: mpsc::Receiver<ChannelRequest>, mut receiver: mpsc::Receiver<ChannelRequest>,
status_sender: mpsc::Sender<bool>, status_sender: mpsc::Sender<bool>,
expected_client_id: &str, expected_client_id: &str,
settings: &Value, settings: &Settings,
) -> tokio::runtime::Runtime { ) -> tokio::runtime::Runtime {
let config = config(settings); let config = config(settings);
let client_id = expected_client_id.to_string(); let client_id = expected_client_id.to_string();
@@ -148,7 +148,7 @@ fn metrics_to_status(metrics: ConnectionMetrics, client_connected: bool) -> Opti
} }
} }
fn config(settings: &Value) -> Config { fn config(settings: &Settings) -> Config {
use librumqttd::rumqttlog::Config as RouterConfig; use librumqttd::rumqttlog::Config as RouterConfig;
use librumqttd::{ use librumqttd::{
ConnectionLoginCredentials, ConnectionSettings, ConsoleSettings, ServerSettings, ConnectionLoginCredentials, ConnectionSettings, ConsoleSettings, ServerSettings,
@@ -169,11 +169,7 @@ fn config(settings: &Value) -> Config {
id.to_string(), id.to_string(),
ServerSettings { ServerSettings {
cert: None, cert: None,
listen: SocketAddrV4::new( listen: SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), settings.port).into(),
Ipv4Addr::new(0, 0, 0, 0),
settings["port"].as_integer().unwrap().try_into().unwrap(),
)
.into(),
next_connection_delay_ms: 1, next_connection_delay_ms: 1,
connections: ConnectionSettings { connections: ConnectionSettings {
connection_timeout_ms: 5000, connection_timeout_ms: 5000,

View File

@@ -1,9 +1,8 @@
use crate::mqtt::start_broker; use crate::mqtt::start_broker;
use crate::util::Settings;
use crate::ChannelRequest; use crate::ChannelRequest;
use sphinx_key_parser as parser; use sphinx_key_parser as parser;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use toml::map::Map;
use toml::Value;
use vls_protocol::serde_bolt::WireString; use vls_protocol::serde_bolt::WireString;
use vls_protocol::{msgs, msgs::Message}; use vls_protocol::{msgs, msgs::Message};
@@ -15,10 +14,7 @@ pub fn run_test() {
let mut id = 0u16; let mut id = 0u16;
let mut sequence = 1; let mut sequence = 1;
let mut map = Map::new(); let settings = Settings::default();
map.insert("port".to_string(), Value::Integer(1883));
map.insert("network".to_string(), Value::String("regtest".to_string()));
let settings = Value::Table(map);
let (tx, rx) = mpsc::channel(1000); let (tx, rx) = mpsc::channel(1000);
let (status_tx, mut status_rx) = mpsc::channel(1000); let (status_tx, mut status_rx) = mpsc::channel(1000);

View File

@@ -1,21 +1,40 @@
use bitcoin::Network;
use std::default::Default;
use std::env; use std::env;
use std::fs; use std::fs;
use std::str::FromStr; use std::str::FromStr;
use toml::map::Map;
use toml::Value; use toml::Value;
pub fn read_broker_config(config_path: &str) -> Value { pub struct Settings {
let mut ret = Value::Table(Map::new()); pub network: Network,
pub port: u16,
}
impl Default for Settings {
fn default() -> Self {
Settings {
network: Network::Regtest,
port: 1883,
}
}
}
pub fn read_broker_config(config_path: &str) -> Settings {
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) {
ret = Value::from_str(&set) let table = Value::from_str(&set)
.expect("Couldn't read broker.conf make sure it follows the toml format"); .expect("Couldn't read broker.conf make sure it follows the toml format");
log::info!("Read broker.conf"); log::info!("Read broker.conf");
if let Some(network) = read_network_setting(&table) {
settings.network = network
}
if let Some(port) = read_port_setting(&table) {
settings.port = port
}
} else { } else {
log::info!("File broker.conf not found, using default settings"); log::info!("File broker.conf not found, using default settings");
} }
validate_network_setting(&mut ret); settings
validate_port_setting(&mut ret);
ret
} }
pub fn setup_logging(who: &str, level_arg: &str) { pub fn setup_logging(who: &str, level_arg: &str) {
@@ -50,36 +69,31 @@ pub fn setup_logging(who: &str, level_arg: &str) {
.expect("log config"); .expect("log config");
} }
fn validate_network_setting(settings: &mut Value) { fn read_network_setting(table: &Value) -> Option<Network> {
if let None = settings.get("network") { if let None = table.get("network") {
log::info!("Network not specified, setting to default regtest"); log::info!("Network not specified, setting to default regtest");
settings None
.as_table_mut()
.unwrap()
.insert("network".to_string(), Value::String("regtest".to_string()));
} else { } else {
if !settings["network"].is_str() if !table["network"].is_str()
|| settings["network"].as_str().unwrap() != "bitcoin" || table["network"].as_str().unwrap() != "bitcoin"
&& settings["network"].as_str().unwrap() != "regtest" && table["network"].as_str().unwrap() != "regtest"
{ {
panic!("The network must be set to either 'bitcoin' or 'regtest'"); panic!("The network must be set to either 'bitcoin' or 'regtest'");
} }
log::info!( log::info!(
"Read network setting: {}", "Read network setting: {}",
settings["network"].as_str().unwrap() table["network"].as_str().unwrap()
); );
Some(Network::from_str(table["network"].as_str().unwrap()).unwrap())
} }
} }
fn validate_port_setting(settings: &mut Value) { fn read_port_setting(table: &Value) -> Option<u16> {
if let None = settings.get("port") { if let None = table.get("port") {
log::info!("Broker port not specified, setting to default 1883"); log::info!("Broker port not specified, setting to default 1883");
settings None
.as_table_mut()
.unwrap()
.insert("port".to_string(), Value::Integer(1883));
} else { } else {
let temp = settings["port"] let temp = table["port"]
.as_integer() .as_integer()
.expect("The port number is not an integer greater than 1023"); .expect("The port number is not an integer greater than 1023");
if temp <= 1023 { if temp <= 1023 {
@@ -89,5 +103,6 @@ fn validate_port_setting(settings: &mut Value) {
panic!("The port number is way too big!") panic!("The port number is way too big!")
} }
log::info!("Read broker port setting: {}", temp); log::info!("Read broker port setting: {}", temp);
Some(temp.try_into().unwrap())
} }
} }