-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathsubscriptions.rs
More file actions
321 lines (280 loc) · 10.7 KB
/
subscriptions.rs
File metadata and controls
321 lines (280 loc) · 10.7 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
use super::{Buffer, Variant};
use commonware_cryptography::Digestible;
use commonware_utils::{
channel::{fallible::OneshotExt, oneshot},
futures::{AbortablePool, Aborter},
};
use std::collections::{btree_map::Entry, BTreeMap};
/// A set of local subscribers waiting for one block.
///
/// Dropping the subscription aborts the backing buffer waiter, if one exists.
struct BlockSubscription<V: Variant> {
subscribers: Vec<oneshot::Sender<V::Block>>,
_aborter: Option<Aborter>,
}
/// The key used to track block subscriptions.
///
/// Digest-scoped and commitment-scoped subscriptions are intentionally distinct
/// so a block that aliases on digest cannot satisfy a different commitment wait.
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
pub(super) enum Key<C, D> {
Digest(D),
Commitment(C),
}
pub(super) type KeyFor<V> =
Key<<V as Variant>::Commitment, <<V as Variant>::Block as Digestible>::Digest>;
pub(super) struct Subscriptions<V: Variant> {
entries: BTreeMap<KeyFor<V>, BlockSubscription<V>>,
}
impl<V: Variant> Subscriptions<V> {
pub(super) const fn new() -> Self {
Self {
entries: BTreeMap::new(),
}
}
pub(super) fn remove(&mut self, key: &KeyFor<V>) {
self.entries.remove(key);
}
pub(super) fn retain_open(&mut self) {
self.entries.retain(|_, subscription| {
subscription
.subscribers
.retain(|subscriber| !subscriber.is_closed());
!subscription.subscribers.is_empty()
});
}
/// Notify any digest- or commitment-scoped subscribers for the provided block.
pub(super) fn notify(&mut self, block: &V::Block) {
if let Some(mut subscription) = self.entries.remove(&Key::Digest(block.digest())) {
for subscriber in subscription.subscribers.drain(..) {
subscriber.send_lossy(block.clone());
}
}
if let Some(mut subscription) = self.entries.remove(&Key::Commitment(V::commitment(block)))
{
for subscriber in subscription.subscribers.drain(..) {
subscriber.send_lossy(block.clone());
}
}
}
pub(super) fn insert<Buf: Buffer<V>>(
&mut self,
key: KeyFor<V>,
response: oneshot::Sender<V::Block>,
waiters: &mut AbortablePool<Result<V::Block, KeyFor<V>>>,
buffer: &Buf,
) {
match self.entries.entry(key) {
Entry::Occupied(mut entry) => {
entry.get_mut().subscribers.push(response);
}
Entry::Vacant(entry) => {
let rx = match key {
Key::Digest(digest) => buffer.subscribe_by_digest(digest),
Key::Commitment(commitment) => buffer.subscribe_by_commitment(commitment),
};
let aborter = rx.map(|rx| {
let waiter_key = key;
waiters.push(async move { rx.await.map_err(|_| waiter_key) })
});
entry.insert(BlockSubscription {
subscribers: vec![response],
_aborter: aborter,
});
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
marshal::{mocks::block::Block, standard::Standard},
types::{Height, Round},
};
use commonware_cryptography::{
ed25519::PublicKey,
sha256::{Digest, Sha256},
Digestible,
};
use commonware_macros::select;
use commonware_p2p::Recipients;
use commonware_runtime::{deterministic, Clock, Runner as _};
use commonware_utils::sync::Mutex;
use futures::FutureExt;
use std::sync::Arc;
type TestBlock = Block<Digest, ()>;
type TestVariant = Standard<TestBlock>;
type TestWaiters = AbortablePool<Result<TestBlock, KeyFor<TestVariant>>>;
type Subscriber = oneshot::Sender<TestBlock>;
type Subscribers = Arc<Mutex<Vec<Subscriber>>>;
#[derive(Clone, Default)]
struct TestBuffer {
digest_subscribers: Subscribers,
commitment_subscribers: Subscribers,
}
impl TestBuffer {
fn digest_subscription_count(&self) -> usize {
self.digest_subscribers.lock().len()
}
fn commitment_subscription_count(&self) -> usize {
self.commitment_subscribers.lock().len()
}
}
impl Buffer<TestVariant> for TestBuffer {
type PublicKey = PublicKey;
async fn find_by_digest(&self, _digest: Digest) -> Option<TestBlock> {
None
}
async fn find_by_commitment(&self, _commitment: Digest) -> Option<TestBlock> {
None
}
fn subscribe_by_digest(&self, _digest: Digest) -> Option<oneshot::Receiver<TestBlock>> {
let (sender, receiver) = oneshot::channel();
self.digest_subscribers.lock().push(sender);
Some(receiver)
}
fn subscribe_by_commitment(&self, _commitment: Digest) -> Option<oneshot::Receiver<TestBlock>> {
let (sender, receiver) = oneshot::channel();
self.commitment_subscribers.lock().push(sender);
Some(receiver)
}
fn finalized(&self, _commitment: Digest) {}
fn send(&self, _round: Round, _block: TestBlock, _recipients: Recipients<PublicKey>) {}
}
fn block(height: u64, timestamp: u64) -> TestBlock {
Block::new::<Sha256>((), Sha256::fill(0), Height::new(height), timestamp)
}
fn assert_receives(receiver: oneshot::Receiver<TestBlock>, expected: &TestBlock) {
let received = receiver
.now_or_never()
.expect("receiver should be ready")
.expect("sender should deliver block");
assert_eq!(received.digest(), expected.digest());
}
#[test]
fn insert_coalesces_duplicate_keys() {
let test_buffer = TestBuffer::default();
let buffer = Some(test_buffer.clone());
let mut waiters = TestWaiters::default();
let mut subscriptions = Subscriptions::<TestVariant>::new();
let block = block(1, 10);
let (first_sender, first_receiver) = oneshot::channel();
subscriptions.insert(
Key::Digest(block.digest()),
first_sender,
&mut waiters,
&buffer,
);
let (second_sender, second_receiver) = oneshot::channel();
subscriptions.insert(
Key::Digest(block.digest()),
second_sender,
&mut waiters,
&buffer,
);
assert_eq!(test_buffer.digest_subscription_count(), 1);
assert_eq!(subscriptions.entries.len(), 1);
subscriptions.notify(&block);
assert_receives(first_receiver, &block);
assert_receives(second_receiver, &block);
assert!(subscriptions.entries.is_empty());
}
#[test]
fn notify_wakes_digest_and_commitment_subscribers() {
let test_buffer = TestBuffer::default();
let buffer = Some(test_buffer.clone());
let mut waiters = TestWaiters::default();
let mut subscriptions = Subscriptions::<TestVariant>::new();
let block = block(2, 20);
let (digest_sender, digest_receiver) = oneshot::channel();
subscriptions.insert(
Key::Digest(block.digest()),
digest_sender,
&mut waiters,
&buffer,
);
let (commitment_sender, commitment_receiver) = oneshot::channel();
subscriptions.insert(
Key::Commitment(block.digest()),
commitment_sender,
&mut waiters,
&buffer,
);
assert_eq!(test_buffer.digest_subscription_count(), 1);
assert_eq!(test_buffer.commitment_subscription_count(), 1);
assert_eq!(subscriptions.entries.len(), 2);
subscriptions.notify(&block);
assert_receives(digest_receiver, &block);
assert_receives(commitment_receiver, &block);
assert!(subscriptions.entries.is_empty());
}
#[test]
fn retain_open_drops_closed_subscribers_and_keeps_open_ones() {
let buffer = Some(TestBuffer::default());
let mut waiters = TestWaiters::default();
let mut subscriptions = Subscriptions::<TestVariant>::new();
let block = block(3, 30);
let (closed_sender, closed_receiver) = oneshot::channel();
subscriptions.insert(
Key::Digest(block.digest()),
closed_sender,
&mut waiters,
&buffer,
);
let (open_sender, open_receiver) = oneshot::channel();
subscriptions.insert(
Key::Digest(block.digest()),
open_sender,
&mut waiters,
&buffer,
);
drop(closed_receiver);
subscriptions.retain_open();
let subscription = subscriptions
.entries
.get(&Key::Digest(block.digest()))
.expect("open subscriber should remain");
assert_eq!(subscription.subscribers.len(), 1);
subscriptions.notify(&block);
assert_receives(open_receiver, &block);
assert!(subscriptions.entries.is_empty());
}
#[test]
fn remove_drops_waiter_and_aborts_buffer_waiter() {
deterministic::Runner::default().start(|context| async move {
let buffer = Some(TestBuffer::default());
let mut waiters = TestWaiters::default();
let mut subscriptions = Subscriptions::<TestVariant>::new();
let block = block(4, 40);
let key = Key::Digest(block.digest());
let (sender, _receiver) = oneshot::channel();
subscriptions.insert(key, sender, &mut waiters, &buffer);
subscriptions.remove(&key);
select! {
completion = waiters.next_completed() => {
assert!(
completion.is_err(),
"removing the subscription should abort the buffer waiter"
);
},
_ = context.sleep(std::time::Duration::from_secs(1)) => {
panic!("waiter should close after subscription removal");
},
}
});
}
#[test]
fn insert_without_buffer_keeps_local_subscriber() {
let mut waiters = TestWaiters::default();
let mut subscriptions = Subscriptions::<TestVariant>::new();
let buffer: Option<TestBuffer> = None;
let block = block(5, 50);
let (sender, receiver) = oneshot::channel();
subscriptions.insert(Key::Digest(block.digest()), sender, &mut waiters, &buffer);
assert_eq!(subscriptions.entries.len(), 1);
subscriptions.notify(&block);
assert_receives(receiver, &block);
assert!(subscriptions.entries.is_empty());
}
}