Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Longarithm committed Nov 6, 2024
1 parent a04b1de commit 07267e6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 62 deletions.
10 changes: 5 additions & 5 deletions core/store/src/trie/mem/mem_trie_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl UpdatedMemTrieNodeWithSize {
}
}

/// Keeps values and internal nodes accessed on updating memtrie.
/// Keeps hashes and encoded trie nodes accessed on updating memtrie.
pub struct TrieAccesses {
/// Hashes and encoded trie nodes.
pub nodes: HashMap<CryptoHash, Arc<[u8]>>,
Expand All @@ -83,11 +83,11 @@ struct TrieChangesTracker {
/// Separated from `refcount_deleted_hashes` to postpone hash computation
/// as far as possible.
refcount_inserted_values: BTreeMap<Vec<u8>, u32>,
/// All observed values and internal nodes.
/// All observed internal nodes.
/// Needed to prepare recorded storage.
/// Note that negative `refcount_changes` does not fully cover it, as node
/// or value of the same hash can be removed and inserted for the same
/// update in different parts of trie!
/// Note that negative `refcount_deleted_hashes` does not fully cover it,
/// as node or value of the same hash can be removed and inserted for the
/// same update in different parts of trie!
accesses: TrieAccesses,
}

Expand Down
14 changes: 3 additions & 11 deletions core/store/src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,15 +1660,6 @@ impl Trie {
for (node_hash, serialized_node) in trie_accesses.nodes {
recorder.borrow_mut().record(&node_hash, serialized_node);
}
// for (value_hash, value) in trie_accesses.values {
// let value = match value {
// FlatStateValue::Ref(_) => {
// self.storage.retrieve_raw_bytes(&value_hash)?
// }
// FlatStateValue::Inlined(value) => value.into(),
// };
// recorder.borrow_mut().record(&value_hash, value);
// }
}
Ok(trie_changes)
}
Expand All @@ -1693,6 +1684,7 @@ impl Trie {
GenericNodeOrIndex::Updated(root_node.0),
);
}

trie_update.flatten_nodes(&self.root, root_node.0)
}
}
Expand Down Expand Up @@ -2236,8 +2228,8 @@ mod tests {
let trie2 = tries.get_trie_for_shard(ShardUId::single_shard(), root).recording_reads();
let updates = vec![(b"doge".to_vec(), None)];
trie2.update(updates).unwrap();
// record extension, branch and both leaves (one with value)
assert_eq!(trie2.recorded_storage().unwrap().nodes.len(), 5);
// record extension, branch and both leaves, but not the value.
assert_eq!(trie2.recorded_storage().unwrap().nodes.len(), 4);
}

