Skip to content

Commit

Permalink
cln-plugin: Populate the options when we get an init call
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker committed Feb 7, 2022
1 parent 2b9ad4a commit 1723cb2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
25 changes: 23 additions & 2 deletions plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
serde_json::to_value(self.handle_get_manifest(c, state).await?).unwrap()
}
messages::Request::Init(c) => {
serde_json::to_value(Plugin::<S, I, O>::handle_init(c, state).await?).unwrap()
serde_json::to_value(self.handle_init(c, state).await?).unwrap()
}
o => panic!("Request {:?} is currently unhandled", o),
};
Expand Down Expand Up @@ -196,9 +196,30 @@ where
}

async fn handle_init(
_call: messages::InitCall,
&mut self,
call: messages::InitCall,
_state: Arc<Mutex<S>>,
) -> Result<messages::InitResponse, Error> {
use options::Value as OValue;
use serde_json::Value as JValue;

// Match up the ConfigOptions and fill in their values if we
// have a matching entry.

for opt in self.options.iter_mut() {
if let Some(val) = call.options.get(opt.name()) {
opt.value = Some(match (opt.default(), &val) {
(OValue::String(_), JValue::String(s)) => OValue::String(s.clone()),
(OValue::Integer(_), JValue::Number(n)) => OValue::Integer(n.as_i64().unwrap()),
(OValue::Boolean(_), JValue::Bool(n)) => OValue::Boolean(*n),

// It's ok to panic, if we get here c-lightning
// has not enforced the option type.
(_, _) => panic!("Mismatching types in options: {:?} != {:?}", opt, val),
});
}
}

Ok(messages::InitResponse::default())
}
}
Expand Down
5 changes: 2 additions & 3 deletions plugins/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ pub(crate) enum Notification {
pub struct GetManifestCall {}

#[derive(Deserialize, Debug)]
pub struct InitCall {
pub options: Value,
pub configuration: HashMap<String, Value>,
pub(crate) struct InitCall {
pub(crate) options: HashMap<String, Value>,
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cln_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def test_plugin_start(node_factory):
"""Start a minimal plugin and ensure it is well-behaved
"""
bin_path = Path.cwd() / "target" / "debug" / "examples" / "cln-plugin-startup"
l1 = node_factory.get_node(options={"plugin": str(bin_path)})
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'test-option': 31337})

cfg = l1.rpc.listconfigs()
p = cfg['plugins'][0]
p['path'] = None # The path is host-specific, so blank it.
expected = {
'name': 'cln-plugin-startup',
'options': {
'test-option': 42
'test-option': 31337
},
'path': None
}
Expand Down

0 comments on commit 1723cb2

Please sign in to comment.