Skip to content

Commit 2b8bc66

Browse files
authored
Merge pull request #35 from rustaceanrob/8-15-client-bin
Create a fast IBD client
2 parents 0cc0e24 + dabd02e commit 2b8bc66

File tree

13 files changed

+469
-5
lines changed

13 files changed

+469
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ Cargo.lock
33
!justfile
44
*.db
55
*.hints
6+
/node/chainstate
7+
/node/blocks
8+
/node/blockfiles

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[workspace]
2-
members = ["accumulator", "hintfile", "network"]
2+
members = ["accumulator", "hintfile", "network", "node"]
33
default-members = ["accumulator", "network"]
44
resolver = "2"
55

66
[workspace.dependencies]
77
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin", default-features = false, rev = "16cc257c3695dea0e7301a5fa9cab44b8ed60598" }
8+
kernel = { package = "bitcoinkernel", git = "https://github.com/alexanderwiederin/rust-bitcoinkernel.git", rev = "353533221e3ba91d672418eab1ae7b83a61214f9" }
9+
p2p = { package = "bitcoin-p2p", git = "https://github.com/2140-dev/bitcoin-p2p.git", rev = "4f67fbeced98e3ddcc65a1333c46823a2b56332a" }

accumulator/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ fn split_in_half(a: [u8; 32]) -> ([u8; 16], [u8; 16]) {
3434
(high, low)
3535
}
3636

37+
/// Update an accumulator by adding or spending a pre-hashed outpoint
38+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39+
pub enum AccumulatorUpdate {
40+
Add([u8; 32]),
41+
Spent([u8; 32]),
42+
}
43+
3744
impl Accumulator {
3845
/// The zero accumulator
3946
pub const ZERO: Accumulator = Accumulator { high: 0, low: 0 };
@@ -59,6 +66,14 @@ impl Accumulator {
5966
*self = Self { high, low };
6067
}
6168

69+
/// Update the accumulator
70+
pub fn update(&mut self, update: AccumulatorUpdate) {
71+
match update {
72+
AccumulatorUpdate::Add(added) => self.add_hashed_outpoint(added),
73+
AccumulatorUpdate::Spent(spent) => self.spend_hashed_outpoint(spent),
74+
}
75+
}
76+
6277
/// Spend the inputs in a block by subtracing them from the accumulator.
6378
pub fn spend(&mut self, outpoint: OutPoint) {
6479
let hash = hash_outpoint(outpoint);
@@ -226,4 +241,27 @@ mod tests {
226241
acc.spend_hashed_outpoint(hash_two);
227242
acc.spend_hashed_outpoint(hash_three);
228243
}
244+
245+
#[test]
246+
fn test_update_method() {
247+
let [outpoint_one, outpoint_two, outpoint_three, outpoint_four, outpoint_five] =
248+
make_five_outpoint();
249+
let hash_one = hash_outpoint(outpoint_one);
250+
let hash_two = hash_outpoint(outpoint_two);
251+
let hash_three = hash_outpoint(outpoint_three);
252+
let hash_four = hash_outpoint(outpoint_four);
253+
let hash_five = hash_outpoint(outpoint_five);
254+
let mut acc = Accumulator::default();
255+
acc.update(AccumulatorUpdate::Add(hash_one));
256+
acc.update(AccumulatorUpdate::Add(hash_two));
257+
acc.update(AccumulatorUpdate::Add(hash_three));
258+
acc.update(AccumulatorUpdate::Add(hash_four));
259+
acc.update(AccumulatorUpdate::Add(hash_five));
260+
acc.update(AccumulatorUpdate::Spent(hash_five));
261+
acc.update(AccumulatorUpdate::Spent(hash_four));
262+
acc.update(AccumulatorUpdate::Spent(hash_three));
263+
acc.update(AccumulatorUpdate::Spent(hash_two));
264+
acc.update(AccumulatorUpdate::Spent(hash_one));
265+
assert!(acc.is_zero());
266+
}
229267
}
-35.7 MB
Binary file not shown.

contrib/data/signet_headers.sqlite

-10.2 MB
Binary file not shown.

hintfile/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
bitcoin = { workspace = true }
8-
kernel = { package = "bitcoinkernel", git = "https://github.com/alexanderwiederin/rust-bitcoinkernel.git", rev = "b7b42d65a70f2c0e5fc8d1d024549bf857211046" }
8+
kernel = { workspace = true }
99

1010
[[bin]]
1111
name = "construct"

hintfile/src/bin/construct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fs::File, io::Write, sync::Arc};
33
use hintfile::write_compact_size;
44
use kernel::{ChainType, ChainstateManager, ChainstateManagerOptions, ContextBuilder, KernelError};
55

6-
const CHAIN_TYPE: ChainType = ChainType::MAINNET;
6+
const CHAIN_TYPE: ChainType = ChainType::SIGNET;
77

88
fn main() {
99
let mut file = File::create("./bitcoin.hints").unwrap();

network/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "peers"
2+
name = "network"
33
version = "0.1.0"
44
edition = "2021"
55

network/tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
22

3-
use peers::dns::DnsQuery;
3+
use network::dns::DnsQuery;
44

55
#[tokio::test]
66
#[cfg(feature = "tokio")]

node/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "node"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
accumulator = { path = "../accumulator/" }
8+
bitcoin = { workspace = true }
9+
kernel = { workspace = true }
10+
hintfile = { path = "../hintfile/" }
11+
network = { path = "../network/" }
12+
p2p = { workspace = true }
13+
14+
tracing = "0.1"
15+
tracing-subscriber = "0.3"
16+
17+
[[bin]]
18+
name = "ibd"

0 commit comments

Comments
 (0)