BlockLocators

Struct BlockLocators 

Source
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 recents map contains entries for blocks at heights N - 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 the recents map has fewer than NUM_RECENT_BLOCKS entries. If RECENT_INTERVAL is 1, the recents map contains entries for the last NUM_RECENT_BLOCKS blocks, i.e. from N - NUM_RECENT_BLOCKS to N - 1; if additionally N < NUM_RECENT_BLOCKS, the recents map contains entries for all the blocks, from 0 to N - 1.
  • The checkpoints map contains an entry for every CHECKPOINT_INTERVAL-th block, starting with 0 and not exceeding N, i.e. it has entries for blocks 0, CHECKPOINT_INTERVAL, 2 * CHECKPOINT_INTERVAL, …, k * CHECKPOINT_INTERVAL, where k is the maximum integer such that k * 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.

We call BlockLocators with the form described above ‘well-formed’.

Well-formed BlockLocators instances are built by [BlockSync::get_block_locators()]. When a BlockLocators instance is received (in a [PrimaryPing]) by a validator, the maps may not be well-formed (if the sending validator is faulty), but the receiving validator ensures that they are well-formed by calling BlockLocators::ensure_is_valid() from BlockLocators::new(), when deserializing in BlockLocators::read_le(). So this well-formedness is an invariant of BlockLocators instances.

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>

Source

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.

Source

pub fn new_genesis(genesis_hash: N::BlockHash) -> Self

Initializes a new genesis instance of the block locators.

Source§

impl<N: Network> BlockLocators<N>

Source

pub fn latest_locator_height(&self) -> u32

Returns the latest locator height.

Source

pub fn get_hash(&self, height: u32) -> Option<N::BlockHash>

Returns the block hash for the given block height, if it exists.

Source

pub fn is_valid(&self) -> bool

Returns true if the block locators are well-formed.

Source

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.

Source

pub fn ensure_is_valid(&self) -> Result<()>

Checks that this block locators instance is well-formed.

Source

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>

Source

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.

Source

pub fn check_block_locators( recents: &IndexMap<u32, N::BlockHash>, checkpoints: &IndexMap<u32, N::BlockHash>, ) -> Result<()>

Checks that the block locators are well-formed.

Trait Implementations§

Source§

impl<N: Clone + Network> Clone for BlockLocators<N>
where N::BlockHash: Clone,

Source§

fn clone(&self) -> BlockLocators<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<N: Debug + Network> Debug for BlockLocators<N>
where N::BlockHash: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, N: Network> Deserialize<'de> for BlockLocators<N>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<N: Network> FromBytes for BlockLocators<N>

Source§

fn read_le<R: Read>(reader: R) -> IoResult<Self>

Reads Self from reader as little-endian bytes.
Source§

fn from_bytes_le(bytes: &[u8]) -> Result<Self, Error>
where Self: Sized,

Returns Self from a byte array in little-endian order.
Source§

impl<N: Network> IntoIterator for BlockLocators<N>

Source§

type IntoIter = IntoIter<u32, <N as Network>::BlockHash>

Which kind of iterator are we turning this into?
Source§

type Item = (u32, <N as Network>::BlockHash)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<N: PartialEq + Network> PartialEq for BlockLocators<N>
where N::BlockHash: PartialEq,

Source§

fn eq(&self, other: &BlockLocators<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<N: Network> Serialize for BlockLocators<N>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<N: Network> ToBytes for BlockLocators<N>

Source§

fn write_le<W: Write>(&self, writer: W) -> IoResult<()>

Writes self into writer as little-endian bytes.
Source§

fn to_bytes_le(&self) -> Result<Vec<u8>, Error>
where Self: Sized,

Returns self as a byte array in little-endian order.
Source§

impl<N: Eq + Network> Eq for BlockLocators<N>
where N::BlockHash: Eq,

Source§

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>
where <N as Network>::BlockHash: Unpin,

§

impl<N> UnwindSafe for BlockLocators<N>
where <N as Network>::BlockHash: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<'de, T> DeserializeExt<'de> for T

Source§

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
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,