Skip to content

Commit

Permalink
tests: Check that transaction is accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
aborg-dev committed Apr 26, 2023
1 parent 055a9e2 commit 3600a2f
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 133 deletions.
1 change: 1 addition & 0 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ impl Client {
Ok(())
}

#[must_use]
pub fn process_tx(
&mut self,
tx: SignedTransaction,
Expand Down
5 changes: 3 additions & 2 deletions integration-tests/src/tests/client/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use borsh::BorshSerialize;
use near_chain::ChainGenesis;
use near_chain_configs::Genesis;
use near_client::test_utils::{create_chunk_on_height, TestEnv};
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType};
use near_primitives::transaction::{Action, DeployContractAction, SignedTransaction};
use nearcore::config::GenesisExt;
Expand Down Expand Up @@ -40,14 +41,14 @@ fn benchmark_large_chunk_production_time() {
let last_block_hash = env.clients[0].chain.head().unwrap().last_block_hash;
for i in 0..n_txes {
let tx = SignedTransaction::from_actions(
i,
i + 1,
account_id.clone(),
account_id.clone(),
&signer,
vec![Action::DeployContract(DeployContractAction { code: vec![92; tx_size] })],
last_block_hash,
);
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}

let t = std::time::Instant::now();
Expand Down
30 changes: 18 additions & 12 deletions integration-tests/src/tests/client/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use near_chain::{Block, Chain, ChainGenesis, ChainStoreAccess, Error, Provenance
use near_chain_configs::Genesis;
use near_chunks::ShardsManager;
use near_client::test_utils::{create_chunk, create_chunk_with_transactions, TestEnv};
use near_client::Client;
use near_client::{Client, ProcessTxResponse};
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_network::test_utils::MockPeerManagerAdapter;
use near_network::types::NetworkRequests;
Expand Down Expand Up @@ -338,17 +338,20 @@ fn test_verify_chunk_invalid_state_challenge() {
let validator_signer = create_test_signer("test0");
let genesis_hash = *env.clients[0].chain.genesis().hash();
env.produce_block(0, 1);
env.clients[0].process_tx(
SignedTransaction::send_money(
0,
"test0".parse().unwrap(),
"test1".parse().unwrap(),
&signer,
1000,
genesis_hash,
assert_eq!(
env.clients[0].process_tx(
SignedTransaction::send_money(
1,
"test0".parse().unwrap(),
"test1".parse().unwrap(),
&signer,
1000,
genesis_hash,
),
false,
false,
),
false,
false,
ProcessTxResponse::ValidTx
);
env.produce_block(0, 2);

Expand Down Expand Up @@ -623,7 +626,10 @@ fn test_fishermen_challenge() {
signer.public_key(),
genesis_hash,
);
env.clients[0].process_tx(stake_transaction, false, false);
assert_eq!(
env.clients[0].process_tx(stake_transaction, false, false),
ProcessTxResponse::ValidTx
);
for i in 1..=11 {
env.produce_block(0, i);
}
Expand Down
11 changes: 6 additions & 5 deletions integration-tests/src/tests/client/cold_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use borsh::BorshDeserialize;
use near_chain::{ChainGenesis, Provenance};
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType};
use near_o11y::testonly::init_test_logger;
use near_primitives::block::Tip;
Expand Down Expand Up @@ -104,7 +105,7 @@ fn test_storage_after_commit_of_cold_update() {
})],
last_hash,
);
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}
// Don't send transactions in last two blocks. Because on last block production a chunk from
// the next block will be produced and information about these transactions will be written
Expand All @@ -124,7 +125,7 @@ fn test_storage_after_commit_of_cold_update() {
})],
last_hash,
);
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}
for i in 0..5 {
let tx = SignedTransaction::send_money(
Expand All @@ -135,7 +136,7 @@ fn test_storage_after_commit_of_cold_update() {
1,
last_hash,
);
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}
}

Expand Down Expand Up @@ -299,7 +300,7 @@ fn test_cold_db_copy_with_height_skips() {
1,
last_hash,
);
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}
}

Expand Down Expand Up @@ -410,7 +411,7 @@ fn test_initial_copy_to_cold(batch_size: usize) {
1,
last_hash,
);
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}

