Skip to content

Commit

Permalink
Add an iterator over values (#3247)
Browse files Browse the repository at this point in the history
# Motivation
The accounts store tests include an iterator over values. We will need
that in account databases, if we are to keep that test in the current
form. There seems no harm in having such an iterator so just add it.

# Changes
* Add an iterator over accounts.

# Tests
- This PR includes a simple test that inserts a few values and verifies
that the account IDs from the iterator are as expected.
- The iterator will be used in this test:
https://github.com/dfinity/nns-dapp/blob/main/rs/backend/src/accounts_store/tests.rs#L1378

# Todos

- [ ] Add entry to changelog (if necessary).
I think this is not necessary; this is a small PR currently affecting
tests only and has no user-facing changes.

---------

Co-authored-by: Max Murphy-Skvorzov <[email protected]>
  • Loading branch information
bitdivine and bitdivine authored Aug 30, 2023
1 parent 42f9b25 commit bba8e7a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions rs/backend/src/accounts_store/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,7 @@ pub trait AccountsDbTrait {
None
}
}

/// Iterates over accounts in the data store.
fn values(&self) -> Box<dyn Iterator<Item = Account> + '_>;
}
9 changes: 9 additions & 0 deletions rs/backend/src/accounts_store/schema/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl AccountsDbTrait for AccountsDbAsMap {
fn db_accounts_len(&self) -> u64 {
self.accounts.len() as u64
}
fn values(&self) -> Box<dyn Iterator<Item = Account> + '_> {
let iterator = self.accounts.values().cloned();
Box::new(iterator)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -55,4 +59,9 @@ mod tests {
fn map_account_counts_should_be_correct() {
generic_tests::assert_account_count_is_correct(AccountsDbAsMap::default());
}

#[test]
fn map_accounts_db_should_iterate_over_values() {
generic_tests::assert_iterates_over_values(AccountsDbAsMap::default());
}
}
24 changes: 23 additions & 1 deletion rs/backend/src/accounts_store/schema/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::super::{AccountIdentifier, CanisterId, NamedCanister, PrincipalId};
use super::*;
use std::collections::HashMap;
use std::collections::{BTreeSet, HashMap};

/// Creates a toy canister.
pub fn toy_canister(account_index: u64, canister_index: u64) -> NamedCanister {
Expand Down Expand Up @@ -265,3 +265,25 @@ where
"Deleting a non-existent canister should not affect the count."
);
}

/// Verifies that the `values()` iterator works corerctly.
pub fn assert_iterates_over_values<D>(mut storage: D)
where
D: AccountsDbTrait,
{
// To store accounts in say a BTreeMap requires adding comparison operators to accounts.
// These are not generally needed or wanted. What we will do instead is to compare
// account IDs.
let mut expected_values = BTreeSet::new();
for account_id in 0..10 {
let account_key = vec![account_id as u8];
let account = toy_account(account_id, 5);
expected_values.insert(account.principal.unwrap());
storage.db_insert_account(&account_key, account.clone());
}
let actual_values = storage
.values()
.map(|account| account.principal.unwrap())
.collect::<BTreeSet<_>>();
assert_eq!(expected_values, actual_values);
}

0 comments on commit bba8e7a

Please sign in to comment.