Skip to content

Commit 4127534

Browse files
authored
Merge pull request #302 from rustaceanrob/builder-2-18
Use declarative naming for methods in builder
2 parents a1a48b1 + 863edb0 commit 4127534

8 files changed

Lines changed: 32 additions & 32 deletions

File tree

example/managed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ async fn main() {
4444
// Only scan blocks strictly after an anchor checkpoint
4545
.anchor_checkpoint(checkpoint)
4646
// The number of connections we would like to maintain
47-
.num_required_peers(1)
47+
.required_peers(1)
4848
// Omit informational messages
4949
.log_level(LogLevel::Warning)
5050
// Create the node and client
51-
.build_node()
51+
.build()
5252
.unwrap();
5353

5454
tokio::task::spawn(async move { node.run().await });

example/rescan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ async fn main() {
3535
NETWORK,
3636
))
3737
// The number of connections we would like to maintain
38-
.num_required_peers(1)
39-
.build_node()
38+
.required_peers(1)
39+
.build()
4040
.unwrap();
4141
// Run the node and wait for the sync message;
4242
tokio::task::spawn(async move { node.run().await });

example/signet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ async fn main() {
4545
// Only scan blocks strictly after an anchor checkpoint
4646
.anchor_checkpoint(checkpoint)
4747
// The number of connections we would like to maintain
48-
.num_required_peers(2)
48+
.required_peers(2)
4949
// Create the node and client
50-
.build_node()
50+
.build()
5151
.unwrap();
5252
// Run the node on a separate task
5353
tokio::task::spawn(async move { node.run().await });

example/testnet4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ async fn main() {
3838
// Store a limited number of peers
3939
.peer_db_size(PeerStoreSizeConfig::Limit(256))
4040
// The number of connections we would like to maintain
41-
.num_required_peers(1)
41+
.required_peers(1)
4242
// Create the node and client
43-
.build_node()
43+
.build()
4444
.unwrap();
4545

4646
// Run the node on a new task

example/tor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ async fn main() {
4949
// Only scan blocks strictly after an anchor checkpoint
5050
.anchor_checkpoint(anchor)
5151
// The number of connections we would like to maintain
52-
.num_required_peers(2)
52+
.required_peers(2)
5353
// We only maintain a list of 256 peers in memory
5454
.peer_db_size(PeerStoreSizeConfig::Limit(256))
5555
// Connect to peers over Tor
56-
.set_connection_type(ConnectionType::Tor(tor))
56+
.connection_type(ConnectionType::Tor(tor))
5757
// Build without the default databases
58-
.build_node()
58+
.build()
5959
.unwrap();
6060
// Run the node
6161
tokio::task::spawn(async move { node.run().await });

src/core/builder.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const MAX_PEERS: u8 = 15;
3939
/// let builder = NodeBuilder::new(Network::Regtest);
4040
/// let (node, client) = builder
4141
/// .add_peers(vec![host.into()])
42-
/// .build_node()
42+
/// .build()
4343
/// .unwrap();
4444
/// ```
4545
///
@@ -68,8 +68,8 @@ const MAX_PEERS: u8 = 15;
6868
/// // Only scan blocks strictly after an anchor checkpoint
6969
/// .anchor_checkpoint(checkpoint)
7070
/// // The number of connections we would like to maintain
71-
/// .num_required_peers(2)
72-
/// .build_node()
71+
/// .required_peers(2)
72+
/// .build()
7373
/// .unwrap();
7474
/// ```
7575
pub struct NodeBuilder {
@@ -110,7 +110,7 @@ impl NodeBuilder {
110110

111111
/// Add a path to the directory where data should be stored. If none is provided, the current
112112
/// working directory will be used.
113-
pub fn add_data_dir(mut self, path: impl Into<PathBuf>) -> Self {
113+
pub fn data_dir(mut self, path: impl Into<PathBuf>) -> Self {
114114
self.config.data_path = Some(path.into());
115115
self
116116
}
@@ -119,7 +119,7 @@ impl NodeBuilder {
119119
/// Adding more connections increases the node's anonymity, but requires waiting for more responses,
120120
/// higher bandwidth, and higher memory requirements. If none is provided, a single connection will be maintained.
121121
/// The number of connections will be clamped to a range of 1 to 15.
122-
pub fn num_required_peers(mut self, num_peers: u8) -> Self {
122+
pub fn required_peers(mut self, num_peers: u8) -> Self {
123123
self.config.required_peers = num_peers.clamp(MIN_PEERS, MAX_PEERS);
124124
self
125125
}
@@ -150,7 +150,7 @@ impl NodeBuilder {
150150
}
151151

152152
/// Set the desired communication channel. Either directly over TCP or over the Tor network.
153-
pub fn set_connection_type(mut self, connection_type: ConnectionType) -> Self {
153+
pub fn connection_type(mut self, connection_type: ConnectionType) -> Self {
154154
self.config.connection_type = connection_type;
155155
self
156156
}
@@ -164,7 +164,7 @@ impl NodeBuilder {
164164
/// nodes may be slower to respond while processing blocks and transactions.
165165
///
166166
/// If none is provided, a timeout of 5 seconds will be used.
167-
pub fn set_response_timeout(mut self, response_timeout: Duration) -> Self {
167+
pub fn response_timeout(mut self, response_timeout: Duration) -> Self {
168168
self.config.response_timeout = response_timeout;
169169
self
170170
}
@@ -176,10 +176,10 @@ impl NodeBuilder {
176176
///
177177
/// This value is configurable as some developers may be satisfied with a peer
178178
/// as long as the peer responds promptly. Other implementations may value finding
179-
/// new a reliable peers faster, so the maximum connection time may be shorter.
179+
/// new and reliable peers faster, so the maximum connection time may be shorter.
180180
///
181181
/// If none is provided, a maximum connection time of two hours will be used.
182-
pub fn set_maximum_connection_time(mut self, max_connection_time: Duration) -> Self {
182+
pub fn maximum_connection_time(mut self, max_connection_time: Duration) -> Self {
183183
self.config.max_connection_time = max_connection_time;
184184
self
185185
}
@@ -207,7 +207,7 @@ impl NodeBuilder {
207207
///
208208
/// Building a node and client will error if a database connection is denied or cannot be found.
209209
#[cfg(feature = "database")]
210-
pub fn build_node(&mut self) -> Result<(NodeDefault, Client), SqlInitializationError> {
210+
pub fn build(&mut self) -> Result<(NodeDefault, Client), SqlInitializationError> {
211211
let peer_store = SqlitePeerDb::new(self.network, self.config.data_path.clone())?;
212212
let header_store = SqliteHeaderDb::new(self.network, self.config.data_path.clone())?;
213213
Ok(Node::new(

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
//! // Create a new node builder
3131
//! let builder = NodeBuilder::new(Network::Signet);
3232
//! // Add node preferences and build the node/client
33-
//! let (mut node, client) = builder
33+
//! let (node, client) = builder
3434
//! // The Bitcoin scripts to monitor
3535
//! .add_scripts(addresses)
3636
//! // Only scan blocks strictly after an anchor checkpoint
3737
//! .anchor_checkpoint(checkpoint)
3838
//! // The number of connections we would like to maintain
39-
//! .num_required_peers(2)
40-
//! .build_node()
39+
//! .required_peers(2)
40+
//! .build()
4141
//! .unwrap();
4242
//! // Run the node and wait for the sync message;
4343
//! tokio::task::spawn(async move { node.run().await });

tests/core.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ fn new_node_sql(
5252
let (node, client) = builder
5353
.add_peer(host)
5454
.add_scripts(addrs)
55-
.add_data_dir(tempdir_path)
56-
.build_node()
55+
.data_dir(tempdir_path)
56+
.build()
5757
.unwrap();
5858
(node, client)
5959
}
@@ -71,9 +71,9 @@ fn new_node_anchor_sql(
7171
let (node, client) = builder
7272
.add_peer(trusted)
7373
.add_scripts(addrs)
74-
.add_data_dir(tempdir_path)
74+
.data_dir(tempdir_path)
7575
.anchor_checkpoint(checkpoint)
76-
.build_node()
76+
.build()
7777
.unwrap();
7878
(node, client)
7979
}
@@ -596,9 +596,9 @@ async fn halting_download_works() {
596596
let (node, client) = builder
597597
.add_peers(vec![host.into()])
598598
.add_scripts(scripts)
599-
.add_data_dir(tempdir)
599+
.data_dir(tempdir)
600600
.halt_filter_download()
601-
.build_node()
601+
.build()
602602
.unwrap();
603603

604604
tokio::task::spawn(async move { node.run().await });
@@ -649,8 +649,8 @@ async fn signet_syncs() {
649649
let (node, client) = builder
650650
.add_peer(host)
651651
.add_scripts(set)
652-
.add_data_dir(tempdir)
653-
.build_node()
652+
.data_dir(tempdir)
653+
.build()
654654
.unwrap();
655655
tokio::task::spawn(async move { node.run().await });
656656
async fn print_and_sync(mut client: Client) {

0 commit comments

Comments
 (0)