Skip to main content

ExchangeTable

Struct ExchangeTable 

Source
pub struct ExchangeTable<C: Config, const N: usize = 64> { /* private fields */ }
Expand description

The fixed-capacity set of open exchanges.

Capacity is C::SESSIONS * C::EXCHANGES_PER_SESSION, which is what Config says the node was built for. A full table is ErrorCode::NoSpace — a value the caller answers BUSY with, never an abort.

Implementations§

Source§

impl<C: Config, const N: usize> ExchangeTable<C, N>

Source

pub fn new(first_id: u16) -> Self

An empty table whose first exchange id is first_id.

first_id should come from Rng: starting at zero every boot makes a node’s exchange ids predictable, and predictable ids make it cheaper for an off-path attacker to guess one that is live.

Source

pub fn len(&self) -> usize

How many exchanges are open.

Source

pub fn is_empty(&self) -> bool

Whether none are open.

Source

pub fn capacity_remaining(&self) -> usize

How many more will fit.

Source

pub fn open_initiator( &mut self, session: SessionId, protocol: ProtocolId, params: MrpParams, peer: Option<Peer>, now: Instant, ) -> Result<ExchangeKey>

Opens an exchange this node initiates, choosing an unused id.

Source

pub fn open_responder( &mut self, session: SessionId, id: ExchangeId, protocol: ProtocolId, params: MrpParams, now: Instant, ) -> Result<ExchangeKey>

Opens an exchange the peer initiated.

Returns ErrorCode::InvalidState if one with that key is already open — a peer reusing a live id is either confused or hostile, and silently adopting the second one would let it hijack the first.

Source

pub fn open_ephemeral( &mut self, session: SessionId, id: ExchangeId, protocol: ProtocolId, params: MrpParams, now: Instant, ) -> Result<ExchangeKey>

Opens §4.10.5.2’s ephemeral exchange: one that exists only to carry a standalone acknowledgement and is closed as soon as it has.

Source

pub fn reap(&mut self, now: Instant, idle: Duration) -> usize

Closes every exchange nothing has used for idle and nothing is pending on.

Returns how many went. Messaging::poll calls this, so a node that drives its timers at all gets it without asking — which is the point: an exchange table that is only reclaimed when the application remembers to reclaim it is one an attacker reclaims on its behalf.

Source

pub fn reap_deadline(&self, idle: Duration) -> Option<Instant>

When ExchangeTable::reap would next have something to do.

Source

pub fn find(&self, key: ExchangeKey) -> Option<&Exchange>

The exchange with this key, if it is open.

Source

pub fn find_mut(&mut self, key: ExchangeKey) -> Option<&mut Exchange>

The exchange with this key, mutably.

Source

pub fn find_for_message( &self, session: SessionId, id: ExchangeId, from_initiator: bool, ) -> Option<&Exchange>

Finds the exchange an arriving message belongs to.

from_initiator is the message’s I flag. A message from the initiator belongs to the exchange in which this node is the responder, and the other way round — getting this inversion wrong is how a node answers its own request.

Source

pub fn close(&mut self, key: ExchangeKey) -> bool

Closes an exchange, returning whether it was open.

Source

pub fn close_session(&mut self, session: SessionId) -> usize

Closes every exchange on a session — what tearing the session down does.

Source

pub fn poll_deadline(&self) -> Option<Instant>

The earliest deadline across every open exchange, which is when the caller’s run loop should next call ExchangeTable::on_timeout.

Source

pub fn on_timeout( &mut self, now: Instant, randomness: u32, on_action: impl FnMut(ExchangeKey, OnTimeout), ) -> usize

Hands a passed deadline to every exchange that has one, calling on_action for each thing that falls out.

Returns how many actions were produced.

Source

pub fn iter(&self) -> impl Iterator<Item = &Exchange>

Every open exchange.

Trait Implementations§

Source§

impl<C: Config, const N: usize> Capacity for ExchangeTable<C, N>

Source§

const PER_FABRIC: usize = N

The specification sets no per-fabric exchange guarantee, so the whole table is the answer: an exchange is opened and closed inside one interaction, and §4.10.5.3’s reclamation is what keeps a stranger from holding them.

Source§

const TOTAL: usize = N

Total slots, across every fabric.
Source§

impl<C: Debug + Config, const N: usize> Debug for ExchangeTable<C, N>

Source§

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

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

impl<C: Config, const N: usize> Default for ExchangeTable<C, N>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C, const N: usize> Forget for ExchangeTable<C, N>

§

impl<C, const N: usize> Freeze for ExchangeTable<C, N>
where VecInner<Exchange, usize, VecStorageInner<[MaybeUninit<Exchange>; N]>>: Freeze, PhantomData<C>: Freeze,

§

impl<C, const N: usize> Move for ExchangeTable<C, N>

§

impl<C, const N: usize> RefUnwindSafe for ExchangeTable<C, N>

§

impl<C, const N: usize> Send for ExchangeTable<C, N>
where VecInner<Exchange, usize, VecStorageInner<[MaybeUninit<Exchange>; N]>>: Send, PhantomData<C>: Send,

§

impl<C, const N: usize> Sync for ExchangeTable<C, N>
where VecInner<Exchange, usize, VecStorageInner<[MaybeUninit<Exchange>; N]>>: Sync, PhantomData<C>: Sync,

§

impl<C, const N: usize> Unpin for ExchangeTable<C, N>
where VecInner<Exchange, usize, VecStorageInner<[MaybeUninit<Exchange>; N]>>: Unpin, PhantomData<C>: Unpin,

§

impl<C, const N: usize> UnsafeUnpin for ExchangeTable<C, N>

§

impl<C, const N: usize> UnwindSafe for ExchangeTable<C, N>

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.