Skip to content

Commit

Permalink
Simplifies the cln plugin option parsing
Browse files Browse the repository at this point in the history
Options include helper functions to convert them to their proper type after
`cln-plugin=0.1.2`. Use that to reduce the option parsing boilerplate.
  • Loading branch information
sr-gi committed Jan 3, 2023
1 parent 4c9141f commit fba958a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
1 change: 1 addition & 0 deletions watchtower-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 33 additions & 34 deletions watchtower-plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions watchtower-plugin/src/options.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit fba958a

Please sign in to comment.