cuprate_p2p_core/services.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
use std::time::Instant;
use cuprate_pruning::{PruningError, PruningSeed};
use cuprate_wire::{CoreSyncData, PeerListEntryBase};
use crate::{
client::InternalPeerID,
handles::ConnectionHandle,
types::{BanState, ConnectionInfo, SetBan},
NetZoneAddress, NetworkAddressIncorrectZone, NetworkZone,
};
/// A request to the core sync service for our node's [`CoreSyncData`].
pub struct CoreSyncDataRequest;
/// A response from the core sync service containing our [`CoreSyncData`].
pub struct CoreSyncDataResponse(pub CoreSyncData);
/// A [`NetworkZone`] specific [`PeerListEntryBase`].
///
/// Using this type instead of [`PeerListEntryBase`] in the address book makes
/// usage easier for the rest of the P2P code as we can guarantee only the correct addresses will be stored and returned.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(
feature = "borsh",
derive(borsh::BorshSerialize, borsh::BorshDeserialize)
)]
pub struct ZoneSpecificPeerListEntryBase<A: NetZoneAddress> {
pub adr: A,
pub id: u64,
pub last_seen: i64,
pub pruning_seed: PruningSeed,
pub rpc_port: u16,
pub rpc_credits_per_hash: u32,
}
impl<A: NetZoneAddress> From<ZoneSpecificPeerListEntryBase<A>> for PeerListEntryBase {
fn from(value: ZoneSpecificPeerListEntryBase<A>) -> Self {
Self {
adr: value.adr.into(),
id: value.id,
last_seen: value.last_seen,
pruning_seed: value.pruning_seed.compress(),
rpc_port: value.rpc_port,
rpc_credits_per_hash: value.rpc_credits_per_hash,
}
}
}
/// An error converting a [`PeerListEntryBase`] into a [`ZoneSpecificPeerListEntryBase`].
#[derive(Debug, thiserror::Error)]
pub enum PeerListConversionError {
#[error("Address is in incorrect zone")]
Address(#[from] NetworkAddressIncorrectZone),
#[error("Pruning seed error: {0}")]
PruningSeed(#[from] PruningError),
}
impl<A: NetZoneAddress> TryFrom<PeerListEntryBase> for ZoneSpecificPeerListEntryBase<A> {
type Error = PeerListConversionError;
fn try_from(value: PeerListEntryBase) -> Result<Self, Self::Error> {
Ok(Self {
adr: value.adr.try_into()?,
id: value.id,
last_seen: value.last_seen,
pruning_seed: PruningSeed::decompress_p2p_rules(value.pruning_seed)?,
rpc_port: value.rpc_port,
rpc_credits_per_hash: value.rpc_credits_per_hash,
})
}
}
/// A request to the address book service.
pub enum AddressBookRequest<Z: NetworkZone> {
/// Tells the address book that we have connected or received a connection from a peer.
NewConnection {
/// The [`InternalPeerID`] of this connection.
internal_peer_id: InternalPeerID<Z::Addr>,
/// The public address of the peer, if this peer has a reachable public address.
public_address: Option<Z::Addr>,
/// The [`ConnectionHandle`] to this peer.
handle: ConnectionHandle,
/// An ID the peer assigned itself.
id: u64,
/// The peers [`PruningSeed`].
pruning_seed: PruningSeed,
/// The peers rpc port.
rpc_port: u16,
/// The peers rpc credits per hash
rpc_credits_per_hash: u32,
},
/// Tells the address book about a peer list received from a peer.
IncomingPeerList(Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>),
/// Takes a random white peer from the peer list. If height is specified
/// then the peer list should retrieve a peer that should have a full
/// block at that height according to it's pruning seed
TakeRandomWhitePeer { height: Option<usize> },
/// Takes a random gray peer from the peer list. If height is specified
/// then the peer list should retrieve a peer that should have a full
/// block at that height according to it's pruning seed
TakeRandomGrayPeer { height: Option<usize> },
/// Takes a random peer from the peer list. If height is specified
/// then the peer list should retrieve a peer that should have a full
/// block at that height according to it's pruning seed.
///
/// The address book will look in the white peer list first, then the gray
/// one if no peer is found.
TakeRandomPeer { height: Option<usize> },
/// Gets the specified number of white peers, or less if we don't have enough.
GetWhitePeers(usize),
/// Get the amount of white & grey peers.
PeerlistSize,
/// Get information on all connections.
ConnectionInfo,
/// Get the amount of incoming & outgoing connections.
ConnectionCount,
/// (Un)ban a peer.
SetBan(SetBan<Z::Addr>),
/// Checks if the given peer is banned.
GetBan(Z::Addr),
/// Get the state of all bans.
GetBans,
}
/// A response from the address book service.
pub enum AddressBookResponse<Z: NetworkZone> {
/// Generic OK response.
///
/// Response to:
/// - [`AddressBookRequest::NewConnection`]
/// - [`AddressBookRequest::IncomingPeerList`]
Ok,
/// Response to:
/// - [`AddressBookRequest::TakeRandomWhitePeer`]
/// - [`AddressBookRequest::TakeRandomGrayPeer`]
/// - [`AddressBookRequest::TakeRandomPeer`]
Peer(ZoneSpecificPeerListEntryBase<Z::Addr>),
/// Response to [`AddressBookRequest::GetWhitePeers`].
Peers(Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>),
/// Response to [`AddressBookRequest::PeerlistSize`].
PeerlistSize { white: usize, grey: usize },
/// Response to [`AddressBookRequest::ConnectionInfo`].
ConnectionInfo(Vec<ConnectionInfo<Z::Addr>>),
/// Response to [`AddressBookRequest::ConnectionCount`].
ConnectionCount { incoming: usize, outgoing: usize },
/// Response to [`AddressBookRequest::GetBan`].
///
/// This returns [`None`] if the peer is not banned,
/// else it returns how long the peer is banned for.
GetBan { unban_instant: Option<Instant> },
/// Response to [`AddressBookRequest::GetBans`].
GetBans(Vec<BanState<Z::Addr>>),
}