Skip to content

Commit 1e08b9b

Browse files
authored
Merge pull request #549 from rustaceanrob/26-3-10-witness-block
Optionally request witness data
2 parents 8f03e58 + cbe4766 commit 1e08b9b

File tree

7 files changed

+37
-5
lines changed

7 files changed

+37
-5
lines changed

examples/bitcoin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ async fn main() {
3434
.add_peers(seeds.into_iter().map(From::from))
3535
// Connections over Tor are supported by Socks5 proxy
3636
// .socks5_proxy(bip157::Socks5Proxy::local())
37+
// Optionally request witness data when fetching a block
38+
// .fetch_witness_data()
3739
// Create the node and client
3840
.build();
3941
// Run the node on a separate task

src/builder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bitcoin::Network;
55
use super::{client::Client, node::Node};
66
use crate::chain::ChainState;
77
use crate::network::ConnectionType;
8-
use crate::{Config, FilterType};
8+
use crate::{BlockType, Config, FilterType};
99
use crate::{Socks5Proxy, TrustedPeer};
1010

1111
const MIN_PEERS: u8 = 1;
@@ -137,6 +137,12 @@ impl Builder {
137137
self
138138
}
139139

140+
/// Request witness data when requesting blocks.
141+
pub fn fetch_witness_data(mut self) -> Self {
142+
self.config.block_type = BlockType::Witness;
143+
self
144+
}
145+
140146
/// Consume the node builder and receive a [`Node`] and [`Client`].
141147
pub fn build(mut self) -> (Node, Client) {
142148
Node::new(self.network, core::mem::take(&mut self.config))

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ pub enum FilterType {
119119
Basic,
120120
}
121121

122+
#[derive(Debug, Clone, Copy, Default)]
123+
enum BlockType {
124+
#[default]
125+
Legacy,
126+
Witness,
127+
}
128+
122129
impl From<FilterType> for u8 {
123130
fn from(value: FilterType) -> Self {
124131
match value {
@@ -335,6 +342,7 @@ struct Config {
335342
connection_type: ConnectionType,
336343
peer_timeout_config: PeerTimeoutConfig,
337344
filter_type: FilterType,
345+
block_type: BlockType,
338346
}
339347

340348
impl Default for Config {
@@ -347,6 +355,7 @@ impl Default for Config {
347355
connection_type: Default::default(),
348356
peer_timeout_config: PeerTimeoutConfig::default(),
349357
filter_type: FilterType::default(),
358+
block_type: BlockType::default(),
350359
}
351360
}
352361
}

src/network/outbound.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ use bitcoin::{
1515
BlockHash, Network, Transaction, Wtxid,
1616
};
1717

18-
use crate::default_port_from_network;
18+
use crate::{default_port_from_network, BlockType};
1919

2020
use super::{KYOTO_VERSION, PROTOCOL_VERSION, RUST_BITCOIN_VERSION};
2121

2222
// Responsible for serializing messages to write over the wire, either encrypted or plaintext.
2323
pub(in crate::network) struct MessageGenerator {
2424
pub network: Network,
2525
pub transport: Transport,
26+
pub block_type: BlockType,
2627
}
2728

2829
pub(in crate::network) enum Transport {
@@ -50,7 +51,10 @@ impl MessageGenerator {
5051
}
5152

5253
pub(in crate::network) fn block(&mut self, hash: BlockHash) -> Vec<u8> {
53-
let inv = Inventory::Block(hash);
54+
let inv = match self.block_type {
55+
BlockType::Legacy => Inventory::Block(hash),
56+
BlockType::Witness => Inventory::WitnessBlock(hash),
57+
};
5458
let msg = NetworkMessage::GetData(vec![inv]);
5559
self.serialize(msg)
5660
}

src/network/peer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use tokio::{
1818
time::{Instant, MissedTickBehavior},
1919
};
2020

21-
use crate::{broadcaster::BroadcastQueue, messages::Warning, Dialog, Info};
21+
use crate::{broadcaster::BroadcastQueue, messages::Warning, BlockType, Dialog, Info};
2222

2323
use super::{
2424
error::PeerError,
@@ -38,6 +38,7 @@ pub(crate) struct Peer {
3838
main_thread_sender: Sender<PeerThreadMessage>,
3939
main_thread_recv: Receiver<MainThreadMessage>,
4040
network: Network,
41+
block_type: BlockType,
4142
dialog: Arc<Dialog>,
4243
db: Arc<Mutex<AddressBook>>,
4344
timeout_config: PeerTimeoutConfig,
@@ -51,6 +52,7 @@ impl Peer {
5152
nonce: PeerId,
5253
source: Record,
5354
network: Network,
55+
block_type: BlockType,
5456
main_thread_sender: Sender<PeerThreadMessage>,
5557
main_thread_recv: Receiver<MainThreadMessage>,
5658
dialog: Arc<Dialog>,
@@ -64,6 +66,7 @@ impl Peer {
6466
main_thread_sender,
6567
main_thread_recv,
6668
network,
69+
block_type,
6770
dialog,
6871
db,
6972
timeout_config,
@@ -97,13 +100,15 @@ impl Peer {
97100
let outbound_messages = MessageGenerator {
98101
network: self.network,
99102
transport: Transport::V2 { encryptor },
103+
block_type: self.block_type,
100104
};
101105
let reader = Reader::new(MessageParser::V2(reader, decryptor), tx);
102106
(outbound_messages, reader)
103107
} else {
104108
let outbound_messages = MessageGenerator {
105109
network: self.network,
106110
transport: Transport::V1,
111+
block_type: self.block_type,
107112
};
108113
let reader = Reader::new(MessageParser::V1(reader, self.network), tx);
109114
(outbound_messages, reader)

src/network/peer_map.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
chain::HeightMonitor,
2626
default_port_from_network,
2727
network::{dns::bootstrap_dns, error::PeerError, peer::Peer, PeerId, PeerTimeoutConfig},
28-
Dialog, TrustedPeer,
28+
BlockType, Dialog, TrustedPeer,
2929
};
3030

3131
use super::{AddressBook, ConnectionType, MainThreadMessage, PeerThreadMessage};
@@ -51,6 +51,7 @@ pub(crate) struct PeerMap {
5151
current_id: PeerId,
5252
heights: Arc<Mutex<HeightMonitor>>,
5353
network: Network,
54+
block_type: BlockType,
5455
mtx: Sender<PeerThreadMessage>,
5556
map: HashMap<PeerId, ManagedPeer>,
5657
db: Arc<Mutex<AddressBook>>,
@@ -65,6 +66,7 @@ impl PeerMap {
6566
pub fn new(
6667
mtx: Sender<PeerThreadMessage>,
6768
network: Network,
69+
block_type: BlockType,
6870
whitelist: Whitelist,
6971
dialog: Arc<Dialog>,
7072
connection_type: ConnectionType,
@@ -76,6 +78,7 @@ impl PeerMap {
7678
current_id: PeerId(0),
7779
heights: height_monitor,
7880
network,
81+
block_type,
7982
mtx,
8083
map: HashMap::new(),
8184
db: Arc::new(Mutex::new(AddressBook::new())),
@@ -122,6 +125,7 @@ impl PeerMap {
122125
self.current_id,
123126
loaded_peer.clone(),
124127
self.network,
128+
self.block_type,
125129
self.mtx.clone(),
126130
prx,
127131
Arc::clone(&self.dialog),

src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl Node {
7575
connection_type,
7676
peer_timeout_config,
7777
filter_type,
78+
block_type,
7879
} = config;
7980
// Set up a communication channel between the node and client
8081
let (info_tx, info_rx) = mpsc::channel::<Info>(32);
@@ -92,6 +93,7 @@ impl Node {
9293
let peer_map = PeerMap::new(
9394
mtx,
9495
network,
96+
block_type,
9597
white_list,
9698
Arc::clone(&dialog),
9799
connection_type,

0 commit comments

Comments
 (0)