Skip to content

Commit

Permalink
feat: resharding - use full alphabet in tests (#9643)
Browse files Browse the repository at this point in the history
I'm not sure who and why hates the letters i..z but I kind of need them
for testing resharding.
  • Loading branch information
wacban authored Oct 5, 2023
1 parent 6af38bc commit d530a8b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
33 changes: 22 additions & 11 deletions core/store/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use itertools::Itertools;
use near_primitives::state::{FlatStateValue, ValueRef};
use near_primitives::trie_key::TrieKey;
use rand::seq::SliceRandom;
Expand Down Expand Up @@ -165,34 +166,44 @@ pub fn test_populate_store_rc(store: &Store, data: &[(DBCol, Vec<u8>, Vec<u8>)])
update.commit().expect("db commit failed");
}

fn gen_alphabet() -> Vec<u8> {
let alphabet = 'a'..='z';
alphabet.map(|c| c as u8).collect_vec()
}

fn gen_accounts_from_alphabet(
rng: &mut impl Rng,
min_size: usize,
max_size: usize,
alphabet: &[u8],
) -> Vec<AccountId> {
let size = rng.gen_range(min_size..=max_size);
std::iter::repeat_with(|| gen_account(rng, alphabet)).take(size).collect()
std::iter::repeat_with(|| gen_account_from_alphabet(rng, alphabet)).take(size).collect()
}

pub fn gen_account(rng: &mut impl Rng, alphabet: &[u8]) -> AccountId {
pub fn gen_account_from_alphabet(rng: &mut impl Rng, alphabet: &[u8]) -> AccountId {
let str_length = rng.gen_range(4..8);
let s: Vec<u8> = (0..str_length).map(|_| *alphabet.choose(rng).unwrap()).collect();
from_utf8(&s).unwrap().parse().unwrap()
}

pub fn gen_account(rng: &mut impl Rng) -> AccountId {
let alphabet = gen_alphabet();
gen_account_from_alphabet(rng, &alphabet)
}

pub fn gen_unique_accounts(rng: &mut impl Rng, min_size: usize, max_size: usize) -> Vec<AccountId> {
let alphabet = b"abcdefghijklmn";
let mut accounts = gen_accounts_from_alphabet(rng, min_size, max_size, alphabet);
let alphabet = gen_alphabet();
let mut accounts = gen_accounts_from_alphabet(rng, min_size, max_size, &alphabet);
accounts.sort();
accounts.dedup();
accounts.shuffle(rng);
accounts
}

pub fn gen_receipts(rng: &mut impl Rng, max_size: usize) -> Vec<Receipt> {
let alphabet = &b"abcdefgh"[0..rng.gen_range(4..8)];
let accounts = gen_accounts_from_alphabet(rng, 1, max_size, alphabet);
let alphabet = gen_alphabet();
let accounts = gen_accounts_from_alphabet(rng, 1, max_size, &alphabet);
accounts
.iter()
.map(|account_id| Receipt {
Expand All @@ -209,8 +220,8 @@ pub fn gen_receipts(rng: &mut impl Rng, max_size: usize) -> Vec<Receipt> {
/// Keys are randomly constructed from alphabet, and they have max_length size.
fn gen_changes_helper(
rng: &mut impl Rng,
max_size: usize,
alphabet: &[u8],
max_size: usize,
max_length: u64,
) -> Vec<(Vec<u8>, Option<Vec<u8>>)> {
let mut state: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();
Expand Down Expand Up @@ -240,15 +251,15 @@ fn gen_changes_helper(
}

pub fn gen_changes(rng: &mut impl Rng, max_size: usize) -> Vec<(Vec<u8>, Option<Vec<u8>>)> {
let alphabet = &b"abcdefgh"[0..rng.gen_range(2..8)];
let alphabet = gen_alphabet();
let max_length = rng.gen_range(2..8);
gen_changes_helper(rng, max_size, alphabet, max_length)
gen_changes_helper(rng, &alphabet, max_size, max_length)
}

pub fn gen_larger_changes(rng: &mut impl Rng, max_size: usize) -> Vec<(Vec<u8>, Option<Vec<u8>>)> {
let alphabet = b"abcdefghijklmnopqrst";
let alphabet = gen_alphabet();
let max_length = rng.gen_range(10..20);
gen_changes_helper(rng, max_size, alphabet, max_length)
gen_changes_helper(rng, &alphabet, max_size, max_length)
}

pub fn simplify_changes(changes: &[(Vec<u8>, Option<Vec<u8>>)]) -> Vec<(Vec<u8>, Option<Vec<u8>>)> {
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/client/resharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ fn generate_create_accounts_txs(
KeyType::ED25519,
&signer_account.to_string(),
);
let account_id = gen_account(&mut rng, b"abcdefghijkmn");
let account_id = gen_account(&mut rng);
if all_accounts.insert(account_id.clone()) {
let signer = InMemorySigner::from_seed(
account_id.clone(),
Expand Down Expand Up @@ -1012,7 +1012,7 @@ fn generate_cross_contract_tx(
new_accounts: &mut HashMap<CryptoHash, AccountId>,
nonce: &mut u64,
) -> Option<SignedTransaction> {
let account_id = gen_account(rng, b"abcdefghijkmn");
let account_id = gen_account(rng);
if !all_accounts.insert(account_id.clone()) {
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/mock-node/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ mod tests {
use near_o11y::testonly::init_integration_logger;
use near_o11y::WithSpanContextExt;
use near_primitives::transaction::SignedTransaction;
use near_store::test_utils::gen_account;
use near_store::test_utils::gen_account_from_alphabet;
use nearcore::config::GenesisExt;
use nearcore::{load_test_config, start_with_config, NEAR_BASE};
use rand::thread_rng;
Expand Down Expand Up @@ -346,7 +346,7 @@ mod tests {
let transaction = SignedTransaction::create_account(
next_nonce,
"test1".parse().unwrap(),
gen_account(&mut rng, b"abcdefghijklmn")
gen_account_from_alphabet(&mut rng, b"abcdefghijklmn")
.parse()
.unwrap(),
5 * NEAR_BASE,
Expand Down

0 comments on commit d530a8b

Please sign in to comment.