Skip to content

Commit

Permalink
refactor(InMemorySinger) - test and test_signer methods application (#…
Browse files Browse the repository at this point in the history
…12537)

Two factory methods for InMemorySigner were provided in #12503. The
following commit applies the methods broadly, reducing verbose test code
where possible.
  • Loading branch information
mkamonMdt authored Dec 2, 2024
1 parent 0194311 commit 9f126e8
Show file tree
Hide file tree
Showing 38 changed files with 162 additions and 289 deletions.
12 changes: 5 additions & 7 deletions chain/client/src/test_utils/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use near_chain_configs::GenesisConfig;
use near_chain_primitives::error::QueryError;
use near_chunks::client::ShardsManagerResponse;
use near_chunks::test_utils::{MockClientAdapterForShardsManager, SynchronousShardsManagerAdapter};
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_network::client::ProcessTxResponse;
use near_network::shards_manager::ShardsManagerRequestFromNetwork;
use near_network::test_utils::MockPeerManagerAdapter;
Expand Down Expand Up @@ -760,10 +760,8 @@ impl TestEnv {
relayer: AccountId,
receiver_id: AccountId,
) -> SignedTransaction {
let inner_signer =
InMemorySigner::from_seed(sender.clone(), KeyType::ED25519, sender.as_str());
let relayer_signer =
InMemorySigner::from_seed(relayer.clone(), KeyType::ED25519, relayer.as_str());
let inner_signer = InMemorySigner::test(&sender);
let relayer_signer = InMemorySigner::test_signer(&relayer);
let tip = self.clients[0].chain.head().unwrap();
let user_nonce = tip.height + 1;
let relayer_nonce = tip.height + 1;
Expand All @@ -784,7 +782,7 @@ impl TestEnv {
relayer_nonce,
relayer,
sender,
&relayer_signer.into(),
&relayer_signer,
vec![Action::Delegate(Box::new(signed_delegate_action))],
tip.last_block_hash,
0,
Expand Down Expand Up @@ -823,7 +821,7 @@ impl TestEnv {
/// `InMemorySigner::from_seed` produces a valid signer that has it's key
/// deployed already.
pub fn call_main(&mut self, account: &AccountId) -> FinalExecutionOutcomeView {
let signer = InMemorySigner::from_seed(account.clone(), KeyType::ED25519, account.as_str());
let signer = InMemorySigner::test(&account.clone());
let actions = vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "main".to_string(),
args: vec![],
Expand Down
6 changes: 3 additions & 3 deletions chain/client/src/tests/query_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use actix::System;
use futures::{future, FutureExt};
use near_actix_test_utils::run_actix;
use near_async::time::{Clock, Duration};
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_network::client::{BlockResponse, ProcessTxRequest, ProcessTxResponse};
use near_network::types::PeerInfo;
use near_o11y::testonly::init_test_logger;
Expand Down Expand Up @@ -149,7 +149,7 @@ fn test_execution_outcome_for_chunk() {
true,
false,
);
let signer = InMemorySigner::from_seed("test".parse().unwrap(), KeyType::ED25519, "test");
let signer = InMemorySigner::test_signer(&"test".parse().unwrap());

actix::spawn(async move {
let block_hash = actor_handles
Expand All @@ -165,7 +165,7 @@ fn test_execution_outcome_for_chunk() {
1,
"test".parse().unwrap(),
"near".parse().unwrap(),
&signer.into(),
&signer,
10,
block_hash,
);
Expand Down
6 changes: 1 addition & 5 deletions chain/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,7 @@ mod tests {
let transactions = (1..=10)
.map(|i| {
let signer_id = AccountId::try_from(format!("user_{}", i)).unwrap();
let signer_seed = signer_id.as_ref();
let signer = Arc::new(
InMemorySigner::from_seed(signer_id.clone(), KeyType::ED25519, signer_seed)
.into(),
);
let signer = Arc::new(InMemorySigner::test_signer(&signer_id));
SignedTransaction::send_money(
i,
signer_id,
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/client/block_corruption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const NOT_BREAKING_CHANGE_MSG: &str = "Not a breaking change";
const BLOCK_NOT_PARSED_MSG: &str = "Corrupt block didn't parse";

fn create_tx_load(height: BlockHeight, last_block: &Block) -> Vec<SignedTransaction> {
let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let tx = SignedTransaction::send_money(
height + 10000,
"test0".parse().unwrap(),
"test1".parse().unwrap(),
&signer.into(),
&signer,
10,
*last_block.hash(),
);
Expand Down
12 changes: 5 additions & 7 deletions integration-tests/src/tests/client/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use near_chain_configs::Genesis;
use near_chunks::shards_manager_actor::ShardsManagerActor;
use near_client::test_utils::{create_chunk, create_chunk_with_transactions, TestEnv};
use near_client::{Client, ProcessTxResponse, ProduceChunkResult};
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_network::types::NetworkRequests;
use near_primitives::bandwidth_scheduler::BandwidthRequests;
use near_primitives::challenge::{
Expand Down Expand Up @@ -240,8 +240,7 @@ fn test_verify_chunk_proofs_malicious_challenge_valid_order_transactions() {
env.produce_block(0, 1);

let genesis_hash = *env.clients[0].chain.genesis().hash();
let signer =
InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());

let (ProduceChunkResult { chunk, .. }, block) = create_chunk_with_transactions(
&mut env.clients[0],
Expand Down Expand Up @@ -277,8 +276,7 @@ fn test_verify_chunk_proofs_challenge_transaction_order() {
env.produce_block(0, 1);

let genesis_hash = *env.clients[0].chain.genesis().hash();
let signer =
InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());

let (ProduceChunkResult { chunk, .. }, block) = create_chunk_with_transactions(
&mut env.clients[0],
Expand Down Expand Up @@ -341,7 +339,7 @@ fn test_verify_chunk_invalid_state_challenge() {
let mut genesis = Genesis::test(vec!["test0".parse().unwrap(), "test1".parse().unwrap()], 1);
genesis.config.min_gas_price = 0;
let mut env = TestEnv::builder(&genesis.config).nightshade_runtimes(&genesis).build();
let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let validator_signer = create_test_signer("test0");
let genesis_hash = *env.clients[0].chain.genesis().hash();
env.produce_block(0, 1);
Expand All @@ -351,7 +349,7 @@ fn test_verify_chunk_invalid_state_challenge() {
1,
"test0".parse().unwrap(),
"test1".parse().unwrap(),
&signer.into(),
&signer,
1000,
genesis_hash,
),
Expand Down
10 changes: 5 additions & 5 deletions integration-tests/src/tests/client/cold_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn test_storage_after_commit_of_cold_update() {

let mut last_hash = *env.clients[0].chain.genesis().hash();
for height in 1..max_height {
let signer = InMemorySigner::from_seed(test0(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&test0());
if height == 1 {
let tx = create_tx_deploy_contract(height, &signer, last_hash);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
Expand Down Expand Up @@ -257,7 +257,7 @@ fn test_cold_db_copy_with_height_skips() {

let mut last_hash = *env.clients[0].chain.genesis().hash();
for height in 1..max_height {
let signer = InMemorySigner::from_seed(test0(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&test0());
// It is still painful to filter out transactions in last two blocks.
// So, as block 19 is skipped, blocks 17 and 18 shouldn't contain any transactions.
// So, we shouldn't send any transactions between block 17 and the previous block.
Expand Down Expand Up @@ -340,7 +340,7 @@ fn test_initial_copy_to_cold(batch_size: usize) {

let mut last_hash = *env.clients[0].chain.genesis().hash();
for height in 1..max_height {
let signer = InMemorySigner::from_seed(test0(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&test0());
for i in 0..5 {
let tx = create_tx_send_money(height * 10 + i, &signer, last_hash);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
Expand Down Expand Up @@ -427,7 +427,7 @@ fn test_cold_loop_on_gc_boundary() {
let mut last_hash = *env.clients[0].chain.genesis().hash();

for height in 1..height_delta {
let signer = InMemorySigner::from_seed(test0(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&test0());
for i in 0..5 {
let tx = create_tx_send_money(height * 10 + i, &signer, last_hash);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
Expand All @@ -446,7 +446,7 @@ fn test_cold_loop_on_gc_boundary() {
update_cold_head(cold_db, &hot_store, &(height_delta - 1)).unwrap();

for height in height_delta..height_delta * 2 {
let signer = InMemorySigner::from_seed(test0(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&test0());
for i in 0..5 {
let tx = create_tx_send_money(height * 10 + i, &signer, last_hash);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ fn test_transaction_hash_collision() {
let mut env = TestEnv::builder(&genesis.config).nightshade_runtimes(&genesis).build();
let genesis_block = env.clients[0].chain.get_block_by_height(0).unwrap();

let signer0 = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer1 =
InMemorySigner::from_seed("test1".parse().unwrap(), KeyType::ED25519, "test1").into();
let signer0 = InMemorySigner::test_signer(&"test0".parse().unwrap());
let signer1 = InMemorySigner::test_signer(&"test1".parse().unwrap());
let send_money_tx = SignedTransaction::send_money(
1,
"test1".parse().unwrap(),
Expand Down Expand Up @@ -91,7 +90,7 @@ fn test_transaction_hash_collision() {
"test1".parse().unwrap(),
NEAR_BASE,
signer1.public_key(),
&signer0.into(),
&signer0,
*genesis_block.hash(),
);
assert_eq!(
Expand Down Expand Up @@ -125,8 +124,7 @@ fn get_status_of_tx_hash_collision_for_near_implicit_account(
let deposit_for_account_creation = 10u128.pow(23);
let mut height = 1;
let blocks_number = 5;
let signer1 =
InMemorySigner::from_seed("test1".parse().unwrap(), KeyType::ED25519, "test1").into();
let signer1 = InMemorySigner::test_signer(&"test1".parse().unwrap());
let near_implicit_account_id = near_implicit_account_signer.account_id.clone();
let near_implicit_account_signer = near_implicit_account_signer.into();

Expand Down Expand Up @@ -239,12 +237,12 @@ fn test_chunk_transaction_validity() {
genesis.config.min_gas_price = 0;
let mut env = TestEnv::builder(&genesis.config).nightshade_runtimes(&genesis).build();
let genesis_block = env.clients[0].chain.get_block_by_height(0).unwrap();
let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let tx = SignedTransaction::send_money(
1,
"test1".parse().unwrap(),
"test0".parse().unwrap(),
&signer.into(),
&signer,
100,
*genesis_block.hash(),
);
Expand Down Expand Up @@ -285,13 +283,13 @@ fn test_transaction_nonce_too_large() {
genesis.config.epoch_length = epoch_length;
let mut env = TestEnv::builder(&genesis.config).nightshade_runtimes(&genesis).build();
let genesis_block = env.clients[0].chain.get_block_by_height(0).unwrap();
let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let large_nonce = AccessKey::ACCESS_KEY_NONCE_RANGE_MULTIPLIER + 1;
let tx = SignedTransaction::send_money(
large_nonce,
"test1".parse().unwrap(),
"test0".parse().unwrap(),
&signer.into(),
&signer,
100,
*genesis_block.hash(),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_crypto::{InMemorySigner, Signer};
use near_parameters::RuntimeConfigStore;
use near_primitives::account::{AccessKey, AccessKeyPermission, FunctionCallPermission};
use near_primitives::errors::{ActionsValidationError, InvalidTxError};
Expand Down Expand Up @@ -35,8 +35,7 @@ fn test_account_id_in_function_call_permission_upgrade() {
.build()
};

let signer: Signer =
InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0").into();
let signer: Signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let tx = TransactionV0 {
signer_id: "test0".parse().unwrap(),
receiver_id: "test0".parse().unwrap(),
Expand Down Expand Up @@ -104,7 +103,7 @@ fn test_very_long_account_id() {
};

let tip = env.clients[0].chain.head().unwrap();
let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let tx = Transaction::V0(TransactionV0 {
signer_id: "test0".parse().unwrap(),
receiver_id: "test0".parse().unwrap(),
Expand All @@ -123,7 +122,7 @@ fn test_very_long_account_id() {
nonce: 0,
block_hash: tip.last_block_hash,
})
.sign(&signer.into());
.sign(&signer);

assert_eq!(
env.clients[0].process_tx(tx, false, false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use near_chain::Provenance;
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_crypto::{InMemorySigner, Signer};
use near_parameters::{ExtCosts, RuntimeConfigStore};
use near_primitives::hash::CryptoHash;
use near_primitives::test_utils::encode;
Expand Down Expand Up @@ -105,8 +105,7 @@ fn compare_node_counts() {
1,
);

let signer =
InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0").into();
let signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let tx_node_counts: Vec<TrieNodesCount> = (0..4)
.map(|i| {
let touching_trie_node_cost: Gas = 16_101_955_926;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn setup_account(
let block_hash = block.hash();

let signer_id = account_parent_id.clone();
let signer = InMemorySigner::from_seed(signer_id.clone(), KeyType::ED25519, signer_id.as_str());
let signer = InMemorySigner::test(&signer_id);

let public_key = PublicKey::from_seed(KeyType::ED25519, account_id.as_str());
let amount = 10 * 10u128.pow(24);
Expand Down Expand Up @@ -149,7 +149,7 @@ fn setup_contract(env: &mut TestEnv, nonce: &mut u64) {
let contract = near_test_contracts::rs_contract();

let signer_id: AccountId = ACCOUNT_PARENT_ID.parse().unwrap();
let signer = InMemorySigner::from_seed(signer_id.clone(), KeyType::ED25519, signer_id.as_str());
let signer = InMemorySigner::test(&signer_id);

*nonce += 1;
let create_contract_tx = SignedTransaction::create_contract(
Expand Down Expand Up @@ -350,7 +350,7 @@ fn slow_test_protocol_upgrade_under_congestion() {
let mut nonce = 10;
setup_contract(&mut env, &mut nonce);

let signer = InMemorySigner::from_seed(sender_id.clone(), KeyType::ED25519, sender_id.as_str());
let signer = InMemorySigner::test(&sender_id);
// Now, congest the network with ~100 Pgas, enough to have some left after the protocol upgrade.
let n = 1000;
submit_n_100tgas_fns(&mut env, n, &mut nonce, &signer);
Expand Down Expand Up @@ -734,10 +734,8 @@ fn measure_tx_limit(
setup_account(&mut env, &mut nonce, &remote_id, &ACCOUNT_PARENT_ID.parse().unwrap());
}

let remote_signer =
InMemorySigner::from_seed(remote_id.clone(), KeyType::ED25519, remote_id.as_str());
let local_signer =
InMemorySigner::from_seed(contract_id.clone(), KeyType::ED25519, contract_id.as_str());
let remote_signer = InMemorySigner::test(&remote_id);
let local_signer = InMemorySigner::test(&contract_id);
let tip = env.clients[0].chain.head().unwrap();
let shard_layout = env.clients[0].epoch_manager.get_shard_layout(&tip.epoch_id).unwrap();
let remote_shard_id = shard_layout.account_id_to_shard_id(&remote_id);
Expand Down Expand Up @@ -831,7 +829,7 @@ fn test_rpc_client_rejection() {
let mut nonce = 10;
setup_contract(&mut env, &mut nonce);

let signer = InMemorySigner::from_seed(sender_id.clone(), KeyType::ED25519, sender_id.as_str());
let signer = InMemorySigner::test(&sender_id);

// Check we can send transactions at the start.
let fn_tx = new_fn_call_100tgas(
Expand Down
5 changes: 2 additions & 3 deletions integration-tests/src/tests/client/features/flat_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tests::client::process_blocks::deploy_test_contract_with_protocol_ver
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_crypto::{InMemorySigner, Signer};
use near_parameters::ExtCosts;
use near_primitives::test_utils::encode;
use near_primitives::transaction::{
Expand Down Expand Up @@ -51,8 +51,7 @@ fn test_flat_storage_upgrade() {
old_protocol_version,
);

let signer: Signer =
InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0").into();
let signer: Signer = InMemorySigner::test_signer(&"test0".parse().unwrap());
let gas = 20_000_000_000_000;
let tx = TransactionV0 {
signer_id: "test0".parse().unwrap(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use assert_matches::assert_matches;
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_parameters::vm::VMKind;
use near_parameters::RuntimeConfigStore;
use near_primitives::transaction::{Action, DeployContractAction};
Expand Down Expand Up @@ -41,7 +41,7 @@ fn test_deploy_cost_increased() {
.build()
};

let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let signer = InMemorySigner::test(&"test0".parse().unwrap());
let actions = vec![Action::DeployContract(DeployContractAction { code: test_contract })];

let tx = env.tx_from_actions(actions.clone(), &signer, signer.account_id.clone());
Expand Down
Loading

0 comments on commit 9f126e8

Please sign in to comment.