diff --git a/Cargo.toml b/Cargo.toml index 1c429bb..c8e03bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] -members = ["accumulator", "node"] -default-members = ["accumulator"] +members = ["aggregate", "node"] +default-members = ["aggregate"] resolver = "2" [workspace.dependencies] diff --git a/README.md b/README.md index a9640cf..b004f34 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,14 @@ # SwiftSync -:warning::construction: This project is still under construction and expected to change significantly. Use at your own risk :construction::warning: +:construction: This is a research project not intended for real use. :construction: -This repository is a collection of crates related to a SwiftSync node implementation. Some crates will be SwiftSync-specific, while others may have broader use cases. +This repository is a collection of crates related to a _SwiftSync_ node implementation. _SwiftSync_ is a protocol that allows nearly-stateless, parallelizable Bitcoin initial block download without adding additional cryptographic assumptions. You may read the [initial writeup here](https://gist.github.com/RubenSomsen/a61a37d14182ccd78760e477c78133cd). -- `accumulator`: A hash-based SwiftSync accumulator used to add and subtrack elements from a set. +## Executables + +See the `node/README.md` to run an initial block download using _SwiftSync_. + +## Crates + +- `aggregate`: A hash-based data structure used to add and subtrack elements from a set. - `node`: Perform fast IBD using a SwiftSync hints file. diff --git a/accumulator/Cargo.toml b/aggregate/Cargo.toml similarity index 91% rename from accumulator/Cargo.toml rename to aggregate/Cargo.toml index 52546c3..cd17b84 100644 --- a/accumulator/Cargo.toml +++ b/aggregate/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "accumulator" +name = "aggregate" version = "0.1.0" edition = "2021" diff --git a/accumulator/README.md b/aggregate/README.md similarity index 100% rename from accumulator/README.md rename to aggregate/README.md diff --git a/accumulator/src/lib.rs b/aggregate/src/lib.rs similarity index 89% rename from accumulator/src/lib.rs rename to aggregate/src/lib.rs index e2e87e5..b8cbf3d 100644 --- a/accumulator/src/lib.rs +++ b/aggregate/src/lib.rs @@ -11,7 +11,7 @@ sha256t_tag! { /// added and removed the equivalent amount of times, the accumulator is zero. In the context of /// bitcoin, this is used to add and remove hashes of [`OutPoint`] data. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, std::hash::Hash)] -pub struct Accumulator { +pub struct Aggregate { high: u128, low: u128, } @@ -36,14 +36,14 @@ fn split_in_half(a: [u8; 32]) -> ([u8; 16], [u8; 16]) { /// Update an accumulator by adding or spending a pre-hashed outpoint #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum AccumulatorUpdate { +pub enum AggregateUpdate { Add([u8; 32]), Spent([u8; 32]), } -impl Accumulator { +impl Aggregate { /// The zero accumulator - pub const ZERO: Accumulator = Accumulator { high: 0, low: 0 }; + pub const ZERO: Aggregate = Aggregate { high: 0, low: 0 }; /// Build a new accumulator. pub const fn new() -> Self { @@ -66,10 +66,10 @@ impl Accumulator { } /// Update the accumulator - pub fn update(&mut self, update: AccumulatorUpdate) { + pub fn update(&mut self, update: AggregateUpdate) { match update { - AccumulatorUpdate::Add(added) => self.add_hashed_outpoint(added), - AccumulatorUpdate::Spent(spent) => self.spend_hashed_outpoint(spent), + AggregateUpdate::Add(added) => self.add_hashed_outpoint(added), + AggregateUpdate::Spent(spent) => self.spend_hashed_outpoint(spent), } } @@ -101,7 +101,7 @@ impl Accumulator { } } -impl Default for Accumulator { +impl Default for Aggregate { fn default() -> Self { Self::ZERO } @@ -218,7 +218,7 @@ mod tests { #[test] fn test_accumulator_is_zero() { - let mut acc = Accumulator::default(); + let mut acc = Aggregate::default(); let [outpoint_one, outpoint_two, outpoint_three, outpoint_four, outpoint_five] = make_five_outpoint(); // Add the members @@ -241,10 +241,10 @@ mod tests { fn test_same_state() { let [outpoint_one, outpoint_two, outpoint_three, outpoint_four, outpoint_five] = make_five_outpoint(); - let mut acc_ref = Accumulator::default(); + let mut acc_ref = Aggregate::default(); acc_ref.add(outpoint_two); acc_ref.add(outpoint_four); - let mut acc_cmp = Accumulator::default(); + let mut acc_cmp = Aggregate::default(); acc_cmp.add(outpoint_one); acc_cmp.add(outpoint_two); acc_cmp.add(outpoint_three); @@ -266,7 +266,7 @@ mod tests { let hash_three = hash_outpoint(outpoint_three); let hash_four = hash_outpoint(outpoint_four); let hash_five = hash_outpoint(outpoint_five); - let mut acc = Accumulator::default(); + let mut acc = Aggregate::default(); acc.add_hashed_outpoint(hash_five); acc.add_hashed_outpoint(hash_four); acc.add_hashed_outpoint(hash_one); @@ -288,17 +288,17 @@ mod tests { let hash_three = hash_outpoint(outpoint_three); let hash_four = hash_outpoint(outpoint_four); let hash_five = hash_outpoint(outpoint_five); - let mut acc = Accumulator::default(); - acc.update(AccumulatorUpdate::Add(hash_one)); - acc.update(AccumulatorUpdate::Add(hash_two)); - acc.update(AccumulatorUpdate::Add(hash_three)); - acc.update(AccumulatorUpdate::Add(hash_four)); - acc.update(AccumulatorUpdate::Add(hash_five)); - acc.update(AccumulatorUpdate::Spent(hash_five)); - acc.update(AccumulatorUpdate::Spent(hash_four)); - acc.update(AccumulatorUpdate::Spent(hash_three)); - acc.update(AccumulatorUpdate::Spent(hash_two)); - acc.update(AccumulatorUpdate::Spent(hash_one)); + let mut acc = Aggregate::default(); + acc.update(AggregateUpdate::Add(hash_one)); + acc.update(AggregateUpdate::Add(hash_two)); + acc.update(AggregateUpdate::Add(hash_three)); + acc.update(AggregateUpdate::Add(hash_four)); + acc.update(AggregateUpdate::Add(hash_five)); + acc.update(AggregateUpdate::Spent(hash_five)); + acc.update(AggregateUpdate::Spent(hash_four)); + acc.update(AggregateUpdate::Spent(hash_three)); + acc.update(AggregateUpdate::Spent(hash_two)); + acc.update(AggregateUpdate::Spent(hash_one)); assert!(acc.is_zero()); } diff --git a/accumulator/tests/test.rs b/aggregate/tests/test.rs similarity index 95% rename from accumulator/tests/test.rs rename to aggregate/tests/test.rs index e2ccd45..c69aae5 100644 --- a/accumulator/tests/test.rs +++ b/aggregate/tests/test.rs @@ -1,4 +1,4 @@ -use accumulator::{Accumulator, MultAggregate}; +use aggregate::{Aggregate, MultAggregate}; use bitcoin::{OutPoint, Txid}; use rusqlite::Connection; @@ -6,7 +6,7 @@ const SELECT_STMT: &str = "SELECT txid, vout FROM utxos"; #[test] fn test_static_utxo_set() { - let mut acc = Accumulator::new(); + let mut acc = Aggregate::new(); let conn = Connection::open("../contrib/data/signet_outpoints.sqlite").unwrap(); let mut stmt = conn.prepare(SELECT_STMT).unwrap(); let mut rows = stmt.query([]).unwrap(); diff --git a/node/Cargo.toml b/node/Cargo.toml index 54390da..55cd558 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" build = "build.rs" [dependencies] -accumulator = { path = "../accumulator/" } +aggregate = { path = "../aggregate/" } bitcoin = { workspace = true } kernel = { workspace = true } hintsfile = { workspace = true } diff --git a/node/src/bin/ibd.rs b/node/src/bin/ibd.rs index 6535c9a..3f45df1 100644 --- a/node/src/bin/ibd.rs +++ b/node/src/bin/ibd.rs @@ -11,7 +11,7 @@ use kernel::{ChainstateManager, ChainstateManagerOptions, ContextBuilder}; use node::{ bootstrap_dns, elapsed_time, get_blocks_for_range, hashes_from_chain, sync_block_headers, - AccumulatorState, ChainExt, + AggregateState, ChainExt, }; use p2p::net::TimeoutParams; @@ -77,7 +77,7 @@ fn main() { 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 mut accumulator_state = AggregateState::new(rx); let acc_task = std::thread::spawn(move || accumulator_state.verify()); let peers = Arc::new(Mutex::new(peers)); let mut tasks = Vec::new(); diff --git a/node/src/lib.rs b/node/src/lib.rs index 3f3f000..1a8dfd4 100644 --- a/node/src/lib.rs +++ b/node/src/lib.rs @@ -11,7 +11,7 @@ use std::{ time::{Duration, Instant}, }; -use accumulator::{Accumulator, AccumulatorUpdate}; +use aggregate::{Aggregate, AggregateUpdate}; use bitcoin::{ consensus, key::rand::{seq::SliceRandom, thread_rng}, @@ -43,15 +43,15 @@ pub fn elapsed_time(then: Instant) { } #[derive(Debug)] -pub struct AccumulatorState { - acc: Accumulator, - update_rx: Receiver, +pub struct AggregateState { + acc: Aggregate, + update_rx: Receiver, } -impl AccumulatorState { - pub fn new(rx: Receiver) -> Self { +impl AggregateState { + pub fn new(rx: Receiver) -> Self { Self { - acc: Accumulator::new(), + acc: Aggregate::new(), update_rx: rx, } } @@ -179,7 +179,7 @@ pub fn get_blocks_for_range( chain: Arc, hints: Arc>, peers: Arc>>, - updater: Sender, + updater: Sender, hashes: Arc>>>, ) { let mut batch = Vec::new(); @@ -264,8 +264,8 @@ pub fn get_blocks_for_range( let tx_hash = transaction.compute_txid(); if !transaction.is_coinbase() { for input in transaction.inputs { - let input_hash = accumulator::hash_outpoint(input.previous_output); - let update = AccumulatorUpdate::Spent(input_hash); + let input_hash = aggregate::hash_outpoint(input.previous_output); + let update = AggregateUpdate::Spent(input_hash); updater .send(update) .expect("accumulator task must not panic"); @@ -282,8 +282,8 @@ pub fn get_blocks_for_range( txid: tx_hash, vout: vout as u32, }; - let input_hash = accumulator::hash_outpoint(outpoint); - let update = AccumulatorUpdate::Add(input_hash); + let input_hash = aggregate::hash_outpoint(outpoint); + let update = AggregateUpdate::Add(input_hash); updater .send(update) .expect("accumulator task must not panic");