Skip to content

Commit

Permalink
impl
Browse files Browse the repository at this point in the history
  • Loading branch information
pugachAG committed Feb 22, 2025
1 parent baf4929 commit bfbeb61
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 148 deletions.
2 changes: 1 addition & 1 deletion core/chain-configs/src/test_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl TestGenesisBuilder {

pub fn add_user_accounts_simple(
mut self,
accounts: &Vec<AccountId>,
accounts: &[AccountId],
initial_balance: Balance,
) -> Self {
for account_id in accounts {
Expand Down
12 changes: 6 additions & 6 deletions core/primitives/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ impl SignedTransaction {

pub fn deploy_global_contract(
nonce: Nonce,
contract_id: AccountId,
account_id: AccountId,
code: Vec<u8>,
signer: &Signer,
block_hash: CryptoHash,
deploy_mode: GlobalContractDeployMode,
) -> SignedTransaction {
let signer_id = contract_id.clone();
let receiver_id = contract_id;
let signer_id = account_id.clone();
let receiver_id = account_id;
SignedTransaction::from_actions(
nonce,
signer_id,
Expand All @@ -303,13 +303,13 @@ impl SignedTransaction {

pub fn use_global_contract(
nonce: Nonce,
contract_id: &AccountId,
account_id: &AccountId,
signer: &Signer,
block_hash: CryptoHash,
contract_identifier: GlobalContractIdentifier,
) -> SignedTransaction {
let signer_id = contract_id.clone();
let receiver_id = contract_id.clone();
let signer_id = account_id.clone();
let receiver_id = account_id.clone();
SignedTransaction::from_actions(
nonce,
signer_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use near_chain_configs::test_genesis::{
build_genesis_and_epoch_config_store, GenesisAndEpochConfigParams, ValidatorsSpec,
};
use near_o11y::testonly::init_test_logger;
use near_primitives::action::{GlobalContractDeployMode, GlobalContractIdentifier};
use near_primitives::hash::CryptoHash;
use near_primitives::shard_layout::ShardLayout;
use near_primitives::types::AccountId;
use near_primitives::version::PROTOCOL_VERSION;
Expand All @@ -21,7 +19,6 @@ use crate::test_loop::utils::get_head_height;
use crate::test_loop::utils::transactions::{
call_contract, check_txs, deploy_contract, make_accounts,
};
use crate::test_loop::utils::transactions::{deploy_global_contract, use_global_contract};

const EPOCH_LENGTH: u64 = 10;
const GENESIS_HEIGHT: u64 = 1000;
Expand Down Expand Up @@ -77,93 +74,6 @@ fn test_contract_distribution_cross_shard() {
env.shutdown_and_drain_remaining_events(Duration::seconds(20));
}

#[cfg_attr(not(feature = "nightly_protocol"), ignore)]
#[test]
fn test_global_contract_by_hash() {
let (env, accounts, contract, rpc_id) = setup_global_contract_test();
let deploy_mode = GlobalContractDeployMode::CodeHash;
test_global_contract(env, deploy_mode, accounts.as_slice(), &contract, rpc_id);
}

#[cfg_attr(not(feature = "nightly_protocol"), ignore)]
#[test]
fn test_global_contract_by_account_id() {
let (env, accounts, contract, rpc_id) = setup_global_contract_test();
let deploy_mode = GlobalContractDeployMode::AccountId;
test_global_contract(env, deploy_mode, accounts.as_slice(), &contract, rpc_id);
}

fn setup_global_contract_test() -> (TestLoopEnv, Vec<AccountId>, ContractCode, AccountId) {
init_test_logger();
let accounts = make_accounts(NUM_ACCOUNTS);

let (env, rpc_id) = setup(&accounts);

let rpc_index = 8;
assert_eq!(accounts[rpc_index], rpc_id);

let contract = ContractCode::new(near_test_contracts::rs_contract().to_vec(), None);
(env, accounts, contract, rpc_id)
}

fn test_global_contract(
mut env: TestLoopEnv,
deploy_mode: GlobalContractDeployMode,
accounts: &[AccountId],
contract: &ContractCode,
rpc_id: AccountId,
) {
let mut nonce = 1;
let deploy_tx = deploy_global_contract(
&mut env.test_loop,
&env.datas,
&rpc_id,
&accounts[0],
contract.code().into(),
deploy_mode.clone(),
nonce,
);
nonce += 1;
env.test_loop.run_for(Duration::seconds(3));
check_txs(&env.test_loop.data, &env.datas, &rpc_id, &[deploy_tx]);
let identifier = match deploy_mode {
GlobalContractDeployMode::CodeHash => {
let code_hash = CryptoHash::hash_bytes(contract.code());
GlobalContractIdentifier::CodeHash(code_hash)
}
GlobalContractDeployMode::AccountId => {
GlobalContractIdentifier::AccountId(accounts[0].clone())
}
};
// test on accounts from different shards
for account in [&accounts[1], &accounts[6]] {
let use_tx = use_global_contract(
&mut env.test_loop,
&env.datas,
&rpc_id,
account,
identifier.clone(),
nonce,
);
nonce += 1;
env.test_loop.run_for(Duration::seconds(3));
check_txs(&env.test_loop.data, &env.datas, &rpc_id, &[use_tx]);
let call_tx = call_contract(
&mut env.test_loop,
&env.datas,
&rpc_id,
account,
account,
"log_something".to_owned(),
vec![],
nonce,
);
env.test_loop.run_for(Duration::seconds(3));
check_txs(&env.test_loop.data, &env.datas, &rpc_id, &[call_tx]);
}
env.shutdown_and_drain_remaining_events(Duration::seconds(20));
}

fn setup(accounts: &Vec<AccountId>) -> (TestLoopEnv, AccountId) {
let builder = TestLoopBuilder::new();

Expand Down
Loading

0 comments on commit bfbeb61

Please sign in to comment.