Skip to main content

PeerPool

Struct PeerPool 

Source
pub struct PeerPool { /* private fields */ }

Implementations§

Source§

impl PeerPool

Source

pub async fn new( network: NetworkType, tls: Connector, max_peers: usize, requirement: PeerRequirement, connect_timeout: Duration, ) -> Result<Self, ChiaQueryError>

Spin up the pool by connecting to max_peers random full-node peers concurrently. Under PeerRequirement::Required at least one peer must succeed, otherwise we return ChiaQueryError::PeerDiscoveryFailed; under PeerRequirement::Optional an empty pool is returned and refills later.

Source

pub fn peak_height(&self) -> u32

Latest peak height observed across all connected peers. Returns 0 if no peak has been received yet.

Source

pub async fn select_peer(&self) -> Option<(Peer, SocketAddr)>

Round-robin select a peer from the pool. Returns None when the pool is empty.

Source

pub async fn select_corroborating_peers( &self, asked: SocketAddr, ) -> Vec<(Peer, SocketAddr)>

Every peer that could CORROBORATE an answer already given by the peer at asked.

A corroborating peer must be two things at once, and neither alone is enough:

  • A different address than asked. Asking the same connection twice returns the same opinion twice, which reads as agreement while being one voice.
  • PeerOrigin::Discovered. A peer reached from a preferred address — an operator’s node, or one on this machine — is an excellent peer to READ from and is not evidence about the chain independent of this host, exactly as independent_peer_count records.

They are returned ALL AT ONCE, and there is deliberately no singular form of this. Asking corroborators one at a time lets the first responder settle a claim about the chain, which is exactly the power a hostile peer has (dig_ecosystem#2462) — and a single corroborator cannot reach CORROBORATION_FLOOR at all, so a caller that took one would be building an answer it is not allowed to report as corroborated.

Returns an empty vector when the pool holds nobody who qualifies, which is the honest answer that there is nobody to corroborate with.

Source

pub async fn eject_peer(&self, addr: SocketAddr)

Remove a peer from the pool and asynchronously connect a replacement.

Source

pub async fn has_peers(&self) -> bool

Whether the pool has at least one usable peer.

Source

pub async fn peer_count(&self) -> usize

How many peers the pool HOLDS right now.

This is a live count of the connections currently in the pool, not max_peers’s target: a pool that is still filling reports what it has, and reports the target only once it has reached it. A caller showing this number to a user is stating a fact about the machine, so a configured intention must never stand in for it.

A peer is removed by eject_peer, which runs when a request to it FAILS. So the count is of peers held and believed usable; a connection that has died silently is still counted until something tries to use it. That is the same liveness standard has_peers has always answered by, made countable.

Source

pub async fn independent_peer_count(&self) -> usize

How many peers the pool holds that are INDEPENDENT opinions.

peer_count answers “how many connections do I have”; this answers “how many of them could corroborate each other”. They differ by the peers reached from a preferred address — an operator’s trusted node or one on this machine — which are excellent peers to READ from and are not evidence about the chain independent of this host. A caller deciding whether enough separate sources agree MUST use this number, because counting a co-resident node as an independent voice is the thing that made a single local process able to look like a full peer set (dig_ecosystem#2648).

Source

pub async fn corroboration_readiness( &self, asked: SocketAddr, ) -> CorroborationReadiness

Whether the pool can honestly attempt a CORROBORATED read of an answer given by asked.

The count is of the peers that WILL be asked to corroborate — precisely the set select_corroborating_peers returns for the same address, because both use is_corroborator to decide the set.

It takes the answering address rather than subtracting one blindly. An earlier version charged the asker’s slot against the independent set whatever the asker was, so a read from the operator’s own node — which is not in that set at all — silently spent an independent voice it had never occupied. On the host this crate is sized for, two genuinely independent peers agreeing with a co-resident node were downgraded to Uncorroborated* and pushed on to the centralized coinset tier, which is the opposite of what NC-12 asks for.

This refuses; it never degrades. Corroborating against however many peers happen to be present turns a four-voice quorum into a two-voice one that still reports itself corroborated, and no consumer downstream can tell those apart. A caller handed CorroborationReadiness::Insufficient must decline the read, not proceed with fewer voices.

Source

pub async fn cycle_expired_peers(&self) -> Option<SocketAddr>

Rotate out the OLDEST discovered peer that has outlived PEER_LIFETIME, if any.

Returns the address ejected, so a caller can log or refill deliberately. This is NC-12’s cycling half and it is driven by AGE alone: a peer that has answered every request is exactly the peer this removes, because a set that never fails is a set an attacker only has to capture once. The pool’s other eviction, eject_peer, fires on request FAILURE and cannot substitute for this — a captured peer does not fail.

Only PeerOrigin::Discovered entries are rotated. A priority entry is the operator’s own node or one on this machine; cycling it would re-dial the same address, spending a handshake to change nothing.

One per call, so cycling can never empty the pool in a single sweep.

Source

pub async fn maintain(&self)

One maintenance pass: rotate out an over-age peer, then refill toward capacity.

Cycling before refilling is deliberate. Refilling first would find the pool at capacity and do nothing, so the rotation would leave a permanently smaller pool.

Source

pub async fn try_refill(&self)

If the pool is under capacity, try to connect one new peer. Also spawns a background task to handle its inbound NewPeakWallet messages.

Source

pub async fn subscribe_frames(&self, capacity: usize) -> FrameSubscription

Subscribe to this pool’s frames, with room for capacity unread ones.

Falling further behind than capacity ENDS the subscription — see FrameSubscription for why a gap is not an option.

Auto Trait Implementations§

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
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> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. 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