Skip to content

Commit

Permalink
WIP. All tests compile, but a few still fail
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenykuzyakov committed Aug 7, 2019
1 parent e5279c8 commit bb6c090
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 126 deletions.
29 changes: 24 additions & 5 deletions near/tests/runtime_fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@ use tempdir::TempDir;

use near::{get_store_path, GenesisConfig, NightshadeRuntime};
use near_chain::{Block, Chain, Provenance};
use near_primitives::crypto::signer::InMemorySigner;
use near_primitives::crypto::signer::{EDSigner, InMemorySigner};
use near_primitives::hash::CryptoHash;
use near_primitives::test_utils::init_test_logger;
use near_primitives::transaction::TransactionBody;
use near_primitives::transaction::{Action, SignedTransaction, Transaction, TransferAction};
use near_primitives::types::{AccountId, Balance, Nonce};
use near_store::create_store;

fn send_money(
nonce: Nonce,
signer_id: AccountId,
receiver_id: AccountId,
deposit: Balance,
signer: Arc<dyn EDSigner>,
) -> SignedTransaction {
Transaction {
nonce,
signer_id,
public_key: signer.public_key(),
receiver_id,

actions: vec![Action::Transfer(TransferAction { deposit })],
}
.sign(&*signer)
}

#[test]
fn runtime_hanldle_fork() {
init_test_logger();
Expand All @@ -24,9 +43,9 @@ fn runtime_hanldle_fork() {

let mut chain = Chain::new(store, runtime, genesis_config.genesis_time).unwrap();

let tx1 = TransactionBody::send_money(1, "near.0", "near.1", 100).sign(&*signer);
let tx2 = TransactionBody::send_money(1, "near.0", "near.1", 500).sign(&*signer);
let tx3 = TransactionBody::send_money(2, "near.0", "near.1", 100).sign(&*signer);
let tx1 = send_money(1, "near.0".to_string(), "near.1".to_string(), 100, signer.clone());
let tx2 = send_money(1, "near.0".to_string(), "near.1".to_string(), 500, signer.clone());
let tx3 = send_money(2, "near.0".to_string(), "near.1".to_string(), 100, signer.clone());
let state_root = chain.get_post_state_root(&chain.genesis().hash()).unwrap().clone();
let b1 = Block::produce(
chain.genesis(),
Expand Down
103 changes: 45 additions & 58 deletions near/tests/stake_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use near::{load_test_config, start_with_config, GenesisConfig, NearConfig};
use near_client::{ClientActor, Query, Status, ViewClientActor};
use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout};
use near_network::NetworkClientMessages;
use near_primitives::crypto::signer::EDSigner;
use near_primitives::rpc::{QueryResponse, ValidatorInfo};
use near_primitives::serialize::BaseEncode;
use near_primitives::test_utils::init_integration_logger;
use near_primitives::transaction::{StakeTransaction, TransactionBody};
use near_primitives::types::AccountId;
use near_primitives::transaction::{Action, SignedTransaction, StakeAction, Transaction};
use near_primitives::types::{AccountId, Balance, Nonce};

lazy_static! {
static ref HEAVY_TESTS_LOCK: Mutex<()> = Mutex::new(());
Expand All @@ -38,6 +39,23 @@ struct TestNode {
view_client: Addr<ViewClientActor>,
}

fn stake_transaction(
nonce: Nonce,
signer_id: AccountId,
stake: Balance,
signer: Arc<dyn EDSigner>,
) -> SignedTransaction {
Transaction {
nonce,
signer_id: signer_id.clone(),
public_key: signer.public_key(),
receiver_id: signer_id,

actions: vec![Action::Stake(StakeAction { stake, public_key: signer.public_key() })],
}
.sign(&*signer)
}

fn init_test_staking(num_accounts: usize, num_nodes: usize, epoch_length: u64) -> Vec<TestNode> {
init_integration_logger();

Expand Down Expand Up @@ -76,20 +94,12 @@ fn test_stake_nodes() {
let system = System::new("NEAR");
let test_nodes = init_test_staking(2, 1, 10);

let tx = TransactionBody::Stake(StakeTransaction {
nonce: 1,
originator: test_nodes[1].account_id.clone(),
amount: TESTING_INIT_STAKE,
public_key: test_nodes[1]
.config
.block_producer
.clone()
.unwrap()
.signer
.public_key()
.to_base(),
})
.sign(&*test_nodes[1].config.block_producer.clone().unwrap().signer);
let tx = stake_transaction(
1,
test_nodes[1].account_id.clone(),
TESTING_INIT_STAKE,
test_nodes[1].config.block_producer.as_ref().unwrap().signer.clone(),
);
actix::spawn(
test_nodes[0]
.client
Expand Down Expand Up @@ -133,20 +143,12 @@ fn test_validator_kickout() {
let stakes = (0..num_nodes / 2).map(|_| rng.gen_range(1, 100));
let stake_transactions = stakes.enumerate().map(|(i, stake)| {
let test_node = &test_nodes[i];
TransactionBody::Stake(StakeTransaction {
nonce: 1,
originator: test_node.account_id.clone(),
amount: stake,
public_key: test_node
.config
.block_producer
.as_ref()
.unwrap()
.signer
.public_key()
.to_base(),
})
.sign(&*test_node.config.block_producer.as_ref().unwrap().signer)
stake_transaction(
1,
test_node.account_id.clone(),
stake,
test_node.config.block_producer.as_ref().unwrap().signer.clone(),
)
});

for (i, stake_transaction) in stake_transactions.enumerate() {
Expand Down Expand Up @@ -253,34 +255,19 @@ fn test_validator_join() {
heavy_test(|| {
let system = System::new("NEAR");
let test_nodes = init_test_staking(4, 2, 16);
let unstake_transaction = TransactionBody::Stake(StakeTransaction {
nonce: 1,
originator: test_nodes[1].account_id.clone(),
amount: 0,
public_key: test_nodes[1]
.config
.block_producer
.as_ref()
.unwrap()
.signer
.public_key()
.to_base(),
})
.sign(&*test_nodes[1].config.block_producer.as_ref().unwrap().signer);
let stake_transaction = TransactionBody::Stake(StakeTransaction {
nonce: 1,
originator: test_nodes[2].account_id.clone(),
amount: TESTING_INIT_STAKE,
public_key: test_nodes[2]
.config
.block_producer
.as_ref()
.unwrap()
.signer
.public_key()
.to_base(),
})
.sign(&*test_nodes[2].config.block_producer.as_ref().unwrap().signer);
let unstake_transaction = stake_transaction(
1,
test_nodes[1].account_id.clone(),
0,
test_nodes[1].config.block_producer.as_ref().unwrap().signer.clone(),
);

let stake_transaction = stake_transaction(
1,
test_nodes[2].account_id.clone(),
TESTING_INIT_STAKE,
test_nodes[2].config.block_producer.as_ref().unwrap().signer.clone(),
);
actix::spawn(
test_nodes[1]
.client
Expand Down
44 changes: 21 additions & 23 deletions runtime/runtime/tests/test_evil_contracts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use near_primitives::transaction::{
CreateAccountTransaction, DeployContractTransaction, TransactionBody,
};
use near_primitives::transaction::FinalTransactionStatus;
use near_primitives::types::Balance;
use testlib::node::{Node, RuntimeNode};

Expand All @@ -9,24 +7,21 @@ const FUNCTION_CALL_AMOUNT: Balance = 1_000_000_000;
fn setup_test_contract(wasm_binary: &[u8]) -> RuntimeNode {
let node = RuntimeNode::new(&"alice.near".to_string());
let account_id = node.account_id().unwrap();
let transaction = TransactionBody::CreateAccount(CreateAccountTransaction {
nonce: node.get_account_nonce(&account_id).unwrap_or_default() + 1,
originator: account_id.clone(),
new_account_id: "test_contract".to_string(),
public_key: node.signer().public_key().0[..].to_vec(),
amount: 0,
})
.sign(&*node.signer());
let user = node.user();
user.add_transaction(transaction).unwrap();
let node_user = node.user();
let transaction_result = node_user.create_account(
account_id.clone(),
"test_contract".to_string(),
node.signer().public_key(),
10,
);
assert_eq!(transaction_result.status, FinalTransactionStatus::Completed);
assert_eq!(transaction_result.transactions.len(), 2);

let transaction_result =
node_user.deploy_contract("test_contract".to_string(), wasm_binary.to_vec());
assert_eq!(transaction_result.status, FinalTransactionStatus::Completed);
assert_eq!(transaction_result.transactions.len(), 2);

let transaction = TransactionBody::DeployContract(DeployContractTransaction {
nonce: node.get_account_nonce(&account_id).unwrap_or_default() + 1,
contract_id: "test_contract".to_string(),
wasm_byte_array: wasm_binary.to_vec(),
})
.sign(&*node.signer());
user.add_transaction(transaction).unwrap();
node
}

Expand All @@ -41,7 +36,8 @@ fn test_evil_deep_trie() {
"test_contract".to_string(),
"insertStrings",
input_data.as_bytes().to_vec(),
FUNCTION_CALL_AMOUNT,
1000000,
0,
);
});
(0..50).rev().for_each(|i| {
Expand All @@ -52,7 +48,8 @@ fn test_evil_deep_trie() {
"test_contract".to_string(),
"deleteStrings",
input_data.as_bytes().to_vec(),
FUNCTION_CALL_AMOUNT,
1000000,
0,
);
});
}
Expand All @@ -68,7 +65,8 @@ fn test_evil_deep_recursion() {
"test_contract".to_string(),
"recurse",
input_data.as_bytes().to_vec(),
FUNCTION_CALL_AMOUNT,
1000000,
0,
);
});
}
2 changes: 1 addition & 1 deletion runtime/wasm/runtest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ mod tests {

match outcome.return_data {
Ok(ReturnData::Promise(promise_id)) => {
assert_eq!(&promise_id, &PromiseId::Callback(b"call_it_please".to_vec()))
assert_eq!(promise_id, vec![2 as usize]);
}
_ => assert!(false, "Expected returned promise"),
};
Expand Down
78 changes: 53 additions & 25 deletions test-utils/loadtester/src/transactions_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use std::sync::{Arc, RwLock};

use near_primitives::transaction::{
DeployContractTransaction, FunctionCallTransaction, SignedTransaction, TransactionBody,
Action, DeployContractAction, FunctionCallAction, SignedTransaction, Transaction,
TransferAction,
};

use crate::remote_node::RemoteNode;
Expand Down Expand Up @@ -38,8 +39,15 @@ impl Generator {
}
};

TransactionBody::send_money(nonce, acc_from.as_str(), acc_to.as_str(), 1)
.sign(&*signer_from)
Transaction {
nonce,
signer_id: acc_from,
public_key: signer_from.public_key.clone(),
receiver_id: acc_to,

actions: vec![Action::Transfer(TransferAction { deposit: 1 })],
}
.sign(&*signer_from)
}

/// Returns transactions that deploy test contract to an every account used by the node.
Expand All @@ -52,12 +60,20 @@ impl Generator {
let nonce = node.nonces[ind];
let signer = node.signers[ind].clone();
let contract_id = signer.account_id.clone();
let t = DeployContractTransaction {
nonce,
contract_id,
wasm_byte_array: wasm_binary.to_vec(),
};
res.push(TransactionBody::DeployContract(t).sign(&*signer));

res.push(
Transaction {
nonce,
signer_id: contract_id.clone(),
public_key: signer.public_key.clone(),
receiver_id: contract_id,

actions: vec![Action::DeployContract(DeployContractAction {
code: wasm_binary.to_vec(),
})],
}
.sign(&*signer),
);
}
res
}
Expand All @@ -73,15 +89,22 @@ impl Generator {

let key = rand::random::<usize>() % 1_000;
let value = rand::random::<usize>() % 1_000;
let t = FunctionCallTransaction {
Transaction {
nonce,
originator: acc_from.clone(),
contract_id: acc_from,
method_name: b"setKeyValue".to_vec(),
args: format!("{{\"key\":\"{}\", \"value\":\"{}\"}}", key, value).as_bytes().to_vec(),
amount: 1,
};
TransactionBody::FunctionCall(t).sign(&*signer_from)
signer_id: acc_from.clone(),
public_key: signer_from.public_key.clone(),
receiver_id: acc_from,

actions: vec![Action::FunctionCall(FunctionCallAction {
method_name: "setKeyValue".to_string(),
args: format!("{{\"key\":\"{}\", \"value\":\"{}\"}}", key, value)
.as_bytes()
.to_vec(),
gas: 100000,
deposit: 1,
})],
}
.sign(&*signer_from)
}

/// Returns a transaction that calls `heavy_storage_blocks` on a contract.
Expand All @@ -96,14 +119,19 @@ impl Generator {
};
let acc_from = signer_from.account_id.clone();

let t = FunctionCallTransaction {
Transaction {
nonce,
originator: acc_from.clone(),
contract_id: acc_from,
method_name: b"heavy_storage_blocks".to_vec(),
args: "{\"n\":1000}".as_bytes().to_vec(),
amount: 1,
};
TransactionBody::FunctionCall(t).sign(&*signer_from)
signer_id: acc_from.clone(),
public_key: signer_from.public_key.clone(),
receiver_id: acc_from,

actions: vec![Action::FunctionCall(FunctionCallAction {
method_name: "heavy_storage_blocks".to_string(),
args: "{\"n\":1000}".as_bytes().to_vec(),
gas: 100000,
deposit: 1,
})],
}
.sign(&*signer_from)
}
}
Loading

0 comments on commit bb6c090

Please sign in to comment.