From d0925323da200fc171eb5984a94c3be6a5a44777 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 15 Apr 2023 17:28:14 +0200 Subject: [PATCH] feat(nakamoto): support the getchaininfo by height This is build on top of this RFC from core lightning https://github.com/ElementsProject/lightning/pull/6181 Signed-off-by: Vincenzo Palazzo --- folgore-common/src/client/mod.rs | 2 +- folgore-esplora/src/lib.rs | 1 + folgore-nakamoto/src/lib.rs | 27 ++++++++++++++++++++++----- folgore-plugin/src/model.rs | 5 +++++ folgore-plugin/src/plugin.rs | 18 +++++++++++++----- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/folgore-common/src/client/mod.rs b/folgore-common/src/client/mod.rs index 977ba24..9eb4137 100644 --- a/folgore-common/src/client/mod.rs +++ b/folgore-common/src/client/mod.rs @@ -11,7 +11,7 @@ pub trait FolgoreBackend { /// - `headercount` (number), the number of fetched block headers /// - `blockcount` (number), the number of fetched block body /// - `ibd` (bool), whether the backend is performing initial block download - fn sync_chain_info(&self, _: &mut Plugin) -> Result; + fn sync_chain_info(&self, _: &mut Plugin, _: Option) -> Result; /// Polled by lightningd to get the current feerate, all values must /// be passed in sat/kVB. diff --git a/folgore-esplora/src/lib.rs b/folgore-esplora/src/lib.rs index 4d9b87c..7143826 100644 --- a/folgore-esplora/src/lib.rs +++ b/folgore-esplora/src/lib.rs @@ -150,6 +150,7 @@ impl FolgoreBackend for Esplora { fn sync_chain_info( &self, _: &mut clightningrpc_plugin::plugin::Plugin, + _: Option, ) -> Result { let current_height = self.client.get_height().map_err(from)?; info!("blockchain height: {current_height}"); diff --git a/folgore-nakamoto/src/lib.rs b/folgore-nakamoto/src/lib.rs index 8ce9307..265fcbc 100644 --- a/folgore-nakamoto/src/lib.rs +++ b/folgore-nakamoto/src/lib.rs @@ -4,6 +4,7 @@ use std::fmt::Display; use std::net::TcpStream; use std::thread::JoinHandle; +use clightningrpc_plugin::types::LogLevel; use serde_json::{json, Value}; use clightningrpc_common::json_utils; @@ -93,7 +94,7 @@ fn from(err: T) -> PluginError { impl FolgoreBackend for Nakamoto { fn sync_block_by_height(&self, _p: &mut Plugin, height: u64) -> Result { let mut response = json_utils::init_payload(); - let header = self.handler.get_block_by_height(height).unwrap(); + let header = self.handler.get_block_by_height(height).map_err(from)?; let blk_chan = self.handler.blocks(); if let None = header { return Ok(json!({ @@ -121,9 +122,26 @@ impl FolgoreBackend for Nakamoto { Ok(response) } - fn sync_chain_info(&self, _: &mut Plugin) -> Result { + fn sync_chain_info( + &self, + plugin: &mut Plugin, + known_height: Option, + ) -> Result { match self.handler.get_tip() { - Ok(Tip { height, .. }) => { + Ok(Tip { mut height, .. }) => { + let mut is_sync = true; + if Some(height) <= known_height { + while let Err(err) = self.handler.wait_for_height(known_height.unwrap()) { + plugin.log(LogLevel::Info, &format!("Waiting for block {height}....")); + plugin.log( + LogLevel::Debug, + &format!("while waiting the block we get an error {err}"), + ) + } + height = known_height.unwrap(); + } else { + is_sync = false; + } let mut resp = json_utils::init_payload(); let height: i64 = height.try_into().unwrap(); json_utils::add_number(&mut resp, "headercount", height); @@ -135,8 +153,7 @@ impl FolgoreBackend for Nakamoto { Network::Signet => "signet", }; json_utils::add_str(&mut resp, "chain", network); - // FIXME: need to be supported - json_utils::add_bool(&mut resp, "ibd", false); + json_utils::add_bool(&mut resp, "ibd", is_sync); Ok(resp) } Err(err) => Err(error!("{err}")), diff --git a/folgore-plugin/src/model.rs b/folgore-plugin/src/model.rs index 9b8ee9e..74944bc 100644 --- a/folgore-plugin/src/model.rs +++ b/folgore-plugin/src/model.rs @@ -17,3 +17,8 @@ pub(crate) struct SendRawTx { pub(crate) tx: String, pub(crate) allowhighfees: bool, } + +#[derive(Deserialize, Serialize)] +pub(crate) struct GetChainInfo { + pub(crate) height: Option, +} diff --git a/folgore-plugin/src/plugin.rs b/folgore-plugin/src/plugin.rs index 59e63ed..daf20ea 100644 --- a/folgore-plugin/src/plugin.rs +++ b/folgore-plugin/src/plugin.rs @@ -13,7 +13,7 @@ use folgore_common::client::FolgoreBackend; use folgore_esplora::Esplora; use folgore_nakamoto::{Config, Nakamoto, Network}; -use crate::model::{BlockByHeight, GetUTxo, SendRawTx}; +use crate::model::{BlockByHeight, GetChainInfo, GetUTxo, SendRawTx}; pub(crate) enum ClientType { Nakamoto, @@ -150,11 +150,17 @@ fn on_init(plugin: &mut Plugin) -> Value { struct GetChainInfoRPC {} impl RPCCommand for GetChainInfoRPC { - fn call<'c>(&self, plugin: &mut Plugin, _: Value) -> Result { + 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(); - let result = client.sync_chain_info(plugin); + 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 } @@ -183,12 +189,14 @@ impl RPCCommand for GetRawBlockByHeightRPC { plugin: &mut Plugin, request: Value, ) -> Result { - plugin.log(LogLevel::Debug, "call get chain info"); + 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) + let result = client.sync_block_by_height(plugin, request.height); + plugin.log(LogLevel::Debug, &format!("cln response {:?}", result)); + result } }