use crate::completion::{CompletionHandle, ContractResponse};
use crate::runtime::RuntimeResourceRef;
use bb_ir::ids::PeerId;
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum SelectParams {
Random {
n: u32,
},
NearKey {
key: Vec<u8>,
n: u32,
},
All,
}
pub trait PeerSelector: Send + Sync {
type Error: std::error::Error + std::fmt::Display + Send + Sync + 'static;
fn select(
&mut self,
ctx: &mut RuntimeResourceRef<'_>,
params: SelectParams,
completion: CompletionHandle<Vec<PeerId>, Self::Error>,
) -> ContractResponse<Vec<PeerId>, Self::Error>;
fn sample(
&mut self,
ctx: &mut RuntimeResourceRef<'_>,
n: u32,
completion: CompletionHandle<Vec<PeerId>, Self::Error>,
) -> ContractResponse<Vec<PeerId>, Self::Error> {
self.select(ctx, SelectParams::Random { n }, completion)
}
fn current_view(
&mut self,
ctx: &mut RuntimeResourceRef<'_>,
completion: CompletionHandle<Vec<PeerId>, Self::Error>,
) -> ContractResponse<Vec<PeerId>, Self::Error>;
}