Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Dec 15, 2024
1 parent ed8f37d commit 041bb7a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 58 deletions.
20 changes: 13 additions & 7 deletions integration-tests/src/test_loop/tests/max_receipt_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn slow_test_max_receipt_size() {

let account0: AccountId = "account0".parse().unwrap();
let account0_signer = &create_user_test_signer(&account0).into();
let rpc_id = "account4".parse().unwrap();

// We can't test receipt limit by submitting large transactions because we hit the transaction size limit
// before hitting the receipt size limit.
Expand All @@ -33,7 +34,7 @@ fn slow_test_max_receipt_size() {
get_shared_block_hash(&env.datas, &env.test_loop),
);
let large_tx_exec_res =
execute_tx(&mut env.test_loop, large_tx, &env.datas, Duration::seconds(5));
execute_tx(&mut env.test_loop, &rpc_id, large_tx, &env.datas, Duration::seconds(5));
assert_matches!(large_tx_exec_res, Err(InvalidTxError::TransactionSizeExceeded { .. }));

// Let's test it by running a contract that generates a large receipt.
Expand All @@ -44,7 +45,7 @@ fn slow_test_max_receipt_size() {
&account0_signer,
get_shared_block_hash(&env.datas, &env.test_loop),
);
run_tx(&mut env.test_loop, deploy_contract_tx, &env.datas, Duration::seconds(5));
run_tx(&mut env.test_loop, &rpc_id, deploy_contract_tx, &env.datas, Duration::seconds(5));

// Calling generate_large_receipt({"account_id": "account0", "method_name": "noop", "total_args_size": 3000000})
// will generate a receipt that has ~3_000_000 bytes. It'll be a single receipt with multiple FunctionCall actions.
Expand All @@ -60,7 +61,7 @@ fn slow_test_max_receipt_size() {
300 * TGAS,
get_shared_block_hash(&env.datas, &env.test_loop),
);
run_tx(&mut env.test_loop, large_receipt_tx, &env.datas, Duration::seconds(5));
run_tx(&mut env.test_loop, &rpc_id, large_receipt_tx, &env.datas, Duration::seconds(5));

// Generating a receipt that is 5 MB should fail, it's above the receipt size limit.
let too_large_receipt_tx = SignedTransaction::call(
Expand All @@ -74,9 +75,14 @@ fn slow_test_max_receipt_size() {
300 * TGAS,
get_shared_block_hash(&env.datas, &env.test_loop),
);
let too_large_receipt_tx_exec_res =
execute_tx(&mut env.test_loop, too_large_receipt_tx, &env.datas, Duration::seconds(5))
.unwrap();
let too_large_receipt_tx_exec_res = execute_tx(
&mut env.test_loop,
&rpc_id,
too_large_receipt_tx,
&env.datas,
Duration::seconds(5),
)
.unwrap();

match too_large_receipt_tx_exec_res.status {
FinalExecutionStatus::Failure(TxExecutionError::ActionError(action_error)) => {
Expand Down Expand Up @@ -111,7 +117,7 @@ fn slow_test_max_receipt_size() {
300 * TGAS,
get_shared_block_hash(&env.datas, &env.test_loop),
);
let sum_4_res = run_tx(&mut env.test_loop, sum_4_tx, &env.datas, Duration::seconds(5));
let sum_4_res = run_tx(&mut env.test_loop, &rpc_id, sum_4_tx, &env.datas, Duration::seconds(5));
assert_eq!(sum_4_res, 10u64.to_le_bytes().to_vec());

env.shutdown_and_drain_remaining_events(Duration::seconds(20));
Expand Down
101 changes: 62 additions & 39 deletions integration-tests/src/test_loop/tests/resharding_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::test_loop::utils::sharding::{
next_block_has_new_shard_layout, print_and_assert_shard_accounts,
};
use crate::test_loop::utils::transactions::{
create_account, delete_account, get_anchor_hash, get_next_nonce, get_shared_block_hash, get_smallest_height_head, run_tx,
store_and_submit_tx, submit_tx,
create_account, delete_account, get_anchor_hash, get_next_nonce, get_shared_block_hash,
get_smallest_height_head, run_tx, store_and_submit_tx, submit_tx,
};
use crate::test_loop::utils::trie_sanity::{
check_state_shard_uid_mapping_after_resharding, TrieSanityCheck,
Expand Down Expand Up @@ -521,19 +521,34 @@ fn get_base_shard_layout(version: u64) -> ShardLayout {
}
}

fn account_exist_in_view_client(
// After resharding and gc-period, assert the deleted `account_id` is still accessible through archival node view client,
// and it is not accessible through a regular, RPC node.
fn check_deleted_account_availability(
env: &mut TestLoopEnv,
node_index: usize,
archival_id: &AccountId,
rpc_id: &AccountId,
account_id: AccountId,
height: u64,
) -> bool {
let view_client_handle = env.datas[node_index].view_client_sender.actor_handle();
let view_client = env.test_loop.data.get_mut(&view_client_handle);
) {
let archival_node_data = get_node_data(&env.datas, &archival_id);
let rpc_node_data = get_node_data(&env.datas, &rpc_id);
let archival_view_client_handle = archival_node_data.view_client_sender.actor_handle();
let rpc_view_client_handle = rpc_node_data.view_client_sender.actor_handle();

let block_reference = BlockReference::BlockId(BlockId::Height(height));
let request = QueryRequest::ViewAccount { account_id };
let msg = Query::new(block_reference, request);
let result = near_async::messaging::Handler::handle(view_client, msg);
result.is_ok()

let archival_node_result = {
let view_client = env.test_loop.data.get_mut(&archival_view_client_handle);
near_async::messaging::Handler::handle(view_client, msg)
};
let rpc_node_result = {
let view_client = env.test_loop.data.get_mut(&rpc_view_client_handle);
near_async::messaging::Handler::handle(view_client, msg)
};
assert!(archival_node_result.is_ok());
assert!(!rpc_node_result.is_ok());
}

/// Base setup to check sanity of Resharding V3.
Expand Down Expand Up @@ -629,7 +644,9 @@ fn test_resharding_v3_base(params: TestReshardingParameters) {
let runtime_config_store = RuntimeConfigStore::with_one_config(runtime_config);
builder = builder.runtime_config_store(runtime_config_store);
}
let num_clients = params.clients.len();

let archival_id = params.archival_clients.iter().next().unwrap().clone();
let rpc_id = params.rpc_clients[0].clone();

let mut env = builder
.genesis(genesis)
Expand All @@ -643,27 +660,39 @@ fn test_resharding_v3_base(params: TestReshardingParameters) {
)
.build();

let mut test_setup_transactions = vec![];
for contract_id in &params.deploy_test_contract {
let signer = &create_user_test_signer(&contract_id).into();
let deploy_contract_tx = SignedTransaction::deploy_contract(
101,
&contract_id,
let deploy_contract_tx = deploy_contract(
&mut env.test_loop,
&env.datas,
&rpc_id,
contract_id,
near_test_contracts::rs_contract().into(),
&signer,
get_shared_block_hash(&env.datas, &env.test_loop),
1,
);
run_tx(&mut env.test_loop, deploy_contract_tx, &env.datas, Duration::seconds(5));
test_setup_transactions.push(deploy_contract_tx);
}

// Create an account that is:
// 1) subaccount of a future resharding boundary account
// 2) temporary, because we will remove it after resharding
// For that two reasons combined, it can catch some bugs that normally would not occur,
// and we can test removing something from state and see if it is kept on archival node.
let rpc_id = params.rpc_clients[0].clone();
// 1) Subaccount of a future resharding boundary account.
// 2) Temporary, because we will remove it after resharding.
// The goal is to test removing some state and see if it is kept on archival node.
// The secondary goal is to catch potential bugs due to the above two conditions making it a special case.
let temporary_account =
format!("{}.{}", new_boundary_account, new_boundary_account).parse().unwrap();
create_account(&mut env, &rpc_id, &new_boundary_account, &temporary_account, 100 * ONE_NEAR);
let create_account_tx = create_account(
&mut env,
&rpc_id,
&new_boundary_account,
&temporary_account,
10 * ONE_NEAR,
2,
);
test_setup_transactions.push(create_account_tx);

// Wait for the test setup transactions to settle and ensure they all succeeded.
env.test_loop.run_for(Duration::seconds(2));
check_txs(&env.test_loop, &env.datas, &rpc_id, &test_setup_transactions);

let client_handles =
env.datas.iter().map(|data| data.client_sender.actor_handle()).collect_vec();
Expand Down Expand Up @@ -736,26 +765,20 @@ fn test_resharding_v3_base(params: TestReshardingParameters) {
let client = &env.test_loop.data.get(&client_handles[0]).client;
trie_sanity_check.check_epochs(client);
let height_after_resharding = latest_block_height.get();
// Produce some blocks after resharding, then delete `temporary_account`.
env.test_loop.run_for(Duration::seconds(2));
delete_account(&mut env, &rpc_id, &temporary_account, &new_boundary_account);
// Wait for garbage collection to kick in, so that it is tested as well.

// Delete `temporary_account`.
delete_account(&mut env, &rpc_id, &temporary_account, &rpc_id);
// Wait for garbage collection to kick in.
env.test_loop
.run_for(Duration::seconds((DEFAULT_GC_NUM_EPOCHS_TO_KEEP * params.epoch_length) as i64));
// After resharding and gc-period, assert the deleted `temporary_account` is still accessible through archival node view client,
// and it is not accessible through a regular, RPC node.
assert!(account_exist_in_view_client(
// Check that the deleted account is still accessible at archival node, but not at a regular node.
check_deleted_account_availability(
&mut env,
num_clients - 1,
temporary_account.clone(),
height_after_resharding
));
assert!(!account_exist_in_view_client(
&mut env,
num_clients - 2,
&archival_id,
&rpc_id,
temporary_account,
height_after_resharding
));
height_after_resharding,
);

env.shutdown_and_drain_remaining_events(Duration::seconds(20));
}
Expand Down
23 changes: 11 additions & 12 deletions integration-tests/src/test_loop/utils/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ pub fn do_create_account(
amount: u128,
) {
tracing::info!(target: "test", "Creating account.");
let tx = create_account(env, rpc_id, originator, new_account_id, amount);
let nonce = get_next_nonce(&env.test_loop.data, &env.datas, originator);
let tx = create_account(env, rpc_id, originator, new_account_id, amount, nonce);
env.test_loop.run_for(Duration::seconds(5));
check_txs(&env.test_loop, &env.datas, rpc_id, &[tx]);
}
Expand Down Expand Up @@ -228,12 +229,11 @@ pub fn create_account(
originator: &AccountId,
new_account_id: &AccountId,
amount: u128,
nonce: u64,
) -> CryptoHash {
let block_hash = get_shared_block_hash(&env.datas, &env.test_loop);

let nonce = get_next_nonce(&env.test_loop.data, &env.datas, originator);
let signer = create_user_test_signer(&originator).into();
let new_signer: Signer = create_user_test_signer(&new_account_id).into();
let signer = create_user_test_signer(&originator);
let new_signer: Signer = create_user_test_signer(&new_account_id);

let tx = SignedTransaction::create_account(
nonce,
Expand Down Expand Up @@ -408,18 +408,19 @@ pub fn get_node_data<'a>(node_datas: &'a [TestData], account_id: &AccountId) ->
return node_data;
}
}
panic!("RPC client not found");
panic!("client not found");
}

/// Run a transaction until completion and assert that the result is "success".
/// Returns the transaction result.
pub fn run_tx(
test_loop: &mut TestLoopV2,
rpc_id: &AccountId,
tx: SignedTransaction,
node_datas: &[TestData],
maximum_duration: Duration,
) -> Vec<u8> {
let tx_res = execute_tx(test_loop, tx, node_datas, maximum_duration).unwrap();
let tx_res = execute_tx(test_loop, rpc_id, tx, node_datas, maximum_duration).unwrap();
assert_matches!(tx_res.status, FinalExecutionStatus::SuccessValue(_));
match tx_res.status {
FinalExecutionStatus::SuccessValue(res) => res,
Expand Down Expand Up @@ -462,22 +463,20 @@ pub fn run_txs_parallel(
/// For valid transactions returns the execution result (which could have an execution error inside, check it!).
pub fn execute_tx(
test_loop: &mut TestLoopV2,
rpc_id: &AccountId,
tx: SignedTransaction,
node_datas: &[TestData],
maximum_duration: Duration,
) -> Result<FinalExecutionOutcomeView, InvalidTxError> {
// Last node is usually the rpc node
let rpc_node_id = node_datas.len().checked_sub(1).unwrap();

let client_sender = &node_datas[rpc_node_id].client_sender;
let client_sender = &get_node_data(node_datas, rpc_id).client_sender;
let future_spawner = test_loop.future_spawner();

let mut tx_runner = TransactionRunner::new(tx, true);

let mut res = None;
test_loop.run_until(
|tl_data| {
let client = &tl_data.get(&node_datas[rpc_node_id].client_sender.actor_handle()).client;
let client = &tl_data.get(&client_sender.actor_handle()).client;
match tx_runner.poll(client_sender, client, &future_spawner) {
Poll::Pending => false,
Poll::Ready(tx_res) => {
Expand Down

0 comments on commit 041bb7a

Please sign in to comment.