Skip to content

Commit

Permalink
fix: make fetch block more solid
Browse files Browse the repository at this point in the history
Sometimes we make a request for a block that do not exist
but we can know before times if this block exist or not.

So we try to make a check with the current blockchain height.

Link: #47
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Sep 11, 2023
1 parent 000fdfb commit c08e0f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
20 changes: 18 additions & 2 deletions folgore-bitcoind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use std::str::FromStr;
use bitcoincore_rpc::bitcoin::consensus::{deserialize, serialize};
use bitcoincore_rpc::bitcoin::Transaction;
use bitcoincore_rpc::bitcoin::Txid;
use bitcoincore_rpc::bitcoincore_rpc_json::EstimateMode;
use bitcoincore_rpc::jsonrpc::serde_json::json;
use bitcoincore_rpc::RpcApi;
use bitcoincore_rpc::{Auth, Client};

use bitcoincore_rpc::bitcoincore_rpc_json::EstimateMode;
use folgore_common::client::fee_estimator::FeeEstimator;
use folgore_common::client::fee_estimator::{FeePriority, FEE_RATES};
use folgore_common::client::FolgoreBackend;
use folgore_common::cln_plugin::types::LogLevel;
use folgore_common::hex;
use folgore_common::prelude::cln_plugin::error;
use folgore_common::prelude::cln_plugin::errors;
Expand Down Expand Up @@ -63,9 +65,23 @@ impl<T: Clone> FolgoreBackend<T> for BitcoinCore {

fn sync_block_by_height(
&self,
_: &mut plugin::Plugin<T>,
plugin: &mut plugin::Plugin<T>,
height: u64,
) -> Result<json::Value, errors::PluginError> {
let current_height = self
.client
.get_block_count()
.map_err(|err| error!("{err}"))?;
if current_height < height {
plugin.log(
LogLevel::Debug,
&format!("requesting block out of best chain. Block height wanted: {height}"),
);
return Ok(json!({
"blockhash": null,
"block": null,
}));
}
let block_header = self
.client
.get_block_hash(height)
Expand Down
39 changes: 28 additions & 11 deletions folgore-esplora/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use clightningrpc_common::json_utils;
use clightningrpc_plugin::error;
use clightningrpc_plugin::errors::PluginError;
use esplora_client::api::{FromHex, Transaction, TxOut, Txid};
use esplora_client::{deserialize, serialize};
use esplora_client::{deserialize, serialize, BlockSummary};
use esplora_client::{BlockingClient, Builder};

use folgore_common::client::FolgoreBackend;
use folgore_common::cln_plugin::types::LogLevel;
use folgore_common::stragegy::RecoveryStrategy;
use folgore_common::utils::ByteBuf;
use folgore_common::utils::{bitcoin_hashes, hex};
Expand Down Expand Up @@ -113,9 +114,14 @@ impl<T: Clone, S: RecoveryStrategy> FolgoreBackend<T> for Esplora<S> {

fn sync_block_by_height(
&self,
_: &mut clightningrpc_plugin::plugin::Plugin<T>,
plugin: &mut clightningrpc_plugin::plugin::Plugin<T>,
height: u64,
) -> Result<serde_json::Value, PluginError> {
let fail_resp = json!({
"blockhash": null,
"block": null,
});

let chain_tip = self.client.get_height().map_err(|err| error!("{err}"))?;
if height > chain_tip.into() {
let resp = json!({
Expand All @@ -124,10 +130,25 @@ impl<T: Clone, S: RecoveryStrategy> FolgoreBackend<T> for Esplora<S> {
});
return Ok(resp);
}
let block = self
.client
.get_blocks(Some(height.try_into().map_err(|err| error!("{err}"))?))
.map_err(from)?;
// Check if the blocks that core lightning wants exist.
let current_height = self
.recovery_strategy
.apply(|| self.client.get_height().map_err(|err| error!("{err}")))
.map_err(|err| error!("{err}"))?;
if current_height < height as u32 {
plugin.log(
LogLevel::Debug,
&format!("requesting block out of best chain. Block height wanted: {height}"),
);
return Ok(fail_resp);
}

// Now that we are sure that the block exist we can requesting it
let block: Vec<BlockSummary> = self.recovery_strategy.apply(|| {
self.client
.get_blocks(Some(height.try_into().expect("height convertion fails")))
.map_err(|err| error!("{err}"))
})?;
let block_hash = block.first().ok_or(error!("block not found"))?;
let block_hash = block_hash.id;

Expand All @@ -144,11 +165,7 @@ impl<T: Clone, S: RecoveryStrategy> FolgoreBackend<T> for Esplora<S> {
return Ok(response);
}
debug!("block not found!");
let resp = json!({
"blockhash": null,
"block": null,
});
Ok(resp)
Ok(fail_resp)
}

fn sync_chain_info(
Expand Down

0 comments on commit c08e0f5

Please sign in to comment.