diff --git a/watchtower-plugin/src/lib.rs b/watchtower-plugin/src/lib.rs index a3cfd1ff..db3a70a2 100755 --- a/watchtower-plugin/src/lib.rs +++ b/watchtower-plugin/src/lib.rs @@ -11,6 +11,7 @@ use teos_common::TowerId; pub mod convert; pub mod dbm; pub mod net; +pub mod options; pub mod retrier; mod ser; pub mod wt_client; diff --git a/watchtower-plugin/src/main.rs b/watchtower-plugin/src/main.rs index cab83fe6..17a0a58d 100755 --- a/watchtower-plugin/src/main.rs +++ b/watchtower-plugin/src/main.rs @@ -24,7 +24,7 @@ use watchtower_plugin::net::http::{ }; use watchtower_plugin::retrier::RetryManager; use watchtower_plugin::wt_client::WTClient; -use watchtower_plugin::TowerStatus; +use watchtower_plugin::{options, TowerStatus}; fn to_cln_error(e: RequestError) -> Error { let e = match e { @@ -55,16 +55,9 @@ async fn register( // TODO: The user should pick the start_time or, at least, check the returned start time against it's known block height. // Otherwise the tower could just generate a subscription starting far in the future. For this we need to access lightning RPC // which is not available in the current version of `cln-plugin` (but already on master). Add it for the next release. - - // FIXME: This is a workaround. Ideally, `cln_plugin::options::Value` will implement `as_u64` so we can simply call and unwrap - // given that we are certain the option exists. let port = params.port.unwrap_or( - if let Value::Integer(x) = plugin.option("watchtower-port").unwrap() { - x as u16 - } else { - // We will never end up here, but we need to define an else. Should be fixed alongside the previous fixme. - 9814 - }, + u16::try_from(plugin.option(options::WT_PORT).unwrap().as_i64().unwrap()) + .map_err(|_| anyhow!("{} out of range", options::WT_PORT))?, ); let tower_net_addr = { @@ -542,18 +535,18 @@ async fn main() -> Result<(), Error> { let builder = Builder::new(stdin(), stdout()) .option(ConfigOption::new( - "watchtower-port", - Value::Integer(9814), + options::WT_PORT, + Value::Integer(options::DEFAULT_WT_PORT), "tower API port", )) .option(ConfigOption::new( - "watchtower-max-retry-time", - Value::Integer(900), + options::WT_MAX_RETRY_TIME, + Value::Integer(options::DEFAULT_WT_MAX_RETRY_TIME), "the time (in seconds) after where the retrier will give up trying to send data to a temporary unreachable tower", )) .option(ConfigOption::new( - "dev-watchtower-max-retry-interval", - Value::Integer(60), + options::DEV_WT_MAX_RETRY_INTERVAL, + Value::Integer(options::DEFAULT_DEV_WT_MAX_RETRY_INTERVAL), "the maximum time (in seconds) for a retrier wait interval", )) .rpcmethod( @@ -615,24 +608,30 @@ async fn main() -> Result<(), Error> { ) .await, )); - // FIXME: This is a workaround. Ideally, `cln_plugin::options::Value` will implement `as_u64` so we can simply call and unwrap - // given that we are certain the option exists. - let max_elapsed_time = - if let Value::Integer(x) = midstate.option("watchtower-max-retry-time").unwrap() { - x as u16 - } else { - // We will never end up here, but we need to define an else. Should be fixed alongside the previous fixme. - 900 - }; - let max_interval_time = if let Value::Integer(x) = midstate - .option("dev-watchtower-max-retry-interval") - .unwrap() - { - x as u16 - } else { - // We will never end up here, but we need to define an else. Should be fixed alongside the previous fixme. - 60 - }; + + let max_elapsed_time = u16::try_from( + midstate + .option(options::WT_MAX_RETRY_TIME) + .unwrap() + .as_i64() + .unwrap(), + ) + .map_err(|e| { + log::error!("{} out of range", options::WT_MAX_RETRY_TIME); + e + })?; + + let max_interval_time = u16::try_from( + midstate + .option(options::DEV_WT_MAX_RETRY_INTERVAL) + .unwrap() + .as_i64() + .unwrap(), + ) + .map_err(|e| { + log::error!("{} out of range", options::DEV_WT_MAX_RETRY_INTERVAL); + e + })?; let plugin = midstate.start(wt_client.clone()).await?; tokio::spawn(async move { diff --git a/watchtower-plugin/src/options.rs b/watchtower-plugin/src/options.rs new file mode 100644 index 00000000..771bdf07 --- /dev/null +++ b/watchtower-plugin/src/options.rs @@ -0,0 +1,8 @@ +/// Collections of plugin option names and default values + +pub const WT_PORT: &str = "watchtower-port"; +pub const DEFAULT_WT_PORT: i64 = 9814; +pub const WT_MAX_RETRY_TIME: &str = "watchtower-max-retry-time"; +pub const DEFAULT_WT_MAX_RETRY_TIME: i64 = 900; +pub const DEV_WT_MAX_RETRY_INTERVAL: &str = "dev-watchtower-max-retry-interval"; +pub const DEFAULT_DEV_WT_MAX_RETRY_INTERVAL: i64 = 60;