mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 13:27:57 +00:00
refactor View to include finalized_number (#2128)
* refactor View to include finalized_number * guide: update the NetworkBridge on BlockFinalized * av-store: fix the tests * actually fix tests * grumbles * ignore macro doctest * use Hash::repeat_bytes more consistently * broadcast empty leaves updates as well * fix issuing view updates on empty leaves updates
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
#![deny(unused_crate_dependencies, unused_results)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use polkadot_primitives::v1::Hash;
|
||||
use polkadot_primitives::v1::{Hash, BlockNumber};
|
||||
use parity_scale_codec::{Encode, Decode};
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
@@ -159,11 +159,32 @@ impl<M> NetworkBridgeEvent<M> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A succinct representation of a peer's view. This consists of a bounded amount of chain heads.
|
||||
/// A succinct representation of a peer's view. This consists of a bounded amount of chain heads
|
||||
/// and the highest known finalized block number.
|
||||
///
|
||||
/// Up to `N` (5?) chain heads.
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq, Encode, Decode)]
|
||||
pub struct View(pub Vec<Hash>);
|
||||
pub struct View {
|
||||
/// A bounded amount of chain heads.
|
||||
pub heads: Vec<Hash>,
|
||||
/// The highest known finalized block number.
|
||||
pub finalized_number: BlockNumber,
|
||||
}
|
||||
|
||||
|
||||
/// Construct a new view with the given chain heads and finalized number 0.
|
||||
/// NOTE: Use for tests only.
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// view![Hash::repeat_byte(1), Hash::repeat_byte(2)]
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! view {
|
||||
( $( $hash:expr ),* $(,)? ) => {
|
||||
View { heads: vec![ $( $hash.clone() ),* ], finalized_number: 0 }
|
||||
};
|
||||
}
|
||||
|
||||
impl View {
|
||||
/// Replace `self` with `new`.
|
||||
@@ -172,22 +193,22 @@ impl View {
|
||||
pub fn replace_difference(&mut self, new: View) -> impl Iterator<Item = &Hash> {
|
||||
let old = std::mem::replace(self, new);
|
||||
|
||||
self.0.iter().filter(move |h| !old.contains(h))
|
||||
self.heads.iter().filter(move |h| !old.contains(h))
|
||||
}
|
||||
|
||||
/// Returns an iterator of the hashes present in `Self` but not in `other`.
|
||||
pub fn difference<'a>(&'a self, other: &'a View) -> impl Iterator<Item = &'a Hash> + 'a {
|
||||
self.0.iter().filter(move |h| !other.contains(h))
|
||||
self.heads.iter().filter(move |h| !other.contains(h))
|
||||
}
|
||||
|
||||
/// An iterator containing hashes present in both `Self` and in `other`.
|
||||
pub fn intersection<'a>(&'a self, other: &'a View) -> impl Iterator<Item = &'a Hash> + 'a {
|
||||
self.0.iter().filter(move |h| other.contains(h))
|
||||
self.heads.iter().filter(move |h| other.contains(h))
|
||||
}
|
||||
|
||||
/// Whether the view contains a given hash.
|
||||
pub fn contains(&self, hash: &Hash) -> bool {
|
||||
self.0.contains(hash)
|
||||
self.heads.contains(hash)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user