{
Expand Down
2 changes: 1 addition & 1 deletion core/store/src/trie/trie_storage_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<'a> GenericTrieUpdate<'a, TrieStorageNodePtr, ValueHandle> for TrieStorageU
fn delete_value(&mut self, value: ValueHandle) -> Result<(), StorageError> {
match value {
ValueHandle::HashAndSize(value) => {
self.trie.internal_retrieve_trie_node(&value.hash, true, true)?;
// Note that we don't need to read the actual value to remove it.
self.refcount_changes.subtract(value.hash, 1);
}
ValueHandle::InMemory(_) => {
Expand Down
127 changes: 82 additions & 45 deletions integration-tests/src/test_loop/utils/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use crate::test_loop::env::TestData;
use crate::test_loop::env::{TestData, TestLoopEnv};
use assert_matches::assert_matches;
use itertools::Itertools;
use near_async::messaging::{CanSend, SendAsync};
use near_async::test_loop::TestLoopV2;
use near_async::time::Duration;
use near_client::test_utils::test_loop::ClientQueries;
use near_client::{Client, ProcessTxResponse};
use near_crypto::{PublicKey, Signer};
use near_crypto::Signer;
use near_network::client::ProcessTxRequest;
use near_primitives::errors::InvalidTxError;
use near_primitives::hash::CryptoHash;
use near_primitives::test_utils::create_user_test_signer;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::AccountId;
use near_primitives::views::{FinalExecutionOutcomeView, FinalExecutionStatus};
use near_primitives::views::{
FinalExecutionOutcomeView, FinalExecutionStatus, QueryRequest, QueryResponseKind,
};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -47,6 +49,25 @@ pub(crate) fn get_anchor_hash(clients: &[&Client]) -> CryptoHash {
anchor_hash
}

/// Get next available nonce for the account's public key.
pub fn get_next_nonce(env: &mut TestLoopEnv, account_id: &AccountId) -> u64 {
let signer: Signer = create_user_test_signer(&account_id).into();
let public_key = signer.public_key();
let clients = env
.datas
.iter()
.map(|data| &env.test_loop.data.get(&data.client_sender.actor_handle()).client)
.collect_vec();
let response = clients.runtime_query(
account_id,
QueryRequest::ViewAccessKey { account_id: account_id.clone(), public_key },
);
let QueryResponseKind::AccessKey(access_key) = response.kind else {
panic!("Expected AccessKey response");
};
access_key.nonce + 1
}

/// Execute money transfers within given `TestLoop` between given accounts.
/// Runs chain long enough for the transfers to be optimistically executed.
/// Used to generate state changes and check that chain is able to update
Expand Down Expand Up @@ -126,18 +147,54 @@ pub(crate) fn execute_money_transfers(
Ok(())
}

/// Create account.
pub fn do_create_account(
env: &mut TestLoopEnv,
rpc_id: &AccountId,
originator: &AccountId,
new_account_id: &AccountId,
amount: u128,
) {
tracing::info!(target: "test", "Creating account.");
let tx = create_account(env, rpc_id, originator, new_account_id, amount);
env.test_loop.run_for(Duration::seconds(5));
check_txs(&env.test_loop, &env.datas, rpc_id, &[tx]);
}

pub fn do_delete_account(
env: &mut TestLoopEnv,
rpc_id: &AccountId,
account_id: &AccountId,
beneficiary_id: &AccountId,
) {
tracing::info!(target: "test", "Deleting account.");
let tx = delete_account(env, rpc_id, account_id, beneficiary_id);
env.test_loop.run_for(Duration::seconds(5));
check_txs(&env.test_loop, &env.datas, rpc_id, &[tx]);
}

pub fn do_deploy_contract(
env: &mut TestLoopEnv,
rpc_id: &AccountId,
contract_id: &AccountId,
code: Vec<u8>,
) {
tracing::info!(target: "test", "Deploying contract.");
let nonce = get_next_nonce(env, contract_id);
let tx = deploy_contract(&mut env.test_loop, &env.datas, rpc_id, contract_id, code, nonce);
env.test_loop.run_for(Duration::seconds(2));
check_txs(&env.test_loop, &env.datas, rpc_id, &[tx]);
}

pub fn create_account(
test_loop: &mut TestLoopV2,
node_datas: &[TestData],
env: &mut TestLoopEnv,
rpc_id: &AccountId,
originator: &AccountId,
new_account_id: &AccountId,
amount: u128,
nonce: u64,
) -> CryptoHash {
let block_hash = get_shared_block_hash(node_datas, test_loop);
let block_hash = get_shared_block_hash(&env.datas, &env.test_loop);

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

Expand All @@ -150,32 +207,22 @@ pub fn create_account(
&signer,
block_hash,
);
let tx_hash = tx.get_hash();
let process_tx_request =
ProcessTxRequest { transaction: tx, is_forwarded: false, check_only: false };

let rpc_node_data = get_node_data(node_datas, rpc_id);
let rpc_node_data_sender = &rpc_node_data.client_sender;

let future = rpc_node_data_sender.send_async(process_tx_request);
drop(future);

let tx_hash = tx.get_hash();
submit_tx(&env.datas, rpc_id, tx);
tracing::debug!(target: "test", ?originator, ?new_account_id, ?tx_hash, "created account");
tx_hash
}

/// Delete account.
pub fn delete_account(
test_loop: &mut TestLoopV2,
node_datas: &[TestData],
env: &mut TestLoopEnv,
rpc_id: &AccountId,
account_id: &AccountId,
beneficiary_id: &AccountId,
nonce: u64,
) -> CryptoHash {
let block_hash = get_shared_block_hash(node_datas, test_loop);

let signer = create_user_test_signer(&account_id).into();
let signer: Signer = create_user_test_signer(&account_id).into();
let nonce = get_next_nonce(env, account_id);
let block_hash = get_shared_block_hash(&env.datas, &env.test_loop);

let tx = SignedTransaction::delete_account(
nonce,
Expand All @@ -185,22 +232,15 @@ pub fn delete_account(
&signer,
block_hash,
);
let tx_hash = tx.get_hash();
let process_tx_request =
ProcessTxRequest { transaction: tx, is_forwarded: false, check_only: false };

let rpc_node_data = get_node_data(node_datas, rpc_id);
let rpc_node_data_sender = &rpc_node_data.client_sender;

let future = rpc_node_data_sender.send_async(process_tx_request);
drop(future);

let tx_hash = tx.get_hash();
submit_tx(&env.datas, rpc_id, tx);
tracing::debug!(target: "test", ?account_id, ?beneficiary_id, ?tx_hash, "deleted account");
tx_hash
}

/// Deploy the test contract to the provided contract_id account. The contract
/// account should already exits. The contract will be deployed from the contract
/// account should already exist. The contract will be deployed from the contract
/// account itself.
///
/// This function does not wait until the transactions is executed.
Expand All @@ -218,14 +258,7 @@ pub fn deploy_contract(

let tx = SignedTransaction::deploy_contract(nonce, contract_id, code, &signer, block_hash);
let tx_hash = tx.get_hash();
let process_tx_request =
ProcessTxRequest { transaction: tx, is_forwarded: false, check_only: false };

let rpc_node_data = get_node_data(node_datas, rpc_id);
let rpc_node_data_sender = &rpc_node_data.client_sender;

let future = rpc_node_data_sender.send_async(process_tx_request);
drop(future);
submit_tx(node_datas, rpc_id, tx);

tracing::debug!(target: "test", ?contract_id, ?tx_hash, "deployed contract");
tx_hash
Expand Down Expand Up @@ -262,7 +295,14 @@ pub fn call_contract(
);

let tx_hash = tx.get_hash();
submit_tx(node_datas, rpc_id, tx);
tracing::debug!(target: "test", ?sender_id, ?contract_id, ?tx_hash, "called contract");
tx_hash
}

/// Submit a transaction to the rpc node with the given account id.
/// Doesn't wait for the result, it must be requested separately.
pub fn submit_tx(node_datas: &[TestData], rpc_id: &AccountId, tx: SignedTransaction) {
let process_tx_request =
ProcessTxRequest { transaction: tx, is_forwarded: false, check_only: false };

Expand All @@ -271,9 +311,6 @@ pub fn call_contract(

let future = rpc_node_data_sender.send_async(process_tx_request);
drop(future);

tracing::debug!(target: "test", ?sender_id, ?contract_id, ?tx_hash, "called contract");
tx_hash
}

/// Check the status of the transactions and assert that they are successful.
Expand Down

0 comments on commit 07267e6

Please sign in to comment.