cuprate/p2p/p2p-core/src/services.rs
Boog900 6da9d2d734
Some checks are pending
Audit / audit (push) Waiting to run
CI / fmt (push) Waiting to run
CI / typo (push) Waiting to run
CI / ci (macos-latest, stable, bash) (push) Waiting to run
CI / ci (ubuntu-latest, stable, bash) (push) Waiting to run
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Waiting to run
Deny / audit (push) Waiting to run
Doc / build (push) Waiting to run
Doc / deploy (push) Blocked by required conditions
P2P: remove peer sync service (#299)
* remove peer sync service

* change `p2p` to not use the peer sync service

* fmt & clippy

* doc updates

* review fixes

* add a little more detail to comment
2024-09-30 22:15:48 +01:00

119 lines
4.6 KiB
Rust

use cuprate_pruning::{PruningError, PruningSeed};
use cuprate_wire::{CoreSyncData, PeerListEntryBase};
use crate::{
client::InternalPeerID, handles::ConnectionHandle, 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),
/// Checks if the given peer is banned.
IsPeerBanned(Z::Addr),
}
/// A response from the address book service.
pub enum AddressBookResponse<Z: NetworkZone> {
Ok,
Peer(ZoneSpecificPeerListEntryBase<Z::Addr>),
Peers(Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>),
/// Contains `true` if the peer is banned.
IsPeerBanned(bool),
}