let block = env.clients[0].produce_block(h).unwrap().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn check_tx_processing(
blocks_number: u64,
) -> BlockHeight {
let tx_hash = tx.get_hash();
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
let next_height = produce_blocks_from_height(env, blocks_number, height);
let final_outcome = env.clients[0].chain.get_final_transaction_result(&tx_hash).unwrap();
assert_matches!(final_outcome.status, FinalExecutionStatus::SuccessValue(_));
Expand Down Expand Up @@ -80,8 +80,14 @@ fn test_transaction_hash_collision() {
*genesis_block.hash(),
);

env.clients[0].process_tx(send_money_tx.clone(), false, false);
env.clients[0].process_tx(delete_account_tx, false, false);
assert_eq!(
env.clients[0].process_tx(send_money_tx.clone(), false, false),
ProcessTxResponse::ValidTx
);
assert_eq!(
env.clients[0].process_tx(delete_account_tx, false, false),
ProcessTxResponse::ValidTx
);

for i in 1..4 {
env.produce_block(0, i);
Expand All @@ -96,14 +102,18 @@ fn test_transaction_hash_collision() {
&signer0,
*genesis_block.hash(),
);
let res = env.clients[0].process_tx(create_account_tx, false, false);
assert_matches!(res, ProcessTxResponse::ValidTx);
assert_eq!(
env.clients[0].process_tx(create_account_tx, false, false),
ProcessTxResponse::ValidTx
);
for i in 4..8 {
env.produce_block(0, i);
}

let res = env.clients[0].process_tx(send_money_tx, false, false);
assert_matches!(res, ProcessTxResponse::InvalidTx(_));
assert_matches!(
env.clients[0].process_tx(send_money_tx, false, false),
ProcessTxResponse::InvalidTx(_)
);
}

/// Helper for checking that duplicate transactions from implicit accounts are properly rejected.
Expand Down Expand Up @@ -179,7 +189,7 @@ fn get_status_of_tx_hash_collision_for_implicit_account(
100,
*block.hash(),
);
let status = env.clients[0].process_tx(send_money_from_implicit_account_tx, false, false);
let response = env.clients[0].process_tx(send_money_from_implicit_account_tx, false, false);

// Check that sending money from implicit account with correct nonce is still valid.
let send_money_from_implicit_account_tx = SignedTransaction::send_money(
Expand All @@ -192,7 +202,7 @@ fn get_status_of_tx_hash_collision_for_implicit_account(
);
check_tx_processing(&mut env, send_money_from_implicit_account_tx, height, blocks_number);

status
response
}

/// Test that duplicate transactions from implicit accounts are properly rejected.
Expand Down Expand Up @@ -272,8 +282,10 @@ fn test_transaction_nonce_too_large() {
100,
*genesis_block.hash(),
);
let res = env.clients[0].process_tx(tx, false, false);
assert_matches!(res, ProcessTxResponse::InvalidTx(InvalidTxError::InvalidAccessKeyError(_)));
assert_matches!(
env.clients[0].process_tx(tx, false, false),
ProcessTxResponse::InvalidTx(InvalidTxError::InvalidAccessKeyError(_))
);
}

