Skip to content
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: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ mockito = "1.7.0"
iroh = "0.34.1"
rand_v8 = { package = "rand", version = "0.8.5", features = ["std"] }
rand_core_v6 = { package = "rand_core", version = "0.6.4", features = ["std"] }

[workspace.package]
version = "0.3.10"
edition = "2021"

[workspace.features]
default = []
testnet = []

[workspace.lints.clippy]
match_same_arms = "warn"
unused_async = "warn"
uninlined_format_args = "warn"

[workspace.lints.rust]
unreachable_pub = "warn"
5 changes: 4 additions & 1 deletion crates/validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "validator"
version.workspace = true
edition.workspace = true

[lints]
workspace = true

[dependencies]
actix-web = { workspace = true }
alloy = { workspace = true }
Expand Down Expand Up @@ -34,4 +37,4 @@ url = { workspace = true }

[dev-dependencies]
mockito = { workspace = true }
tempfile = "=3.14.0"
tempfile = "=3.14.0"
12 changes: 12 additions & 0 deletions crates/validator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod metrics;
mod p2p;
mod store;
mod validators;

pub use metrics::export_metrics;
pub use metrics::MetricsContext;
pub use p2p::P2PClient;
pub use store::redis::RedisStore;
pub use validators::hardware::HardwareValidator;
pub use validators::synthetic_data::types::InvalidationType;
pub use validators::synthetic_data::SyntheticDataValidator;
22 changes: 8 additions & 14 deletions crates/validator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
pub mod metrics;
pub mod p2p;
pub mod store;
pub mod validators;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use alloy::primitives::utils::Unit;
use alloy::primitives::{Address, U256};
use anyhow::{Context, Result};
use clap::Parser;
use log::{debug, LevelFilter};
use log::{error, info};
use metrics::MetricsContext;
use p2p::P2PClient;
use serde_json::json;
use shared::models::api::ApiResponse;
use shared::models::node::DiscoveryNode;
Expand All @@ -24,14 +18,15 @@ use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use store::redis::RedisStore;
use tokio::signal::unix::{signal, SignalKind};
use tokio_util::sync::CancellationToken;
use url::Url;
use validators::hardware::HardwareValidator;
use validators::synthetic_data::SyntheticDataValidator;

use crate::validators::synthetic_data::types::InvalidationType;
use validator::{
export_metrics, HardwareValidator, InvalidationType, MetricsContext, P2PClient, RedisStore,
SyntheticDataValidator,
};

