From 91091a3270766da62afc76e9113ac79439a3d9f9 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 21 Jan 2022 18:09:16 +0100 Subject: [PATCH] cln-plugin: Populate the options when we get an `init` call --- plugins/src/lib.rs | 25 +++++++++++++++++++++++-- plugins/src/messages.rs | 5 ++--- tests/test_cln_rs.py | 5 ++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index feb1833f71db..2d6382454809 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -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::::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>, ) -> Result { + 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()) } } diff --git a/plugins/src/messages.rs b/plugins/src/messages.rs index 540019b6d4e3..b966446dc680 100644 --- a/plugins/src/messages.rs +++ b/plugins/src/messages.rs @@ -62,9 +62,8 @@ pub(crate) enum Notification { pub struct GetManifestCall {} #[derive(Deserialize, Debug)] -pub struct InitCall { - pub options: Value, - pub configuration: HashMap, +pub(crate) struct InitCall { + pub(crate) options: HashMap, } #[derive(Debug)] diff --git a/tests/test_cln_rs.py b/tests/test_cln_rs.py index 93740d69ed7f..4cbcca0f8d86 100644 --- a/tests/test_cln_rs.py +++ b/tests/test_cln_rs.py @@ -25,7 +25,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] @@ -33,9 +33,8 @@ def test_plugin_start(node_factory): expected = { 'name': 'cln-plugin-startup', 'options': { - 'test-option': 42 + 'test-option': 31337 }, 'path': None } assert expected == p -