Skip to content

Commit

Permalink
feat(plugin): use the new plugin macro api of cln4rust
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jul 29, 2023
1 parent 8efbac6 commit 196f911
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 127 deletions.
35 changes: 31 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion folgore-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions folgore-esplora/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions folgore-nakamoto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 5 additions & 1 deletion folgore-nakamoto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ impl<T: Clone> FolgoreBackend<T> for Nakamoto {

fn sync_get_utxo(&self, _: &mut Plugin<T>, txid: &str, idx: u64) -> Result<Value, PluginError> {
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,
Expand Down
5 changes: 3 additions & 2 deletions folgore-plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
199 changes: 84 additions & 115 deletions folgore-plugin/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +74,21 @@ impl PluginState {
}

pub fn build_plugin() -> Plugin<PluginState> {
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",
Expand All @@ -94,39 +111,7 @@ pub fn build_plugin() -> Plugin<PluginState> {
"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<PluginState>) -> Value {
Expand All @@ -145,93 +130,77 @@ fn on_init(plugin: &mut Plugin<PluginState>) -> Value {
json!({})
}

// FIXME use the plugin_macros to semplify all this code
#[derive(Clone)]
struct GetChainInfoRPC {}

impl RPCCommand<PluginState> for GetChainInfoRPC {
fn call<'c>(
&self,
plugin: &mut Plugin<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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<PluginState>, request: Value) -> Result<Value, PluginError> {
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<PluginState> for EstimateFeesRPC {
fn call<'c>(&self, plugin: &mut Plugin<PluginState>, _: Value) -> Result<Value, PluginError> {
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<PluginState>, _: Value) -> Result<Value, PluginError> {
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<PluginState> for GetRawBlockByHeightRPC {
fn call<'c>(
&self,
plugin: &mut Plugin<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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<PluginState> for GetUtxOutRPC {
fn call<'c>(
&self,
plugin: &mut Plugin<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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<PluginState>, request: Value) -> Result<Value, PluginError> {
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<PluginState> for SendRawTransactionRPC {
fn call<'c>(
&self,
plugin: &mut Plugin<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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)
}

0 comments on commit 196f911

Please sign in to comment.