forked from anoma/ferveo
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fe8753
commit 884a78b
Showing
6 changed files
with
140 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use std::{ | ||
fs::File, | ||
io::{self, BufRead}, | ||
path::PathBuf, | ||
}; | ||
|
||
use hex::FromHex; | ||
|
||
#[derive(Debug)] | ||
struct ValidatorTranscript { | ||
validator_address: String, | ||
validator_pk: Vec<u8>, | ||
transcript: Vec<u8>, | ||
} | ||
|
||
fn parse_file(file_path: &PathBuf) -> io::Result<Vec<ValidatorTranscript>> { | ||
let mut records = Vec::new(); | ||
|
||
let file = File::open(file_path)?; | ||
let reader = io::BufReader::new(file); | ||
|
||
for line in reader.lines() { | ||
let line = line?; | ||
let parts: Vec<&str> = line.split_whitespace().collect(); | ||
if parts.len() != 3 { | ||
eprintln!("Invalid line: {line}"); | ||
return Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
"Invalid line", | ||
)); | ||
} | ||
|
||
let validator_address = parts[0].to_string(); | ||
let validator_pk = Vec::from_hex(parts[1]) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
let transcript = Vec::from_hex(parts[2]) | ||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
|
||
if validator_address.len() != 42 { | ||
eprintln!( | ||
"Invalid validator_address length: {}", | ||
validator_address.len() | ||
); | ||
return Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
"Invalid validator_address length", | ||
)); | ||
} | ||
if validator_pk.len() != 96 { | ||
eprintln!("Invalid validator_pk length: {}", validator_pk.len()); | ||
return Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
"Invalid validator_pk length", | ||
)); | ||
} | ||
if transcript.len() != 3784 { | ||
eprintln!("Invalid transcript length: {}", transcript.len()); | ||
return Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
"Invalid transcript length", | ||
)); | ||
} | ||
|
||
records.push(ValidatorTranscript { | ||
validator_address, | ||
validator_pk, | ||
transcript, | ||
}); | ||
} | ||
|
||
Ok(records) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_file() { | ||
let filename = "gistfile1.csv"; | ||
let file_path = | ||
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(filename); | ||
let records = parse_file(&file_path).unwrap(); | ||
assert_eq!(records.len(), 30); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters