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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use kitsune_p2p_dht_arc::DhtLocation;
pub trait KitsuneBinType:
'static
+ Send
+ Sync
+ std::fmt::Debug
+ Clone
+ std::hash::Hash
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ std::convert::Into<Vec<u8>>
{
fn new(bytes: Vec<u8>) -> Self;
fn get_bytes(&self) -> &[u8];
fn get_loc(&self) -> DhtLocation;
}
fn bytes_to_loc(bytes: &[u8]) -> u32 {
(bytes[0] as u32)
+ ((bytes[1] as u32) << 8)
+ ((bytes[2] as u32) << 16)
+ ((bytes[3] as u32) << 24)
}
macro_rules! make_kitsune_bin_type {
($($doc:expr, $name:ident),*,) => {
$(
#[doc = $doc]
#[derive(
Clone,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
shrinkwraprs::Shrinkwrap,
derive_more::Into,
serde::Serialize,
serde::Deserialize,
)]
#[shrinkwrap(mutable)]
pub struct $name(#[serde(with = "serde_bytes")] pub Vec<u8>);
impl KitsuneBinType for $name {
fn new(mut bytes: Vec<u8>) -> Self {
if bytes.len() != 36 {
debug_assert_eq!(bytes.len(), 32);
bytes.append(&mut [0; 4].to_vec());
}
debug_assert_eq!(bytes.len(), 36);
Self(bytes)
}
fn get_bytes(&self) -> &[u8] {
&self.0[..self.0.len() - 4]
}
fn get_loc(&self) -> DhtLocation {
DhtLocation::new(bytes_to_loc(&self.0[self.0.len() - 4..]))
}
}
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}(0x", stringify!($name)))?;
for byte in &self.0 {
f.write_fmt(format_args!("{:02x}", byte))?;
}
f.write_fmt(format_args!(")"))?;
Ok(())
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
base64::encode_config(&self.0, base64::URL_SAFE_NO_PAD).fmt(f)
}
}
impl AsRef<[u8]> for $name {
fn as_ref(&self) -> &[u8] {
self.0.as_slice()
}
}
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for $name {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let mut buf = [0; 36];
buf[..]
.copy_from_slice(u.bytes(36)?);
Ok(Self::new(buf.to_vec()))
}
}
)*
};
}
make_kitsune_bin_type! {
"Distinguish multiple categories of communication within the same network module.",
KitsuneSpace,
"Distinguish multiple agents within the same network module.",
KitsuneAgent,
"The basis hash/coordinate when identifying a neighborhood.",
KitsuneBasis,
r#"Top-level "KitsuneDataHash" items are buckets of related meta-data.
These metadata "Operations" each also have unique OpHashes."#,
KitsuneOpHash,
}
#[derive(PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct KitsuneOpData(
#[serde(with = "serde_bytes")]
pub Vec<u8>,
);
impl KitsuneOpData {
pub fn new(op: Vec<u8>) -> KOp {
KOp::new(Self(op))
}
pub fn size(&self) -> usize {
self.0.len()
}
}
pub fn fmt_many_bytes(
name: &str,
f: &mut std::fmt::Formatter<'_>,
bytes: &[u8],
) -> std::fmt::Result {
if bytes.len() <= 32 {
let mut t = f.debug_tuple(name);
t.field(&bytes).finish()
} else {
let mut t = f.debug_struct(name);
let l = bytes.len();
t.field("length", &l);
t.field(
"bytes",
&format!(
"[{},{},{},{},{},{},{},{},...,{},{},{},{},{},{},{},{}]",
bytes[0],
bytes[1],
bytes[2],
bytes[3],
bytes[4],
bytes[5],
bytes[6],
bytes[7],
bytes[l - 1],
bytes[l - 2],
bytes[l - 3],
bytes[l - 4],
bytes[l - 5],
bytes[l - 6],
bytes[l - 7],
bytes[l - 8],
),
)
.finish()
}
}
impl std::fmt::Debug for KitsuneOpData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt_many_bytes("KitsuneOpData", f, self.0.as_slice())
}
}
pub type KOp = std::sync::Arc<KitsuneOpData>;
#[derive(
Clone,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
shrinkwraprs::Shrinkwrap,
derive_more::From,
derive_more::Into,
serde::Deserialize,
serde::Serialize,
)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[shrinkwrap(mutable)]
pub struct KitsuneSignature(#[serde(with = "serde_bytes")] pub Vec<u8>);
impl std::fmt::Debug for KitsuneSignature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("Signature(0x"))?;
for byte in &self.0 {
f.write_fmt(format_args!("{:02x}", byte))?;
}
f.write_fmt(format_args!(")"))?;
Ok(())
}
}