Skip to content

Commit

Permalink
feat: -connect cli option
Browse files Browse the repository at this point in the history
This option makes your node connect with a given peer, and only this peer
  • Loading branch information
Davidson-Souza committed Jan 10, 2024
1 parent c205d03 commit c42fdb2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
25 changes: 22 additions & 3 deletions crates/floresta-wire/src/p2p_wire/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub struct NodeCommon<Chain: BlockchainInterface + UpdatableChainstate> {
address_man: AddressMan,
max_banscore: u32,
socks5: Option<Socks5StreamBuilder>,
fixed_peer: Option<LocalAddress>,
}

pub struct UtreexoNode<Context, Chain: BlockchainInterface + UpdatableChainstate>(
Expand Down Expand Up @@ -210,6 +211,7 @@ where
datadir: String,
proxy: Option<SocketAddr>,
max_banscore: Option<u32>,
fixed_peer: Option<LocalAddress>,
) -> Self {
let (node_tx, node_rx) = channel::unbounded();
let socks5 = proxy.map(Socks5StreamBuilder::new);
Expand Down Expand Up @@ -238,6 +240,7 @@ where
datadir,
socks5,
max_banscore: max_banscore.unwrap_or(50),
fixed_peer,
},
T::default(),
)
Expand Down Expand Up @@ -639,12 +642,21 @@ where
}

async fn maybe_open_connection(&mut self) -> Result<(), WireError> {
// If the user passes in a `--connect` cli argument, we only connect with
// that particular peer.
if self.fixed_peer.is_some() && !self.peers.is_empty() {
return Ok(());
}
if self.peers.len() < MAX_OUTGOING_PEERS {
self.create_connection(false).await;
}
Ok(())
}
async fn open_feeler_connection(&mut self) -> Result<(), WireError> {
// No feeler if `-connect` is set
if self.fixed_peer.is_some() {
return Ok(());
}
self.create_connection(true).await;
Ok(())
}
Expand Down Expand Up @@ -679,11 +691,17 @@ where
} else {
ServiceFlags::NETWORK | ServiceFlags::WITNESS
};
let (peer_id, address) = self
.address_man
.get_address_to_connect(required_services, feeler)?;

let (peer_id, address) = match &self.fixed_peer {
Some(address) => (0, address.clone()),
None => self
.address_man
.get_address_to_connect(required_services, feeler)?,
};

self.address_man
.update_set_state(peer_id, AddressState::Connected);

// Don't connect to the same peer twice
if self
.0
Expand Down Expand Up @@ -759,6 +777,7 @@ where
);
Ok(())
}

/// Creates a new outgoing connection with `address`. Connection may or may not be feeler,
/// a special connection type that is used to learn about good peers, but are not kept afer
/// handshake.
Expand Down
1 change: 1 addition & 0 deletions crates/floresta/examples/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async fn main() {
DATA_DIR.into(),
None,
None,
None,
);
// A handle is a simple way to interact with the node. It implements a queue of requests
// that will be processed by the node.
Expand Down
2 changes: 2 additions & 0 deletions florestad/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub enum Commands {
assume_valid: Option<BlockHash>,
#[arg(long, short)]
zmq_address: Option<String>,
#[arg(long)]
connect: Option<String>,
},
}

Expand Down
11 changes: 11 additions & 0 deletions florestad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod wallet_input;
mod zmq;

use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;

use async_std::sync::RwLock;
Expand All @@ -53,6 +54,7 @@ use floresta_electrum::electrum_protocol::ElectrumServer;
use floresta_watch_only::kv_database::KvDatabase;
use floresta_watch_only::AddressCache;
use floresta_watch_only::AddressCacheDatabase;
use floresta_wire::address_man::LocalAddress;
use floresta_wire::mempool::Mempool;
use floresta_wire::node::UtreexoNode;
use log::debug;
Expand Down Expand Up @@ -83,6 +85,7 @@ struct Ctx {
cfilter_types: Vec<FilterType>,
#[cfg(feature = "zmq-server")]
zmq_address: Option<String>,
connect: Option<String>,
}
fn main() {
// Setup global logger
Expand All @@ -104,6 +107,7 @@ fn main() {
zmq_address: _zmq_address,
cfilters,
cfilter_types,
connect,
}) => {
// By default, we build filters for WPKH and TR outputs, as they are the newest.
// We also build the `inputs` filters to find spends
Expand All @@ -127,7 +131,9 @@ fn main() {
cfilter_types,
#[cfg(feature = "zmq-server")]
zmq_address: _zmq_address,
connect,
};

run_with_ctx(ctx);
}

Expand Down Expand Up @@ -271,6 +277,10 @@ fn run_with_ctx(ctx: Ctx) {
#[cfg(not(feature = "compact-filters"))]
let cfilters = None;

let connect = ctx
.connect
.map(|host| LocalAddress::from_str(&host).unwrap());

// Chain Provider (p2p)
let chain_provider = UtreexoNode::new(
blockchain_state.clone(),
Expand All @@ -279,6 +289,7 @@ fn run_with_ctx(ctx: Ctx) {
data_dir,
ctx.proxy.map(|x| x.parse().expect("Invalid proxy address")),
None,
connect,
);

// ZMQ
Expand Down

0 comments on commit c42fdb2

Please sign in to comment.