Skip to content

Commit

Permalink
feat(audit checks): audit checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cong-or committed Oct 5, 2023
1 parent 797a7c0 commit 615a8fb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

2 changes: 0 additions & 2 deletions src/audit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ serde_yaml = "0.8.17"
tracing.workspace = true
tracing-subscriber.workspace = true
rand = "0.8.3"
rayon = "1.8.0"


[dev-dependencies]
rand_chacha = "0.3"
Expand Down
14 changes: 9 additions & 5 deletions src/audit/src/find/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!

use clap::Parser;
use lib::find::{all_voters, convert_key_formats, find_vote, read_lines};
use lib::find::{all_voters, batch_key_check, convert_key_formats, find_vote, read_lines};
use tracing::{info, Level};

use color_eyre::Result;
Expand Down Expand Up @@ -31,7 +31,7 @@ pub struct Args {
#[clap(short, long)]
key_to_convert: Option<String>,
/// check batch of keys and write history to file
#[clap(short, long)]
#[clap(short, long, requires = "fragments")]
key_file: Option<String>,
}

Expand Down Expand Up @@ -92,7 +92,11 @@ fn main() -> Result<(), Box<dyn Error>> {

if let Some(_aggregate) = args.aggregate {
// Load and replay fund fragments from storage
let storage_path = PathBuf::from(args.fragments.expect("enforced by clap: infallible"));
let storage_path = PathBuf::from(
args.fragments
.clone()
.expect("enforced by clap: infallible"),
);

info!("collecting all voting keys in ca and 0x format");

Expand Down Expand Up @@ -127,9 +131,9 @@ fn main() -> Result<(), Box<dyn Error>> {
}

if let Some(keyfile) = args.key_file {
let keys = read_lines(&keyfile);
let storage_path = PathBuf::from(args.fragments.expect("enforced by clap: infallible"));

batch_key_check(jormungandr_database, key_file)
batch_key_check(&storage_path, keyfile);
}

if let Some(voting_key) = args.key_to_convert {
Expand Down
15 changes: 11 additions & 4 deletions src/audit/src/lib/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ pub fn read_lines(filename: &str) -> Vec<String> {
}

/// check key history of multiple keys and write metadata to file
pub fn batch_key_check(jormungandr_database: &Path, key_file: String) {
pub fn batch_key_check(
jormungandr_database: &Path,
key_file: String,
) -> Result<(), Box<dyn error::Error>> {
let mut flagged_keys = HashMap::new();

let keys = read_lines(&key_file);
Expand All @@ -264,7 +267,7 @@ pub fn batch_key_check(jormungandr_database: &Path, key_file: String) {

let voting_key = voting_key_61824_format.public_key().unwrap().to_string();

let votes = find_vote(&jormungandr_database, voting_key).unwrap();
let votes = find_vote(&jormungandr_database, voting_key)?;

flagged_keys.insert(key.clone(), votes.clone());

Expand All @@ -277,11 +280,15 @@ pub fn batch_key_check(jormungandr_database: &Path, key_file: String) {
.write(true)
.create(true)
.truncate(true)
.open(flagged_file)
.open(flagged_file.clone())
.unwrap();
let writer = BufWriter::new(file);

serde_json::to_writer_pretty(writer, &flagged_keys).unwrap();
serde_json::to_writer_pretty(writer, &flagged_keys)?;

info!("flagged keys and metadata saved here {:?}", flagged_file);

Ok(())
}

#[cfg(test)]
Expand Down

0 comments on commit 615a8fb

Please sign in to comment.