Skip to content

Commit

Permalink
feat(nakamoto): support the getchaininfo by height
Browse files Browse the repository at this point in the history
This is build on top of this RFC from core lightning
ElementsProject/lightning#6181

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 15, 2023
1 parent 53ce888 commit d092532
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion folgore-common/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait FolgoreBackend<T: Clone> {
/// - `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<T>) -> Result<Value, PluginError>;
fn sync_chain_info(&self, _: &mut Plugin<T>, _: Option<u64>) -> Result<Value, PluginError>;

/// Polled by lightningd to get the current feerate, all values must
/// be passed in sat/kVB.
Expand Down
1 change: 1 addition & 0 deletions folgore-esplora/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl<T: Clone> FolgoreBackend<T> for Esplora {
fn sync_chain_info(
&self,
_: &mut clightningrpc_plugin::plugin::Plugin<T>,
_: Option<u64>,
) -> Result<serde_json::Value, PluginError> {
let current_height = self.client.get_height().map_err(from)?;
info!("blockchain height: {current_height}");
Expand Down
27 changes: 22 additions & 5 deletions folgore-nakamoto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,7 +94,7 @@ fn from<T: Display>(err: T) -> PluginError {
impl<T: Clone> FolgoreBackend<T> for Nakamoto {
fn sync_block_by_height(&self, _p: &mut Plugin<T>, height: u64) -> Result<Value, PluginError> {
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!({
Expand Down Expand Up @@ -121,9 +122,26 @@ impl<T: Clone> FolgoreBackend<T> for Nakamoto {
Ok(response)
}

fn sync_chain_info(&self, _: &mut Plugin<T>) -> Result<Value, PluginError> {
fn sync_chain_info(
&self,
plugin: &mut Plugin<T>,
known_height: Option<u64>,
) -> Result<Value, PluginError> {
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);
Expand All @@ -135,8 +153,7 @@ impl<T: Clone> FolgoreBackend<T> 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}")),
Expand Down
5 changes: 5 additions & 0 deletions folgore-plugin/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
}
18 changes: 13 additions & 5 deletions folgore-plugin/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -150,11 +150,17 @@ fn on_init(plugin: &mut Plugin<PluginState>) -> Value {
struct GetChainInfoRPC {}

impl RPCCommand<PluginState> for GetChainInfoRPC {
fn call<'c>(&self, plugin: &mut Plugin<PluginState>, _: Value) -> Result<Value, PluginError> {
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();
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
}
Expand Down Expand Up @@ -183,12 +189,14 @@ impl RPCCommand<PluginState> for GetRawBlockByHeightRPC {
plugin: &mut Plugin<PluginState>,
request: Value,
) -> Result<Value, PluginError> {
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
}
}

Expand Down

0 comments on commit d092532

Please sign in to comment.