-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathancestry.rs
More file actions
258 lines (225 loc) · 9.31 KB
/
ancestry.rs
File metadata and controls
258 lines (225 loc) · 9.31 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
//! A stream that yields the ancestors of a block while prefetching parents.
use crate::{types::Height, Block, Heightable};
use commonware_cryptography::Digestible;
use futures::{
future::{BoxFuture, OptionFuture},
FutureExt, Stream,
};
use pin_project::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
/// A stream of blocks used by application propose and verify calls.
pub trait Ancestry<B: Block>: Stream<Item = B> + Send + Unpin + 'static {}
impl<T, B> Ancestry<B> for T
where
T: Stream<Item = B> + Send + Unpin + 'static,
B: Block,
{
}
/// An interface for providing parent blocks.
pub trait BlockProvider: Clone + Send + 'static {
/// The block type the provider walks.
type Block: Block;
/// Subscribe to the parent of a known block.
///
/// If the parent is found available locally, the parent will be returned immediately.
///
/// If the parent is not available locally, the subscription will be registered and the caller
/// will be notified when the parent is available. If the parent is not finalized, it's possible
/// that it may never become available.
///
/// Returns `None` when the subscription is canceled or the provider can no longer deliver
/// the parent.
///
/// The child block can carry variant-specific context needed to retrieve its parent.
fn subscribe_parent(
self,
block: Self::Block,
) -> impl Future<Output = Option<Self::Block>> + Send;
}
/// Yields the ancestors of a block while prefetching parents, _not_ including the genesis block.
///
// TODO(<https://github.com/commonwarexyz/monorepo/issues/2982>): Once marshal can also yield the genesis block,
// this stream should end at block height 0 rather than 1.
#[pin_project]
pub struct AncestorStream<M: BlockProvider> {
buffered: Vec<M::Block>,
marshal: M,
#[pin]
pending: OptionFuture<BoxFuture<'static, Option<M::Block>>>,
}
impl<M: BlockProvider> AncestorStream<M> {
/// Creates a new [AncestorStream] starting from the given ancestry.
///
/// # Panics
///
/// Panics if the initial blocks are not contiguous in height.
pub(crate) fn new(marshal: M, initial: impl IntoIterator<Item = M::Block>) -> Self {
let mut buffered = initial.into_iter().collect::<Vec<M::Block>>();
buffered.sort_by_key(Heightable::height);
// Check that the initial blocks are contiguous in height.
buffered.windows(2).for_each(|window| {
assert_eq!(
window[0].height().next(),
window[1].height(),
"initial blocks must be contiguous in height"
);
assert_eq!(
window[0].digest(),
window[1].parent(),
"initial blocks must be contiguous in ancestry"
);
});
Self {
marshal,
buffered,
pending: None.into(),
}
}
}
impl<M> Stream for AncestorStream<M>
where
M: BlockProvider,
M::Block: Clone,
{
type Item = M::Block;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
// Because marshal cannot currently yield the genesis block, we stop at height 1.
const END_BOUND: Height = Height::new(1);
let mut this = self.project();
// If a result has been buffered, return it and queue the parent fetch if needed.
if let Some(block) = this.buffered.pop() {
let height = block.height();
let should_walk_parent = height > END_BOUND;
let end_of_buffered = this.buffered.is_empty();
if should_walk_parent && end_of_buffered {
let future = this.marshal.clone().subscribe_parent(block.clone()).boxed();
*this.pending.as_mut() = Some(future).into();
// Explicitly poll the next future to kick off the fetch. If it's already ready,
// buffer it for the next poll.
match this.pending.as_mut().poll(cx) {
Poll::Ready(Some(Some(block))) => {
this.buffered.push(block);
}
Poll::Ready(Some(None)) => {
*this.pending.as_mut() = None.into();
}
Poll::Ready(None) | Poll::Pending => {}
}
} else if !should_walk_parent {
// No more parents to fetch; Finish the stream.
*this.pending.as_mut() = None.into();
}
return Poll::Ready(Some(block));
}
match this.pending.as_mut().poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(None) | Poll::Ready(Some(None)) => {
*this.pending.as_mut() = None.into();
Poll::Ready(None)
}
Poll::Ready(Some(Some(block))) => {
let height = block.height();
let should_walk_parent = height > END_BOUND;
if should_walk_parent {
let future = this.marshal.clone().subscribe_parent(block.clone()).boxed();
*this.pending.as_mut() = Some(future).into();
// Explicitly poll the next future to kick off the fetch. If it's already ready,
// buffer it for the next poll.
match this.pending.as_mut().poll(cx) {
Poll::Ready(Some(Some(block))) => {
this.buffered.push(block);
}
Poll::Ready(Some(None)) => {
*this.pending.as_mut() = None.into();
}
Poll::Ready(None) | Poll::Pending => {}
}
} else {
// No more parents to fetch; Finish the stream.
*this.pending.as_mut() = None.into();
}
Poll::Ready(Some(block))
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::marshal::mocks::block::Block;
use commonware_cryptography::{sha256::Digest as Sha256Digest, Digest, Sha256};
use commonware_macros::test_async;
use futures::StreamExt;
#[derive(Default, Clone)]
struct MockProvider(Vec<Block<Sha256Digest, ()>>);
impl BlockProvider for MockProvider {
type Block = Block<Sha256Digest, ()>;
async fn subscribe_parent(self, block: Self::Block) -> Option<Self::Block> {
let parent = block.parent;
self.0.into_iter().find(|b| b.digest() == parent)
}
}
#[test]
#[should_panic = "initial blocks must be contiguous in height"]
fn test_panics_on_non_contiguous_initial_blocks_height() {
AncestorStream::new(
MockProvider::default(),
vec![
Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(1), 1),
Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(3), 3),
],
);
}
#[test]
#[should_panic = "initial blocks must be contiguous in ancestry"]
fn test_panics_on_non_contiguous_initial_blocks_digest() {
AncestorStream::new(
MockProvider::default(),
vec![
Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(1), 1),
Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(2), 2),
],
);
}
#[test_async]
async fn test_empty_yields_none() {
let mut stream: AncestorStream<MockProvider> =
AncestorStream::new(MockProvider::default(), vec![]);
assert_eq!(stream.next().await, None);
}
#[test_async]
async fn test_yields_ancestors() {
let block1 = Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(1), 1);
let block2 = Block::new::<Sha256>((), block1.digest(), Height::new(2), 2);
let block3 = Block::new::<Sha256>((), block2.digest(), Height::new(3), 3);
let provider = MockProvider(vec![block1.clone(), block2.clone()]);
let stream = AncestorStream::new(provider, [block3.clone()]);
let results = stream.collect::<Vec<_>>().await;
assert_eq!(results, vec![block3, block2, block1]);
}
#[test_async]
async fn test_yields_ancestors_all_buffered() {
let block1 = Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(1), 1);
let block2 = Block::new::<Sha256>((), block1.digest(), Height::new(2), 2);
let block3 = Block::new::<Sha256>((), block2.digest(), Height::new(3), 3);
let provider = MockProvider(vec![]);
let stream =
AncestorStream::new(provider, [block1.clone(), block2.clone(), block3.clone()]);
let results = stream.collect::<Vec<_>>().await;
assert_eq!(results, vec![block3, block2, block1]);
}
#[test_async]
async fn test_missing_parent_ends_stream() {
let block1 = Block::new::<Sha256>((), Sha256Digest::EMPTY, Height::new(1), 1);
let block2 = Block::new::<Sha256>((), block1.digest(), Height::new(2), 2);
let block3 = Block::new::<Sha256>((), block2.digest(), Height::new(3), 3);
let provider = MockProvider(vec![block1]);
let stream = AncestorStream::new(provider, [block3.clone()]);
let results = stream.collect::<Vec<_>>().await;
assert_eq!(results, vec![block3]);
}
}