// Track the last time the validation loop ran
static LAST_VALIDATION_TIMESTAMP: AtomicI64 = AtomicI64::new(0);
// Maximum allowed time between validation loops (2 minutes)
Expand Down Expand Up @@ -407,7 +402,7 @@ async fn main() -> anyhow::Result<()> {
.route(
"/metrics",
web::get().to(|| async {
match metrics::export_metrics() {
match export_metrics() {
Ok(metrics) => {
HttpResponse::Ok().content_type("text/plain").body(metrics)
}
Expand Down Expand Up @@ -634,20 +629,19 @@ async fn main() -> anyhow::Result<()> {

#[cfg(test)]
mod tests {

use actix_web::{test, App};
use actix_web::{
web::{self, post},
HttpResponse, Scope,
};
use shared::models::challenge::{calc_matrix, ChallengeRequest, ChallengeResponse, FixedF64};

pub async fn handle_challenge(challenge: web::Json<ChallengeRequest>) -> HttpResponse {
async fn handle_challenge(challenge: web::Json<ChallengeRequest>) -> HttpResponse {
let result = calc_matrix(&challenge);
HttpResponse::Ok().json(result)
}

pub fn challenge_routes() -> Scope {
fn challenge_routes() -> Scope {
web::scope("/challenge")
.route("", post().to(handle_challenge))
.route("/", post().to(handle_challenge))
Expand Down
2 changes: 1 addition & 1 deletion crates/validator/src/p2p/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod client;
pub(crate) mod client;

pub use client::P2PClient;
2 changes: 1 addition & 1 deletion crates/validator/src/store/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod redis;
pub(crate) mod redis;
6 changes: 3 additions & 3 deletions crates/validator/src/validators/hardware_challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use shared::models::{
};
use std::str::FromStr;

pub struct HardwareChallenge<'a> {
pub(crate) struct HardwareChallenge<'a> {
p2p_client: &'a P2PClient,
}

impl<'a> HardwareChallenge<'a> {
pub fn new(p2p_client: &'a P2PClient) -> Self {
pub(crate) fn new(p2p_client: &'a P2PClient) -> Self {
Self { p2p_client }
}

pub async fn challenge_node(&self, node: &DiscoveryNode) -> Result<i32, Error> {
pub(crate) async fn challenge_node(&self, node: &DiscoveryNode) -> Result<i32, Error> {
// Check if node has P2P ID and addresses
let p2p_id = node
.node
Expand Down
13 changes: 3 additions & 10 deletions crates/validator/src/validators/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
pub mod hardware;
pub mod hardware_challenge;
pub mod synthetic_data;
/// Common trait for all validators
pub trait Validator {
type Error;

/// Returns the name of the validator
fn name(&self) -> &str;
}
pub(crate) mod hardware;
pub(crate) mod hardware_challenge;
pub(crate) mod synthetic_data;
9 changes: 4 additions & 5 deletions crates/validator/src/validators/synthetic_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use crate::store::redis::RedisStore;
use crate::validators::synthetic_data::types::{InvalidationType, RejectionInfo};
use alloy::primitives::U256;
use anyhow::{Context as _, Error, Result};
use chrono;
use futures::future;
use log::{debug, warn};
use log::{error, info};
use redis::AsyncCommands;
use serde_json;
use shared::utils::StorageProvider;
use shared::web3::contracts::implementations::prime_network_contract::PrimeNetworkContract;
use shared::web3::contracts::implementations::work_validators::synthetic_data_validator::{
Expand All @@ -19,11 +17,12 @@ use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
pub mod chain_operations;

pub(crate) mod chain_operations;
#[cfg(test)]
mod tests;
pub mod toploc;
pub mod types;
pub(crate) mod toploc;
pub(crate) mod types;

use toploc::{GroupValidationResult, Toploc, ToplocConfig};
use types::{
Expand Down
18 changes: 9 additions & 9 deletions crates/validator/src/validators/synthetic_data/toploc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub struct ToplocConfig {
}

#[derive(Clone, Debug)]
pub struct Toploc {
pub(crate) struct Toploc {
config: ToplocConfig,
client: reqwest::Client,
metrics: Option<MetricsContext>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct GroupValidationResult {
pub(crate) struct GroupValidationResult {
pub status: ValidationResult,
pub input_flops: f64,
pub output_flops: f64,
Expand All @@ -31,7 +31,7 @@ pub struct GroupValidationResult {
}

impl Toploc {
pub fn new(config: ToplocConfig, metrics: Option<MetricsContext>) -> Self {
pub(crate) fn new(config: ToplocConfig, metrics: Option<MetricsContext>) -> Self {
let client = reqwest::Client::builder()
.default_headers({
let mut headers = reqwest::header::HeaderMap::new();
Expand All @@ -56,7 +56,7 @@ impl Toploc {
}
}

pub fn name(&self) -> String {
pub(crate) fn name(&self) -> String {
let prefix = self
.config
.file_prefix_filter
Expand All @@ -80,7 +80,7 @@ impl Toploc {
}
}

pub fn matches_file_name(&self, file_name: &str) -> bool {
pub(crate) fn matches_file_name(&self, file_name: &str) -> bool {
let normalized_name = self.normalize_path(file_name);
match &self.config.file_prefix_filter {
Some(prefix) => {
Expand All @@ -93,7 +93,7 @@ impl Toploc {
}
}

pub async fn trigger_single_file_validation(
pub(crate) async fn trigger_single_file_validation(
&self,
file_sha: &str,
key_address: &str,
Expand Down Expand Up @@ -157,7 +157,7 @@ impl Toploc {
}
}

pub async fn trigger_group_file_validation(
pub(crate) async fn trigger_group_file_validation(
&self,
file_name: &str,
file_shas: Vec<String>,
Expand Down Expand Up @@ -227,7 +227,7 @@ impl Toploc {
}
}

pub async fn get_group_file_validation_status(
pub(crate) async fn get_group_file_validation_status(
&self,
file_name: &str,
) -> Result<GroupValidationResult, Error> {
Expand Down Expand Up @@ -328,7 +328,7 @@ impl Toploc {
}
}

pub async fn get_single_file_validation_status(
pub(crate) async fn get_single_file_validation_status(
&self,
file_name: &str,
) -> Result<ValidationResult, Error> {
Expand Down
10 changes: 3 additions & 7 deletions crates/validator/src/validators/synthetic_data/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt;
use std::str::FromStr;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum ValidationResult {
pub(crate) enum ValidationResult {
Accept,
Reject,
Crashed,
Expand All @@ -33,7 +33,7 @@ impl fmt::Display for ValidationResult {
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct WorkValidationInfo {
pub(crate) struct WorkValidationInfo {
pub status: ValidationResult,
pub reason: Option<String>,
}
Expand Down Expand Up @@ -61,9 +61,8 @@ impl fmt::Display for InvalidationType {
}

#[derive(Debug)]
pub enum ProcessWorkKeyError {
pub(crate) enum ProcessWorkKeyError {
FileNameResolutionError(String),
ValidationTriggerError(String),
ValidationPollingError(String),
InvalidatingWorkError(String),
MaxAttemptsReached(String),
Expand All @@ -83,9 +82,6 @@ impl fmt::Display for ProcessWorkKeyError {
ProcessWorkKeyError::FileNameResolutionError(msg) => {
write!(f, "File name resolution error: {msg}")
}
ProcessWorkKeyError::ValidationTriggerError(msg) => {
write!(f, "Validation trigger error: {msg}")
}
ProcessWorkKeyError::ValidationPollingError(msg) => {
write!(f, "Validation polling error: {msg}")
}
Expand Down