Spawn the task to select validators to dial

This commit is contained in:
Luke Parker
2025-01-07 18:16:34 -05:00
parent 419223c54e
commit 2121a9b131
3 changed files with 27 additions and 11 deletions

View File

@@ -28,13 +28,19 @@ const TARGET_PEERS_PER_NETWORK: usize = 5;
*/
// TODO const TARGET_DIALED_PEERS_PER_NETWORK: usize = 3;
struct DialTask {
pub(crate) struct DialTask {
serai: Serai,
validators: Validators,
peers: Peers,
to_dial: mpsc::UnboundedSender<DialOpts>,
}
impl DialTask {
pub(crate) fn new(serai: Serai, peers: Peers, to_dial: mpsc::UnboundedSender<DialOpts>) -> Self {
DialTask { serai: serai.clone(), validators: Validators::new(serai), peers, to_dial }
}
}
impl ContinuallyRan for DialTask {
// Only run every five minutes, not the default of every five seconds
const DELAY_BETWEEN_ITERATIONS: u64 = 5 * 60;

View File

@@ -13,7 +13,7 @@ use serai_client::{
use tokio::sync::{mpsc, RwLock};
use serai_task::Task;
use serai_task::{Task, ContinuallyRan};
use libp2p::{
multihash::Multihash,
@@ -26,7 +26,7 @@ use libp2p::{
/// A struct to sync the validators from the Serai node in order to keep track of them.
mod validators;
use validators::{Validators, UpdateValidatorsTask};
use validators::UpdateValidatorsTask;
/// The authentication protocol upgrade to limit the P2P network to active validators.
mod authenticate;
@@ -34,6 +34,7 @@ use authenticate::OnlyValidators;
/// The dial task, to find new peers to connect to
mod dial;
use dial::DialTask;
/// The request-response messages and behavior
mod reqres;
@@ -105,7 +106,10 @@ pub(crate) fn new(serai_key: &Zeroizing<Keypair>, serai: Serai) -> P2p {
// Define the dial task
let (dial_task_def, dial_task) = Task::new();
let (to_dial_send, to_dial_recv) = mpsc::unbounded_channel();
todo!("TODO: Dial task");
tokio::spawn(
DialTask::new(serai.clone(), peers.clone(), to_dial_send)
.continually_run(dial_task_def, vec![]),
);
// Define the Validators object used for validating new connections
let connection_validators = UpdateValidatorsTask::spawn(serai.clone());
@@ -166,5 +170,7 @@ pub(crate) fn new(serai_key: &Zeroizing<Keypair>, serai: Serai) -> P2p {
inbound_request_responses_recv,
);
todo!("TODO")
// gossip_send, signed_cosigns_recv, tributary_gossip_recv, outbound_requests_send,
// heartbeat_requests_recv, notable_cosign_requests_recv, inbound_request_responses_send
todo!("TODO");
}

View File

@@ -27,6 +27,15 @@ pub(crate) struct Validators {
}
impl Validators {
pub(crate) fn new(serai: Serai) -> Self {
Validators {
serai,
sessions: HashMap::new(),
by_network: HashMap::new(),
validators: HashMap::new(),
}
}
async fn session_changes(
serai: impl Borrow<Serai>,
sessions: impl Borrow<HashMap<NetworkId, Session>>,
@@ -138,12 +147,7 @@ impl UpdateValidatorsTask {
/// This returns a reference to the Validators it updates after spawning itself.
pub(crate) fn spawn(serai: Serai) -> Arc<RwLock<Validators>> {
// The validators which will be updated
let validators = Arc::new(RwLock::new(Validators {
serai,
sessions: HashMap::new(),
by_network: HashMap::new(),
validators: HashMap::new(),
}));
let validators = Arc::new(RwLock::new(Validators::new(serai)));
// Define the task
let (update_validators_task, update_validators_task_handle) = Task::new();