Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: remove unnecessary &mut self #34

Merged
merged 1 commit into from
Jul 8, 2024
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
4 changes: 2 additions & 2 deletions example/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main() {
// Create a new node builder
let builder = NodeBuilder::new(bitcoin::Network::Signet);
// Add node preferences and build the node/client
let (mut node, mut client) = builder
let (mut node, client) = builder
// Add the peer
.add_peers(vec![(peer, None).into()])
// The Bitcoin scripts to monitor
Expand All @@ -56,7 +56,7 @@ async fn main() {
// Split the client into components that send messages and listen to messages.
// With this construction, different parts of the program can take ownership of
// specific tasks.
let (mut sender, mut receiver) = client.split();
let (sender, mut receiver) = client.split();
// Continually listen for events until the node is synced to its peers.
loop {
if let Ok(message) = receiver.recv().await {
Expand Down
4 changes: 2 additions & 2 deletions example/rescan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn main() {
// Create a new node builder
let builder = NodeBuilder::new(bitcoin::Network::Signet);
// Add node preferences and build the node/client
let (mut node, mut client) = builder
let (mut node, client) = builder
// The Bitcoin scripts to monitor
.add_scripts(addresses)
// Only scan blocks strictly after an anchor checkpoint
Expand All @@ -41,7 +41,7 @@ async fn main() {
tokio::task::spawn(async move { node.run().await });
tracing::info!("Running the node and waiting for a sync message. Please wait a minute!");
// Split the client into components that send messages and listen to messages
let (mut sender, mut receiver) = client.split();
let (sender, mut receiver) = client.split();
// Sync with the single script added
loop {
if let Ok(message) = receiver.recv().await {
Expand Down
4 changes: 2 additions & 2 deletions example/signet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() {
// Create a new node builder
let builder = NodeBuilder::new(bitcoin::Network::Signet);
// Add node preferences and build the node/client
let (mut node, mut client) = builder
let (mut node, client) = builder
// Add the peers
.add_peers(vec![(peer_1, None).into(), (peer_2, None).into()])
// The Bitcoin scripts to monitor
Expand All @@ -52,7 +52,7 @@ async fn main() {
// Split the client into components that send messages and listen to messages.
// With this construction, different parts of the program can take ownership of
// specific tasks.
let (mut sender, mut receiver) = client.split();
let (sender, mut receiver) = client.split();
// Continually listen for events until the node is synced to its peers.
loop {
if let Ok(message) = receiver.recv().await {
Expand Down
14 changes: 7 additions & 7 deletions src/node/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Client {
/// For a majority of cases, some parts of your program will respond to node events, and other parts of the program
/// will send events to the node. This method returns a [`ClientSender`] to issue commands to the node, and a
/// [`Receiver`] to continually respond to events issued from the node.
pub fn split(&mut self) -> (ClientSender, Receiver<NodeMessage>) {
pub fn split(&self) -> (ClientSender, Receiver<NodeMessage>) {
(self.sender(), self.receiver())
}

Expand All @@ -43,12 +43,12 @@ impl Client {
/// receivers have gotten the message.
/// You should only call this twice if two separate portions of your application need to process
/// data differently. For example, a Lightning Network node implementation.
pub fn receiver(&mut self) -> Receiver<NodeMessage> {
pub fn receiver(&self) -> Receiver<NodeMessage> {
self.nrx.subscribe()
}

/// Tell the node to stop running.
pub async fn shutdown(&mut self) -> Result<(), ClientError> {
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Shutdown)
.await
Expand Down Expand Up @@ -134,31 +134,31 @@ impl ClientSender {
}

/// Tell the node to shut down.
pub async fn shutdown(&mut self) -> Result<(), ClientError> {
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Shutdown)
.await
.map_err(|_| ClientError::SendError)
}

/// Broadcast a new transaction to the network.
pub async fn broadcast_tx(&mut self, tx: TxBroadcast) -> Result<(), ClientError> {
pub async fn broadcast_tx(&self, tx: TxBroadcast) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Broadcast(tx))
.await
.map_err(|_| ClientError::SendError)
}

/// Add more Bitcoin [`ScriptBuf`] to watch for. Does not rescan the filters.
pub async fn add_scripts(&mut self, scripts: HashSet<ScriptBuf>) -> Result<(), ClientError> {
pub async fn add_scripts(&self, scripts: HashSet<ScriptBuf>) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::AddScripts(scripts))
.await
.map_err(|_| ClientError::SendError)
}

/// Starting at the configured anchor checkpoint, look for block inclusions with newly added scripts.
pub async fn rescan(&mut self) -> Result<(), ClientError> {
pub async fn rescan(&self) -> Result<(), ClientError> {
self.ntx
.send(ClientMessage::Rescan)
.await
Expand Down
12 changes: 7 additions & 5 deletions src/node/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub enum NodeState {
Behind,
/// We may start downloading compact block filter headers.
HeadersSynced,
// We may start scanning compact block filters
/// We may start scanning compact block filters
FilterHeadersSynced,
// We may start asking for blocks with matches
/// We may start asking for blocks with matches
FiltersSynced,
// We found all known transactions to the wallet
/// We found all known transactions to the wallet
TransactionsSynced,
}

Expand Down Expand Up @@ -439,8 +439,7 @@ impl Node {
{
self.dialog
.send_warning(format!(
"Encountered error adding peer to the database: {}",
e
"Encountered error adding peer to the database: {e}. The most likely cause is due to an unrecognized service flag."
))
.await;
}
Expand Down Expand Up @@ -630,6 +629,9 @@ impl Node {
NodeState::HeadersSynced => None,
_ => {
chain.clear_filters().await;
self.dialog
.send_data(NodeMessage::StateChange(NodeState::FilterHeadersSynced))
.await;
*state = NodeState::FilterHeadersSynced;
Some(MainThreadMessage::GetFilters(
chain.next_filter_message().await,
Expand Down
38 changes: 19 additions & 19 deletions tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ async fn test_reorg() {
let mut scripts = HashSet::new();
let other = rpc.get_new_address(None, None).unwrap().assume_checked();
scripts.insert(other.into());
let (mut node, mut client) = new_node(scripts.clone()).await;
let (mut node, client) = new_node(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (mut sender, mut recv) = client.split();
let (sender, mut recv) = client.split();
sync_assert(&best, &mut recv).await;
// Reorganize the blocks
let old_best = best;
Expand Down Expand Up @@ -178,7 +178,7 @@ async fn test_mine_after_reorg() {
let mut scripts = HashSet::new();
let other = rpc.get_new_address(None, None).unwrap().assume_checked();
scripts.insert(other.into());
let (mut node, mut client) = new_node(scripts.clone()).await;
let (mut node, client) = new_node(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
sync_assert(&best, &mut recv).await;
Expand Down Expand Up @@ -270,9 +270,9 @@ async fn test_broadcast_success() {
// Make sure we sync up to the tip under usual conditions.
let mut scripts = HashSet::new();
scripts.insert(burn.into());
let (mut node, mut client) = new_node(scripts.clone()).await;
let (mut node, client) = new_node(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (mut sender, mut recv) = client.split();
let (sender, mut recv) = client.split();
// Broadcast the transaction to the network
sender
.broadcast_tx(TxBroadcast::new(
Expand Down Expand Up @@ -354,9 +354,9 @@ async fn test_broadcast_fail() {
println!("Built unsigned transaction with TXID {}", tx.compute_txid());
let mut scripts = HashSet::new();
scripts.insert(burn.into());
let (mut node, mut client) = new_node(scripts.clone()).await;
let (mut node, client) = new_node(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (mut sender, mut recv) = client.split();
let (sender, mut recv) = client.split();
// Broadcast the transaction to the network
sender
.broadcast_tx(TxBroadcast::new(tx, kyoto::TxBroadcastPolicy::AllPeers))
Expand Down Expand Up @@ -401,9 +401,9 @@ async fn test_long_chain() {
let mut scripts = HashSet::new();
let other = rpc.get_new_address(None, None).unwrap().assume_checked();
scripts.insert(other.into());
let (mut node, mut client) = new_node(scripts.clone()).await;
let (mut node, client) = new_node(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (mut sender, mut recv) = client.split();
let (sender, mut recv) = client.split();
sync_assert(&best, &mut recv).await;
sender.shutdown().await.unwrap();
rpc.stop().unwrap();
Expand Down Expand Up @@ -432,7 +432,7 @@ async fn test_sql_reorg() {
let mut scripts = HashSet::new();
let other = rpc.get_new_address(None, None).unwrap().assume_checked();
scripts.insert(other.into());
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
sync_assert(&best, &mut recv).await;
Expand All @@ -444,7 +444,7 @@ async fn test_sql_reorg() {
mine_blocks(&rpc, &miner, 2, 1).await;
let best = rpc.get_best_block_hash().unwrap();
// Spin up the node on a cold start
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
// Make sure the reorganization is caught after a cold start
Expand All @@ -470,7 +470,7 @@ async fn test_sql_reorg() {
mine_blocks(&rpc, &miner, 2, 1).await;
let best = rpc.get_best_block_hash().unwrap();
// Make sure the node does not have any corrupted headers
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
// The node properly syncs after persisting a reorg
Expand Down Expand Up @@ -502,7 +502,7 @@ async fn test_two_deep_reorg() {
let mut scripts = HashSet::new();
let other = rpc.get_new_address(None, None).unwrap().assume_checked();
scripts.insert(other.into());
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
sync_assert(&best, &mut recv).await;
Expand All @@ -516,7 +516,7 @@ async fn test_two_deep_reorg() {
mine_blocks(&rpc, &miner, 3, 1).await;
let best = rpc.get_best_block_hash().unwrap();
// Make sure the reorganization is caught after a cold start
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
while let Ok(message) = recv.recv().await {
Expand All @@ -541,7 +541,7 @@ async fn test_two_deep_reorg() {
mine_blocks(&rpc, &miner, 2, 1).await;
let best = rpc.get_best_block_hash().unwrap();
// Make sure the node does not have any corrupted headers
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
// The node properly syncs after persisting a reorg
Expand Down Expand Up @@ -576,7 +576,7 @@ async fn test_sql_stale_anchor() {
let mut scripts = HashSet::new();
let other = rpc.get_new_address(None, None).unwrap().assume_checked();
scripts.insert(other.into());
let (mut node, mut client) = new_node_sql(scripts.clone()).await;
let (mut node, client) = new_node_sql(scripts.clone()).await;
tokio::task::spawn(async move { node.run().await });
let (_, mut recv) = client.split();
sync_assert(&best, &mut recv).await;
Expand All @@ -588,7 +588,7 @@ async fn test_sql_stale_anchor() {
mine_blocks(&rpc, &miner, 2, 1).await;
let best = rpc.get_best_block_hash().unwrap();
// Spin up the node on a cold start with a stale tip
let (mut node, mut client) = new_node_anchor_sql(
let (mut node, client) = new_node_anchor_sql(
scripts.clone(),
HeaderCheckpoint::new(old_height as u32, old_best),
)
Expand Down Expand Up @@ -619,7 +619,7 @@ async fn test_sql_stale_anchor() {
let old_height = rpc.get_block_count().unwrap();
let best = rpc.get_best_block_hash().unwrap();
// Make sure the node does not have any corrupted headers
let (mut node, mut client) = new_node_anchor_sql(
let (mut node, client) = new_node_anchor_sql(
scripts.clone(),
HeaderCheckpoint::new(old_height as u32, cp),
)
Expand All @@ -635,7 +635,7 @@ async fn test_sql_stale_anchor() {
mine_blocks(&rpc, &miner, 2, 1).await;
let best = rpc.get_best_block_hash().unwrap();
// Make sure the node does not have any corrupted headers
let (mut node, mut client) = new_node_anchor_sql(
let (mut node, client) = new_node_anchor_sql(
scripts.clone(),
HeaderCheckpoint::new(old_height as u32, cp),
)
Expand Down
Loading