cln-plugin: Populate the options when we get an init call

This commit is contained in:
Christian Decker 2022-01-21 18:09:16 +01:00 committed by Rusty Russell
parent 249fa8675a
commit fbcb4c33ad
3 changed files with 27 additions and 8 deletions

View File

@ -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),
};
@ -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())
}
}

View File

@ -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)]

View File

@ -24,7 +24,7 @@ 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]
@ -32,9 +32,8 @@ def test_plugin_start(node_factory):
expected = {
'name': 'cln-plugin-startup',
'options': {
'test-option': 42
'test-option': 31337
},
'path': None
}
assert expected == p