Skip to content

Commit afc6910

Browse files
[marshal] Remove Parent by Digest (#3843)
1 parent 02ecfe1 commit afc6910

5 files changed

Lines changed: 26 additions & 59 deletions

File tree

consensus/src/marshal/ancestry.rs

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,24 @@ where
2323
{
2424
}
2525

26-
/// An interface for providing blocks.
26+
/// An interface for providing parent blocks.
2727
pub trait BlockProvider: Clone + Send + 'static {
2828
/// The block type the provider walks.
2929
type Block: Block;
3030

31-
/// Subscribe to a block by its digest without requesting it from the network.
31+
/// Subscribe to the parent of a known block.
3232
///
33-
/// If the block is found available locally, the block will be returned immediately.
33+
/// If the parent is found available locally, the parent will be returned immediately.
3434
///
35-
/// If the block is not available locally, the subscription will be registered and the caller
36-
/// will be notified when the block is available. If the block is not finalized, it's possible
35+
/// If the parent is not available locally, the subscription will be registered and the caller
36+
/// will be notified when the parent is available. If the parent is not finalized, it's possible
3737
/// that it may never become available.
3838
///
3939
/// Returns `None` when the subscription is canceled or the provider can no longer deliver
40-
/// the block.
40+
/// the parent.
4141
///
42-
/// This is intentionally narrower than [`Self::subscribe_parent`]. A digest is enough to
43-
/// identify a block in local storage, but it may not be enough to form the network request
44-
/// used by a marshal variant. Variants whose consensus commitment contains extra context
45-
/// should keep that logic in [`Self::subscribe_parent`], where the known child block is
46-
/// still available.
47-
fn subscribe(
48-
self,
49-
digest: <Self::Block as Digestible>::Digest,
50-
) -> impl Future<Output = Option<Self::Block>> + Send;
51-
52-
/// Subscribe to the parent of a known block.
53-
///
54-
/// This is a separate hook from [`Self::subscribe`] because the child block can carry
55-
/// variant-specific context needed to retrieve its parent. The default implementation
56-
/// follows the digest link and waits locally, but providers may override this to derive a
57-
/// full parent commitment and issue a fetching subscription.
58-
fn subscribe_parent(
59-
self,
60-
block: Self::Block,
61-
) -> impl Future<Output = Option<Self::Block>> + Send {
62-
let digest = block.parent();
63-
self.subscribe(digest)
64-
}
42+
/// The child block can carry variant-specific context needed to retrieve its parent.
43+
fn subscribe(self, block: Self::Block) -> impl Future<Output = Option<Self::Block>> + Send;
6544
}
6645

