Skip to content

Commit

Permalink
florestad: Add lib.rs (#124)
Browse files Browse the repository at this point in the history
* florestad: Add lib.rs

This commit introduces a lib.rs for florestad. With this change, other softwares
can spawn a florestad process and run on the background.
  • Loading branch information
Davidson-Souza authored Apr 30, 2024
1 parent 081adfb commit 2a81f19
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 502 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedSta

let mut guard = write_lock!(self);
guard.best_block.validation_index = assumed_hash;
guard.best_block.rescan_index = None;
info!("assuming chain with hash={assumed_hash}");
guard.acc = acc;

Expand Down
12 changes: 4 additions & 8 deletions crates/floresta-chain/src/pruned_utreexo/partial_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
//!
//! - Shared ownership is forbidden: if you have two threads or tasks owning this, you'll have
//! data race. If you want to hold shared ownership for this module, you need to place a
//! [PartialChainState] inside an `Arc<Mutex>` yourself. Note that you can't just Arc this,
//! because [PartialChainState] isn't [Sync].
//! [PartialChainState] inside an `Arc<Mutex>` yourself. Don't just Arc this and expect it to
//! work, as you are garanteed to have data races.
//! - The interior is toxic, so no peeking: no references, mutable or not, to any field should
//! leak through the API, as we are not enforcing lifetime or borrowing rules at compile time.
//! - Sending is fine: There's nothing in this module that makes it not sendable to between
Expand Down Expand Up @@ -84,7 +84,7 @@ pub(crate) struct PartialChainStateInner {
/// We could just use a mutex, but this is not required and very wateful. Partial chains
/// differ from the normal chain because they only have one owner, the worker responsible
/// for driving this chain to it's completion. Because of that, we can simply use a UnsafeCell
/// and forbit shared access between threads (i.e. don't implement [Sync])
/// and forbit shared access between threads by not implementing [Clone].
pub struct PartialChainState(pub(crate) UnsafeCell<PartialChainStateInner>);

/// We need to send [PartialChainState] between threads/tasks, because the worker thread, once it
Expand All @@ -93,12 +93,8 @@ pub struct PartialChainState(pub(crate) UnsafeCell<PartialChainStateInner>);
///
/// All itens inside the [UnsafeCell] are [Send], most importantly, there are no references or
/// smart pointers inside it, so sending shouldn't be a problem.
///
/// Note that [PartialChainState] isn't meant to be shared among threads, so we shouldn't implement [Sync].
/// The idea of a partial chain is be owned by a single worker that will download all blocks in
/// the range covered by this chain and validate each block, so only the worker thread (or task)
/// will own this at any given time.
unsafe impl Send for PartialChainState {}
unsafe impl Sync for PartialChainState {}

impl PartialChainStateInner {
/// Returns the height we have synced up to so far
Expand Down
8 changes: 7 additions & 1 deletion crates/floresta-wire/src/p2p_wire/running_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ where
Ok(())
}

pub async fn run(mut self, kill_signal: Arc<RwLock<bool>>) {
pub async fn run(
mut self,
kill_signal: Arc<RwLock<bool>>,
stop_signal: futures::channel::oneshot::Sender<()>,
) {
try_and_log!(self.init_peers().await);
let startup_tip = self.chain.get_height().unwrap();

Expand Down Expand Up @@ -421,6 +425,8 @@ where
try_and_log!(self.ask_missed_block().await);
}
}

stop_signal.send(()).unwrap();
}

async fn ask_missed_block(&mut self) -> Result<(), WireError> {
Expand Down
1 change: 1 addition & 0 deletions crates/floresta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ floresta = { version = "0.1.0", path = "../floresta", features = [
"electrum-server",
"watch-only-wallet",
] }
futures = "0.3.30"

[features]
default = ["bitcoinconsensus", "electrum-server", "watch-only-wallet"]
Expand Down
4 changes: 3 additions & 1 deletion crates/floresta/examples/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ async fn main() {
// that will be processed by the node.
let handle = p2p.get_handle();

let (sender, _receiver) = futures::channel::oneshot::channel();

// Start the node. This will start the IBD process, and will return once the node is synced.
// It will also start the mempool, which will start rebroadcasting our transactions every hour.
// The node will keep running until the process is killed, by setting kill_signal to true. In
// this example, we don't kill the node, so it will keep running forever.
p2p.run(Arc::new(RwLock::new(false))).await;
p2p.run(Arc::new(RwLock::new(false)), sender).await;

// That's it! The node is now running, and will keep running until the process is killed.
// You can now use the chain state to query the current state of the accumulator, or the
Expand Down
8 changes: 8 additions & 0 deletions florestad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ jsonrpc-core-client = { version = "18.0.0", features = [
zmq = { version = "0.10.0", optional = true }
latest = "0.1.1"

[lib]
name = "florestad"
path = "src/lib.rs"

[[bin]]
name = "florestad"
path = "src/main.rs"

[dev-dependencies]
pretty_assertions = "1"

Expand Down
5 changes: 2 additions & 3 deletions florestad/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bitcoin::BlockHash;
use clap::arg;
use clap::command;
use clap::Parser;
Expand Down Expand Up @@ -79,7 +78,7 @@ pub enum Commands {
batch_sync: Option<String>,
/// Assume blocks before this one as having valid signatures, same with bitcoin core
#[arg(long)]
assume_valid: Option<BlockHash>,
assume_valid: Option<String>,
},
#[cfg(feature = "experimental-p2p")]
/// Starts your wallet and server
Expand Down Expand Up @@ -111,7 +110,7 @@ pub enum Commands {
wallet_descriptor: Option<Vec<String>>,
/// Assume blocks before this one as having valid signatures, same with bitcoin core
#[arg(long)]
assume_valid: Option<BlockHash>,
assume_valid: Option<String>,
#[arg(long, short)]
zmq_address: Option<String>,
#[arg(long)]
Expand Down
Loading

0 comments on commit 2a81f19

Please sign in to comment.