/// This test tests the logic regarding requesting chunks for orphan.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ fn test_account_id_in_function_call_permission_upgrade() {
let tip = env.clients[0].chain.head().unwrap();
let signed_transaction =
Transaction { nonce: 10, block_hash: tip.last_block_hash, ..tx.clone() }.sign(&signer);
let res = env.clients[0].process_tx(signed_transaction, false, false);
assert_eq!(res, ProcessTxResponse::ValidTx);
assert_eq!(
env.clients[0].process_tx(signed_transaction, false, false),
ProcessTxResponse::ValidTx
);
for i in 0..3 {
env.produce_block(0, tip.height + i + 1);
}
Expand All @@ -84,9 +86,8 @@ fn test_account_id_in_function_call_permission_upgrade() {
let tip = env.clients[0].chain.head().unwrap();
let signed_transaction =
Transaction { nonce: 11, block_hash: tip.last_block_hash, ..tx }.sign(&signer);
let res = env.clients[0].process_tx(signed_transaction, false, false);
assert_eq!(
res,
env.clients[0].process_tx(signed_transaction, false, false),
ProcessTxResponse::InvalidTx(InvalidTxError::ActionsValidation(
ActionsValidationError::InvalidAccountId { account_id: "#".to_string() }
))
Expand Down Expand Up @@ -132,9 +133,8 @@ fn test_very_long_account_id() {
}
.sign(&signer);

let res = env.clients[0].process_tx(tx, false, false);
assert_eq!(
res,
env.clients[0].process_tx(tx, false, false),
ProcessTxResponse::InvalidTx(InvalidTxError::ActionsValidation(
ActionsValidationError::InvalidAccountId { account_id: "A".repeat(128) }
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use assert_matches::assert_matches;
use near_chain::{ChainGenesis, Provenance, RuntimeWithEpochManagerAdapter};
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_epoch_manager::shard_tracker::TrackedConfig;
use near_primitives::config::ExtCosts;
Expand Down Expand Up @@ -56,7 +57,7 @@ fn process_transaction(
last_block_hash,
);
let tx_hash = tx.get_hash();
env.clients[0].process_tx(tx, false, false);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);

for i in next_height..next_height + num_blocks {
let mut block = env.clients[0].produce_block(i).unwrap().unwrap();
Expand Down
11 changes: 9 additions & 2 deletions integration-tests/src/tests/client/features/flat_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::tests::client::runtimes::create_nightshade_runtimes;
use near_chain::ChainGenesis;
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_primitives::test_utils::encode;
use near_primitives::transaction::{Action, ExecutionMetadata, FunctionCallAction, Transaction};
Expand Down Expand Up @@ -78,7 +79,10 @@ fn test_flat_storage_upgrade() {
}
.sign(&signer);
let tx_hash = signed_transaction.get_hash();
env.clients[0].process_tx(signed_transaction, false, false);
assert_eq!(
env.clients[0].process_tx(signed_transaction, false, false),
ProcessTxResponse::ValidTx
);
for i in 0..blocks_to_process_txn {
env.produce_block(0, tip.height + i + 1);
}
Expand All @@ -103,7 +107,10 @@ fn test_flat_storage_upgrade() {
}
.sign(&signer);
let tx_hash = signed_transaction.get_hash();
env.clients[0].process_tx(signed_transaction, false, false);
assert_eq!(
env.clients[0].process_tx(signed_transaction, false, false),
ProcessTxResponse::ValidTx
);
for i in 0..blocks_to_process_txn {
env.produce_block(0, tip.height + i + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! We also test unaffected cases to make sure compute costs only affect
//! parameters they should.

use assert_matches::assert_matches;
use near_chain::ChainGenesis;
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
Expand Down Expand Up @@ -294,8 +293,7 @@ fn produce_saturated_chunk(
tx_ids.push(tx.get_hash());

// add tx to the mempool but don't execute it yet
let res = env.clients[0].process_tx(tx, false, false);
assert_matches!(res, ProcessTxResponse::ValidTx);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
}

// process the queued transactions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use assert_matches::assert_matches;
use near_chain::{ChainGenesis, Provenance, RuntimeWithEpochManagerAdapter};
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_epoch_manager::shard_tracker::TrackedConfig;
use near_o11y::testonly::init_test_logger;
Expand Down Expand Up @@ -90,7 +91,7 @@ fn protocol_upgrade() {
Transaction { nonce: tip.height + 1, block_hash: tip.last_block_hash, ..tx.clone() }
.sign(&signer);
let tx_hash = signed_tx.get_hash();
env.clients[0].process_tx(signed_tx, false, false);
assert_eq!(env.clients[0].process_tx(signed_tx, false, false), ProcessTxResponse::ValidTx);
produce_blocks_from_height_with_protocol_version(
&mut env,
epoch_length,
Expand All @@ -110,7 +111,7 @@ fn protocol_upgrade() {
Transaction { nonce: tip.height + 1, block_hash: tip.last_block_hash, ..tx }
.sign(&signer);
let tx_hash = signed_tx.get_hash();
env.clients[0].process_tx(signed_tx, false, false);
assert_eq!(env.clients[0].process_tx(signed_tx, false, false), ProcessTxResponse::ValidTx);
for i in 0..epoch_length {
let block = env.clients[0].produce_block(tip.height + i + 1).unwrap().unwrap();
env.process_block(0, block.clone(), Provenance::PRODUCED);
Expand Down Expand Up @@ -147,7 +148,7 @@ fn protocol_upgrade() {
Transaction { nonce: tip.height + 1, block_hash: tip.last_block_hash, ..tx }
.sign(&signer);
let tx_hash = signed_tx.get_hash();
env.clients[0].process_tx(signed_tx, false, false);
assert_eq!(env.clients[0].process_tx(signed_tx, false, false), ProcessTxResponse::ValidTx);
for i in 0..epoch_length {
let block = env.clients[0].produce_block(tip.height + i + 1).unwrap().unwrap();
env.process_block(0, block.clone(), Provenance::PRODUCED);
Expand Down
11 changes: 9 additions & 2 deletions integration-tests/src/tests/client/features/wasmer2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::tests::client::process_blocks::{create_nightshade_runtimes, deploy_te
use near_chain::ChainGenesis;
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_client::ProcessTxResponse;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::{Action, FunctionCallAction, Transaction};
Expand Down Expand Up @@ -61,7 +62,10 @@ fn test_near_vm_upgrade() {
let tip = env.clients[0].chain.head().unwrap();
let signed_transaction =
Transaction { nonce: 10, block_hash: tip.last_block_hash, ..tx.clone() }.sign(&signer);
env.clients[0].process_tx(signed_transaction, false, false);
assert_eq!(
env.clients[0].process_tx(signed_transaction, false, false),
ProcessTxResponse::ValidTx
);
for i in 0..3 {
env.produce_block(0, tip.height + i + 1);
}
Expand All @@ -75,7 +79,10 @@ fn test_near_vm_upgrade() {
let tip = env.clients[0].chain.head().unwrap();
let signed_transaction =
Transaction { nonce: 11, block_hash: tip.last_block_hash, ..tx }.sign(&signer);
env.clients[0].process_tx(signed_transaction, false, false);
assert_eq!(
env.clients[0].process_tx(signed_transaction, false, false),
ProcessTxResponse::ValidTx
);
for i in 0..3 {
env.produce_block(0, tip.height + i + 1);
}
Expand Down
Loading

0 comments on commit 3600a2f

Please sign in to comment.