Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/floresta-electrum/src/electrum_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ mod test {
max_inflight: 20,
assume_utreexo: None,
backfill: false,
filter_start_height: None,
};

let chain_provider: UtreexoNode<RunningNode, Arc<ChainState<KvChainStore>>> =
Expand Down
10 changes: 10 additions & 0 deletions crates/floresta-wire/src/p2p_wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ pub struct UtreexoNodeConfig {
/// is that we are vulnerable to a fraud proof attack for a few hours, but we can spot it
/// and react in a couple of hours at most, so the attack window is very small.
pub backfill: bool,
/// If we are using network-provided block filters, we may not need to download the whole
/// chain of filters, as our wallets may not have been created at the beginning of the chain.
/// With this option, we can make a rough estimate of the block height we need to start
/// and only download the filters from that height.
///
/// If the value is negative, it's relative to the current tip. For example, if the current
/// tip is at height 1000, and we set this value to -100, we will start downloading filters
/// from height 900.
pub filter_start_height: Option<i32>,
}

impl Default for UtreexoNodeConfig {
Expand All @@ -71,6 +80,7 @@ impl Default for UtreexoNodeConfig {
proxy: None,
backfill: false,
assume_utreexo: None,
filter_start_height: None,
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions crates/floresta-wire/src/p2p_wire/running_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,21 @@ where
return Ok(());
};

let height = filters.get_height()?;
let mut height = filters.get_height()?;
let best_height = self.chain.get_height()?;

if height == 0 {
let user_height = self.config.filter_start_height.unwrap_or(0);

height = if user_height < 0 {
best_height.saturating_sub(user_height.unsigned_abs())
} else {
user_height as u32
};

filters.save_height(height)?;
}

if height >= best_height {
return Ok(());
}
Expand Down Expand Up @@ -812,14 +824,12 @@ where
debug!("Got a block filter for block {hash} from peer {peer}");

if let Some(filters) = self.block_filters.as_ref() {
filters.push_filter(filter)?;

let current_height = filters.get_height()?;
let Some(this_height) = self.chain.get_block_height(&hash)? else {
warn!("Filter for block {} received, but we don't have it", hash);
return Ok(());
};
filters.save_height(current_height + 1)?;

// we expect to receive them in order
if current_height + 1 != this_height {
warn!(
Expand All @@ -831,6 +841,9 @@ where
return Ok(());
}

filters.save_height(current_height + 1)?;
filters.push_filter(filter)?;

if self.last_filter == hash {
self.inflight.remove(&InflightRequests::GetFilters);
self.download_filters().await?;
Expand Down
3 changes: 3 additions & 0 deletions florestad/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,8 @@ pub enum Commands {
rpc_address: Option<String>,
#[arg(long)]
electrum_address: Option<String>,
#[arg(long)]
/// Download block filters starting at this height. Negative numbers are relative to the current tip.
filters_start_height: Option<i32>,
},
}
9 changes: 9 additions & 0 deletions florestad/src/florestad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ pub struct Config {
/// is very inefficient and resource/time consuming. But keep in mind that filters will take
/// up disk space.
pub cfilters: bool,
/// If we are using block filters, we may not need to download the whole chain of filters, as
/// our wallets may not have been created at the beginning of the chain. With this option, we
/// can make a rough estimate of the block height we need to start downloading filters.
///
/// If the value is negative, it's relative to the current tip. For example, if the current tip
/// is at height 1000, and we set this value to -100, we will start downloading filters from
/// height 900.
pub filters_start_height: Option<i32>,
#[cfg(feature = "zmq-server")]
/// The address to listen to for our ZMQ server
///
Expand Down Expand Up @@ -335,6 +343,7 @@ impl Florestad {
max_inflight: 20,
assume_utreexo,
backfill: false,
filter_start_height: self.config.filters_start_height,
};

// Chain Provider (p2p)
Expand Down
2 changes: 2 additions & 0 deletions florestad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async fn main() {
connect,
rpc_address,
electrum_address,
filters_start_height,
}) => Config {
debug: params.debug,
data_dir,
Expand All @@ -65,6 +66,7 @@ async fn main() {
log_to_file: true,
log_to_stdout: true,
assume_utreexo: true,
filters_start_height,
},

// We may have more commands here, like setup and dump wallet
Expand Down