Skip to content

Commit

Permalink
refactor(InMemorySinger) - Implement a InMemorySigner::test method (#…
Browse files Browse the repository at this point in the history
…12515)

Following the issue #12503 the commit provides and factory methods for
testing purposes only. Use case examples are also provided.

The general idea is to wrap verbose but prevalent pattern of setting up
a test case with calls.
  • Loading branch information
mkamonMdt authored Nov 28, 2024
1 parent 9841806 commit 2f5411b
Show file tree
Hide file tree
Showing 26 changed files with 112 additions and 222 deletions.
90 changes: 21 additions & 69 deletions chain/chain/src/runtime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use near_chain_configs::{
default_produce_chunk_add_transactions_time_limit, Genesis, MutableConfigValue,
DEFAULT_GC_NUM_EPOCHS_TO_KEEP, NEAR_BASE,
};
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_crypto::{InMemorySigner, Signer};
use near_o11y::testonly::init_test_logger;
use near_primitives::block::Tip;
use near_primitives::challenge::{ChallengesResult, PartialState, SlashedValidator};
Expand Down Expand Up @@ -452,16 +452,12 @@ fn test_validator_rotation() {
let mut env = TestEnv::new(vec![validators.clone()], 2, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signer =
InMemorySigner::from_seed(validators[0].clone(), KeyType::ED25519, validators[0].as_ref())
.into();
let signer = InMemorySigner::test_signer(&validators[0]);
// test1 doubles stake and the new account stakes the same, so test2 will be kicked out.`
let staking_transaction = stake(1, &signer, &block_producers[0], TESTING_INIT_STAKE * 2);
let new_account = AccountId::try_from(format!("test{}", num_nodes + 1)).unwrap();
let new_validator = create_test_signer(new_account.as_str());
let new_signer: Signer =
InMemorySigner::from_seed(new_account.clone(), KeyType::ED25519, new_account.as_ref())
.into();
let new_signer: Signer = InMemorySigner::test_signer(&new_account);
let create_account_transaction = SignedTransaction::create_account(
2,
block_producers[0].validator_id().clone(),
Expand All @@ -475,12 +471,7 @@ fn test_validator_rotation() {
let transactions = {
// With the new validator selection algorithm, test2 needs to have less stake to
// become a fisherman.
let signer = InMemorySigner::from_seed(
validators[1].clone(),
KeyType::ED25519,
validators[1].as_ref(),
)
.into();
let signer = InMemorySigner::test_signer(&validators[1]);
vec![
staking_transaction,
create_account_transaction,
Expand Down Expand Up @@ -541,9 +532,7 @@ fn test_validator_stake_change() {
let mut env = TestEnv::new(vec![validators.clone()], 2, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signer =
InMemorySigner::from_seed(validators[0].clone(), KeyType::ED25519, validators[0].as_ref())
.into();
let signer = InMemorySigner::test_signer(&validators[0]);

let desired_stake = 2 * TESTING_INIT_STAKE / 3;
let staking_transaction = stake(1, &signer, &block_producers[0], desired_stake);
Expand Down Expand Up @@ -578,10 +567,7 @@ fn test_validator_stake_change_multiple_times() {
let mut env = TestEnv::new(vec![validators.clone()], 4, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()).into())
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();

let staking_transaction = stake(1, &signers[0], &block_producers[0], TESTING_INIT_STAKE - 1);
let staking_transaction1 = stake(2, &signers[0], &block_producers[0], TESTING_INIT_STAKE - 2);
Expand Down Expand Up @@ -673,10 +659,7 @@ fn test_stake_in_last_block_of_an_epoch() {
let mut env = TestEnv::new(vec![validators.clone()], 5, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()).into())
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();
let staking_transaction =
stake(1, &signers[0], &block_producers[0], TESTING_INIT_STAKE + TESTING_INIT_STAKE / 6);
env.step_default(vec![staking_transaction]);
Expand Down Expand Up @@ -708,8 +691,7 @@ fn test_verify_validator_signature() {
(0..2).map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap()).collect::<Vec<_>>();
let env = TestEnv::new(vec![validators.clone()], 2, true);
let data = [0; 32];
let signer =
InMemorySigner::from_seed(validators[0].clone(), KeyType::ED25519, validators[0].as_ref());
let signer = InMemorySigner::test(&validators[0]);
let signature = signer.sign(&data);
assert!(env
.epoch_manager
Expand All @@ -735,9 +717,7 @@ fn test_state_sync() {
let mut env = TestEnv::new(vec![validators.clone()], 2, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signer =
InMemorySigner::from_seed(validators[0].clone(), KeyType::ED25519, validators[0].as_ref())
.into();
let signer = InMemorySigner::test_signer(&validators[0]);
let staking_transaction = stake(1, &signer, &block_producers[0], TESTING_INIT_STAKE + 1);
env.step_default(vec![staking_transaction]);
env.step_default(vec![]);
Expand Down Expand Up @@ -833,9 +813,7 @@ fn test_get_validator_info() {
let mut env = TestEnv::new(vec![validators.clone()], 2, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signer =
InMemorySigner::from_seed(validators[0].clone(), KeyType::ED25519, validators[0].as_ref())
.into();
let signer = InMemorySigner::test_signer(&validators[0]);
let staking_transaction = stake(1, &signer, &block_producers[0], 0);
let mut expected_blocks = [0, 0];
let mut expected_chunks = [0, 0];
Expand Down Expand Up @@ -1056,7 +1034,7 @@ fn test_challenges() {
bps.sort_unstable();
assert_eq!(bps, vec![("test1".parse().unwrap(), false), ("test2".parse().unwrap(), true)]);
let msg = vec![0, 1, 2];
let signer = InMemorySigner::from_seed("test2".parse().unwrap(), KeyType::ED25519, "test2");
let signer = InMemorySigner::test_signer(&"test2".parse().unwrap());
let signature = signer.sign(&msg);
assert!(!env
.epoch_manager
Expand Down Expand Up @@ -1087,9 +1065,7 @@ fn test_double_sign_challenge_not_all_slashed() {
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();

let signer =
InMemorySigner::from_seed(validators[2].clone(), KeyType::ED25519, validators[2].as_ref())
.into();
let signer = InMemorySigner::test_signer(&validators[2]);
let staking_transaction = stake(1, &signer, &block_producers[2], TESTING_INIT_STAKE / 3);
env.step(
vec![vec![staking_transaction]],
Expand All @@ -1114,7 +1090,7 @@ fn test_double_sign_challenge_not_all_slashed() {
]
);
let msg = vec![0, 1, 2];
let signer = InMemorySigner::from_seed("test2".parse().unwrap(), KeyType::ED25519, "test2");
let signer = InMemorySigner::test_signer(&"test2".parse().unwrap());
let signature = signer.sign(&msg);
assert!(!env
.epoch_manager
Expand Down Expand Up @@ -1166,10 +1142,7 @@ fn test_double_sign_challenge_all_slashed() {
.map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap())
.collect::<Vec<_>>();
let mut env = TestEnv::new(vec![validators.clone()], 5, false);
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()))
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();
env.step(vec![vec![]], vec![true], vec![SlashedValidator::new("test1".parse().unwrap(), true)]);
env.step(vec![vec![]], vec![true], vec![SlashedValidator::new("test2".parse().unwrap(), true)]);
let msg = vec![0, 1, 2];
Expand Down Expand Up @@ -1263,10 +1236,7 @@ fn test_fishermen_stake() {
);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()).into())
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();
let fishermen_stake = 3300 * NEAR_BASE + 1;

let staking_transaction = stake(1, &signers[0], &block_producers[0], fishermen_stake);
Expand Down Expand Up @@ -1330,10 +1300,7 @@ fn test_fishermen_unstake() {
);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()).into())
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();
let fishermen_stake = 3300 * NEAR_BASE + 1;

let staking_transaction = stake(1, &signers[0], &block_producers[0], fishermen_stake);
Expand Down Expand Up @@ -1404,10 +1371,7 @@ fn test_delete_account_after_unstake() {
let mut env = TestEnv::new(vec![validators.clone()], 4, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()))
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test(&id)).collect();

let staking_transaction1 = stake(1, &signers[1].clone().into(), &block_producers[1], 0);
env.step_default(vec![staking_transaction1]);
Expand Down Expand Up @@ -1452,10 +1416,7 @@ fn test_proposal_deduped() {
let mut env = TestEnv::new(vec![validators.clone()], 4, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()).into())
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();

let staking_transaction1 = stake(1, &signers[1], &block_producers[1], TESTING_INIT_STAKE - 100);
let staking_transaction2 = stake(2, &signers[1], &block_producers[1], TESTING_INIT_STAKE - 10);
Expand All @@ -1473,10 +1434,7 @@ fn test_insufficient_stake() {
let mut env = TestEnv::new(vec![validators.clone()], 4, false);
let block_producers: Vec<_> =
validators.iter().map(|id| create_test_signer(id.as_str())).collect();
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()).into())
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test_signer(&id)).collect();

let staking_transaction1 = stake(1, &signers[1], &block_producers[1], 100);
let staking_transaction2 = stake(2, &signers[1], &block_producers[1], 100 * NEAR_BASE);
Expand Down Expand Up @@ -1513,10 +1471,7 @@ fn test_trie_and_flat_state_equality() {
.map(|i| AccountId::try_from(format!("test{}", i + 1)).unwrap())
.collect::<Vec<_>>();
let mut env = TestEnv::new(vec![validators.clone()], 4, false);
let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()))
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test(&id)).collect();

let transfer_tx = SignedTransaction::from_actions(
4,
Expand Down Expand Up @@ -1672,10 +1627,7 @@ fn get_test_env_with_chain_and_pool() -> (TestEnv, Chain, TransactionPool) {
// Produce a single block, so that `prev_block_hash` is valid.
env.step_default(vec![]);

let signers: Vec<_> = validators
.iter()
.map(|id| InMemorySigner::from_seed(id.clone(), KeyType::ED25519, id.as_ref()))
.collect();
let signers: Vec<_> = validators.iter().map(|id| InMemorySigner::test(&id)).collect();

let transaction_pool = generate_transaction_pool(&signers, env.head.prev_block_hash);
(env, chain, transaction_pool)
Expand Down
5 changes: 2 additions & 3 deletions chain/client/src/test_utils/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,12 @@ impl TestEnv {

pub fn send_money(&mut self, id: usize) -> ProcessTxResponse {
let account_id = self.get_client_id(0);
let signer =
InMemorySigner::from_seed(account_id.clone(), KeyType::ED25519, account_id.as_ref());
let signer = InMemorySigner::test_signer(&account_id);
let tx = SignedTransaction::send_money(
1,
account_id.clone(),
account_id,
&signer.into(),
&signer,
100,
self.clients[id].chain.head().unwrap().last_block_hash,
);
Expand Down
9 changes: 2 additions & 7 deletions chain/client/src/tests/bug_repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::test_utils::{setup_mock_all_validators, ActorHandlesForTesting};
use crate::GetBlock;
use near_actix_test_utils::run_actix;
use near_chain::test_utils::{account_id_to_shard_id, ValidatorSchedule};
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_network::client::{BlockApproval, BlockResponse, ProcessTxRequest};
use near_network::types::NetworkRequests::PartialEncodedChunkMessage;
use near_network::types::PeerInfo;
Expand Down Expand Up @@ -120,12 +120,7 @@ fn slow_test_repro_1183() {
block.header().height() * 16 + nonce_delta,
from.clone(),
to,
&InMemorySigner::from_seed(
from.clone(),
KeyType::ED25519,
from.as_ref(),
)
.into(),
&InMemorySigner::test_signer(&from),
1,
*block.header().prev_hash(),
),
Expand Down
11 changes: 3 additions & 8 deletions chain/client/src/tests/catching_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{ClientActor, Query};
use near_actix_test_utils::run_actix;
use near_chain::test_utils::{account_id_to_shard_id, ValidatorSchedule};
use near_chain_configs::TEST_STATE_SYNC_TIMEOUT;
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_network::client::ProcessTxRequest;
use near_network::types::PeerInfo;
use near_network::types::{NetworkRequests, NetworkResponses, PeerManagerMessageRequest};
Expand Down Expand Up @@ -71,16 +71,11 @@ fn send_tx(
nonce: u64,
block_hash: CryptoHash,
) {
let signer = InMemorySigner::from_seed("test1".parse().unwrap(), KeyType::ED25519, "test1");
let signer = InMemorySigner::test_signer(&"test1".parse().unwrap());
connector.do_send(
ProcessTxRequest {
transaction: SignedTransaction::send_money(
nonce,
from,
to,
&signer.into(),
amount,
block_hash,
nonce, from, to, &signer, amount, block_hash,
),
is_forwarded: false,
check_only: false,
Expand Down
6 changes: 3 additions & 3 deletions chain/client/src/tests/cross_shard_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::{future, FutureExt};
use near_actix_test_utils::run_actix;
use near_async::time::Clock;
use near_chain::test_utils::{account_id_to_shard_id, ValidatorSchedule};
use near_crypto::{InMemorySigner, KeyType};
use near_crypto::InMemorySigner;
use near_network::client::{ProcessTxRequest, ProcessTxResponse};
use near_network::types::PeerInfo;
use near_network::types::{
Expand Down Expand Up @@ -104,7 +104,7 @@ fn send_tx(
block_hash: CryptoHash,
) {
let connectors1 = connectors.clone();
let signer = InMemorySigner::from_seed(from.clone(), KeyType::ED25519, from.as_ref());
let signer = InMemorySigner::test_signer(&from);
actix::spawn(
connectors.write().unwrap()[connector_ordinal]
.client_actor
Expand All @@ -114,7 +114,7 @@ fn send_tx(
nonce,
from.clone(),
to.clone(),
&signer.into(),
&signer,
amount,
block_hash,
),
Expand Down
Loading

0 comments on commit 2f5411b

Please sign in to comment.