Skip to content

Commit

Permalink
feat(control): add method to sign an elections payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Oct 31, 2024
1 parent 9e74a36 commit cddf5ca
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 99 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tracing-subscriber = { workspace = true }
# local deps
tycho-block-util = { workspace = true }
tycho-collator = { workspace = true }
tycho-control = { workspace = true }
tycho-control = { workspace = true, features = ["full"] }
tycho-core = { workspace = true }
tycho-network = { workspace = true }
tycho-rpc = { workspace = true }
Expand Down
31 changes: 27 additions & 4 deletions cli/src/node/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use clap::{Parser, Subcommand};
use everscale_types::models::BlockId;
use serde::Serialize;
use tycho_control::ControlClient;
use tycho_core::block_strider::ManualGcTrigger;
use tycho_util::cli::signal;
use tycho_util::futures::JoinTask;

Expand All @@ -16,6 +15,7 @@ use crate::util::print_json;
#[derive(Subcommand)]
pub enum CmdControl {
Ping(CmdPing),
GetInfo(CmdGetInfo),
FindArchive(CmdFindArchive),
ListArchives(CmdListArchives),
DumpArchive(CmdDumpArchive),
Expand All @@ -34,6 +34,7 @@ impl CmdControl {
pub fn run(self) -> Result<()> {
match self {
Self::Ping(cmd) => cmd.run(),
Self::GetInfo(cmd) => cmd.run(),
Self::FindArchive(cmd) => cmd.run(),
Self::ListArchives(cmd) => cmd.run(),
Self::DumpArchive(cmd) => cmd.run(),
Expand Down Expand Up @@ -68,6 +69,28 @@ impl CmdPing {
}
}

/// Get a brief node info.
#[derive(Parser)]
pub struct CmdGetInfo {
/// Unix socket path to connect to.
#[clap(short, long)]
sock: Option<PathBuf>,
}

impl CmdGetInfo {
pub fn run(self) -> Result<()> {
control_rt(self.sock, |client| async move {
let info = client.get_node_info().await?;
print_json(serde_json::json!({
"public_addr": info.public_addr,
"local_addr": info.local_addr.to_string(),
"adnl_id": info.adnl_id,
"validator_public_key": info.public_addr,
}))
})
}
}

/// Trigger a garbage collection of archives.
#[derive(Parser)]
pub struct CmdGcArchives {
Expand Down Expand Up @@ -527,11 +550,11 @@ struct TriggerBy {
pub distance: Option<u32>,
}

impl From<TriggerBy> for ManualGcTrigger {
impl From<TriggerBy> for tycho_control::proto::TriggerGcRequest {
fn from(value: TriggerBy) -> Self {
match (value.seqno, value.distance) {
(Some(seqno), None) => ManualGcTrigger::Exact(seqno),
(None, Some(distance)) => ManualGcTrigger::Distance(distance),
(Some(seqno), None) => tycho_control::proto::TriggerGcRequest::Exact(seqno),
(None, Some(distance)) => tycho_control::proto::TriggerGcRequest::Distance(distance),
_ => unreachable!(),
}
}
Expand Down
8 changes: 6 additions & 2 deletions cli/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub struct Node {

zerostate: ZerostateId,

network: Network,
dht_client: DhtClient,
peer_resolver: PeerResolver,
overlay_service: OverlayService,
Expand Down Expand Up @@ -367,7 +368,7 @@ impl Node {

let blockchain_rpc_client = BlockchainRpcClient::builder()
.with_public_overlay_client(PublicOverlayClient::new(
network,
network.clone(),
public_overlay,
node_config.public_overlay_client,
))
Expand All @@ -384,6 +385,7 @@ impl Node {

Ok(Self {
keypair,
network,
zerostate,
dht_client,
peer_resolver,
Expand Down Expand Up @@ -561,8 +563,10 @@ impl Node {
let _control_state = if let Some(config) = &self.control_config {
let server = {
let mut builder = ControlServer::builder()
.with_network(&self.network)
.with_gc_subscriber(gc_subscriber.clone())
.with_storage(self.storage.clone());
.with_storage(self.storage.clone())
.with_validator_keypair(self.keypair.clone());

#[cfg(feature = "jemalloc")]
if let Some(profiler) = JemallocMemoryProfiler::connect() {
Expand Down
15 changes: 12 additions & 3 deletions control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ rust-version.workspace = true
repository.workspace = true
license.workspace = true

# TODO: Make all dependencies optional to allow using just a plain proto

[dependencies]
# crates.io deps
anyhow = { workspace = true }
async-trait = { workspace = true }
everscale-crypto = { workspace = true }
everscale-types = { workspace = true }
futures-util = { workspace = true }
serde = { workspace = true }
tarpc = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tokio = { workspace = true, features = ["sync", "rt"] }
tracing = { workspace = true }

# local deps
tycho-core = { workspace = true }
tycho-storage = { workspace = true }
tycho-core = { workspace = true, optional = true }
tycho-network = { workspace = true, optional = true }
tycho-storage = { workspace = true, optional = true }
tycho-util = { workspace = true }

[features]
full = ["client", "server"]
client = []
server = ["dep:tycho-core", "dep:tycho-network", "dep:tycho-storage"]
20 changes: 13 additions & 7 deletions control/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use tarpc::tokio_serde::formats::Bincode;
use tarpc::{client, context};
use tokio::sync::mpsc;
use tracing::Instrument;
use tycho_core::block_strider::ManualGcTrigger;
use tycho_util::compression::ZstdDecompressStream;
use tycho_util::futures::JoinTask;

Expand All @@ -33,23 +32,30 @@ impl ControlClient {
self.inner.ping(current_context()).await.map_err(Into::into)
}

pub async fn trigger_archives_gc(&self, trigger: ManualGcTrigger) -> ClientResult<()> {
pub async fn get_node_info(&self) -> ClientResult<NodeInfoResponse> {
self.inner
.trigger_archives_gc(current_context(), trigger)
.get_node_info(current_context())
.await
.map_err(Into::into)
}

pub async fn trigger_blocks_gc(&self, trigger: ManualGcTrigger) -> ClientResult<()> {
pub async fn trigger_archives_gc(&self, req: TriggerGcRequest) -> ClientResult<()> {
self.inner
.trigger_blocks_gc(current_context(), trigger)
.trigger_archives_gc(current_context(), req)
.await
.map_err(Into::into)
}

pub async fn trigger_states_gc(&self, trigger: ManualGcTrigger) -> ClientResult<()> {
pub async fn trigger_blocks_gc(&self, req: TriggerGcRequest) -> ClientResult<()> {
self.inner
.trigger_states_gc(current_context(), trigger)
.trigger_blocks_gc(current_context(), req)
.await
.map_err(Into::into)
}

pub async fn trigger_states_gc(&self, req: TriggerGcRequest) -> ClientResult<()> {
self.inner
.trigger_states_gc(current_context(), req)
.await
.map_err(Into::into)
}
Expand Down
2 changes: 2 additions & 0 deletions control/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};

#[cfg(feature = "client")]
#[derive(Debug, thiserror::Error)]
pub enum ClientError {
#[error("client failed: {0}")]
Expand All @@ -10,6 +11,7 @@ pub enum ClientError {
Internal(#[from] ServerError),
}

#[cfg(feature = "client")]
pub type ClientResult<T, E = ClientError> = std::result::Result<T, E>;

#[derive(Debug, thiserror::Error, Serialize, Deserialize)]
Expand Down
14 changes: 12 additions & 2 deletions control/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#[cfg(feature = "client")]
pub use self::client::ControlClient;
pub use self::error::{ClientError, ServerResult};
#[cfg(feature = "client")]
pub use self::error::{ClientError, ClientResult};
pub use self::error::{ServerError, ServerResult};
#[cfg(feature = "server")]
pub use self::profiler::{MemoryProfiler, StubMemoryProfiler};
#[cfg(feature = "server")]
pub use self::server::{ControlEndpoint, ControlServer, ControlServerBuilder, ControlServerConfig};

mod client;
mod error;
#[cfg(feature = "server")]
mod profiler;
pub mod proto;

#[cfg(feature = "client")]
mod client;

#[cfg(feature = "server")]
mod server;

// TODO: Change the path to a more general setup.
Expand Down
52 changes: 48 additions & 4 deletions control/src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::net::SocketAddr;
use std::num::{NonZeroU32, NonZeroU64};

use everscale_types::models::{BlockId, BlockIdShort};
use everscale_types::prelude::*;
use serde::{Deserialize, Serialize};
use tycho_core::block_strider::ManualGcTrigger;
use tycho_util::serde_helpers;

use crate::error::ServerResult;

Expand All @@ -11,14 +13,17 @@ pub trait ControlServer {
/// Ping a node. Returns node timestamp in milliseconds.
async fn ping() -> u64;

/// Returns a node info.
async fn get_node_info() -> NodeInfoResponse;

/// Trigger manual GC for archives.
async fn trigger_archives_gc(trigger: ManualGcTrigger);
async fn trigger_archives_gc(req: TriggerGcRequest);

/// Trigger manual GC for blocks.
async fn trigger_blocks_gc(trigger: ManualGcTrigger);
async fn trigger_blocks_gc(req: TriggerGcRequest);

/// Trigger manual GC for states.
async fn trigger_states_gc(trigger: ManualGcTrigger);
async fn trigger_states_gc(req: TriggerGcRequest);

/// Sets memory profiler state. Returns whether the state was changed.
async fn set_memory_profiler_enabled(enabled: bool) -> bool;
Expand Down Expand Up @@ -46,6 +51,27 @@ pub trait ControlServer {

/// Returns list of all block ids.
async fn get_block_ids(req: BlockListRequest) -> ServerResult<BlockListResponse>;

/// Signs an elections payload.
async fn sign_elections_payload(
req: ElectionsPayloadRequest,
) -> ServerResult<ElectionsPayloadResponse>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInfoResponse {
// TODO: Somehow expose tycho_network::Address?
pub public_addr: String,
pub local_addr: SocketAddr,
pub adnl_id: HashBytes,
pub validator_public_key: Option<HashBytes>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "ty", content = "seqno")]
pub enum TriggerGcRequest {
Exact(u32),
Distance(u32),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -99,3 +125,21 @@ pub struct BlockListResponse {
pub blocks: Vec<BlockId>,
pub continuation: Option<BlockIdShort>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ElectionsPayloadRequest {
pub election_id: u32,
pub address: HashBytes,
pub max_factor: u32,
pub public_key: HashBytes,
pub adnl_addr: HashBytes,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ElectionsPayloadResponse {
// TODO: Add `serde(with = "base64")`
pub data: Vec<u8>,
pub public_key: HashBytes,
#[serde(with = "serde_helpers::signature")]
pub signature: Box<[u8; 64]>,
}
Loading

0 comments on commit cddf5ca

Please sign in to comment.