1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::block::Block;
use crate::error::BitswapError;
use crate::prefix::Prefix;
use core::convert::TryFrom;
use libipld::cid::Cid;
use prost::Message;
use std::collections::{HashMap, HashSet};
mod bitswap_pb {
include!(concat!(env!("OUT_DIR"), "/bitswap_pb.rs"));
}
pub type Priority = i32;
#[derive(Clone, Default, Eq, PartialEq)]
pub struct BitswapMessage {
want: HashMap<Cid, Priority>,
cancel: HashSet<Cid>,
full: bool,
blocks: Vec<Block>,
}
impl core::fmt::Debug for BitswapMessage {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
for (cid, priority) in self.want() {
writeln!(fmt, "want: {} {}", cid.to_string(), priority)?;
}
for cid in self.cancel() {
writeln!(fmt, "cancel: {}", cid.to_string())?;
}
for block in self.blocks() {
writeln!(fmt, "block: {}", block.cid().to_string())?;
}
Ok(())
}
}
impl BitswapMessage {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.want.is_empty() && self.cancel.is_empty() && self.blocks.is_empty()
}
pub fn blocks(&self) -> &[Block] {
&self.blocks
}
pub fn want(&self) -> impl Iterator<Item = (&Cid, Priority)> {
self.want.iter().map(|(cid, priority)| (cid, *priority))
}
pub fn cancel(&self) -> impl Iterator<Item = &Cid> {
self.cancel.iter()
}
pub fn add_block(&mut self, block: Block) {
self.blocks.push(block);
}
pub fn remove_block(&mut self, cid: &Cid) {
self.blocks.retain(|block| block.cid() != cid);
}
pub fn want_block(&mut self, cid: &Cid, priority: Priority) {
self.cancel.remove(cid);
self.want.insert(cid.clone(), priority);
}
pub fn cancel_block(&mut self, cid: &Cid) {
if self.want.contains_key(cid) {
self.want.remove(cid);
} else {
self.cancel.insert(cid.clone());
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut proto = bitswap_pb::Message::default();
let mut wantlist = bitswap_pb::message::Wantlist::default();
for (cid, priority) in self.want() {
let mut entry = bitswap_pb::message::wantlist::Entry::default();
entry.block = cid.to_bytes();
entry.priority = priority;
wantlist.entries.push(entry);
}
for cid in self.cancel() {
let mut entry = bitswap_pb::message::wantlist::Entry::default();
entry.block = cid.to_bytes();
entry.cancel = true;
wantlist.entries.push(entry);
}
for block in self.blocks() {
let mut payload = bitswap_pb::message::Block::default();
let prefix: Prefix = block.cid().into();
payload.prefix = prefix.to_bytes();
payload.data = block.data().to_vec();
proto.payload.push(payload);
}
if !wantlist.entries.is_empty() {
proto.wantlist = Some(wantlist);
}
let mut res = Vec::with_capacity(proto.encoded_len());
proto
.encode(&mut res)
.expect("there is no situation in which the protobuf message can be invalid");
res
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, BitswapError> {
Self::try_from(bytes)
}
}
impl TryFrom<&[u8]> for BitswapMessage {
type Error = BitswapError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let proto: bitswap_pb::Message = bitswap_pb::Message::decode(bytes)?;
let mut message = Self::new();
for entry in proto.wantlist.unwrap_or_default().entries {
let cid = Cid::try_from(entry.block)?;
if entry.cancel {
message.cancel_block(&cid);
} else {
message.want_block(&cid, entry.priority);
}
}
for payload in proto.payload {
let prefix = Prefix::new(&payload.prefix)?;
let cid = prefix.to_cid(&payload.data)?;
let block = Block {
cid,
data: payload.data.to_vec().into_boxed_slice(),
};
message.add_block(block);
}
Ok(message)
}
}
impl From<()> for BitswapMessage {
fn from(_: ()) -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::block::tests::create_block;
#[test]
fn test_empty_message_to_from_bytes() {
let message = BitswapMessage::new();
let bytes = message.to_bytes();
let new_message = BitswapMessage::from_bytes(&bytes).unwrap();
assert_eq!(message, new_message);
}
#[test]
fn test_want_message_to_from_bytes() {
let mut message = BitswapMessage::new();
let block = create_block(b"hello world");
message.want_block(&block.cid(), 1);
let bytes = message.to_bytes();
let new_message = BitswapMessage::from_bytes(&bytes).unwrap();
assert_eq!(message, new_message);
}
#[test]
fn test_cancel_message_to_from_bytes() {
let mut message = BitswapMessage::new();
let block = create_block(b"hello world");
message.cancel_block(&block.cid());
let bytes = message.to_bytes();
let new_message = BitswapMessage::from_bytes(&bytes).unwrap();
assert_eq!(message, new_message);
}
#[test]
fn test_payload_message_to_from_bytes() {
let mut message = BitswapMessage::new();
let block = create_block(b"hello world");
message.add_block(block);
let bytes = message.to_bytes();
let new_message = BitswapMessage::from_bytes(&bytes).unwrap();
assert_eq!(message, new_message);
}
}