-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathassets.rs
More file actions
1719 lines (1566 loc) · 66.2 KB
/
Copy pathassets.rs
File metadata and controls
1719 lines (1566 loc) · 66.2 KB
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pub mod cleanup;
#[cfg(test)]
pub mod test_utils;
use crate::db::{DBCol, SecretDB, SecretDBUpdate};
use crate::primitives::{ParticipantId, UniqueId};
use crate::providers::HasParticipants;
use borsh::BorshDeserialize;
use futures::FutureExt;
use near_time::Clock;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::collections::{HashSet, VecDeque};
use std::sync::{Arc, Mutex};
/// The cold queue contains a collection of assets and a condition function.
/// The queue is divided into three sections by two barriers:
///
/// 0 cold_ready cold_available queue.len()
/// -------------------------------- -------------------- -----------------------------
/// │ Condition-satisfying assets | Unknown assets │ Non-satisfying assets |
/// -----------------------------------------------------------------------------------
///
/// The queue may be modified in the following ways:
/// 1. When taking assets which satisfy the condition we poll the front
/// of the queue, but not beyond the cold_available barrier.
/// 2. When discarding assets *not* satisfying the condition we poll the back
/// of the queue, but not beyond the cold_ready barrier.
/// 3. The condition is always evaluated before adding elements to the queue.
/// If the element *satisfies* the condition it is inserted at the front.
/// If the element *doesn't satisfy* the condition it is inserted at the back.
/// 4. When the condition changes the barriers are reset, marking
/// the entire queue as unknown.
/// 5. When taking an asset matching a caller-supplied condition value we may
/// remove from any position before the cold_available barrier. Barriers
/// past the removed position shift down by one.
///
/// NB: Assets may be reordered by these operations. No guarantees are made on the order in which
/// assets are taken or discarded from the queue.
///
struct ColdQueue<T, CondVal: Default + Eq> {
cold_ready: usize,
cold_available: usize,
cold_queue: VecDeque<(UniqueId, T)>,
/// The last condition value that was used to check against the cold queue elements.
/// Whenever the current condition value changes, we need to update the cold_available barrier.
last_condition_value: CondVal,
/// The actual condition function; this doesn't change.
condition: fn(&CondVal, &T) -> bool,
/// Function to fetch the condition value.
condition_value_fetcher: Arc<dyn Fn() -> CondVal + Send + Sync>,
/// The time when we should next fetch the condition value.
next_fetch_due: near_time::Instant,
clock: Clock,
}
impl<T, CondVal: Default + Eq> ColdQueue<T, CondVal> {
pub(self) fn new(
clock: Clock,
condition: fn(&CondVal, &T) -> bool,
condition_value_fetcher: Arc<dyn Fn() -> CondVal + Send + Sync>,
) -> Self {
Self {
cold_ready: 0,
cold_available: 0,
cold_queue: VecDeque::new(),
last_condition_value: Default::default(),
condition,
condition_value_fetcher,
next_fetch_due: clock.now(),
clock,
}
}
/// Unconditionally update the condition value;
/// If the condition value changed, reset the barriers.
pub(self) fn update_condition_value(&mut self) {
const CONDITION_REFRESH_INTERVAL: near_time::Duration = near_time::Duration::seconds(1);
self.next_fetch_due = self.clock.now() + CONDITION_REFRESH_INTERVAL;
let new_condition_value = (self.condition_value_fetcher)();
if new_condition_value != self.last_condition_value {
self.last_condition_value = new_condition_value;
self.cold_ready = 0;
self.cold_available = self.cold_queue.len();
}
}
fn update_condition_value_if_due(&mut self) {
if self.clock.now() < self.next_fetch_due {
return;
}
self.update_condition_value();
}
/// Try to remove and return an element that satisfies the current condition.
/// If the element doesn't match, it will be moved to the end of the queue.
pub(self) fn take(&mut self) -> ColdQueueTakeResult<T> {
self.update_condition_value_if_due();
if self.cold_available == 0 {
return ColdQueueTakeResult::NotTakenAndNoneAvailable;
}
let (id, value) = self.cold_queue.pop_front().unwrap(); // can't fail
self.cold_available -= 1;
if self.cold_ready > 0 {
self.cold_ready -= 1;
return ColdQueueTakeResult::Taken((id, value));
}
if (self.condition)(&self.last_condition_value, &value) {
return ColdQueueTakeResult::Taken((id, value));
}
self.cold_queue.push_back((id, value));
ColdQueueTakeResult::NotTakenButSomeMayBeAvailable
}
/// Try to remove and return an element that *doesn't* satisfy the current condition.
/// If the element does satisfy it, it will be moved to the front of the queue.
pub(self) fn discard(&mut self) -> ColdQueueDiscardResult<T> {
self.update_condition_value_if_due();
if self.cold_ready == self.cold_queue.len() {
return ColdQueueDiscardResult::NotDiscardedAndNoneAvailable;
}
let (id, value) = self.cold_queue.pop_back().unwrap(); // can't fail
let condition_satisfied = if self.cold_available > self.cold_queue.len() {
self.cold_available -= 1;
(self.condition)(&self.last_condition_value, &value)
} else {
false
};
if !condition_satisfied {
return ColdQueueDiscardResult::Discarded((id, value));
}
self.cold_queue.push_front((id, value));
self.cold_ready += 1;
self.cold_available += 1;
ColdQueueDiscardResult::NotDiscardedButSomeMayBeAvailable
}
/// Adds an element to the cold queue. If the condition is *not* satisfied,
/// instead of adding, it is returned. Otherwise, adds it to the front of the queue.
pub(self) fn add_if_condition_satisfied(
&mut self,
id: UniqueId,
value: T,
) -> ColdQueueAddIfSatisfiedResult<T> {
self.update_condition_value_if_due();
if (self.condition)(&self.last_condition_value, &value) {
self.cold_queue.push_front((id, value));
self.cold_ready += 1;
self.cold_available += 1;
return ColdQueueAddIfSatisfiedResult::Enqueued;
}
ColdQueueAddIfSatisfiedResult::ConditionNotSatisfied(value)
}
/// Adds an element to the cold queue. If the condition is satisfied,
/// instead of adding, it is returned. Otherwise, adds it to the end of the cold
/// queue after the barrier.
pub(self) fn add_if_condition_not_satisfied(
&mut self,
id: UniqueId,
value: T,
) -> ColdQueueAddIfNotSatisfiedResult<T> {
self.update_condition_value_if_due();
if (self.condition)(&self.last_condition_value, &value) {
return ColdQueueAddIfNotSatisfiedResult::ConditionSatisfied(value);
}
self.cold_queue.push_back((id, value));
ColdQueueAddIfNotSatisfiedResult::Enqueued
}
/// Adds an element to the cold queue unconditionally, never returned.
pub(self) fn ingest(&mut self, id: UniqueId, value: T) {
self.update_condition_value_if_due();
if (self.condition)(&self.last_condition_value, &value) {
self.cold_queue.push_front((id, value));
self.cold_ready += 1;
self.cold_available += 1;
} else {
self.cold_queue.push_back((id, value));
}
}
/// Removes and returns the first element satisfying both the standing
/// condition and the caller-supplied `cond_val`, shifting the barriers
/// ([`ColdQueue::cold_ready`] and [`ColdQueue::cold_available`]) that lie
/// past the removed position.
pub(self) fn take_first_matching(&mut self, cond_val: &CondVal) -> Option<(UniqueId, T)> {
self.update_condition_value_if_due();
let pos = self
.cold_queue
.iter()
.take(self.cold_available)
.position(|(_, val)| {
(self.condition)(&self.last_condition_value, val) && (self.condition)(cond_val, val)
})?;
if pos < self.cold_ready {
self.cold_ready -= 1;
}
if pos < self.cold_available {
self.cold_available -= 1;
}
self.cold_queue.remove(pos)
}
}
enum ColdQueueTakeResult<T> {
Taken((UniqueId, T)),
NotTakenButSomeMayBeAvailable,
NotTakenAndNoneAvailable,
}
enum ColdQueueDiscardResult<T> {
Discarded((UniqueId, T)),
NotDiscardedButSomeMayBeAvailable,
NotDiscardedAndNoneAvailable,
}
enum ColdQueueAddIfSatisfiedResult<T> {
ConditionNotSatisfied(T),
Enqueued,
}
enum ColdQueueAddIfNotSatisfiedResult<T> {
ConditionSatisfied(T),
Enqueued,
}
pub struct DoubleQueue<T, CondVal: Default + Eq>
where
T: Send + 'static,
{
hot_sender: flume::Sender<(UniqueId, T)>,
hot_receiver: flume::Receiver<(UniqueId, T)>,
cold_queue: Arc<Mutex<ColdQueue<T, CondVal>>>,
clock: Clock,
cold_queue_changed: tokio::sync::Notify,
}
impl<T, CondVal: Default + Eq> DoubleQueue<T, CondVal>
where
T: Send + 'static,
{
pub fn new(
clock: Clock,
condition: fn(&CondVal, &T) -> bool,
condition_value_fetcher: Arc<dyn Fn() -> CondVal + Send + Sync>,
) -> Self {
let (hot_sender, hot_receiver) = flume::unbounded();
Self {
hot_sender,
hot_receiver,
cold_queue: Arc::new(Mutex::new(ColdQueue::new(
clock.clone(),
condition,
condition_value_fetcher,
))),
clock,
cold_queue_changed: tokio::sync::Notify::new(),
}
}
pub fn add_owned(&self, id: UniqueId, value: T) {
self.hot_sender.send((id, value)).unwrap()
}
pub async fn take_owned(&self) -> (UniqueId, T) {
// Always query the new condition value before taking an element.
// This is to prevent the case where the condition has been updated,
// but we're not yet aware of it, and the caller calls this in a loop and
// we keep yielding undesired elements, but the caller keeps throwing them
// away and we quickly exhaust the available assets.
self.cold_queue.lock().unwrap().update_condition_value();
loop {
let cold_queue_changed = self.cold_queue_changed.notified();
let taken = self.cold_queue.lock().unwrap().take();
match taken {
ColdQueueTakeResult::Taken(result) => {
return result;
}
ColdQueueTakeResult::NotTakenButSomeMayBeAvailable => {
continue;
}
ColdQueueTakeResult::NotTakenAndNoneAvailable => {
// If the cold queue is exhausted, wait for a new element that is just produced.
// Then, if that element also doesn't satisfy our condition, we put it in the cold
// queue and continue.
tokio::select! {
_ = self.clock.sleep(near_time::Duration::seconds(1)) => {
// Don't wait for too long, because the condition could have changed
// making a cold queue element eligible.
continue;
}
_ = cold_queue_changed => {
continue;
}
received = self.hot_receiver.recv_async() => {
let (id, value) = received.expect("should never fail because self keeps a sender");
match self.cold_queue.lock().unwrap().add_if_condition_not_satisfied(id, value) {
ColdQueueAddIfNotSatisfiedResult::ConditionSatisfied(value) => {
return (id, value);
}
ColdQueueAddIfNotSatisfiedResult::Enqueued => {
continue;
}
}
}
}
}
}
}
}
pub async fn take_owned_matching(&self, cond_val: CondVal) -> (UniqueId, T) {
loop {
let cold_queue_changed = self.cold_queue_changed.notified();
let (taken, ingested) = {
let mut cold = self.cold_queue.lock().unwrap();
let mut ingested = false;
while let Some(Ok((id, value))) = self.hot_receiver.recv_async().now_or_never() {
cold.ingest(id, value);
ingested = true;
}
(cold.take_first_matching(&cond_val), ingested)
};
if ingested {
self.cold_queue_changed.notify_waiters();
}
if let Some(taken) = taken {
return taken;
}
// If the cold queue is exhausted, wait for a new element.
tokio::select! {
_ = self.clock.sleep(near_time::Duration::seconds(1)) => {
continue;
}
_ = cold_queue_changed => {
continue;
}
received = self.hot_receiver.recv_async() => {
let (id, value) = received.expect("should never fail because self keeps a sender");
self.cold_queue.lock().unwrap().ingest(id, value);
}
}
}
}
/// Process `num_elements_to_process`, removing any that doesn't satisfy condition.
/// Return ids, that were removed from cold storage.
pub async fn maybe_discard_owned(&self, mut num_elements_to_process: usize) -> Vec<UniqueId> {
self.cold_queue.lock().unwrap().update_condition_value();
let mut removed_from_cold_queue: Vec<UniqueId> = vec![];
// First process elements in the cold queue
while num_elements_to_process > 0 {
let discarded = self.cold_queue.lock().unwrap().discard();
match discarded {
ColdQueueDiscardResult::Discarded((id, _)) => {
removed_from_cold_queue.push(id);
num_elements_to_process -= 1;
continue;
}
ColdQueueDiscardResult::NotDiscardedButSomeMayBeAvailable => {
num_elements_to_process -= 1;
continue;
}
ColdQueueDiscardResult::NotDiscardedAndNoneAvailable => {
break;
}
}
}
// If the cold queue is exhausted, process elements buffered in the hot queue
while num_elements_to_process > 0 {
match self.hot_receiver.recv_async().now_or_never() {
Some(Ok((id, value))) => {
num_elements_to_process -= 1;
let _ = self
.cold_queue
.lock()
.unwrap()
.add_if_condition_satisfied(id, value);
}
_ => {
// Nothing waiting in the hot queue
break;
}
}
}
removed_from_cold_queue
}
pub fn available(&self) -> usize {
self.hot_receiver.len() + self.cold_queue.lock().unwrap().cold_available
}
pub fn ready(&self) -> usize {
self.cold_queue.lock().unwrap().cold_ready
}
pub fn offline(&self) -> usize {
let cold_queue = self.cold_queue.lock().unwrap();
cold_queue.cold_queue.len() - cold_queue.cold_available
}
}
/// Persistent storage for a single type of asset (triples or presignatures).
/// The storage is distributed across all participants, with each participant
/// owning some of the assets. Each asset has exactly one owner.
///
/// Only the owner of an asset may pick the asset for use in an MPC computation.
/// As the owner, the `take_owned` method removes a usable asset from the
/// storage and returns it, waiting if there isn't one available yet. An asset is
/// usable iff the set of participants associated with it are all alive.
///
/// As a passive participant of a computation, unowned assets are taken using
/// `take_unowned`.
pub struct DistributedAssetStorage<T>
where
T: Serialize + DeserializeOwned + Send + 'static,
{
db: Arc<SecretDB>,
col: DBCol,
/// Byte prefix prepended to every key written under `col`. Empty [`Vec`] means
/// no prefix (i.e. the original layout where keys were just
/// `borsh(UniqueId)`).
prefix: Vec<u8>,
my_participant_id: ParticipantId,
owned_queue: DoubleQueue<T, Vec<ParticipantId>>,
last_id: Mutex<Option<UniqueId>>,
/// Guards against concurrent `take_unowned` calls for the same ID.
/// An ID is inserted before the DB read and removed after the delete commits,
/// so two racing callers cannot both succeed for the same asset.
unowned_in_flight: Mutex<HashSet<UniqueId>>,
}
/// Iterates over a key range in column `db_col`, determined by
/// [`DistributedAssetStorage::<T>::make_prefix_range(my_participant_id, prefix)`],
/// and stages a delete on `update_writer` for every entry that evaluates `false`
/// for `is_subset_of_active_participants(persistent_participants)`.
///
/// The caller is responsible for committing `update_writer`. Sharing a writer
/// across multiple calls lets a single cleanup pass (per-`t` triple columns +
/// per-domain presignature columns + epoch marker) be committed as one atomic
/// batch.
pub fn clean_db<T>(
db: &Arc<SecretDB>,
update_writer: &mut SecretDBUpdate,
db_col: DBCol,
persistent_participants: &[ParticipantId],
my_participant_id: ParticipantId,
prefix: &[u8],
) -> anyhow::Result<()>
where
T: Serialize + DeserializeOwned + Send + 'static + HasParticipants,
{
let (start, end): (Vec<u8>, Vec<u8>) =
DistributedAssetStorage::<T>::make_prefix_range(my_participant_id, prefix);
for item in db.iter_range(db_col, &start, &end) {
let (key, value) = item?;
let value: T = serde_json::from_slice(&value)?;
if !value.is_subset_of_active_participants(persistent_participants) {
update_writer.delete(db_col, &key);
}
}
Ok(())
}
impl<T> DistributedAssetStorage<T>
where
T: Serialize + DeserializeOwned + Send + 'static,
{
pub fn new(
clock: Clock,
db: Arc<SecretDB>,
col: DBCol,
prefix: Vec<u8>,
my_participant_id: ParticipantId,
condition: fn(&Vec<ParticipantId>, &T) -> bool,
alive_participant_ids_query: Arc<dyn Fn() -> Vec<ParticipantId> + Send + Sync>,
) -> anyhow::Result<Self> {
let owned_queue = DoubleQueue::new(clock, condition, alive_participant_ids_query);
// We're just going to replicate the owned assets to memory. It's not the most efficient,
// but it's the simplest way to implement a multi-consumer, multi-producer queue that
// supports asynchronous blocking when an asset isn't available.
let mut last_id = None;
let (start, end) = Self::make_prefix_range(my_participant_id, &prefix);
for item in db.iter_range(col, &start, &end) {
let (key, value) = item?;
let id = Self::decode_key(&key, prefix.len())?;
let value = serde_json::from_slice(&value)?;
owned_queue.add_owned(id, value);
last_id = Some(id);
}
Ok(Self {
db,
col,
prefix,
my_participant_id,
owned_queue,
last_id: Mutex::new(last_id),
unowned_in_flight: Mutex::new(HashSet::new()),
})
}
fn make_prefix_range(participant_id: ParticipantId, prefix: &[u8]) -> (Vec<u8>, Vec<u8>) {
let mut start = prefix.to_vec();
let mut end = prefix.to_vec();
start.extend_from_slice(&UniqueId::prefix_for_participant_id(participant_id));
end.extend_from_slice(&UniqueId::prefix_for_participant_id(
ParticipantId::from_raw(participant_id.raw().checked_add(1).unwrap()),
));
(start, end)
}
fn make_key(&self, id: UniqueId) -> Vec<u8> {
let mut key = self.prefix.clone();
key.extend_from_slice(&borsh::to_vec(&id).unwrap());
key
}
fn decode_key(key: &[u8], prefix_len: usize) -> anyhow::Result<UniqueId> {
Ok(UniqueId::try_from_slice(&key[prefix_len..])?)
}
/// Generates an ID that won't conflict with existing ones, and reserves it
/// so that the next call to the same function will return a different one.
/// TODO(#10): This reservation does not persist across restarts, leading to
/// the assumption that the clock moves forward at least a second across
/// restarts.
pub fn generate_and_reserve_id(&self) -> UniqueId {
self.generate_and_reserve_id_range(1)
}
/// Same as `generate_and_reserve_id`, but for a range of IDs.
/// The returned ID represents a range that starts from that ID and ending at
/// that ID .add_to_counter(count - 1).
pub fn generate_and_reserve_id_range(&self, count: u32) -> UniqueId {
assert!(count > 0);
let mut last_id = self.last_id.lock().unwrap();
let start = match *last_id {
Some(last_id) => last_id.pick_new_after(),
None => UniqueId::generate(self.my_participant_id),
};
let end = start.add_to_counter(count - 1).unwrap();
*last_id = Some(end);
start
}
/// Returns the current number of owned assets in the database.
/// Excludes assets which are known to have offline participants.
pub fn num_owned(&self) -> usize {
self.owned_queue.available()
}
/// Returns the current number of owned assets in the database which
/// are known to have all participants alive.
pub fn num_owned_ready(&self) -> usize {
self.owned_queue.ready()
}
/// Returns the current number of owned assets in the database which
/// are known to have some participant offline.
pub fn num_owned_offline(&self) -> usize {
self.owned_queue.offline()
}
pub async fn take_owned(&self) -> (UniqueId, T) {
let (id, asset) = self.owned_queue.take_owned().await;
let mut update = self.db.update();
update.delete(self.col, &self.make_key(id));
update
.commit()
.expect("Unrecoverable error writing to database");
(id, asset)
}
/// Adds an owned asset to the storage.
pub fn add_owned(&self, id: UniqueId, value: T) {
let key = self.make_key(id);
let value_ser = serde_json::to_vec(&value).unwrap();
let mut update = self.db.update();
update.put(self.col, &key, &value_ser);
update
.commit()
.expect("Unrecoverable error writing to database");
// Can't fail, because we keep a receiver alive.
self.owned_queue.add_owned(id, value);
}
/// Examines up to `num_assets_to_process` elements in the storage.
/// If any are found not to satisfy the current condition, they are discarded.
/// Otherwise, they are kept aside as ready for immediate use.
pub async fn maybe_discard_owned(&self, num_assets_to_process: usize) {
let removed_cold_ids = self
.owned_queue
.maybe_discard_owned(num_assets_to_process)
.await;
if !removed_cold_ids.is_empty() {
let mut update = self.db.update();
for id in removed_cold_ids {
update.delete(self.col, &self.make_key(id));
}
update
.commit()
.expect("Unrecoverable error writing to database");
}
}
/// Adds an unowned asset to the storage.
pub fn add_unowned(&self, id: UniqueId, value: T) {
let key = self.make_key(id);
let value_ser = serde_json::to_vec(&value).unwrap();
let mut update = self.db.update();
update.put(self.col, &key, &value_ser);
update
.commit()
.expect("Unrecoverable error writing to database");
}
/// Removes an unowned asset from the storage and returns it. Returns
/// an error if we do not have the asset in our database or if a concurrent
/// call is already taking the same asset.
pub fn take_unowned(&self, id: UniqueId) -> anyhow::Result<T> {
// Prevent two concurrent callers from both reading the same asset
// before either commits the delete (read-then-delete race).
{
let mut in_flight = self.unowned_in_flight.lock().unwrap();
if !in_flight.insert(id) {
anyhow::bail!(
"Unowned {} is already being taken by another task: {:?}",
self.col,
id
);
}
}
let result = self.take_unowned_inner(id);
// Always remove from in-flight, whether the take succeeded or not.
self.unowned_in_flight.lock().unwrap().remove(&id);
result
}
/// Takes an owned asset satisfying both the standing alive-condition and
/// the supplied `eligible` set. Blocks indefinitely if none becomes
/// available.
/// Callers are expected to enforce their own timeout.
pub async fn take_owned_matching(&self, eligible: Vec<ParticipantId>) -> (UniqueId, T) {
let (id, val) = self.owned_queue.take_owned_matching(eligible).await;
let mut update = self.db.update();
update.delete(self.col, &self.make_key(id));
update
.commit()
// TODO(#4090): propagate err instead in here and rest of the functions
// in this file.
.expect("Unrecoverable error writing to database");
(id, val)
}
fn take_unowned_inner(&self, id: UniqueId) -> anyhow::Result<T> {
let key = self.make_key(id);
let value_ser = self.db.get(self.col, &key)?.ok_or_else(|| {
anyhow::anyhow!("Unowned {} not found in the database: {:?}", self.col, id)
})?;
let mut update = self.db.update();
update.delete(self.col, &key);
update
.commit()
.expect("Unrecoverable error writing to database");
Ok(serde_json::from_slice(&value_ser)?)
}
}
#[cfg(test)]
mod tests {
use super::{ColdQueue, DistributedAssetStorage, DoubleQueue, UniqueId};
use crate::assets::clean_db;
use crate::async_testing::{MaybeReady, run_future_once};
use crate::db::DBCol;
use crate::primitives::ParticipantId;
use crate::providers::HasParticipants;
use borsh::BorshDeserialize;
use futures::FutureExt;
use mpc_primitives::domain::DomainId;
use near_time::FakeClock;
use serde::{Deserialize, Serialize};
use std::cmp::Eq;
use std::default::Default;
use std::sync::atomic::{AtomicI32, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
/// Adapter used by tests that previously took `Option<DomainId>` to compose
/// the equivalent prefix bytes for the generalized [`DistributedAssetStorage`].
fn domain_id_to_prefix(domain_id: Option<DomainId>) -> Vec<u8> {
match domain_id {
Some(d) => d.0.to_be_bytes().to_vec(),
None => Vec::new(),
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
struct ParticipantsWithI32(pub Vec<ParticipantId>, pub i32);
impl HasParticipants for ParticipantsWithI32 {
fn is_subset_of_active_participants(&self, active_participants: &[ParticipantId]) -> bool {
self.0.iter().all(|p| active_participants.contains(p))
}
}
fn verify_cold_queue_internal_consistency<T, CondVal: Default + Eq>(
queue: &ColdQueue<T, CondVal>,
expected_len: usize,
) {
assert!(queue.cold_ready <= queue.cold_available);
assert!(queue.cold_available <= queue.cold_queue.len());
assert_eq!(expected_len, queue.cold_queue.len());
for (i, (_id, val)) in queue.cold_queue.iter().enumerate() {
let satisfies = (queue.condition)(&queue.last_condition_value, val);
if i < queue.cold_ready {
assert!(satisfies);
}
if queue.cold_available <= i {
assert!(!satisfies);
}
}
}
#[test]
fn test_cold_queue() {
let clock = FakeClock::default();
let cond_value = Arc::new(AtomicI32::new(0));
let mut queue = ColdQueue::new(clock.clock(), |cond, val| val % 2 == *cond, {
let cond_value = cond_value.clone();
Arc::new(move || cond_value.load(Ordering::Relaxed))
});
// Operations on empty
verify_cold_queue_internal_consistency(&queue, 0);
queue.discard();
verify_cold_queue_internal_consistency(&queue, 0);
queue.take();
verify_cold_queue_internal_consistency(&queue, 0);
cond_value.store(1, Ordering::Relaxed);
queue.update_condition_value();
verify_cold_queue_internal_consistency(&queue, 0);
let id1 = UniqueId::new(ParticipantId::from_raw(42), 1, 0);
let id2 = id1.add_to_counter(1).unwrap();
// Insert and remove
queue.add_if_condition_not_satisfied(id1, 1);
verify_cold_queue_internal_consistency(&queue, 0);
queue.add_if_condition_satisfied(id1, 1);
verify_cold_queue_internal_consistency(&queue, 1);
queue.discard();
verify_cold_queue_internal_consistency(&queue, 1);
queue.add_if_condition_not_satisfied(id2, 2);
verify_cold_queue_internal_consistency(&queue, 2);
queue.take();
verify_cold_queue_internal_consistency(&queue, 1);
queue.discard();
verify_cold_queue_internal_consistency(&queue, 0);
// Reset then discard
queue.add_if_condition_satisfied(id1, 1);
cond_value.store(0, Ordering::Relaxed);
queue.update_condition_value();
queue.take();
verify_cold_queue_internal_consistency(&queue, 1);
queue.discard();
verify_cold_queue_internal_consistency(&queue, 0);
// Reset then take
queue.add_if_condition_not_satisfied(id1, 1);
cond_value.store(1, Ordering::Relaxed);
queue.update_condition_value();
queue.discard();
verify_cold_queue_internal_consistency(&queue, 1);
queue.take();
verify_cold_queue_internal_consistency(&queue, 0);
// Take from known satisfying
queue.add_if_condition_satisfied(id1, 1);
verify_cold_queue_internal_consistency(&queue, 1);
queue.take();
verify_cold_queue_internal_consistency(&queue, 0);
// Discard from known non-satisfying
queue.add_if_condition_not_satisfied(id2, 2);
verify_cold_queue_internal_consistency(&queue, 1);
queue.discard();
verify_cold_queue_internal_consistency(&queue, 0);
}
#[test]
fn test_double_queue_discard() {
let clock = FakeClock::default();
let cond_value = Arc::new(AtomicI32::new(0));
let cond_value_query_count = Arc::new(AtomicUsize::new(0));
let queue = DoubleQueue::new(clock.clock(), |cond, val| val % 2 == *cond, {
let cond_value = cond_value.clone();
let cond_value_query_count = cond_value_query_count.clone();
Arc::new(move || {
cond_value_query_count.fetch_add(1, Ordering::Relaxed);
cond_value.load(Ordering::Relaxed)
})
});
// Discard should never block, even if the queue is completely empty
queue.maybe_discard_owned(3).now_or_never().unwrap();
// Add 3 elements, 2 of which don't match the condition
let id1 = UniqueId::new(ParticipantId::from_raw(42), 123, 456);
let id2 = id1.add_to_counter(1).unwrap();
let id3 = id1.add_to_counter(2).unwrap();
let id4 = id1.add_to_counter(3).unwrap();
queue.add_owned(id1, 1);
queue.add_owned(id2, 2);
queue.add_owned(id3, 3);
assert_eq!(queue.available(), 3);
queue.maybe_discard_owned(1).now_or_never().unwrap();
assert_eq!(queue.available(), 2);
assert_eq!(queue.take_owned().now_or_never().unwrap(), (id2, 2));
assert_eq!(queue.available(), 1);
queue.maybe_discard_owned(1).now_or_never().unwrap();
assert_eq!(queue.available(), 0);
queue.add_owned(id4, 4);
assert_eq!(queue.available(), 1);
queue.maybe_discard_owned(1).now_or_never().unwrap();
assert_eq!(queue.available(), 1);
assert_eq!(queue.take_owned().now_or_never().unwrap(), (id4, 4));
assert_eq!(queue.available(), 0);
}
// This test covers tricky cases around updates to the condition value
#[test]
fn test_double_queue_condition_value() {
let clock = FakeClock::default();
let cond_value = Arc::new(AtomicI32::new(0));
let cond_value_query_count = Arc::new(AtomicUsize::new(0));
let queue = DoubleQueue::new(clock.clock(), |cond, val| val % 2 == *cond, {
let cond_value = cond_value.clone();
let cond_value_query_count = cond_value_query_count.clone();
Arc::new(move || {
cond_value_query_count.fetch_add(1, Ordering::Relaxed);
cond_value.load(Ordering::Relaxed)
})
});
let id1 = UniqueId::new(ParticipantId::from_raw(42), 123, 456);
let id2 = id1.add_to_counter(1).unwrap();
let id3 = id1.add_to_counter(2).unwrap();
let id4 = id1.add_to_counter(3).unwrap();
queue.add_owned(id1, 1);
queue.add_owned(id2, 3);
queue.add_owned(id3, 5);
// Make condition "% 2 == 1".
cond_value.store(1, Ordering::Relaxed);
assert_eq!(queue.take_owned().now_or_never().unwrap(), (id1, 1));
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 1);
// Make condition "% 2 == 0" and start taking an element.
cond_value.store(0, Ordering::Relaxed);
let fut = queue.take_owned();
let MaybeReady::Future(fut) = run_future_once(fut) else {
panic!("should not be able to take value when no element meets condition");
};
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 2);
// Change the condition to "% 2 == 1". The task that has been waiting for an element
// does not immediately notice the condition change, until a timer has passed.
cond_value.store(1, Ordering::Relaxed);
let MaybeReady::Future(fut) = run_future_once(fut) else {
panic!("should not be able to take value even when cond value changed");
};
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 2);
// Advance the clock so that the waiting task notices the condition change.
clock.advance(near_time::Duration::seconds(1));
assert_eq!(fut.now_or_never().unwrap(), (id2, 3));
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 3);
// This time change the condition before starting to take an element.
// It will be observed immediately even though the clock has not been advanced.
cond_value.store(0, Ordering::Relaxed);
let fut = queue.take_owned();
let MaybeReady::Future(fut) = run_future_once(fut) else {
panic!("should not be able to take value when no element meets condition");
};
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 4);
// Change the condition without advancing the clock. The waiting task won't notice.
cond_value.store(1, Ordering::Relaxed);
let MaybeReady::Future(fut) = run_future_once(fut) else {
panic!("should not be able to take value even when cond value changed");
};
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 4);
queue.add_owned(id4, 4);
// Even though the condition changed, we may get an element returned that satisfied a
// stale condition (there's no point to prevent that because there can always be
// races).
assert_eq!(fut.now_or_never().unwrap(), (id4, 4));
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 4);
// However, if we take_owned() again, we'll use the correct condition.
assert_eq!(queue.take_owned().now_or_never().unwrap(), (id3, 5));
assert_eq!(cond_value_query_count.load(Ordering::Relaxed), 5);
}
#[test]
fn test_distributed_assets_storage() {
let clock = FakeClock::default();
let dir = tempfile::tempdir().unwrap();
let db = crate::db::SecretDB::new(dir.path(), [1; 16]).unwrap();
let all_participants = vec![
ParticipantId::from_raw(0),
ParticipantId::from_raw(1),
ParticipantId::from_raw(2),
ParticipantId::from_raw(3),
];
let first_participants_subset = vec![
ParticipantId::from_raw(0),
ParticipantId::from_raw(1),
ParticipantId::from_raw(2),
];
let second_participants_subset = vec![
ParticipantId::from_raw(1),
ParticipantId::from_raw(2),
ParticipantId::from_raw(3),
];
let alive_participants = Arc::new(Mutex::new(all_participants.clone()));
let store = DistributedAssetStorage::<ParticipantsWithI32>::new(
clock.clock(),
db,
crate::db::DBCol::TripleV2,
Vec::new(),
ParticipantId::from_raw(42),
|cond, val| val.is_subset_of_active_participants(cond),
{
let alive_participants = alive_participants.clone();
Arc::new(move || alive_participants.lock().unwrap().clone())
},
)
.unwrap();
assert_eq!(store.num_owned(), 0);
let id1 = store.generate_and_reserve_id();
let id2 = store.generate_and_reserve_id();
let id3 = store.generate_and_reserve_id();
let id4 = store.generate_and_reserve_id();
let id5 = store.generate_and_reserve_id();
store.add_owned(id1, ParticipantsWithI32(all_participants.clone(), 123));
assert_eq!(store.num_owned(), 1);
store.add_owned(id2, ParticipantsWithI32(all_participants.clone(), 456));
assert_eq!(store.num_owned(), 2);
let asset1 = store.take_owned().now_or_never().unwrap();
assert_eq!(
asset1,
(id1, ParticipantsWithI32(all_participants.clone(), 123))
);
assert_eq!(store.num_owned(), 1);
store.add_owned(
id3,
ParticipantsWithI32(second_participants_subset.clone(), 789),
);
assert_eq!(store.num_owned(), 2);
*alive_participants.lock().unwrap() = first_participants_subset.clone();
let asset_fut = store.take_owned();
let MaybeReady::Future(asset_fut) = run_future_once(asset_fut) else {
panic!("Cannot take value since set of participants has changed");
};
store.add_owned(
id4,
ParticipantsWithI32(first_participants_subset.clone(), 101112),
);
let asset3 = store.take_owned().now_or_never().unwrap();
assert_eq!(
asset3,
(
id4,
ParticipantsWithI32(first_participants_subset.clone(), 101112)
)
);
let MaybeReady::Future(asset_fut) = run_future_once(asset_fut) else {
panic!("Cannot take value since set of participants has changed");