Skip to content

Commit

Permalink
fix: replace N_ASSETS to N_CURRENCIES
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Mar 7, 2024
1 parent 88e797e commit 04fb8d3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
12 changes: 6 additions & 6 deletions kzg_prover/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::utils::big_intify_username;
/// An entry in the Merkle Sum Tree from the database of the CEX.
/// It contains the username and the balances of the user.
#[derive(Clone, Debug)]
pub struct Entry<const N_ASSETS: usize> {
pub struct Entry<const N_CURRENCIES: usize> {
username_as_big_uint: BigUint,
balances: [BigUint; N_ASSETS],
balances: [BigUint; N_CURRENCIES],
username: String,
}

impl<const N_ASSETS: usize> Entry<N_ASSETS> {
pub fn new(username: String, balances: [BigUint; N_ASSETS]) -> Result<Self, &'static str> {
impl<const N_CURRENCIES: usize> Entry<N_CURRENCIES> {
pub fn new(username: String, balances: [BigUint; N_CURRENCIES]) -> Result<Self, &'static str> {
Ok(Entry {
username_as_big_uint: big_intify_username(&username),
balances,
Expand All @@ -21,7 +21,7 @@ impl<const N_ASSETS: usize> Entry<N_ASSETS> {
}

pub fn init_empty() -> Self {
let empty_balances: [BigUint; N_ASSETS] = std::array::from_fn(|_| BigUint::from(0u32));
let empty_balances: [BigUint; N_CURRENCIES] = std::array::from_fn(|_| BigUint::from(0u32));

Entry {
username_as_big_uint: BigUint::from(0u32),
Expand All @@ -30,7 +30,7 @@ impl<const N_ASSETS: usize> Entry<N_ASSETS> {
}
}

pub fn balances(&self) -> &[BigUint; N_ASSETS] {
pub fn balances(&self) -> &[BigUint; N_CURRENCIES] {
&self.balances
}

Expand Down
6 changes: 3 additions & 3 deletions kzg_prover/src/utils/csv_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::path::Path;
use crate::cryptocurrency::Cryptocurrency;
use crate::entry::Entry;

pub fn parse_csv_to_entries<P: AsRef<Path>, const N_ASSETS: usize>(
pub fn parse_csv_to_entries<P: AsRef<Path>, const N_CURRENCIES: usize>(
path: P,
entries: &mut [Entry<N_ASSETS>],
entries: &mut [Entry<N_CURRENCIES>],
cryptocurrencies: &mut [Cryptocurrency],
) -> Result<(), Box<dyn Error>> {
let file = File::open(path)?;
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn parse_csv_to_entries<P: AsRef<Path>, const N_ASSETS: usize>(
}
}

let mut balances_acc: Vec<BigUint> = vec![BigUint::from(0_usize); N_ASSETS];
let mut balances_acc: Vec<BigUint> = vec![BigUint::from(0_usize); N_CURRENCIES];

for (i, result) in rdr.deserialize().enumerate() {
let record: HashMap<String, String> = result?;
Expand Down
54 changes: 30 additions & 24 deletions kzg_prover/src/utils/dummy_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use crate::cryptocurrency::Cryptocurrency;
use crate::entry::Entry;

// This is for testing purposes with a large dataset instead of using a CSV file
pub fn generate_dummy_entries<const N_ASSETS: usize>(
entries: &mut [Entry<N_ASSETS>],
pub fn generate_dummy_entries<const N_CURRENCIES: usize>(
entries: &mut [Entry<N_CURRENCIES>],
cryptocurrencies: &mut [Cryptocurrency],
) -> Result<(), Box<dyn Error>> {
// Ensure N_ASSETS is greater than 0.
if N_ASSETS == 0 {
return Err("N_ASSETS must be greater than 0".into());
// Ensure N_CURRENCIES is greater than 0.
if N_CURRENCIES == 0 {
return Err("N_CURRENCIES must be greater than 0".into());
}

// Ensure the length of `cryptocurrencies` matches `N_ASSETS`.
if cryptocurrencies.len() != N_ASSETS {
return Err("cryptocurrencies length must be equal to N_ASSETS".into());
// Ensure the length of `cryptocurrencies` matches `N_CURRENCIES`.
if cryptocurrencies.len() != N_CURRENCIES {
return Err("cryptocurrencies length must be equal to N_CURRENCIES".into());
}

for (i, cryptocurrency) in cryptocurrencies.iter_mut().enumerate() {
Expand All @@ -33,7 +33,7 @@ pub fn generate_dummy_entries<const N_ASSETS: usize>(

let username: String = (0..10).map(|_| rng.sample(Alphanumeric) as char).collect();

let balances: [BigUint; N_ASSETS] =
let balances: [BigUint; N_CURRENCIES] =
std::array::from_fn(|_| BigUint::from(rng.gen_range(1000..90000) as u32));

*entry = Entry::new(username, balances).expect("Failed to create entry");
Expand All @@ -51,46 +51,52 @@ mod tests {
#[test]
fn test_generate_random_entries() {
const N_USERS: usize = 1 << 17;
const N_ASSETS: usize = 2;
const N_CURRENCIES: usize = 2;

// Setup a buffer for entries and cryptocurrencies
let mut entries = vec![Entry::<N_ASSETS>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_ASSETS];
let mut entries = vec![Entry::<N_CURRENCIES>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_CURRENCIES];

// Attempt to generate random entries
assert!(generate_dummy_entries::<N_ASSETS>(&mut entries, &mut cryptocurrencies).is_ok());
assert!(
generate_dummy_entries::<N_CURRENCIES>(&mut entries, &mut cryptocurrencies).is_ok()
);

// Verify that entries are populated
assert_eq!(entries.len(), N_USERS);
for entry in entries {
assert!(!entry.username().is_empty());
assert_eq!(entry.balances().len(), N_ASSETS);
assert_eq!(entry.balances().len(), N_CURRENCIES);
}
}

#[test]
fn test_asset_not_zero() {
const N_USERS: usize = 1 << 17;
const N_ASSETS: usize = 0;
const N_CURRENCIES: usize = 0;

// Setup a buffer for entries and cryptocurrencies
let mut entries = vec![Entry::<N_ASSETS>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_ASSETS];
let mut entries = vec![Entry::<N_CURRENCIES>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_CURRENCIES];

// `N_ASSETS` is zero, so this should fail
assert!(generate_dummy_entries::<N_ASSETS>(&mut entries, &mut cryptocurrencies).is_err());
// `N_CURRENCIES` is zero, so this should fail
assert!(
generate_dummy_entries::<N_CURRENCIES>(&mut entries, &mut cryptocurrencies).is_err()
);
}

#[test]
fn test_wrong_cryptocurrencies() {
const N_USERS: usize = 1 << 17;
const N_ASSETS: usize = 2;
const N_CURRENCIES: usize = 2;

// Setup a buffer for entries and cryptocurrencies
let mut entries = vec![Entry::<N_ASSETS>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_ASSETS + 1];
let mut entries = vec![Entry::<N_CURRENCIES>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_CURRENCIES + 1];

// `cryptocurrencies` length is not equal to `N_ASSETS`, so this should fail
assert!(generate_dummy_entries::<N_ASSETS>(&mut entries, &mut cryptocurrencies).is_err());
// `cryptocurrencies` length is not equal to `N_CURRENCIES`, so this should fail
assert!(
generate_dummy_entries::<N_CURRENCIES>(&mut entries, &mut cryptocurrencies).is_err()
);
}
}

0 comments on commit 04fb8d3

Please sign in to comment.