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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! AsyncDht node.
use std::{
net::SocketAddrV4,
pin::Pin,
task::{Context, Poll},
};
use futures_lite::{Stream, StreamExt};
use crate::{
common::{
hash_immutable, AnnouncePeerRequestArguments, FindNodeRequestArguments,
GetPeersRequestArguments, GetValueRequestArguments, Id, MutableItem, Node,
PutImmutableRequestArguments, PutMutableRequestArguments, PutRequestSpecific,
},
dht::{ActorMessage, Dht, PutMutableError, ResponseSender},
rpc::{GetRequestSpecific, Info, PutError, PutQueryError},
};
impl Dht {
/// Return an async version of the Dht client.
pub fn as_async(self) -> AsyncDht {
AsyncDht(self)
}
}
#[derive(Debug, Clone)]
/// Async version of the Dht node.
pub struct AsyncDht(Dht);
impl AsyncDht {
/// Information and statistics about this [Dht] node.
pub async fn info(&self) -> Info {
let (tx, rx) = flume::bounded::<Info>(1);
self.send(ActorMessage::Info(tx));
rx.recv_async()
.await
.expect("actor thread unexpectedly shutdown")
}
/// Turn this node's routing table to a list of bootstrapping nodes.
pub async fn to_bootstrap(&self) -> Vec<String> {
let (tx, rx) = flume::bounded::<Vec<String>>(1);
self.send(ActorMessage::ToBootstrap(tx));
rx.recv_async()
.await
.expect("actor thread unexpectedly shutdown")
}
// === Public Methods ===
/// Await until the bootstrapping query is done.
///
/// Returns true if the bootstrapping was successful.
pub async fn bootstrapped(&self) -> bool {
let info = self.info().await;
let nodes = self.find_node(*info.id()).await;
!nodes.is_empty()
}
// === Find nodes ===
/// Returns the closest 20 [secure](Node::is_secure) nodes to a target [Id].
///
/// Mostly useful to crawl the DHT.
///
/// The returned nodes are claims by other nodes, they may be lies, or may have churned
/// since they were last seen, but haven't been pinged yet.
///
/// You might need to ping them to confirm they exist, and responsive, or if you want to
/// learn more about them like the client they are using, or if they support a given BEP.
///
/// If you are trying to find the closest nodes to a target with intent to [Self::put],
/// a request directly to these nodes (using `extra_nodes` parameter), then you should
/// use [Self::get_closest_nodes] instead.
pub async fn find_node(&self, target: Id) -> Box<[Node]> {
let (tx, rx) = flume::bounded::<Box<[Node]>>(1);
self.send(ActorMessage::Get(
GetRequestSpecific::FindNode(FindNodeRequestArguments { target }),
ResponseSender::ClosestNodes(tx),
));
rx.recv_async()
.await
.expect("Query was dropped before sending a response, please open an issue.")
}
// === Peers ===
/// Get peers for a given infohash.
///
/// Note: each node of the network will only return a _random_ subset (usually 20)
/// of the total peers it has for a given infohash, so if you are getting responses
/// from 20 nodes, you can expect up to 400 peers in total, but if there are more
/// announced peers on that infohash, you are likely to miss some, the logic here
/// for Bittorrent is that any peer will introduce you to more peers through "peer exchange"
/// so if you are implementing something different from Bittorrent, you might want
/// to implement your own logic for gossipping more peers after you discover the first ones.
pub fn get_peers(&self, info_hash: Id) -> GetStream<Vec<SocketAddrV4>> {
let (tx, rx) = flume::unbounded::<Vec<SocketAddrV4>>();
self.send(ActorMessage::Get(
GetRequestSpecific::GetPeers(GetPeersRequestArguments { info_hash }),
ResponseSender::Peers(tx),
));
GetStream(rx.into_stream())
}
/// Announce a peer for a given infohash.
///
/// The peer will be announced on this process IP.
/// If explicit port is passed, it will be used, otherwise the port will be implicitly
/// assumed by remote nodes to be the same ase port they received the request from.
pub async fn announce_peer(
&self,
info_hash: Id,
port: Option<u16>,
) -> Result<Id, PutQueryError> {
let (port, implied_port) = match port {
Some(port) => (port, None),
None => (0, Some(true)),
};
self.put(
PutRequestSpecific::AnnouncePeer(AnnouncePeerRequestArguments {
info_hash,
port,
implied_port,
}),
None,
)
.await
.map_err(|error| match error {
PutError::Query(error) => error,
PutError::Concurrency(_) => {
unreachable!("should not receive a concurrency error from announce peer query")
}
})
}
// === Immutable data ===
/// Get an Immutable data by its sha1 hash.
pub async fn get_immutable(&self, target: Id) -> Option<Box<[u8]>> {
let (tx, rx) = flume::unbounded::<Box<[u8]>>();
self.send(ActorMessage::Get(
GetRequestSpecific::GetValue(GetValueRequestArguments {
target,
seq: None,
salt: None,
}),
ResponseSender::Immutable(tx),
));
rx.recv_async().await.map(Some).unwrap_or(None)
}
/// Put an immutable data to the DHT.
pub async fn put_immutable(&self, value: &[u8]) -> Result<Id, PutQueryError> {
let target: Id = hash_immutable(value).into();
self.put(
PutRequestSpecific::PutImmutable(PutImmutableRequestArguments {
target,
v: value.into(),
}),
None,
)
.await
.map_err(|error| match error {
PutError::Query(error) => error,
PutError::Concurrency(_) => {
unreachable!("should not receive a concurrency error from put immutable query")
}
})
}
// === Mutable data ===
/// Get a mutable data by its `public_key` and optional `salt`.
///
/// You can ask for items `more_recent_than` than a certain `seq`,
/// usually one that you already have seen before, similar to `If-Modified-Since` header in HTTP.
///
/// # Order
///
/// The order of [MutableItem]s returned by this stream is not guaranteed to
/// reflect their `seq` value. You should not assume that the later items are
/// more recent than earlier ones.
///
/// Consider using [Self::get_mutable_most_recent] if that is what you need.
pub fn get_mutable(
&self,
public_key: &[u8; 32],
salt: Option<&[u8]>,
more_recent_than: Option<i64>,
) -> GetStream<MutableItem> {
let salt = salt.map(|s| s.into());
let target = MutableItem::target_from_key(public_key, salt.as_deref());
let (tx, rx) = flume::unbounded::<MutableItem>();
self.send(ActorMessage::Get(
GetRequestSpecific::GetValue(GetValueRequestArguments {
target,
seq: more_recent_than,
salt,
}),
ResponseSender::Mutable(tx),
));
GetStream(rx.into_stream())
}
/// Get the most recent [MutableItem] from the network.
pub async fn get_mutable_most_recent(
&self,
public_key: &[u8; 32],
salt: Option<&[u8]>,
) -> Option<MutableItem> {
let mut most_recent: Option<MutableItem> = None;
let mut stream = self.get_mutable(public_key, salt, None);
while let Some(item) = stream.next().await {
if let Some(mr) = &most_recent {
if item.seq() == mr.seq && item.value() > &mr.value {
most_recent = Some(item)
}
} else {
most_recent = Some(item);
}
}
most_recent
}
/// Put a mutable data to the DHT.
///
/// # Lost Update Problem
///
/// As mainline DHT is a distributed system, it is vulnerable to [Write–write conflict](https://en.wikipedia.org/wiki/Write-write_conflict).
///
/// ## Read first
///
/// To mitigate the risk of lost updates, you should call the [Self::get_mutable_most_recent] method
/// then start authoring the new [MutableItem] based on the most recent as in the following example:
///
///```rust
/// use mainline::{Dht, MutableItem, SigningKey, Testnet};
/// use std::net::Ipv4Addr;
///
/// let testnet = Testnet::builder(3).build().unwrap();
/// let dht = Dht::builder()
/// .bootstrap(&testnet.bootstrap)
/// .bind_address(Ipv4Addr::LOCALHOST)
/// .build()
/// .unwrap()
/// .as_async();
///
/// let signing_key = SigningKey::from_bytes(&[0; 32]);
/// let key = signing_key.verifying_key().to_bytes();
/// let salt = Some(b"salt".as_ref());
///
/// futures::executor::block_on(async move {
/// let (item, cas) = if let Some(most_recent) = dht.get_mutable_most_recent(&key, salt).await {
/// // 1. Optionally Create a new value to take the most recent's value in consideration.
/// let mut new_value = most_recent.value().to_vec();
/// new_value.extend_from_slice(b" more data");
///
/// // 2. Increment the sequence number to be higher than the most recent's.
/// let most_recent_seq = most_recent.seq();
/// let new_seq = most_recent_seq + 1;
///
/// (
/// MutableItem::new(signing_key, &new_value, new_seq, salt),
/// // 3. Use the most recent [MutableItem::seq] as a `CAS`.
/// Some(most_recent_seq)
/// )
/// } else {
/// (MutableItem::new(signing_key, b"first value", 1, salt), None)
/// };
///
/// dht.put_mutable(item, cas).await.unwrap();
/// });
/// ```
///
/// ## Errors
///
/// In addition to the [PutQueryError] common with all PUT queries, PUT mutable item
/// query has other [Concurrency errors][crate::rpc::ConcurrencyError], that try to detect write conflict
/// risks or obvious conflicts.
///
/// If you are lucky to get one of these errors (which is not guaranteed), then you should
/// read the most recent item again, and repeat the steps in the previous example.
pub async fn put_mutable(
&self,
item: MutableItem,
cas: Option<i64>,
) -> Result<Id, PutMutableError> {
let request = PutRequestSpecific::PutMutable(PutMutableRequestArguments::from(item, cas));
self.put(request, None).await.map_err(|error| match error {
PutError::Query(err) => PutMutableError::Query(err),
PutError::Concurrency(err) => PutMutableError::Concurrency(err),
})
}
// === Raw ===
/// Get closet nodes to a specific target, that support [BEP_0044](https://www.bittorrent.org/beps/bep_0044.html).
///
/// Useful to [Self::put] a request to nodes further from the 20 closest nodes to the
/// [PutRequestSpecific::target]. Which itself is useful to circumvent [extreme vertical sybil attacks](https://github.com/pubky/mainline/blob/main/docs/censorship-resistance.md#extreme-vertical-sybil-attacks).
pub async fn get_closest_nodes(&self, target: Id) -> Box<[Node]> {
let (tx, rx) = flume::unbounded::<Box<[Node]>>();
self.send(ActorMessage::Get(
GetRequestSpecific::GetValue(GetValueRequestArguments {
target,
salt: None,
seq: None,
}),
ResponseSender::ClosestNodes(tx),
));
rx.recv_async()
.await
.expect("Query was dropped before sending a response, please open an issue.")
}
/// Send a PUT request to the closest nodes, and optionally some extra nodes.
///
/// This is useful to put data to regions of the DHT other than the closest nodes
/// to this request's [target][PutRequestSpecific::target].
///
/// You can find nodes close to other regions of the network by calling
/// [Self::get_closest_nodes] with the target that you want to find the closest nodes to.
///
/// Note: extra nodes need to have [Node::valid_token].
pub async fn put(
&self,
request: PutRequestSpecific,
extra_nodes: Option<Box<[Node]>>,
) -> Result<Id, PutError> {
self.put_inner(request, extra_nodes)
.recv_async()
.await
.expect("Query was dropped before sending a response, please open an issue.")
}
// === Private Methods ===
pub(crate) fn put_inner(
&self,
request: PutRequestSpecific,
extra_nodes: Option<Box<[Node]>>,
) -> flume::Receiver<Result<Id, PutError>> {
let (tx, rx) = flume::bounded::<Result<Id, PutError>>(1);
self.send(ActorMessage::Put(request, tx, extra_nodes));
rx
}
fn send(&self, message: ActorMessage) {
self.0.send(message)
}
}
/// A [Stream] of incoming peers, immutable or mutable values.
pub struct GetStream<T: 'static>(flume::r#async::RecvStream<'static, T>);
impl<T> Stream for GetStream<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
this.0.poll_next(cx)
}
}
#[cfg(test)]
mod test {
use std::net::Ipv4Addr;
use std::{str::FromStr, time::Duration};
use ed25519_dalek::SigningKey;
use futures::StreamExt;
use crate::{dht::Testnet, rpc::ConcurrencyError};
use super::*;
#[test]
fn announce_get_peer() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let a = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let b = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let info_hash = Id::random();
a.announce_peer(info_hash, Some(45555))
.await
.expect("failed to announce");
let peers = b.get_peers(info_hash).next().await.expect("No peers");
assert_eq!(peers.first().unwrap().port(), 45555);
}
futures::executor::block_on(test());
}
#[test]
fn put_get_immutable() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let a = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let b = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let value = b"Hello World!";
let expected_target = Id::from_str("e5f96f6f38320f0f33959cb4d3d656452117aadb").unwrap();
let target = a.put_immutable(value).await.unwrap();
assert_eq!(target, expected_target);
let response = b.get_immutable(target).await;
assert_eq!(response, Some(value.to_vec().into_boxed_slice()));
}
futures::executor::block_on(test());
}
#[test]
fn put_get_mutable() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let a = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let b = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let seq = 1000;
let value = b"Hello World!";
let item = MutableItem::new(signer.clone(), value, seq, None);
a.put_mutable(item.clone(), None).await.unwrap();
let response = b
.get_mutable(signer.verifying_key().as_bytes(), None, None)
.next()
.await
.expect("No mutable values");
assert_eq!(&response, &item);
}
futures::executor::block_on(test());
}
#[test]
fn put_get_mutable_no_more_recent_value() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let a = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let b = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let seq = 1000;
let value = b"Hello World!";
let item = MutableItem::new(signer.clone(), value, seq, None);
a.put_mutable(item.clone(), None).await.unwrap();
let response = b
.get_mutable(signer.verifying_key().as_bytes(), None, Some(seq))
.next()
.await;
assert!(&response.is_none());
}
futures::executor::block_on(test());
}
#[test]
fn repeated_put_query() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let a = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let first = a.put_immutable(&[1, 2, 3]).await;
let second = a.put_immutable(&[1, 2, 3]).await;
assert_eq!(first.unwrap(), second.unwrap());
}
futures::executor::block_on(test());
}
#[test]
fn concurrent_get_mutable() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let a = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let b = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let seq = 1000;
let value = b"Hello World!";
let item = MutableItem::new(signer.clone(), value, seq, None);
a.put_mutable(item.clone(), None).await.unwrap();
let _response_first = b
.get_mutable(signer.verifying_key().as_bytes(), None, None)
.next()
.await
.expect("No mutable values");
let response_second = b
.get_mutable(signer.verifying_key().as_bytes(), None, None)
.next()
.await
.expect("No mutable values");
assert_eq!(&response_second, &item);
}
futures::executor::block_on(test());
}
#[test]
fn concurrent_put_mutable_same() {
let testnet = Testnet::builder(10).build().unwrap();
let dht = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let seq = 1000;
let value = b"Hello World!";
let item = MutableItem::new(signer.clone(), value, seq, None);
let mut handles = vec![];
for _ in 0..2 {
let dht = dht.clone();
let item = item.clone();
let handle = std::thread::spawn(move || {
futures::executor::block_on(async { dht.put_mutable(item, None).await.unwrap() });
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}
#[test]
fn concurrent_put_mutable_different() {
let testnet = Testnet::builder(10).build().unwrap();
let dht = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let mut handles = vec![];
for i in 0..2 {
let dht = dht.clone();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let seq = 1000;
let mut value = b"Hello World!".to_vec();
value.push(i);
let item = MutableItem::new(signer.clone(), &value, seq, None);
let handle = std::thread::spawn(move || {
futures::executor::block_on(async {
let result = dht.put_mutable(item, None).await;
if i == 0 {
assert!(result.is_ok())
} else {
assert!(matches!(
result,
Err(PutMutableError::Concurrency(ConcurrencyError::ConflictRisk))
))
}
})
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}
#[test]
fn concurrent_put_mutable_different_with_cas() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let dht = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let value = b"Hello World!".to_vec();
// First
{
let item = MutableItem::new(signer.clone(), &value, 1000, None);
let (sender, _) = flume::bounded::<Result<Id, PutError>>(1);
let request =
PutRequestSpecific::PutMutable(PutMutableRequestArguments::from(item, None));
dht.0
.0
.send(ActorMessage::Put(request, sender, None))
.unwrap();
}
std::thread::sleep(Duration::from_millis(100));
// Second
{
let item = MutableItem::new(signer, &value, 1001, None);
let most_recent = dht.get_mutable_most_recent(item.key(), None).await;
if let Some(cas) = most_recent.map(|item| item.seq()) {
dht.put_mutable(item, Some(cas)).await.unwrap();
} else {
dht.put_mutable(item, None).await.unwrap();
}
}
}
futures::executor::block_on(test());
}
#[test]
fn conflict_301_cas() {
async fn test() {
let testnet = Testnet::builder(10).build().unwrap();
let dht = Dht::builder()
.bootstrap(&testnet.bootstrap)
.bind_address(Ipv4Addr::LOCALHOST)
.build()
.unwrap()
.as_async();
let signer = SigningKey::from_bytes(&[
56, 171, 62, 85, 105, 58, 155, 209, 189, 8, 59, 109, 137, 84, 84, 201, 221, 115, 7,
228, 127, 70, 4, 204, 182, 64, 77, 98, 92, 215, 27, 103,
]);
let value = b"Hello World!".to_vec();
dht.put_mutable(MutableItem::new(signer.clone(), &value, 1001, None), None)
.await
.unwrap();
assert!(matches!(
dht.put_mutable(MutableItem::new(signer, &value, 1002, None), Some(1000))
.await,
Err(PutMutableError::Concurrency(ConcurrencyError::CasFailed))
));
}
futures::executor::block_on(test());
}
}