Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

feat: remove the register commands macro #426

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions crates/topos-commands/Cargo.toml

This file was deleted.

66 changes: 0 additions & 66 deletions crates/topos-commands/src/lib.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/topos-tce-gatekeeper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ tracing.workspace = true
tokio = { workspace = true, features = ["full"] }
topos-core = { workspace = true, features = ["uci"] }
topos-p2p = { path = "../topos-p2p" }
topos-commands = { path = "../topos-commands" }

[dev-dependencies]
rstest.workspace = true
Expand Down
8 changes: 1 addition & 7 deletions crates/topos-tce-gatekeeper/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::future::IntoFuture;

use futures::{future::BoxFuture, FutureExt};
use tokio::sync::mpsc;
use topos_p2p::PeerId;

use crate::{client::GatekeeperClient, Gatekeeper, GatekeeperError};

Expand All @@ -16,16 +15,11 @@ impl IntoFuture for GatekeeperBuilder {

fn into_future(self) -> Self::IntoFuture {
let (shutdown_channel, shutdown) = mpsc::channel(1);
let (commands, commands_recv) = mpsc::channel(100);

futures::future::ok((
GatekeeperClient {
shutdown_channel,
commands,
},
GatekeeperClient { shutdown_channel },
Gatekeeper {
shutdown,
commands: commands_recv,
..Gatekeeper::default()
},
))
Expand Down
9 changes: 1 addition & 8 deletions crates/topos-tce-gatekeeper/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::{GatekeeperCommand, GatekeeperError, GetAllSubnets};
use crate::GatekeeperError;
use tokio::sync::{mpsc, oneshot};
use topos_core::uci::SubnetId;
use topos_p2p::PeerId;

#[derive(Clone)]
pub struct GatekeeperClient {
pub(crate) shutdown_channel: mpsc::Sender<oneshot::Sender<()>>,
pub(crate) commands: mpsc::Sender<GatekeeperCommand>,
}

impl GatekeeperClient {
Expand All @@ -19,8 +16,4 @@ impl GatekeeperClient {

Ok(receiver.await?)
}

pub async fn get_all_subnets(&self) -> Result<Vec<SubnetId>, GatekeeperError> {
GetAllSubnets.send_to(&self.commands).await
}
}
37 changes: 1 addition & 36 deletions crates/topos-tce-gatekeeper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{future::IntoFuture, time::Duration};

use builder::GatekeeperBuilder;
use futures::{future::BoxFuture, FutureExt};
use rand::{seq::SliceRandom, thread_rng};
use rand::seq::SliceRandom;
use thiserror::Error;
use tokio::{
sync::{mpsc, oneshot},
Expand All @@ -16,14 +16,11 @@ mod client;
mod tests;

pub use client::GatekeeperClient;
use topos_commands::{Command, CommandHandler, RegisterCommands};
use topos_core::uci::SubnetId;
use topos_p2p::PeerId;
use tracing::{info, warn};

pub struct Gatekeeper {
pub(crate) shutdown: mpsc::Receiver<oneshot::Sender<()>>,
pub(crate) commands: mpsc::Receiver<GatekeeperCommand>,
pub(crate) tick_duration: Duration,

subnet_list: Vec<SubnetId>,
Expand All @@ -32,27 +29,16 @@ pub struct Gatekeeper {
impl Default for Gatekeeper {
fn default() -> Self {
let (_shutdown_channel, shutdown) = mpsc::channel(1);
let (_commands, commands_recv) = mpsc::channel(1);
let tick_duration = Duration::from_secs(Self::DEFAULT_TICK_DURATION);

Self {
shutdown,
commands: commands_recv,
tick_duration,
subnet_list: Vec::default(),
}
}
}

#[async_trait::async_trait]
impl CommandHandler<GetAllSubnets> for Gatekeeper {
type Error = GatekeeperError;

async fn handle(&mut self, _command: GetAllSubnets) -> Result<Vec<SubnetId>, Self::Error> {
Ok(self.subnet_list.clone())
}
}

impl IntoFuture for Gatekeeper {
type Output = Result<(), GatekeeperError>;

Expand All @@ -68,11 +54,6 @@ impl IntoFuture for Gatekeeper {
sender = self.shutdown.recv() => {
break sender;
}
Some(command) = self.commands.recv() => match command {
GatekeeperCommand::GetAllSubnets(command, response_channel) => {
_ = response_channel.send(self.handle(command).await)
},
}
}
};

Expand All @@ -99,9 +80,6 @@ impl Gatekeeper {

#[derive(Debug, Error)]
pub enum GatekeeperError {
#[error("Unable to communicate with Gatekeeper: {0}")]
CommunicationChannel(#[from] mpsc::error::SendError<GatekeeperCommand>),

#[error("Unable to receive expected response from Gatekeeper: {0}")]
ResponseChannel(#[from] oneshot::error::RecvError),

Expand All @@ -114,16 +92,3 @@ pub enum GatekeeperError {
#[error("The command produce no update")]
NoUpdate,
}

RegisterCommands!(
name = GatekeeperCommand,
error = GatekeeperError,
commands = [GetAllSubnets]
);

#[derive(Debug)]
pub struct GetAllSubnets;

impl Command for GetAllSubnets {
type Result = Vec<SubnetId>;
}
1 change: 1 addition & 0 deletions crates/topos-tce/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub async fn run(
let addr: Multiaddr = format!("/ip4/0.0.0.0/tcp/{}", config.tce_local_port).parse()?;

let mut boot_peers = config.boot_peers.clone();

// Remove myself from the bootnode list
boot_peers.retain(|(p, _)| *p != peer_id);
let is_validator = config.validators.contains(&validator_id);
Expand Down