From 196f9112be84cc21fd7fc4469d5bf0f690c4fc80 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 29 Jul 2023 14:50:45 +0200 Subject: [PATCH] feat(plugin): use the new plugin macro api of cln4rust Signed-off-by: Vincenzo Palazzo --- Cargo.lock | 35 +++++- folgore-common/Cargo.toml | 2 +- folgore-esplora/Cargo.toml | 4 +- folgore-nakamoto/Cargo.toml | 4 +- folgore-nakamoto/src/lib.rs | 6 +- folgore-plugin/Cargo.toml | 5 +- folgore-plugin/src/plugin.rs | 199 +++++++++++++++-------------------- 7 files changed, 128 insertions(+), 127 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5bb7f47..5cd7261 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,8 +124,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clightningrpc-common" -version = "0.3.0-beta.3" -source = "git+https://github.com/laanwj/cln4rust.git?branch=macros/api#8823c6193e2313615dcaa21149a3400c947b4faf" +version = "0.3.0-beta.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dc4030d1ff7bf3c8705b48c8fd73319a2c7cfccae086399c6b3a6c07026cf5a" dependencies = [ "serde", "serde_json", @@ -133,14 +134,33 @@ dependencies = [ [[package]] name = "clightningrpc-plugin" -version = "0.3.0-beta.7" -source = "git+https://github.com/laanwj/cln4rust.git?branch=macros/api#8823c6193e2313615dcaa21149a3400c947b4faf" +version = "0.3.0-beta.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9807dd1f895fadfc9fe8e9f355cabbccf994d4c7b0e3a921726679faa3e2713d" dependencies = [ "clightningrpc-common", "serde", "serde_json", ] +[[package]] +name = "clightningrpc-plugin-macros" +version = "0.3.0-beta.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef81a4e59115a4ed470ea218793a76fa67d9974e6fdf9b93cb1c693130eb7755" +dependencies = [ + "clightningrpc-plugin", + "convert_case", + "kproc-parser", + "serde_json", +] + +[[package]] +name = "convert_case" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb4a24b1aaf0fd0ce8b45161144d6f42cd91677fd5940fd431183eb023b3a2b8" + [[package]] name = "core-foundation" version = "0.9.3" @@ -300,6 +320,7 @@ version = "0.1.0" dependencies = [ "clightningrpc-common", "clightningrpc-plugin", + "clightningrpc-plugin-macros", "folgore-common", "folgore-esplora", "folgore-nakamoto", @@ -539,6 +560,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "kproc-parser" +version = "0.0.1-beta.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a3c5d81e3af35bb269378a933889f7f45c884bdf820a9c268711035430f3f7" + [[package]] name = "lazy_static" version = "1.4.0" diff --git a/folgore-common/Cargo.toml b/folgore-common/Cargo.toml index 071d29e..76cd79e 100644 --- a/folgore-common/Cargo.toml +++ b/folgore-common/Cargo.toml @@ -4,6 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -clightningrpc-plugin = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } +clightningrpc-plugin = "0.3.0-beta.8" serde_json = "1.0" bitcoin_hashes = "0.12.0" diff --git a/folgore-esplora/Cargo.toml b/folgore-esplora/Cargo.toml index 3cfec16..c003fd6 100644 --- a/folgore-esplora/Cargo.toml +++ b/folgore-esplora/Cargo.toml @@ -7,8 +7,8 @@ edition = "2021" [dependencies] folgore-common = { path = "../folgore-common" } -clightningrpc-common = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } -clightningrpc-plugin = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } +clightningrpc-plugin = "0.3.0-beta.8" +clightningrpc-common = "0.3.0-beta.4" serde_json = "1.0" esplora-client = "0.5.0" log = "0.4.17" diff --git a/folgore-nakamoto/Cargo.toml b/folgore-nakamoto/Cargo.toml index b2d2279..9e495fe 100644 --- a/folgore-nakamoto/Cargo.toml +++ b/folgore-nakamoto/Cargo.toml @@ -10,6 +10,6 @@ nakamoto-net-poll = { git = "https://github.com/vincenzopalazzo/nakamoto", branc nakamoto-p2p = { git = "https://github.com/vincenzopalazzo/nakamoto", branch = "macros/client_model-fixes" } nakamoto-common = { git = "https://github.com/vincenzopalazzo/nakamoto", branch = "macros/client_model-fixes" } folgore-common = { path = "../folgore-common" } -clightningrpc-common = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } -clightningrpc-plugin = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } +clightningrpc-common = "0.3.0-beta.4" +clightningrpc-plugin = "0.3.0-beta.8" serde_json = "1.0" diff --git a/folgore-nakamoto/src/lib.rs b/folgore-nakamoto/src/lib.rs index cc80cb7..76c6792 100644 --- a/folgore-nakamoto/src/lib.rs +++ b/folgore-nakamoto/src/lib.rs @@ -225,7 +225,11 @@ impl FolgoreBackend for Nakamoto { fn sync_get_utxo(&self, _: &mut Plugin, txid: &str, idx: u64) -> Result { let txid = Txid::from_hex(txid).unwrap(); - let Some(utxo) = self.handler.get_utxo(&txid, idx.try_into().unwrap()).map_err(from)? else { + let Some(utxo) = self + .handler + .get_utxo(&txid, idx.try_into().unwrap()) + .map_err(from)? + else { return Ok(json!({ "amount": null, "script": null, diff --git a/folgore-plugin/Cargo.toml b/folgore-plugin/Cargo.toml index 583ae32..ce5e2dc 100644 --- a/folgore-plugin/Cargo.toml +++ b/folgore-plugin/Cargo.toml @@ -4,8 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -clightningrpc-common = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } -clightningrpc-plugin = { git = "https://github.com/laanwj/cln4rust.git", branch = "macros/api" } +clightningrpc-common = "0.3.0-beta.4" +clightningrpc-plugin = "0.3.0-beta.8" +clightningrpc-plugin-macros = "0.3.0-beta.4" folgore-common = { path = "../folgore-common" } folgore-nakamoto = { path = "../folgore-nakamoto" } folgore-esplora = { path = "../folgore-esplora" } diff --git a/folgore-plugin/src/plugin.rs b/folgore-plugin/src/plugin.rs index 22d99b6..137a86d 100644 --- a/folgore-plugin/src/plugin.rs +++ b/folgore-plugin/src/plugin.rs @@ -2,12 +2,15 @@ use std::str::FromStr; use std::sync::Arc; +use clightningrpc_plugin_macros::rpc_method; +use serde_json::{json, Value}; + use clightningrpc_plugin::commands::{types::CLNConf, RPCCommand}; use clightningrpc_plugin::error; use clightningrpc_plugin::errors::PluginError; use clightningrpc_plugin::plugin::Plugin; use clightningrpc_plugin::types::LogLevel; -use serde_json::{json, Value}; +use clightningrpc_plugin_macros::plugin; use folgore_common::client::FolgoreBackend; use folgore_esplora::Esplora; @@ -71,7 +74,21 @@ impl PluginState { } pub fn build_plugin() -> Plugin { - let plugin = Plugin::new(PluginState::new(), false) + let mut plugin = plugin! { + state: PluginState::new(), + dynamic: false, + notification: [], + methods: [ + get_chain_info, + estimate_fees, + get_raw_block_by_height, + getutxout, + send_rawtransaction, + + ], + hooks: [], + }; + plugin .add_opt( "bitcoin-rpcpassword", "string", @@ -94,39 +111,7 @@ pub fn build_plugin() -> Plugin { "A custom esplora backend url where to fetch the bitcoin data", false, ) - .add_rpc_method( - "getchaininfo", - "", - "getchaininfo to fetch information the data from the client", - GetChainInfoRPC {}, - ) - .add_rpc_method( - "estimatefees", - "", - "estimatefees to fetch the feed estimation from the client", - EstimateFeesRPC {}, - ) - .add_rpc_method( - "getrawblockbyheight", - "", - "getrawblockbyheight to fetch the raw block by height", - GetRawBlockByHeightRPC {}, - ) - .add_rpc_method( - "getutxout", - "", - "getutxout to fetch a utx with {txid} and {vout}", - GetUtxOutRPC {}, - ) - .add_rpc_method( - "sendrawtransaction", - "", - "sendrawtransaction to publish a new transaction", - SendRawTransactionRPC {}, - ) .on_init(on_init) - .to_owned(); - plugin } fn on_init(plugin: &mut Plugin) -> Value { @@ -145,93 +130,77 @@ fn on_init(plugin: &mut Plugin) -> Value { json!({}) } -// FIXME use the plugin_macros to semplify all this code -#[derive(Clone)] -struct GetChainInfoRPC {} - -impl RPCCommand for GetChainInfoRPC { - fn call<'c>( - &self, - plugin: &mut Plugin, - request: Value, - ) -> Result { - plugin.log(LogLevel::Debug, "call get chain info"); - let mut plg = plugin.to_owned(); - let client = plg.state.client.as_mut().unwrap(); - plugin.log(LogLevel::Info, &format!("cln request {request}")); - let request: GetChainInfo = serde_json::from_value(request)?; - let result = client.sync_chain_info(plugin, request.height); - plugin.log(LogLevel::Debug, &format!("{:?}", result)); - result - } +#[rpc_method( + rpc_name = "getchaininfo", + description = "getchaininfo to fetch information the data from the client" +)] +fn get_chain_info(plugin: &mut Plugin, request: Value) -> Result { + plugin.log(LogLevel::Debug, "call get chain info"); + let mut plg = plugin.to_owned(); + let client = plg.state.client.as_mut().unwrap(); + plugin.log(LogLevel::Info, &format!("cln request {request}")); + let request: GetChainInfo = serde_json::from_value(request)?; + let result = client.sync_chain_info(plugin, request.height); + plugin.log(LogLevel::Debug, &format!("{:?}", result)); + result } -#[derive(Clone)] -struct EstimateFeesRPC {} - -impl RPCCommand for EstimateFeesRPC { - fn call<'c>(&self, plugin: &mut Plugin, _: Value) -> Result { - plugin.log(LogLevel::Debug, "call estimate fee info"); - let mut plg = plugin.to_owned(); - let client = plg.state.client.as_mut().unwrap(); - let result = client.sync_estimate_fees(plugin); - plugin.log(LogLevel::Debug, &format!("{:?}", result)); - result - } +#[rpc_method( + rpc_name = "estimatefees", + description = "estimatefees to fetch the feed estimation from the client" +)] +fn estimate_fees(plugin: &mut Plugin, _: Value) -> Result { + plugin.log(LogLevel::Debug, "call estimate fee info"); + let mut plg = plugin.to_owned(); + let client = plg.state.client.as_mut().unwrap(); + let result = client.sync_estimate_fees(plugin); + plugin.log(LogLevel::Debug, &format!("{:?}", result)); + result } -#[derive(Clone)] -struct GetRawBlockByHeightRPC {} - -impl RPCCommand for GetRawBlockByHeightRPC { - fn call<'c>( - &self, - plugin: &mut Plugin, - request: Value, - ) -> Result { - plugin.log(LogLevel::Debug, "call get block by height"); - let mut plg = plugin.to_owned(); - let client = plg.state.client.as_mut().unwrap(); - plugin.log(LogLevel::Info, &format!("cln request {request}")); - let request: BlockByHeight = serde_json::from_value(request)?; - client.sync_block_by_height(plugin, request.height) - } +#[rpc_method( + rpc_name = "getrawblockbyheight", + description = "getrawblockbyheight to fetch the raw block by height" +)] +fn get_raw_block_by_height( + plugin: &mut Plugin, + request: Value, +) -> Result { + plugin.log(LogLevel::Debug, "call get block by height"); + let mut plg = plugin.to_owned(); + let client = plg.state.client.as_mut().unwrap(); + plugin.log(LogLevel::Info, &format!("cln request {request}")); + let request: BlockByHeight = serde_json::from_value(request)?; + client.sync_block_by_height(plugin, request.height) } -#[derive(Clone)] -struct GetUtxOutRPC {} - -impl RPCCommand for GetUtxOutRPC { - fn call<'c>( - &self, - plugin: &mut Plugin, - request: Value, - ) -> Result { - plugin.log(LogLevel::Debug, "call get utxo"); - let mut plg = plugin.to_owned(); - let client = plg.state.client.as_mut().unwrap(); - plugin.log(LogLevel::Info, &format!("cln request: {request}")); - let request: GetUTxo = serde_json::from_value(request)?; - let result = client.sync_get_utxo(plugin, &request.txid, request.vout); - plugin.log(LogLevel::Debug, &format!("{:?}", result)); - result - } +#[rpc_method( + rpc_name = "getutxout", + description = "getutxout to fetch a utx with {txid} and {vout}" +)] +fn getutxout(plugin: &mut Plugin, request: Value) -> Result { + plugin.log(LogLevel::Debug, "call get utxo"); + let mut plg = plugin.to_owned(); + let client = plg.state.client.as_mut().unwrap(); + plugin.log(LogLevel::Info, &format!("cln request: {request}")); + let request: GetUTxo = serde_json::from_value(request)?; + let result = client.sync_get_utxo(plugin, &request.txid, request.vout); + plugin.log(LogLevel::Debug, &format!("{:?}", result)); + result } -#[derive(Clone)] -struct SendRawTransactionRPC {} - -impl RPCCommand for SendRawTransactionRPC { - fn call<'c>( - &self, - plugin: &mut Plugin, - request: Value, - ) -> Result { - plugin.log(LogLevel::Debug, "call send raw transaction"); - let mut plg = plugin.to_owned(); - let client = plg.state.client.as_mut().unwrap(); - plugin.log(LogLevel::Info, &format!("cln request: {request}")); - let request: SendRawTx = serde_json::from_value(request)?; - client.sync_send_raw_transaction(plugin, &request.tx, request.allowhighfees) - } +#[rpc_method( + rpc_name = "sendrawtransaction", + description = "sendrawtransaction to publish a new transaction" +)] +fn send_rawtransaction( + plugin: &mut Plugin, + request: Value, +) -> Result { + plugin.log(LogLevel::Debug, "call send raw transaction"); + let mut plg = plugin.to_owned(); + let client = plg.state.client.as_mut().unwrap(); + plugin.log(LogLevel::Info, &format!("cln request: {request}")); + let request: SendRawTx = serde_json::from_value(request)?; + client.sync_send_raw_transaction(plugin, &request.tx, request.allowhighfees) }