pub struct BlockLocators<N: Network> {
pub recents: IndexMap<u32, N::BlockHash>,
pub checkpoints: IndexMap<u32, N::BlockHash>,
}Expand description
Block locator maps.
This data structure is used by validators to advertise the blocks that
they have and can provide to other validators to help them sync.
Periodically, each validator broadcasts a [PrimaryPing],
which contains a BlockLocators instance.
Recall that blocks are indexed by their u32 height, starting with 0 for the genesis block.
The keys of the recents and checkpoints maps are the block heights;
the values of the maps are the corresponding block hashes.
If a validator has N blocks, the recents and checkpoints maps are as follows:
- The
recentsmap contains entries for blocks at heightsN - 1 - (NUM_RECENT_BLOCKS - 1) * RECENT_INTERVAL,N - 1 - (NUM_RECENT_BLOCKS - 2) * RECENT_INTERVAL, …,N - 1. If any of the just listed heights are negative, there are no entries for them of course, and therecentsmap has fewer thanNUM_RECENT_BLOCKSentries. IfRECENT_INTERVALis 1, therecentsmap contains entries for the lastNUM_RECENT_BLOCKSblocks, i.e. fromN - NUM_RECENT_BLOCKStoN - 1; if additionallyN < NUM_RECENT_BLOCKS, therecentsmap contains entries for all the blocks, from0toN - 1. - The
checkpointsmap contains an entry for everyCHECKPOINT_INTERVAL-th block, starting with 0 and not exceedingN, i.e. it has entries for blocks0,CHECKPOINT_INTERVAL,2 * CHECKPOINT_INTERVAL, …,k * CHECKPOINT_INTERVAL, wherekis the maximum integer such thatk * CHECKPOINT_INTERVAL <= N.
The recents and checkpoints maps may have overlapping entries,
e.g. if N-1 is a multiple of CHECKPOINT_INTERVAL;
but if CHECKPOINT_INTERVAL is much larger than NUM_RECENT_BLOCKS,
there is no overlap most of the time.
BlockLocators instances are built, in the form described above,
by [BlockSync::get_block_locators()].
When a BlockLocators instance is received (in a [PrimaryPing]) by a validator,
the maps may not have the form described below; the deserializer does not enforce that.
However, before incorporating the BlockLocators instance information into its state,
the validator checks that the maps have the correct form,
via BlockLocators::ensure_is_valid().
Fields§
§recents: IndexMap<u32, N::BlockHash>The map of recent blocks.
checkpoints: IndexMap<u32, N::BlockHash>The map of block checkpoints.
Implementations§
Source§impl<N: Network> BlockLocators<N>
impl<N: Network> BlockLocators<N>
Sourcepub fn new(
recents: IndexMap<u32, N::BlockHash>,
checkpoints: IndexMap<u32, N::BlockHash>,
) -> Result<Self>
pub fn new( recents: IndexMap<u32, N::BlockHash>, checkpoints: IndexMap<u32, N::BlockHash>, ) -> Result<Self>
Initializes a new instance of the block locators, checking the validity of the block locators.
Sourcepub fn new_unchecked(
recents: IndexMap<u32, N::BlockHash>,
checkpoints: IndexMap<u32, N::BlockHash>,
) -> Self
pub fn new_unchecked( recents: IndexMap<u32, N::BlockHash>, checkpoints: IndexMap<u32, N::BlockHash>, ) -> Self
Initializes a new instance of the block locators, without checking the validity of the block locators.
Sourcepub fn new_genesis(genesis_hash: N::BlockHash) -> Self
pub fn new_genesis(genesis_hash: N::BlockHash) -> Self
Initializes a new genesis instance of the block locators.
Source§impl<N: Network> BlockLocators<N>
impl<N: Network> BlockLocators<N>
Sourcepub fn latest_locator_height(&self) -> u32
pub fn latest_locator_height(&self) -> u32
Returns the latest locator height.
Sourcepub fn get_hash(&self, height: u32) -> Option<N::BlockHash>
pub fn get_hash(&self, height: u32) -> Option<N::BlockHash>
Returns the block hash for the given block height, if it exists.
Sourcepub fn is_consistent_with(&self, other: &Self) -> bool
pub fn is_consistent_with(&self, other: &Self) -> bool
Returns true if the given block locators are consistent with this one.
This function assumes the given block locators are well-formed.
Sourcepub fn ensure_is_valid(&self) -> Result<()>
pub fn ensure_is_valid(&self) -> Result<()>
Checks that this block locators are well-formed.
Sourcepub fn ensure_is_consistent_with(&self, other: &Self) -> Result<()>
pub fn ensure_is_consistent_with(&self, other: &Self) -> Result<()>
Returns true if the given block locators are consistent with this one.
This function assumes the given block locators are well-formed.
Source§impl<N: Network> BlockLocators<N>
impl<N: Network> BlockLocators<N>
Sourcepub fn check_consistent_block_locators(
old_locators: &BlockLocators<N>,
new_locators: &BlockLocators<N>,
) -> Result<()>
pub fn check_consistent_block_locators( old_locators: &BlockLocators<N>, new_locators: &BlockLocators<N>, ) -> Result<()>
Checks the old and new block locators share a consistent view of block history. This function assumes the given block locators are well-formed.
Trait Implementations§
Source§impl<N: Clone + Network> Clone for BlockLocators<N>
impl<N: Clone + Network> Clone for BlockLocators<N>
Source§fn clone(&self) -> BlockLocators<N>
fn clone(&self) -> BlockLocators<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'de, N: Network> Deserialize<'de> for BlockLocators<N>
impl<'de, N: Network> Deserialize<'de> for BlockLocators<N>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<N: Network> FromBytes for BlockLocators<N>
impl<N: Network> FromBytes for BlockLocators<N>
Source§impl<N: Network> IntoIterator for BlockLocators<N>
impl<N: Network> IntoIterator for BlockLocators<N>
Source§impl<N: Network> Serialize for BlockLocators<N>
impl<N: Network> Serialize for BlockLocators<N>
Source§impl<N: Network> ToBytes for BlockLocators<N>
impl<N: Network> ToBytes for BlockLocators<N>
impl<N: Eq + Network> Eq for BlockLocators<N>
impl<N: Network> StructuralPartialEq for BlockLocators<N>
Auto Trait Implementations§
impl<N> Freeze for BlockLocators<N>
impl<N> RefUnwindSafe for BlockLocators<N>
impl<N> Send for BlockLocators<N>
impl<N> Sync for BlockLocators<N>
impl<N> Unpin for BlockLocators<N>
impl<N> UnwindSafe for BlockLocators<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<'de, T> DeserializeExt<'de> for Twhere
T: DeserializeOwned,
impl<'de, T> DeserializeExt<'de> for Twhere
T: DeserializeOwned,
fn take_from_value<D>(
value: &mut Value,
field: &str,
) -> Result<T, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more