Skip to content

Commit

Permalink
Configurable RandomX mode in certifier
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Nov 28, 2023
1 parent 891690a commit 0663daf
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
13 changes: 10 additions & 3 deletions certifier/src/certifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use axum::{extract::State, Json};
use axum::{routing::post, Router};
use ed25519_dalek::{Signer, SigningKey};
use post::config::{InitConfig, ProofConfig};
use post::pow::randomx::{PoW, RandomXFlag};
use post::pow::randomx::PoW;
use post::verification::Verifier;
use serde::{Deserialize, Serialize};
use serde_with::{base64::Base64, serde_as};
use tracing::instrument;

use crate::configuration::RandomXMode;

#[derive(Debug, Deserialize, Serialize)]
pub struct CertifyRequest {
pub proof: post::prove::Proof<'static>,
Expand Down Expand Up @@ -66,10 +68,15 @@ struct AppState {
signer: SigningKey,
}

pub fn new(cfg: ProofConfig, init_cfg: InitConfig, signer: SigningKey) -> Router {
pub fn new(
cfg: ProofConfig,
init_cfg: InitConfig,
signer: SigningKey,
randomx_mode: RandomXMode,
) -> Router {
let state = AppState {
verifier: Verifier::new(Box::new(
PoW::new(RandomXFlag::get_recommended_flags()).expect("creating RandomX PoW verifier"),
PoW::new(randomx_mode.into()).expect("creating RandomX PoW verifier"),
)),
cfg,
init_cfg,
Expand Down
35 changes: 34 additions & 1 deletion certifier/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
use std::path::Path;

use ed25519_dalek::SecretKey;
use post::pow::randomx::RandomXFlag;
use serde_with::{base64::Base64, serde_as};
use tracing::info;

/// RandomX modes of operation
///
/// They are interchangeable as they give the same results but have different
/// purpose and memory requirements.
#[derive(Debug, Default, Copy, Clone, serde::Deserialize)]
pub enum RandomXMode {
/// Fast mode for proving. Requires 2080 MiB of memory.
Fast,
/// Light mode for verification. Requires only 256 MiB of memory, but runs significantly slower
#[default]
Light,
}

impl From<RandomXMode> for RandomXFlag {
fn from(val: RandomXMode) -> Self {
match val {
RandomXMode::Fast => RandomXFlag::get_recommended_flags() | RandomXFlag::FLAG_FULL_MEM,
RandomXMode::Light => RandomXFlag::get_recommended_flags(),
}
}
}

fn max_concurrency() -> usize {
std::thread::available_parallelism()
.expect("fetching number of cores")
.get()
}

#[serde_as]
#[derive(serde::Deserialize, Clone)]
pub struct Config {
Expand All @@ -12,7 +41,8 @@ pub struct Config {

/// The maximum number of requests to process in parallel.
/// Typically set to the number of cores, which is the default (if not set).
pub max_concurrent_requests: Option<usize>,
#[serde(default = "max_concurrency")]
pub max_concurrent_requests: usize,

#[serde_as(as = "Base64")]
/// The base64-encoded secret key used to sign the proofs.
Expand All @@ -21,6 +51,9 @@ pub struct Config {
pub post_cfg: post::config::ProofConfig,
pub init_cfg: post::config::InitConfig,

#[serde(default)]
pub randomx_mode: RandomXMode,

/// Address to expose metrics on.
/// Metrics are disabled if not configured.
pub metrics: Option<std::net::SocketAddr>,
Expand Down
23 changes: 14 additions & 9 deletions certifier/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, thread};
use std::path::PathBuf;

use axum::routing::get;
use axum_prometheus::PrometheusMetricLayerBuilder;
Expand Down Expand Up @@ -75,14 +75,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("listening on: {:?}, pubkey: {}", config.listen, pubkey_b64,);
info!("POST proof configuration: {:?}", config.post_cfg);
info!("POST init configuration: {:?}", config.init_cfg);

let max_concurrent_requests = config
.max_concurrent_requests
.unwrap_or(thread::available_parallelism()?.get());
info!("max concurrent requests: {max_concurrent_requests}");

let mut app = certifier::certifier::new(config.post_cfg, config.init_cfg, signer)
.layer(ConcurrencyLimitLayer::new(max_concurrent_requests));
info!("RandomX mode: {:?}", config.randomx_mode);
info!(
"max concurrent requests: {}",
config.max_concurrent_requests
);

let mut app = certifier::certifier::new(
config.post_cfg,
config.init_cfg,
signer,
config.randomx_mode,
)
.layer(ConcurrencyLimitLayer::new(config.max_concurrent_requests));

if let Some(addr) = config.metrics {
info!("metrics enabled on: http://{addr:?}/metrics");
Expand Down
4 changes: 2 additions & 2 deletions certifier/tests/test_certify.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::atomic::AtomicBool;

use certifier::certifier::CertifyRequest;
use certifier::{certifier::CertifyRequest, configuration::RandomXMode};
use ed25519_dalek::SigningKey;
use post::{
config::{InitConfig, ProofConfig, ScryptParams},
Expand Down Expand Up @@ -50,7 +50,7 @@ async fn test_certificate_post_proof() {

// Spawn the certifier service
let signer = SigningKey::generate(&mut rand::rngs::OsRng);
let app = certifier::certifier::new(cfg, init_cfg, signer);
let app = certifier::certifier::new(cfg, init_cfg, signer, RandomXMode::Light);
let server = axum::Server::bind(&([127, 0, 0, 1], 0).into()).serve(app.into_make_service());
let addr = server.local_addr();
tokio::spawn(server);
Expand Down
1 change: 1 addition & 0 deletions src/pow/randomx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl PoW {
} else {
(Some(cache), None)
};
log::debug!("RandomX initialized");

Ok(Self {
cache,
Expand Down

0 comments on commit 0663daf

Please sign in to comment.