generated from rustaceanrob/rust-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
Create a fast IBD client #35
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e14446
Add `node` binary
rustaceanrob 691ad55
Update with Add/Spend enum
rustaceanrob aa7fb13
Add initial `node` format
rustaceanrob 7a1b087
node: Add header syncing logic
rustaceanrob c6eb163
node: Add specialized `justfile`
rustaceanrob 67c0648
Pass `Signet`
rustaceanrob dabd02e
Write `Signet` blocks to disk
rustaceanrob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,6 @@ Cargo.lock | |
| !justfile | ||
| *.db | ||
| *.hints | ||
| /node/chainstate | ||
| /node/blocks | ||
| /node/blockfiles | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| [workspace] | ||
| members = ["accumulator", "hintfile", "network"] | ||
| members = ["accumulator", "hintfile", "network", "node"] | ||
| default-members = ["accumulator", "network"] | ||
| resolver = "2" | ||
|
|
||
| [workspace.dependencies] | ||
| bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin", default-features = false, rev = "16cc257c3695dea0e7301a5fa9cab44b8ed60598" } | ||
| kernel = { package = "bitcoinkernel", git = "https://github.com/alexanderwiederin/rust-bitcoinkernel.git", rev = "353533221e3ba91d672418eab1ae7b83a61214f9" } | ||
| p2p = { package = "bitcoin-p2p", git = "https://github.com/2140-dev/bitcoin-p2p.git", rev = "4f67fbeced98e3ddcc65a1333c46823a2b56332a" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [package] | ||
| name = "peers" | ||
| name = "network" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name = "node" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| accumulator = { path = "../accumulator/" } | ||
| bitcoin = { workspace = true } | ||
| kernel = { workspace = true } | ||
| hintfile = { path = "../hintfile/" } | ||
| network = { path = "../network/" } | ||
| p2p = { workspace = true } | ||
|
|
||
| tracing = "0.1" | ||
| tracing-subscriber = "0.3" | ||
|
|
||
| [[bin]] | ||
| name = "ibd" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ibd: | ||
| cargo run --bin ibd ./bitcoin.hints --release | ||
|
|
||
| delete: | ||
| rm -rf blocks chainstate blockfiles |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| use std::{ | ||
| fs::File, | ||
| path::Path, | ||
| sync::{mpsc::channel, Arc, Mutex}, | ||
| time::Instant, | ||
| }; | ||
|
|
||
| use bitcoin::Network; | ||
| use hintfile::Hints; | ||
| use kernel::{ChainType, ChainstateManager, ChainstateManagerOptions, ContextBuilder}; | ||
|
|
||
| use node::{ | ||
| bootstrap_dns, elapsed_time, get_blocks_for_range, hashes_from_chain, sync_block_headers, | ||
| AccumulatorState, | ||
| }; | ||
|
|
||
| const CHAIN_TYPE: ChainType = ChainType::SIGNET; | ||
| const NETWORK: Network = Network::Signet; | ||
| const TASKS: usize = 256; | ||
| const BLOCK_FILE_PATH: &str = "./blockfiles"; | ||
|
|
||
| fn main() { | ||
| let mut args = std::env::args(); | ||
| let _ = args.next(); | ||
| let hint_path = args.next().expect("Usage: <path_to_hints_file>"); | ||
| // Logging | ||
| let subscriber = tracing_subscriber::FmtSubscriber::new(); | ||
| tracing::subscriber::set_global_default(subscriber).unwrap(); | ||
| let hintfile_start_time = Instant::now(); | ||
| tracing::info!("Reading in {hint_path}"); | ||
| let mut hintfile = File::open(hint_path).expect("invalid hintfile path"); | ||
| let hints = Arc::new(Hints::from_file(&mut hintfile)); | ||
| elapsed_time(hintfile_start_time); | ||
| let block_file_path = Path::new(BLOCK_FILE_PATH); | ||
| std::fs::create_dir(block_file_path).expect("could not create block file directory"); | ||
| let stop_hash = hints.stop_hash(); | ||
| tracing::info!("Assume valid hash: {stop_hash}"); | ||
| tracing::info!("Finding peers with DNS"); | ||
| let dns_start_time = Instant::now(); | ||
| let peers = bootstrap_dns(NETWORK); | ||
| elapsed_time(dns_start_time); | ||
| tracing::info!("Initializing bitcoin kernel"); | ||
| let kernel_start_time = Instant::now(); | ||
| let ctx = ContextBuilder::new() | ||
| .chain_type(CHAIN_TYPE) | ||
| .build() | ||
| .unwrap(); | ||
| let options = ChainstateManagerOptions::new(&ctx, ".", "./blocks").unwrap(); | ||
| let chainman = ChainstateManager::new(options).unwrap(); | ||
| elapsed_time(kernel_start_time); | ||
| let tip = chainman.best_header().height(); | ||
| tracing::info!("Kernel best header: {tip}"); | ||
| let chain = Arc::new(chainman); | ||
| sync_block_headers(stop_hash, &peers, Arc::clone(&chain), NETWORK); | ||
| tracing::info!("Assume valid height: {}", chain.best_header().height()); | ||
| let (tx, rx) = channel(); | ||
| let main_routine_time = Instant::now(); | ||
| let mut accumulator_state = AccumulatorState::new(rx); | ||
| let acc_task = std::thread::spawn(move || accumulator_state.verify()); | ||
| let peers = Arc::new(Mutex::new(peers)); | ||
| let mut tasks = Vec::new(); | ||
| let chunk_size = chain.best_header().height() as usize / TASKS; | ||
| let hashes = hashes_from_chain(Arc::clone(&chain), chunk_size); | ||
| for (task_id, chunk) in hashes.into_iter().enumerate() { | ||
| let chain = Arc::clone(&chain); | ||
| let tx = tx.clone(); | ||
| let peers = Arc::clone(&peers); | ||
| let hints = Arc::clone(&hints); | ||
| let block_task = std::thread::spawn(move || { | ||
| get_blocks_for_range( | ||
| task_id as u32, | ||
| NETWORK, | ||
| block_file_path, | ||
| chain, | ||
| &hints, | ||
| peers, | ||
| tx, | ||
| chunk, | ||
| ) | ||
| }); | ||
| tasks.push(block_task); | ||
| } | ||
| for task in tasks { | ||
| if let Err(e) = task.join() { | ||
| tracing::warn!("{:?}", e.downcast::<String>()); | ||
| } | ||
| } | ||
| drop(tx); | ||
| let acc_result = acc_task.join().unwrap(); | ||
| tracing::info!("Verified: {acc_result}"); | ||
| elapsed_time(main_routine_time); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.