cln-plugin: Add notification subscriptions and hooks to the plugins

For now hooks are treated identically to rpcmethods, with the
exception of not being returned in the `getmanifest` call. Later on we
can add typed handlers as well.
This commit is contained in:
Christian Decker
2022-02-23 19:00:25 +01:00
committed by Rusty Russell
parent 8c6af21169
commit 60e773239c
4 changed files with 141 additions and 75 deletions

View File

@@ -14,6 +14,8 @@ async fn main() -> Result<(), anyhow::Error> {
"a test-option with default 42",
))
.rpcmethod("testmethod", "This is a test", Box::new(testmethod))
.subscribe("connect", Box::new(connect_handler))
.hook("peer_connected", Box::new(peer_connected_handler))
.start()
.await?;
plugin.join().await
@@ -22,3 +24,13 @@ async fn main() -> Result<(), anyhow::Error> {
fn testmethod(_p: Plugin<()>, _v: &serde_json::Value) -> Result<serde_json::Value, Error> {
Ok(json!("Hello"))
}
fn connect_handler(_p: Plugin<()>, v: &serde_json::Value) -> Result<(), Error> {
log::info!("Got a connect notification: {}", v);
Ok(())
}
fn peer_connected_handler(_p: Plugin<()>, v: &serde_json::Value) -> Result<serde_json::Value, Error> {
log::info!("Got a connect hook call: {}", v);
Ok(json!({"result": "continue"}))
}