cln-plugin: Defer binding the plugin state until after configuring

We had a bit of a chicken-and-egg problem, where we instantiated the
`state` to be managed by the `Plugin` during the very first step when
creating the `Builder`, but then the state might depend on the
configuration we only get later. This would force developers to add
placeholders in the form of `Option` into the state, when really
they'd never be none after configuring.

This defers the binding until after we get the configuration and
cleans up the semantics:

 - `Builder`: declare options, hooks, etc
 - `ConfiguredPlugin`: we have exchanged the handshake with
   `lightningd`, now we can construct the `state` accordingly
 - `Plugin`: Running instance of the plugin

Changelog-Changed: cln-plugin: Moved the state binding to the plugin until after the configuration step
This commit is contained in:
Christian Decker
2022-07-20 16:56:27 +02:00
parent 3f5ff0c148
commit 8898511cf6
4 changed files with 114 additions and 111 deletions

View File

@@ -22,13 +22,7 @@ async fn main() -> Result<()> {
let directory = std::env::current_dir()?;
let (identity, ca_cert) = tls::init(&directory)?;
let state = PluginState {
rpc_path: path.into(),
identity,
ca_cert,
};
let plugin = match Builder::new(state.clone(), tokio::io::stdin(), tokio::io::stdout())
let plugin = match Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new(
"grpc-port",
options::Value::Integer(-1),
@@ -54,7 +48,13 @@ async fn main() -> Result<()> {
Some(o) => return Err(anyhow!("grpc-port is not a valid integer: {:?}", o)),
};
let plugin = plugin.start().await?;
let state = PluginState {
rpc_path: path.into(),
identity,
ca_cert,
};
let plugin = plugin.start(state.clone()).await?;
let bind_addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap();