Skip to content

Commit

Permalink
feat: redis options and making logs more uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd authored and sebasti810 committed Jul 8, 2024
1 parent 42dfe7b commit bcd2599
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 46 deletions.
51 changes: 38 additions & 13 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct CommandLineArgs {
#[arg(short = 'c', long)]
celestia_client: Option<String>,

#[arg(short = 'r', long)]
redis_client: Option<String>,

/// Celestia Namespace ID
#[arg(short = 'n', long)]
celestia_namespace_id: Option<String>,
Expand All @@ -35,11 +38,11 @@ pub struct CommandLineArgs {
#[arg(short, long)]
epoch_time: Option<u64>,

/// IP address
/// IP address for the webserver to listen on
#[arg(short, long)]
ip: Option<String>,
host: Option<String>,

/// Port number
/// Port number for the webserver to listen on
#[arg(short, long)]
port: Option<u16>,

Expand All @@ -52,12 +55,15 @@ pub struct CommandLineArgs {

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub log_level: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub webserver: Option<WebServerConfig>,
pub da_layer: DALayerOption,
#[serde(skip_serializing_if = "Option::is_none")]
pub celestia_config: Option<CelestiaConfig>,
#[serde(skip_serializing_if = "Option::is_none")]

pub log_level: String,
pub da_layer: DALayerOption,
pub redis_config: Option<RedisConfig>,
pub epoch_time: u64,
pub public_key: Option<String>,
}
Expand All @@ -73,23 +79,36 @@ pub enum DALayerOption {

#[derive(Debug, Deserialize, Clone)]
pub struct WebServerConfig {
pub ip: String,
pub host: String,
pub port: u16,
}

impl Default for WebServerConfig {
fn default() -> Self {
WebServerConfig {
ip: "127.0.0.1".to_string(),
host: "127.0.0.1".to_string(),
port: 8080,
}
}
}

#[derive(Debug, Deserialize, Clone)]
pub struct RedisConfig {
pub connection_string: String
}

impl Default for RedisConfig {
fn default() -> Self {
RedisConfig{
connection_string: "redis://127.0.0.1/".to_string()
}
}
}

#[derive(Debug, Deserialize, Clone)]
pub struct CelestiaConfig {
connection_string: String,
namespace_id: String,
pub connection_string: String,
pub namespace_id: String,
}

impl Default for CelestiaConfig {
Expand All @@ -108,6 +127,7 @@ impl Default for Config {
log_level: "DEBUG".to_string(),
da_layer: DALayerOption::default(),
celestia_config: Some(CelestiaConfig::default()),
redis_config: Some(RedisConfig::default()),
epoch_time: 60,
public_key: None,
}
Expand All @@ -129,14 +149,19 @@ pub fn load_config(args: CommandLineArgs) -> Result<Config, config::ConfigError>
Ok(Config {
log_level: args.log_level.unwrap_or(default_config.log_level),
webserver: Some(WebServerConfig {
ip: args
.ip
.unwrap_or(default_config.webserver.as_ref().unwrap().ip.clone()),
host: args
.host
.unwrap_or(default_config.webserver.as_ref().unwrap().host.clone()),
port: args
.port
.unwrap_or(default_config.webserver.as_ref().unwrap().port),
}),
da_layer: DALayerOption::default(),
redis_config: Some(RedisConfig {
connection_string: args.redis_client.unwrap_or(
default_config.redis_config.as_ref().unwrap().connection_string.clone()
)
}),
celestia_config: Some(CelestiaConfig {
connection_string: args.celestia_client.unwrap_or(
default_config
Expand Down Expand Up @@ -181,6 +206,6 @@ pub async fn initialize_da_layer(config: &Config) -> Arc<dyn DataAvailabilityLay
}
}
DALayerOption::InMemory => Arc::new(LocalDataAvailabilityLayer::new()) as Arc<dyn DataAvailabilityLayer + 'static>,
DALayerOption::None => panic!("No DALayer"),
DALayerOption::None => panic!("No DA Layer"),
}
}
10 changes: 5 additions & 5 deletions src/da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl DataAvailabilityLayer for CelestiaConnection {
let height = extended_header.header.height.value();
match synctarget_buffer.send(height).await {
Ok(_) => {
debug!("Sent sync target update to height {}", height);
debug!("sent sync target update for height {}", height);
}
Err(_) => {
DataAvailabilityError::SyncTargetError(
Expand Down Expand Up @@ -337,7 +337,7 @@ impl DataAvailabilityLayer for LocalDataAvailabilityLayer {
let mut contents = String::new();

file.lock_exclusive().expect("Unable to lock file");
info!("File locked");
info!("file locked");

file.read_to_string(&mut contents)
.expect("Unable to read file");
Expand All @@ -364,7 +364,7 @@ impl DataAvailabilityLayer for LocalDataAvailabilityLayer {
.expect("Unable to set file length");

file.unlock().expect("Unable to unlock file");
info!("File unlocked");
info!("file unlocked");

Ok(epoch.height)
}
Expand Down Expand Up @@ -487,12 +487,12 @@ mod da_tests {
#[tokio::test]
async fn test_sequencer_and_light_client() {
if let Err(e) = clear_file("data.json") {
debug!("Fehler beim Löschen der Datei: {}", e);
error!("deleting file: {}", e);
}

// simulate sequencer start
let sequencer = tokio::spawn(async {
let mut sequencer_layer = LocalDataAvailabilityLayer::new();
let sequencer_layer = LocalDataAvailabilityLayer::new();
// write all 60 seconds proofs and commitments
// create a new tree
let mut tree = build_empty_tree();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod cfg;
pub mod cfg;
pub mod consts;
pub mod da;
pub mod error;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> {
Commands::LightClient {} => Arc::new(LightClient::new(da, config.public_key)),
Commands::Sequencer {} => Arc::new(Sequencer::new(
Arc::new(
RedisConnections::new()
RedisConnections::new(&config.clone().redis_config.unwrap())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?,
),
da,
Expand Down
20 changes: 12 additions & 8 deletions src/node_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ impl NodeType for Sequencer {
}
}
Err(e) => {
// TODO: custom error
error!("sequencer_loop: getting derived keys: {}", e);
}
}
Expand Down Expand Up @@ -101,7 +100,7 @@ impl NodeType for LightClient {
loop {
// target is updated when a new header is received
let target = self.da.get_message().await.unwrap();
debug!("Updated sync target to height {}", target);
debug!("updated sync target to height {}", target);
for i in current_position..target {
trace!("processing height: {}", i);
match self.da.get(i + 1).await {
Expand All @@ -122,12 +121,12 @@ impl NodeType for LightClient {
)
.is_ok()
{
debug!("Signature is valid");
trace!("valid signature for height {}", i);
} else {
panic!("Invalid signature");
panic!("invalid signature in retrieved epoch on height {}", i);
}
} else {
warn!("No public key found");
error!("epoch on height {} was not signed", i);
}

match validate_epoch(
Expand Down Expand Up @@ -417,19 +416,24 @@ impl Sequencer {
/// * `false` if the operation was unsuccessful, e.g., due to an invalid signature or other errors.
///
pub fn update_entry(&self, signature: &UpdateEntryJson) -> bool {
info!("Updating entry...");
debug!("updating entry for uid {} with msg {}", signature.id, signature.signed_message);
let signed_content = match verify_signature(signature, Some(signature.public_key.clone())) {
Ok(content) => content,
Err(_) => {
info!("Signature is invalid");
error!(
"updating entry for uid {}: invalid signature with pubkey {} on msg {}",
signature.id,
signature.public_key,
signature.signed_message
);
return false;
}
};

let message_obj: IncomingEntry = match serde_json::from_str(&signed_content) {
Ok(obj) => obj,
Err(e) => {
error!("Failed to parse signed content: {}", e);
error!("parsing signed content: {}", e);
return false;
}
};
Expand Down
15 changes: 8 additions & 7 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::thread::sleep;
use std::time::Duration;
use std::{self, fmt::Display, sync::Mutex};

use crate::cfg::RedisConfig;
use crate::utils::Signable;
use crate::{
error::{DatabaseError, DeimosError, GeneralError},
Expand Down Expand Up @@ -165,17 +166,17 @@ pub trait Database: Send + Sync {
}

impl RedisConnections {
pub fn new() -> Result<RedisConnections, Box<dyn std::error::Error>> {
let try_client = Client::open("redis://127.0.0.1/")?;
pub fn new(cfg: &RedisConfig) -> Result<RedisConnections, Box<dyn std::error::Error>> {
let try_client = Client::open(cfg.clone().connection_string)?;
let try_connection = try_client.get_connection();

if try_connection.is_err() {
debug!("Starting redis-server...");
debug!("starting redis-server...");

let _child = Command::new("redis-server").spawn()?;

sleep(Duration::from_secs(5));
debug!("Redis-server started.");
debug!("redis-server started");
}

let client = Client::open("redis://127.0.0.1/")?;
Expand Down Expand Up @@ -466,15 +467,15 @@ impl Database for RedisConnections {
"empty hash as first entry in the derived dictionary"
))
})?;
debug!("Added empty hash to derived dict");
debug!("added empty hash to derived dict");

// add the empty hash to the input order as first node
input_con
.rpush::<&str, String, u32>("input_order", empty_hash.clone())
.map_err(|_| {
DatabaseError::WriteError(format!("empty hash as first entry in input order"))
})?;
debug!("Added empty hash to input order");
debug!("added empty hash to input order");

Ok(())
}
Expand Down Expand Up @@ -518,7 +519,7 @@ mod tests {

// set up redis connection and flush database before each test
fn setup() -> RedisConnections {
let redis_connections = RedisConnections::new().unwrap();
let redis_connections = RedisConnections::new(&RedisConfig::default()).unwrap();
redis_connections.flush_database().unwrap();
redis_connections
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ pub fn create_and_verify_snark(
) -> Result<(groth16::Proof<Bls12>, VerifyingKey<Bls12>), DeimosError> {
let rng = &mut OsRng;

trace!("Creating parameters with BLS12-381 pairing-friendly elliptic curve construction....");
trace!("creating parameters with BLS12-381 pairing-friendly elliptic curve construction....");
let params = groth16::generate_random_parameters::<Bls12, _, _>(circuit.clone(), rng)
.map_err(|_| DeimosError::Proof(ProofError::ProofUnpackError))?;

trace!("Creating proof for zkSNARK...");
trace!("creating proof for zkSNARK...");
let proof = groth16::create_random_proof(circuit, &params, rng)
.map_err(|_| DeimosError::Proof(ProofError::GenerationError))?;

trace!("Preparing verifying key for zkSNARK...");
trace!("preparing verifying key for zkSNARK...");
let pvk = groth16::prepare_verifying_key(&params.vk);

groth16::verify_proof(&pvk, &proof, &scalars)
Expand Down
16 changes: 8 additions & 8 deletions src/webserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl WebServer {
/* let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder.set_private_key_file(env.key_path, SslFiletype::PEM).unwrap();
builder.set_certificate_chain_file(env.cert_path).unwrap(); */
info!("starting webserver on {}:{}", self.cfg.ip, self.cfg.port);
info!("starting webserver on {}:{}", self.cfg.host, self.cfg.port);
let ctx = Data::new(session.clone());
let (ip, port) = (self.cfg.ip.clone(), self.cfg.port);
let (ip, port) = (self.cfg.host.clone(), self.cfg.port);

HttpServer::new(move || {
let cors = Cors::default()
Expand Down Expand Up @@ -376,7 +376,7 @@ async fn handle_validate_proof(con: web::Data<Arc<Sequencer>>, req_body: String)
// Returns an HTTP response containing either a confirmation of successful validation or an error.
#[post("/validate-epoch")]
async fn handle_validate_epoch(con: web::Data<Arc<Sequencer>>, req_body: String) -> impl Responder {
debug!("Validating epoch {}", req_body);
debug!("validating epoch {}", req_body);
let epoch: String = match serde_json::from_str(&req_body) {
Ok(epoch) => epoch,
Err(_) => return HttpResponse::BadRequest().body("Invalid epoch"),
Expand Down Expand Up @@ -450,20 +450,20 @@ async fn handle_validate_hashchain_proof(
let circuit = match HashChainEntryCircuit::create(&incoming_value.value, hashchain) {
Ok(circuit) => circuit,
Err(e) => {
error!("Error creating circuit: {}", e);
error!("creating circuit: {}", e);
return HttpResponse::BadRequest().json("Could not create circuit");
}
};

let rng = &mut OsRng;

// debug!("Creating parameters with BLS12-381 pairing-friendly elliptic curve construction....");
trace!("creating parameters with BLS12-381 pairing-friendly elliptic curve construction");
let params = groth16::generate_random_parameters::<Bls12, _, _>(circuit.clone(), rng).unwrap();

// debug!("Creating proof for zkSNARK...");
trace!("creating proof for zkSNARK");
let proof = groth16::create_random_proof(circuit.clone(), &params, rng).unwrap();

// debug!("Prepare verifying key for zkSNARK...");
trace!("prepare verifying key for zkSNARK");
let pvk = groth16::prepare_verifying_key(&params.vk);

let public_param = match HashChainEntryCircuit::create_public_parameter(&incoming_value.value) {
Expand All @@ -473,7 +473,7 @@ async fn handle_validate_hashchain_proof(
}
};

// debug!("Verifying zkSNARK proof...");
trace!("verifying zkSNARK proof");
match groth16::verify_proof(&pvk, &proof, &[public_param]) {
Ok(_) => {
info!("proof successfully verified with: {:?}", public_param);
Expand Down

0 comments on commit bcd2599

Please sign in to comment.