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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![deny(missing_docs)]
pub mod dependencies {
pub use ::futures;
pub use ::ghost_actor;
pub use ::lair_keystore_api;
pub use ::lair_keystore_api_0_0;
pub use ::observability;
pub use ::paste;
pub use ::rustls;
pub use ::serde;
pub use ::serde_json;
pub use ::thiserror;
pub use ::tokio;
pub use ::url2;
}
pub type ProcCountMicros = i64;
pub fn proc_count_now_us() -> ProcCountMicros {
use once_cell::sync::Lazy;
use tokio::time::Instant;
static PROC_COUNT: Lazy<Instant> = Lazy::new(Instant::now);
let r = *PROC_COUNT;
Instant::now().saturating_duration_since(r).as_micros() as i64
}
pub fn proc_count_us_elapsed(pc: ProcCountMicros) -> std::time::Duration {
let dur = proc_count_now_us() - pc;
let dur = if dur < 0 { 0 } else { dur as u64 };
std::time::Duration::from_micros(dur)
}
pub fn unit_ok_fut<E1, E2>() -> Result<MustBoxFuture<'static, Result<(), E2>>, E1> {
use futures::FutureExt;
Ok(async move { Ok(()) }.boxed().into())
}
pub fn ok_fut<E1, R: Send + 'static>(result: R) -> Result<MustBoxFuture<'static, R>, E1> {
use futures::FutureExt;
Ok(async move { result }.boxed().into())
}
use ::ghost_actor::dependencies::tracing;
use ghost_actor::dependencies::must_future::MustBoxFuture;
pub use ::lair_keystore_api_0_0::actor::CertDigest;
#[derive(Clone)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Tx2Cert(pub Arc<(CertDigest, String, String)>);
impl Tx2Cert {
pub fn as_digest(&self) -> &CertDigest {
self.as_ref()
}
pub fn as_bytes(&self) -> &[u8] {
self.as_ref()
}
pub fn as_str(&self) -> &str {
self.as_ref()
}
pub fn as_nick(&self) -> &str {
&self.0 .2
}
}
impl std::fmt::Debug for Tx2Cert {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Cert(")?;
f.write_str(self.as_nick())?;
f.write_str(")")?;
Ok(())
}
}
impl PartialEq for Tx2Cert {
fn eq(&self, oth: &Self) -> bool {
self.0 .0.eq(&oth.0 .0)
}
}
impl Eq for Tx2Cert {}
impl PartialOrd for Tx2Cert {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.0 .0.partial_cmp(&other.0 .0)
}
}
impl Ord for Tx2Cert {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0 .0.cmp(&other.0 .0)
}
}
impl std::hash::Hash for Tx2Cert {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0 .0.hash(state);
}
}
impl std::ops::Deref for Tx2Cert {
type Target = CertDigest;
fn deref(&self) -> &Self::Target {
&self.0 .0
}
}
impl std::convert::AsRef<CertDigest> for Tx2Cert {
fn as_ref(&self) -> &CertDigest {
std::ops::Deref::deref(self)
}
}
impl std::convert::AsRef<[u8]> for Tx2Cert {
fn as_ref(&self) -> &[u8] {
&self.0 .0
}
}
impl std::convert::AsRef<str> for Tx2Cert {
fn as_ref(&self) -> &str {
&self.0 .1
}
}
impl From<Vec<u8>> for Tx2Cert {
fn from(v: Vec<u8>) -> Self {
let d: CertDigest = v.into();
d.into()
}
}
impl From<Arc<Vec<u8>>> for Tx2Cert {
fn from(v: Arc<Vec<u8>>) -> Self {
let d: CertDigest = v.into();
d.into()
}
}
impl From<CertDigest> for Tx2Cert {
fn from(c: CertDigest) -> Self {
let b64 = base64::encode_config(&**c, base64::URL_SAFE_NO_PAD);
let nick = {
let (start, _) = b64.split_at(6);
let (_, end) = b64.split_at(b64.len() - 6);
format!("{}..{}", start, end)
};
Self(Arc::new((c, b64, nick)))
}
}
impl From<&CertDigest> for Tx2Cert {
fn from(c: &CertDigest) -> Self {
c.clone().into()
}
}
impl From<Tx2Cert> for CertDigest {
fn from(d: Tx2Cert) -> Self {
d.0 .0.clone()
}
}
impl From<&Tx2Cert> for CertDigest {
fn from(d: &Tx2Cert) -> Self {
d.0 .0.clone()
}
}
use config::KitsuneP2pTuningParams;
use std::sync::Arc;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum KitsuneErrorKind {
#[error("Unit")]
Unit,
#[error("Operation timed out")]
TimedOut,
#[error("This object is closed, calls on it are invalid.")]
Closed,
#[error(transparent)]
Other(Box<dyn std::error::Error + Send + Sync>),
}
impl PartialEq for KitsuneErrorKind {
fn eq(&self, oth: &Self) -> bool {
match self {
Self::TimedOut => {
if let Self::TimedOut = oth {
return true;
}
}
Self::Closed => {
if let Self::Closed = oth {
return true;
}
}
_ => (),
}
false
}
}
#[derive(Clone, Debug)]
pub struct KitsuneError(pub Arc<KitsuneErrorKind>);
impl std::fmt::Display for KitsuneError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for KitsuneError {}
impl KitsuneError {
pub fn kind(&self) -> &KitsuneErrorKind {
&self.0
}
pub fn other(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
Self(Arc::new(KitsuneErrorKind::Other(e.into())))
}
}
impl From<KitsuneErrorKind> for KitsuneError {
fn from(k: KitsuneErrorKind) -> Self {
Self(Arc::new(k))
}
}
impl From<String> for KitsuneError {
fn from(s: String) -> Self {
#[derive(Debug, thiserror::Error)]
struct OtherError(String);
impl std::fmt::Display for OtherError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
KitsuneError::other(OtherError(s))
}
}
impl From<&str> for KitsuneError {
fn from(s: &str) -> Self {
s.to_string().into()
}
}
impl From<KitsuneError> for () {
fn from(_: KitsuneError) {}
}
impl From<()> for KitsuneError {
fn from(_: ()) -> Self {
KitsuneErrorKind::Unit.into()
}
}
pub type KitsuneResult<T> = Result<T, KitsuneError>;
mod timeout;
pub use timeout::*;
pub mod agent_info;
pub mod async_lazy;
mod auto_stream_select;
pub use auto_stream_select::*;
pub mod bin_types;
pub mod bootstrap;
pub mod codec;
pub mod combinators;
pub mod config;
pub mod consistency;
pub mod metrics;
pub mod reverse_semaphore;
pub mod task_agg;
pub mod tls;
pub mod transport;
pub mod transport_mem;
pub mod transport_pool;
pub mod tx2;
pub use kitsune_p2p_dht_arc as dht_arc;
use metrics::metric_task;
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(flavor = "multi_thread")]
async fn test_tx2_digest() {
let d: Tx2Cert = vec![0xdb; 32].into();
println!("raw_debug: {:?}", d);
println!("as_digest: {:?}", d.as_digest());
println!("as_bytes: {:?}", d.as_bytes());
println!("as_str: {:?}", d.as_str());
println!("as_nick: {:?}", d.as_nick());
}
}