6746
/// Yields the ancestors of a block while prefetching parents, _not_ including the genesis block.
@@ -124,10 +103,10 @@ where
124103
// If a result has been buffered, return it and queue the parent fetch if needed.
125104
if let Some(block) = this.buffered.pop() {
126105
let height = block.height();
127-
let should_subscribe_parent = height > END_BOUND;
106+
let should_walk_parent = height > END_BOUND;
128107
let end_of_buffered = this.buffered.is_empty();
129-
if should_subscribe_parent && end_of_buffered {
130-
let future = this.marshal.clone().subscribe_parent(block.clone()).boxed();
108+
if should_walk_parent && end_of_buffered {
109+
let future = this.marshal.clone().subscribe(block.clone()).boxed();
131110
*this.pending.as_mut() = Some(future).into();
132111

133112
// Explicitly poll the next future to kick off the fetch. If it's already ready,
@@ -141,7 +120,7 @@ where
141120
}
142121
Poll::Ready(None) | Poll::Pending => {}
143122
}
144-
} else if !should_subscribe_parent {
123+
} else if !should_walk_parent {
145124
// No more parents to fetch; Finish the stream.
146125
*this.pending.as_mut() = None.into();
147126
}
@@ -157,9 +136,9 @@ where
157136
}
158137
Poll::Ready(Some(Some(block))) => {
159138
let height = block.height();
160-
let should_subscribe_parent = height > END_BOUND;
161-
if should_subscribe_parent {
162-
let future = this.marshal.clone().subscribe_parent(block.clone()).boxed();
139+
let should_walk_parent = height > END_BOUND;
140+
if should_walk_parent {
141+
let future = this.marshal.clone().subscribe(block.clone()).boxed();
163142
*this.pending.as_mut() = Some(future).into();
164143

165144
// Explicitly poll the next future to kick off the fetch. If it's already ready,
@@ -197,8 +176,9 @@ mod test {
197176
impl BlockProvider for MockProvider {
198177
type Block = Block<Sha256Digest, ()>;
199178

200-
async fn subscribe(self, digest: Sha256Digest) -> Option<Self::Block> {
201-
self.0.into_iter().find(|b| b.digest() == digest)
179+
async fn subscribe(self, block: Self::Block) -> Option<Self::Block> {
180+
let parent = block.parent;
181+
self.0.into_iter().find(|b| b.digest() == parent)
202182
}
203183
}
204184

consensus/src/marshal/coding/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ mod tests {
491491
};
492492
let child = make_coding_block(child_ctx, parent.digest(), Height::new(2), 200);
493493
let subscription = context
494-
.child("subscribe_parent")
495-
.spawn(move |_| BlockProvider::subscribe_parent(marshal, child));
494+
.child("subscribe")
495+
.spawn(move |_| BlockProvider::subscribe(marshal, child));
496496

497497
context.sleep(Duration::from_millis(100)).await;
498498
assert_eq!(

consensus/src/marshal/coding/variant.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
shards,
66
types::{CodedBlock, CodedBlockCfg, StoredCodedBlock},
77
},
8-
core::{Buffer, CommitmentFallback, DigestFallback, Mailbox, Variant},
8+
core::{Buffer, CommitmentFallback, Mailbox, Variant},
99
},
1010
simplex::types::Context,
1111
types::{coding::Commitment, Round},
@@ -125,14 +125,7 @@ where
125125
{
126126
type Block = B;
127127

128-
async fn subscribe(self, digest: B::Digest) -> Option<Self::Block> {
129-
self.subscribe_by_digest(digest, DigestFallback::Wait)
130-
.await
131-
.ok()
132-
.map(<Coding<B, C, H, P> as Variant>::into_inner)
133-
}
134-
135-
async fn subscribe_parent(self, block: Self::Block) -> Option<Self::Block> {
128+
async fn subscribe(self, block: Self::Block) -> Option<Self::Block> {
136129
let parent_height = block.height().previous()?;
137130
let commitment = block.context().parent.1;
138131
self.subscribe_by_commitment(

consensus/src/marshal/standard/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ mod tests {
132132
let parent = make_raw_block(Sha256::hash(b""), Height::new(1), 100);
133133
let child = make_raw_block(parent.digest(), Height::new(2), 200);
134134
let subscription = context
135-
.child("subscribe_parent")
136-
.spawn(move |_| BlockProvider::subscribe_parent(mailbox, child));
135+
.child("subscribe")
136+
.spawn(move |_| BlockProvider::subscribe(mailbox, child));
137137

138138
context.sleep(Duration::from_millis(100)).await;
139139
assert_eq!(

consensus/src/marshal/standard/variant.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::{
77
marshal::{
88
ancestry::BlockProvider,
9-
core::{Buffer, CommitmentFallback, DigestFallback, Mailbox, Variant},
9+
core::{Buffer, CommitmentFallback, Mailbox, Variant},
1010
},
1111
types::Round,
1212
Block,
@@ -100,13 +100,7 @@ where
100100
{
101101
type Block = B;
102102

103-
async fn subscribe(self, digest: B::Digest) -> Option<Self::Block> {
104-
self.subscribe_by_digest(digest, DigestFallback::Wait)
105-
.await
106-
.ok()
107-
}
108-
109-
async fn subscribe_parent(self, block: Self::Block) -> Option<Self::Block> {
103+
async fn subscribe(self, block: Self::Block) -> Option<Self::Block> {
110104
let parent_height = block.height().previous()?;
111105
self.subscribe_by_commitment(
112106
block.parent(),

0 commit comments

Comments
 (0)