Skip to content

Commit 2557016

Browse files
authored
Merge pull request #48 from rustaceanrob/9-5-write-opt
Make writing blocks optional
2 parents 6b96ec7 + 8e17271 commit 2557016

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

node/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ cargo run --bin ibd --release -- <args>
1919

2020
```
2121
Arguments:
22-
2322
--hintfile The path to your `bitcoin.hints` file that will
24-
be used for IBD. Default is `./bitcoin.hints
25-
--blocks-dir Directory where you would like to store the
26-
bitcoin blocks. Default `./blockfiles`
23+
be used for IBD. Default is `./bitcoin.hints`
24+
--blocks-dir Optional directory to store the blocks. Used
25+
only to measure performance.
2726
--network The bitcoin network to operate on. Default `
2827
bitcoin`. Options are `bitcoin` or `signet`
28+
--min-blocks-per-sec The minimum rate a peer has to respond to block
29+
requests.
30+
--tasks The number of tasks to download blocks. Default
31+
is 64. Each task uses two OS threads.
2932
--ping-timeout The time (seconds) a peer has to respond to a `
3033
ping` message. Pings are sent aggressively
3134
throughout IBD to find slow peers.
@@ -35,8 +38,4 @@ Arguments:
3538
stream until the connection is killed.
3639
--write-timeout The maximum time (seconds) to write to a TCP
3740
stream until the connection is killed.
38-
--min-blocks-per-sec The minimum rate a peer has to respond to block
39-
requests.
40-
--tasks The number of tasks to download blocks. Default
41-
is 64. Each task uses two OS threads.
4241
```

node/config_spec.toml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@
22
name = "hintfile"
33
type = "String"
44
default = "\"./bitcoin.hints\".into()"
5-
doc = "The path to your `bitcoin.hints` file that will be used for IBD. Default is `./bitcoin.hints"
5+
doc = "The path to your `bitcoin.hints` file that will be used for IBD. Default is `./bitcoin.hints`"
66

77
[[param]]
88
name = "blocks_dir"
99
type = "String"
10-
default = "\"./blockfiles\".into()"
11-
doc = "Directory where you would like to store the bitcoin blocks. Default `./blockfiles`"
10+
optional = true
11+
doc = "Optional directory to store the blocks. Used only to measure performance."
1212

1313
[[param]]
1414
name = "network"
1515
type = "String"
1616
default = "\"bitcoin\".into()"
1717
doc = "The bitcoin network to operate on. Default `bitcoin`. Options are `bitcoin` or `signet`"
1818

19+
[[param]]
20+
name = "min_blocks_per_sec"
21+
type = "f64"
22+
default = "3."
23+
doc = "The minimum rate a peer has to respond to block requests."
24+
25+
[[param]]
26+
name = "tasks"
27+
type = "usize"
28+
default = "32"
29+
doc = "The number of tasks to download blocks. Default is 64. Each task uses two OS threads."
30+
1931
[[param]]
2032
name = "ping_timeout"
2133
type = "u64"
@@ -40,14 +52,3 @@ type = "u64"
4052
default = "2"
4153
doc = "The maximum time (seconds) to write to a TCP stream until the connection is killed."
4254

43-
[[param]]
44-
name = "min_blocks_per_sec"
45-
type = "f64"
46-
default = "3."
47-
doc = "The minimum rate a peer has to respond to block requests."
48-
49-
[[param]]
50-
name = "tasks"
51-
type = "usize"
52-
default = "32"
53-
doc = "The number of tasks to download blocks. Default is 64. Each task uses two OS threads."

node/src/bin/ibd.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ fn main() {
4545
let mut hintfile = File::open(hint_path).expect("invalid hintfile path");
4646
let hints = Arc::new(Hints::from_file(&mut hintfile));
4747
elapsed_time(hintfile_start_time);
48-
let block_file_path = PathBuf::from(&blocks_dir);
49-
std::fs::create_dir(&block_file_path).expect("could not create block file directory");
48+
let block_file_path = blocks_dir.map(PathBuf::from);
49+
if let Some(block_file_path) = block_file_path.as_ref() {
50+
std::fs::create_dir(block_file_path).expect("could not create block file directory");
51+
}
5052
let stop_hash =
5153
consensus::deserialize::<BlockHash>(&hints.stop_hash()).expect("stop hash is not valid");
5254
tracing::info!("Assume valid hash: {stop_hash}");
@@ -88,7 +90,7 @@ fn main() {
8890
block_per_sec,
8991
ping_timeout,
9092
network,
91-
&block_file_path,
93+
block_file_path,
9294
chain,
9395
&hints,
9496
peers,

node/src/lib.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
fs::File,
44
io::Write,
55
net::{IpAddr, Ipv4Addr, SocketAddr},
6-
path::Path,
6+
path::PathBuf,
77
sync::{
88
mpsc::{Receiver, Sender},
99
Arc, Mutex,
@@ -170,7 +170,7 @@ pub fn get_blocks_for_range(
170170
blocks_per_sec: f64,
171171
_ping_timeout: Duration,
172172
network: Network,
173-
block_dir: &Path,
173+
block_dir: Option<PathBuf>,
174174
chain: Arc<ChainstateManager>,
175175
hints: &Hints,
176176
peers: Arc<Mutex<Vec<SocketAddr>>>,
@@ -222,19 +222,22 @@ pub fn get_blocks_for_range(
222222
let unspent_indexes: HashSet<u64> =
223223
hints.get_indexes(block_height).into_iter().collect();
224224
// tracing::info!("{task_id} -> {block_height}:{hash}");
225-
let file_path = block_dir.join(format!("{hash}.block"));
226-
let file = File::create_new(file_path);
227-
let mut file = match file {
228-
Ok(file) => file,
229-
Err(e) => {
230-
tracing::warn!("Conflicting open files at: {}", block_height);
231-
tracing::warn!("{e}");
232-
panic!("files cannot conflict");
233-
}
234-
};
235-
let block_bytes = consensus::serialize(&block);
236-
file.write_all(&block_bytes)
237-
.expect("failed to write block file");
225+
if let Some(block_dir) = block_dir.as_ref() {
226+
let file_path = block_dir.join(format!("{hash}.block"));
227+
let file = File::create_new(file_path);
228+
let mut file = match file {
229+
Ok(file) => file,
230+
Err(e) => {
231+
tracing::warn!("Conflicting open files at: {}", block_height);
232+
tracing::warn!("{e}");
233+
panic!("files cannot conflict");
234+
}
235+
};
236+
let block_bytes = consensus::serialize(&block);
237+
file.write_all(&block_bytes)
238+
.expect("failed to write block file");
239+
file.sync_data().expect("could not sync file with OS");
240+
}
238241
// tracing::info!("Wrote {hash} to file");
239242
let (_, transactions) = block.into_parts();
240243
let mut output_index = 0;

0 commit comments

Comments
 (0)