diff --git a/Cargo.lock b/Cargo.lock index 827e57dc102..e0937cb014c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2275,6 +2275,7 @@ dependencies = [ "funty", "futures", "hex", + "lazy_static", "log", "near-actix-test-utils", "near-chain", @@ -2283,6 +2284,8 @@ dependencies = [ "near-client", "near-client-primitives", "near-crypto", + "near-jsonrpc", + "near-jsonrpc-client", "near-jsonrpc-primitives", "near-logger-utils", "near-metrics", @@ -2294,9 +2297,11 @@ dependencies = [ "near-vm-runner", "nearcore", "node-runtime", + "primitive-types", "rand 0.7.3", "serde", "serde_json", + "tempfile", "testlib", ] @@ -2511,6 +2516,7 @@ dependencies = [ "env_logger", "futures", "git-version", + "integration-tests", "log", "near-crypto", "near-jsonrpc", @@ -4309,12 +4315,12 @@ version = "0.1.0" dependencies = [ "clap 2.33.3", "env_logger", + "integration-tests", "log", "near-crypto", "near-jsonrpc-client", "near-primitives", "nearcore", - "testlib", ] [[package]] @@ -4376,6 +4382,15 @@ dependencies = [ "librocksdb-sys", ] +[[package]] +name = "run-nodes" +version = "0.1.0" +dependencies = [ + "clap 2.33.3", + "integration-tests", + "near-logger-utils", +] + [[package]] name = "runtime-params-estimator" version = "3.0.0" @@ -5046,7 +5061,6 @@ dependencies = [ "near-store", "near-test-contracts", "near-vm-errors", - "nearcore", "node-runtime", "num-rational", "rand 0.7.3", diff --git a/Cargo.toml b/Cargo.toml index 9b5e2c162f8..2e4eaec5673 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,11 +27,11 @@ members = [ "chain/jsonrpc-primitives", "chain/rosetta-rpc", "test-utils/actix-test-utils", - "test-utils/testlib", "test-utils/loadtester", "test-utils/runtime-tester", "test-utils/state-viewer", "test-utils/store-validator", + "test-utils/testlib", "neard", "nearcore", "tools/rpctypegen/core", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index aec6d4937a6..efdc1d95b31 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -14,10 +14,13 @@ chrono = { version = "0.4.4", features = ["serde"] } funty = "=1.1.0" # Pin dependency to avoid compilation errors: https://github.com/myrrlyn/funty/issues/3 futures = "0.3" hex = "0.4" +lazy_static = "1.4" log = "0.4" +primitive-types = "0.9" rand = "0.7" serde = { version = "1", features = ["derive"] } serde_json = "1" +tempfile = "3" near-actix-test-utils = { path = "../test-utils/actix-test-utils" } near-chain = { path = "../chain/chain" } @@ -26,6 +29,8 @@ near-chunks = { path = "../chain/chunks" } near-client = { path = "../chain/client" } near-client-primitives = { path = "../chain/client-primitives" } near-crypto = { path = "../core/crypto" } +near-jsonrpc = { path = "../chain/jsonrpc" } +near-jsonrpc-client = { path = "../chain/jsonrpc/client" } near-jsonrpc-primitives = { path = "../chain/jsonrpc-primitives" } near-logger-utils = { path = "../test-utils/logger" } near-metrics = { path = "../core/metrics" } @@ -47,7 +52,7 @@ adversarial = ["nearcore/adversarial"] metric_recorder = ["near-client-primitives/metric_recorder"] protocol_feature_alt_bn128 = ["nearcore/protocol_feature_alt_bn128"] protocol_feature_block_header_v3 = ["near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "near-store/protocol_feature_block_header_v3"] -nightly_protocol_features = ["nearcore/nightly_protocol_features", "testlib/nightly_protocol_features", "protocol_feature_alt_bn128", "protocol_feature_block_header_v3", "protocol_feature_restore_receipts_after_fix"] -nightly_protocol = ["nearcore/nightly_protocol", "testlib/nightly_protocol"] +nightly_protocol_features = ["nearcore/nightly_protocol_features", "protocol_feature_alt_bn128", "protocol_feature_block_header_v3", "protocol_feature_restore_receipts_after_fix"] +nightly_protocol = ["nearcore/nightly_protocol"] protocol_feature_restore_receipts_after_fix = ["nearcore/protocol_feature_restore_receipts_after_fix"] sandbox = ["near-network/sandbox", "near-chain/sandbox", "node-runtime/sandbox", "near-client/sandbox"] diff --git a/integration-tests/src/genesis_helpers.rs b/integration-tests/src/genesis_helpers.rs new file mode 100644 index 00000000000..29ed409ebce --- /dev/null +++ b/integration-tests/src/genesis_helpers.rs @@ -0,0 +1,37 @@ +use std::sync::Arc; + +use tempfile::tempdir; + +use near_chain::{Chain, ChainGenesis, DoomslugThresholdMode}; +use near_chain_configs::Genesis; +use near_primitives::block::{Block, BlockHeader}; +use near_primitives::hash::CryptoHash; +use near_store::test_utils::create_test_store; +use nearcore::NightshadeRuntime; + +/// Compute genesis hash from genesis. +pub fn genesis_hash(genesis: &Genesis) -> CryptoHash { + *genesis_header(genesis).hash() +} + +/// Utility to generate genesis header from config for testing purposes. +pub fn genesis_header(genesis: &Genesis) -> BlockHeader { + let dir = tempdir().unwrap(); + let store = create_test_store(); + let chain_genesis = ChainGenesis::from(genesis); + let runtime = + Arc::new(NightshadeRuntime::new(dir.path(), store, genesis, vec![], vec![], None, None)); + let chain = Chain::new(runtime, &chain_genesis, DoomslugThresholdMode::TwoThirds).unwrap(); + chain.genesis().clone() +} + +/// Utility to generate genesis header from config for testing purposes. +pub fn genesis_block(genesis: &Genesis) -> Block { + let dir = tempdir().unwrap(); + let store = create_test_store(); + let chain_genesis = ChainGenesis::from(genesis); + let runtime = + Arc::new(NightshadeRuntime::new(dir.path(), store, genesis, vec![], vec![], None, None)); + let mut chain = Chain::new(runtime, &chain_genesis, DoomslugThresholdMode::TwoThirds).unwrap(); + chain.get_block(&chain.genesis().hash().clone()).unwrap().clone() +} diff --git a/integration-tests/src/lib.rs b/integration-tests/src/lib.rs new file mode 100644 index 00000000000..3accc773243 --- /dev/null +++ b/integration-tests/src/lib.rs @@ -0,0 +1,5 @@ +pub mod genesis_helpers; +pub mod node; +pub mod runtime_utils; +pub mod test_helpers; +pub mod user; diff --git a/test-utils/testlib/src/node/mod.rs b/integration-tests/src/node/mod.rs similarity index 98% rename from test-utils/testlib/src/node/mod.rs rename to integration-tests/src/node/mod.rs index 3c386f04dee..e686ab6b06a 100644 --- a/test-utils/testlib/src/node/mod.rs +++ b/integration-tests/src/node/mod.rs @@ -1,9 +1,15 @@ use std::sync::Arc; use std::sync::RwLock; +pub use crate::node::process_node::ProcessNode; +pub use crate::node::runtime_node::RuntimeNode; +pub use crate::node::thread_node::ThreadNode; +use crate::user::{AsyncUser, User}; use near_chain_configs::Genesis; use near_crypto::{InMemorySigner, Signer}; use near_jsonrpc_primitives::errors::ServerError; +use near_primitives::contract::ContractCode; +use near_primitives::num_rational::Rational; use near_primitives::state_record::StateRecord; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, Balance, NumSeats}; @@ -13,13 +19,7 @@ use nearcore::config::{ create_testnet_configs, create_testnet_configs_from_seeds, Config, GenesisExt, }; use nearcore::NearConfig; - -pub use crate::node::process_node::ProcessNode; -pub use crate::node::runtime_node::RuntimeNode; -pub use crate::node::thread_node::ThreadNode; -use crate::user::{AsyncUser, User}; -use near_primitives::contract::ContractCode; -use num_rational::Rational; +use testlib::runtime_utils::{alice_account, bob_account}; mod process_node; mod runtime_node; @@ -29,7 +29,6 @@ pub const TEST_BLOCK_FETCH_LIMIT: u64 = 5; pub const TEST_BLOCK_MAX_SIZE: u32 = 1000; pub fn configure_chain_spec() -> Genesis { - use super::runtime_utils::{alice_account, bob_account}; Genesis::test(vec![alice_account(), bob_account()], 2) } diff --git a/test-utils/testlib/src/node/process_node.rs b/integration-tests/src/node/process_node.rs similarity index 100% rename from test-utils/testlib/src/node/process_node.rs rename to integration-tests/src/node/process_node.rs diff --git a/test-utils/testlib/src/node/runtime_node.rs b/integration-tests/src/node/runtime_node.rs similarity index 96% rename from test-utils/testlib/src/node/runtime_node.rs rename to integration-tests/src/node/runtime_node.rs index bb518185b56..e8b3e06aca5 100644 --- a/test-utils/testlib/src/node/runtime_node.rs +++ b/integration-tests/src/node/runtime_node.rs @@ -6,11 +6,11 @@ use near_primitives::types::AccountId; use nearcore::config::GenesisExt; use crate::node::Node; -use crate::runtime_utils::{ - add_test_contract, alice_account, bob_account, get_runtime_and_trie_from_genesis, -}; use crate::user::runtime_user::MockClient; use crate::user::{RuntimeUser, User}; +use testlib::runtime_utils::{ + add_test_contract, alice_account, bob_account, get_runtime_and_trie_from_genesis, +}; pub struct RuntimeNode { pub client: Arc>, @@ -90,10 +90,10 @@ impl Node for RuntimeNode { #[cfg(test)] mod tests { - use crate::fees_utils::FeeHelper; use crate::node::runtime_node::RuntimeNode; use crate::node::Node; - use crate::runtime_utils::{alice_account, bob_account}; + use testlib::fees_utils::FeeHelper; + use testlib::runtime_utils::{alice_account, bob_account}; #[test] pub fn test_send_money() { diff --git a/test-utils/testlib/src/node/thread_node.rs b/integration-tests/src/node/thread_node.rs similarity index 100% rename from test-utils/testlib/src/node/thread_node.rs rename to integration-tests/src/node/thread_node.rs diff --git a/integration-tests/src/runtime_utils.rs b/integration-tests/src/runtime_utils.rs new file mode 100644 index 00000000000..337c2dd3cfb --- /dev/null +++ b/integration-tests/src/runtime_utils.rs @@ -0,0 +1,22 @@ +use near_chain_configs::Genesis; +use near_primitives::types::StateRoot; +use near_store::{ShardTries, TrieUpdate}; +use nearcore::config::GenesisExt; +use node_runtime::{state_viewer::TrieViewer, Runtime}; +use testlib::runtime_utils::{ + add_test_contract, alice_account, bob_account, get_runtime_and_trie_from_genesis, +}; + +pub fn get_runtime_and_trie() -> (Runtime, ShardTries, StateRoot) { + let mut genesis = + Genesis::test(vec![alice_account(), bob_account(), "carol.near".parse().unwrap()], 3); + add_test_contract(&mut genesis, &"test.contract".parse().unwrap()); + get_runtime_and_trie_from_genesis(&genesis) +} + +pub fn get_test_trie_viewer() -> (TrieViewer, TrieUpdate) { + let (_, tries, root) = get_runtime_and_trie(); + let trie_viewer = TrieViewer::default(); + let state_update = tries.new_trie_update(0, root); + (trie_viewer, state_update) +} diff --git a/test-utils/testlib/src/test_helpers.rs b/integration-tests/src/test_helpers.rs similarity index 100% rename from test-utils/testlib/src/test_helpers.rs rename to integration-tests/src/test_helpers.rs diff --git a/test-utils/testlib/src/user/mod.rs b/integration-tests/src/user/mod.rs similarity index 100% rename from test-utils/testlib/src/user/mod.rs rename to integration-tests/src/user/mod.rs diff --git a/test-utils/testlib/src/user/rpc_user.rs b/integration-tests/src/user/rpc_user.rs similarity index 100% rename from test-utils/testlib/src/user/rpc_user.rs rename to integration-tests/src/user/rpc_user.rs diff --git a/test-utils/testlib/src/user/runtime_user.rs b/integration-tests/src/user/runtime_user.rs similarity index 100% rename from test-utils/testlib/src/user/runtime_user.rs rename to integration-tests/src/user/runtime_user.rs diff --git a/integration-tests/tests/client/chunks_management.rs b/integration-tests/tests/client/chunks_management.rs index 2d26ded1526..aba32cb4b58 100644 --- a/integration-tests/tests/client/chunks_management.rs +++ b/integration-tests/tests/client/chunks_management.rs @@ -7,6 +7,7 @@ use actix::{Addr, System}; use futures::{future, FutureExt}; use log::info; +use integration_tests::test_helpers::heavy_test; use near_actix_test_utils::run_actix; use near_chunks::{ CHUNK_REQUEST_RETRY_MS, CHUNK_REQUEST_SWITCH_TO_FULL_FETCH_MS, @@ -20,7 +21,6 @@ use near_network::{NetworkClientMessages, NetworkRequests, NetworkResponses, Pee use near_primitives::hash::CryptoHash; use near_primitives::transaction::SignedTransaction; use near_primitives::types::AccountId; -use testlib::test_helpers::heavy_test; /// Runs block producing client and stops after network mock received seven blocks /// Confirms that the blocks form a chain (which implies the chunks are distributed). diff --git a/integration-tests/tests/nearcore/main.rs b/integration-tests/tests/nearcore/main.rs new file mode 100644 index 00000000000..19145d13676 --- /dev/null +++ b/integration-tests/tests/nearcore/main.rs @@ -0,0 +1,8 @@ +mod node_cluster; +mod rpc_error_structs; +mod rpc_nodes; +mod run_nodes; +mod stake_nodes; +mod sync_nodes; +mod sync_state_nodes; +mod track_shards; diff --git a/nearcore/tests/node_cluster.rs b/integration-tests/tests/nearcore/node_cluster.rs similarity index 50% rename from nearcore/tests/node_cluster.rs rename to integration-tests/tests/nearcore/node_cluster.rs index 578b0756542..a0c4415dc75 100644 --- a/nearcore/tests/node_cluster.rs +++ b/integration-tests/tests/nearcore/node_cluster.rs @@ -1,9 +1,74 @@ +use actix::Addr; +use actix_rt::ArbiterHandle; use futures::future; - +use integration_tests::test_helpers::heavy_test; use near_actix_test_utils::{run_actix, spawn_interruptible}; +use near_chain_configs::Genesis; use near_client::{ClientActor, ViewClientActor}; -use near_primitives::types::{BlockHeight, BlockHeightDelta, NumSeats, NumShards}; -use testlib::{start_nodes, test_helpers::heavy_test}; +use near_logger_utils::init_integration_logger; +use near_network::test_utils::{convert_boot_nodes, open_port}; +use near_primitives::types::{BlockHeight, BlockHeightDelta, NumSeats, NumShards, ShardId}; +use nearcore::{config::GenesisExt, load_test_config, start_with_config}; +use tempfile::TempDir; + +pub fn start_nodes( + num_shards: NumShards, + dirs: &[TempDir], + num_validator_seats: NumSeats, + num_lightclient: usize, + epoch_length: BlockHeightDelta, + genesis_height: BlockHeight, +) -> (Genesis, Vec, Vec<(Addr, Addr, Vec)>) { + init_integration_logger(); + + let num_nodes = dirs.len(); + let num_tracking_nodes = num_nodes - num_lightclient; + let seeds = (0..num_nodes).map(|i| format!("near.{}", i)).collect::>(); + let mut genesis = Genesis::test_sharded( + seeds.iter().map(|s| s.parse().unwrap()).collect(), + num_validator_seats, + (0..num_shards).map(|_| num_validator_seats).collect(), + ); + genesis.config.epoch_length = epoch_length; + genesis.config.genesis_height = genesis_height; + + let validators = (0..num_validator_seats).map(|i| format!("near.{}", i)).collect::>(); + let mut near_configs = vec![]; + let first_node = open_port(); + let mut rpc_addrs = vec![]; + for i in 0..num_nodes { + let mut near_config = load_test_config( + if i < num_validator_seats as usize { &validators[i] } else { "" }, + if i == 0 { first_node } else { open_port() }, + genesis.clone(), + ); + rpc_addrs.push(near_config.rpc_addr().unwrap().clone()); + near_config.client_config.min_num_peers = num_nodes - 1; + if i > 0 { + near_config.network_config.boot_nodes = + convert_boot_nodes(vec![("near.0", first_node)]); + } + // if non validator, add some shards to track. + if i >= (num_validator_seats as usize) && i < num_tracking_nodes { + let shards_per_node = + num_shards as usize / (num_tracking_nodes - num_validator_seats as usize); + let (from, to) = ( + ((i - num_validator_seats as usize) * shards_per_node) as ShardId, + ((i - (num_validator_seats as usize) + 1) * shards_per_node) as ShardId, + ); + near_config.client_config.tracked_shards.extend(&(from..to).collect::>()); + } + near_config.client_config.epoch_sync_enabled = false; + near_configs.push(near_config); + } + + let mut res = vec![]; + for (i, near_config) in near_configs.into_iter().enumerate() { + let (client, view_client, arbiters) = start_with_config(dirs[i].path(), near_config); + res.push((client, view_client, arbiters)) + } + (genesis, rpc_addrs, res) +} #[derive(Debug, Default)] pub struct NodeCluster { diff --git a/nearcore/tests/rpc_error_structs.rs b/integration-tests/tests/nearcore/rpc_error_structs.rs similarity index 99% rename from nearcore/tests/rpc_error_structs.rs rename to integration-tests/tests/nearcore/rpc_error_structs.rs index 6974d9a6c83..f3e8d03ae70 100644 --- a/nearcore/tests/rpc_error_structs.rs +++ b/integration-tests/tests/nearcore/rpc_error_structs.rs @@ -2,9 +2,10 @@ use std::str::FromStr; use actix::{Actor, System}; use borsh::BorshSerialize; - use futures::{future, FutureExt, TryFutureExt}; +use crate::node_cluster::NodeCluster; +use integration_tests::genesis_helpers::genesis_block; use near_actix_test_utils::spawn_interruptible; use near_client::GetBlock; use near_crypto::{InMemorySigner, KeyType}; @@ -12,16 +13,10 @@ use near_jsonrpc::client::new_client; use near_logger_utils::init_integration_logger; use near_network::test_utils::WaitOrTimeout; use near_primitives::hash::CryptoHash; - use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; use near_primitives::types::BlockId; -use testlib::genesis_block; - -mod node_cluster; -use node_cluster::NodeCluster; - // Queries json-rpc block that doesn't exists // Checks if the struct is expected and contains the proper data #[test] diff --git a/nearcore/tests/rpc_nodes.rs b/integration-tests/tests/nearcore/rpc_nodes.rs similarity index 99% rename from nearcore/tests/rpc_nodes.rs rename to integration-tests/tests/nearcore/rpc_nodes.rs index 6af21a9c7f8..78eb1f1221e 100644 --- a/nearcore/tests/rpc_nodes.rs +++ b/integration-tests/tests/nearcore/rpc_nodes.rs @@ -6,6 +6,7 @@ use borsh::BorshSerialize; use futures::future::join_all; use futures::{future, FutureExt, TryFutureExt}; +use integration_tests::genesis_helpers::genesis_block; use near_actix_test_utils::spawn_interruptible; use near_client::{GetBlock, GetExecutionOutcome, GetValidatorInfo}; use near_crypto::{InMemorySigner, KeyType}; @@ -20,10 +21,8 @@ use near_primitives::types::{ BlockId, BlockReference, EpochId, EpochReference, Finality, TransactionOrReceiptId, }; use near_primitives::views::{ExecutionOutcomeView, ExecutionStatusView}; -use testlib::genesis_block; -mod node_cluster; -use node_cluster::NodeCluster; +use crate::node_cluster::NodeCluster; macro_rules! panic_on_rpc_error { ($e:expr) => { diff --git a/nearcore/tests/run_nodes.rs b/integration-tests/tests/nearcore/run_nodes.rs similarity index 98% rename from nearcore/tests/run_nodes.rs rename to integration-tests/tests/nearcore/run_nodes.rs index d2ea43f2a0c..c95dd45e188 100644 --- a/nearcore/tests/run_nodes.rs +++ b/integration-tests/tests/nearcore/run_nodes.rs @@ -7,8 +7,7 @@ use near_network::test_utils::WaitOrTimeout; use near_primitives::types::{BlockHeightDelta, NumSeats, NumShards}; use rand::{thread_rng, Rng}; -mod node_cluster; -use node_cluster::NodeCluster; +use crate::node_cluster::NodeCluster; fn run_heavy_nodes( num_shards: NumShards, diff --git a/nearcore/tests/stake_nodes.rs b/integration-tests/tests/nearcore/stake_nodes.rs similarity index 99% rename from nearcore/tests/stake_nodes.rs rename to integration-tests/tests/nearcore/stake_nodes.rs index 92569ab87c9..525860cc1f4 100644 --- a/nearcore/tests/stake_nodes.rs +++ b/integration-tests/tests/nearcore/stake_nodes.rs @@ -6,9 +6,11 @@ use std::sync::Arc; use actix::{Actor, Addr, System}; use actix_rt::ArbiterHandle; use futures::{future, FutureExt}; -use num_rational::Rational; +use near_primitives::num_rational::Rational; use rand::Rng; +use integration_tests::genesis_helpers::genesis_hash; +use integration_tests::test_helpers::heavy_test; use near_actix_test_utils::run_actix; use near_chain_configs::Genesis; use near_client::{ClientActor, GetBlock, Query, Status, ViewClientActor}; @@ -22,7 +24,6 @@ use near_primitives::types::{AccountId, BlockHeightDelta, BlockReference, NumSea use near_primitives::views::{QueryRequest, QueryResponseKind, ValidatorInfo}; use nearcore::config::{GenesisExt, TESTING_INIT_BALANCE, TESTING_INIT_STAKE}; use nearcore::{load_test_config, start_with_config, NearConfig, NEAR_BASE}; -use testlib::{genesis_hash, test_helpers::heavy_test}; use {near_primitives::types::BlockId, primitive_types::U256}; diff --git a/nearcore/tests/sync_nodes.rs b/integration-tests/tests/nearcore/sync_nodes.rs similarity index 98% rename from nearcore/tests/sync_nodes.rs rename to integration-tests/tests/nearcore/sync_nodes.rs index 0929863e535..e59772db02f 100644 --- a/nearcore/tests/sync_nodes.rs +++ b/integration-tests/tests/nearcore/sync_nodes.rs @@ -4,8 +4,9 @@ use std::time::Duration; use actix::{Actor, Addr, System}; use futures::{future, FutureExt}; -use num_rational::Rational; +use integration_tests::genesis_helpers::genesis_block; +use integration_tests::test_helpers::heavy_test; use near_actix_test_utils::run_actix; use near_chain::{Block, Chain}; use near_chain_configs::Genesis; @@ -16,6 +17,7 @@ use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout}; use near_network::{NetworkClientMessages, PeerInfo}; use near_primitives::block::Approval; use near_primitives::merkle::PartialMerkleTree; +use near_primitives::num_rational::Rational; use near_primitives::transaction::SignedTransaction; use near_primitives::types::validator_stake::ValidatorStake; use near_primitives::types::{BlockHeightDelta, EpochId}; @@ -23,7 +25,6 @@ use near_primitives::validator_signer::{InMemoryValidatorSigner, ValidatorSigner use near_primitives::version::PROTOCOL_VERSION; use nearcore::config::{GenesisExt, TESTING_INIT_STAKE}; use nearcore::{load_test_config, start_with_config, NearConfig}; -use testlib::{genesis_block, test_helpers::heavy_test}; // This assumes that there is no height skipped. Otherwise epoch hash calculation will be wrong. fn add_blocks( diff --git a/nearcore/tests/sync_state_nodes.rs b/integration-tests/tests/nearcore/sync_state_nodes.rs similarity index 99% rename from nearcore/tests/sync_state_nodes.rs rename to integration-tests/tests/nearcore/sync_state_nodes.rs index 7cca32bcce1..d4d414787e9 100644 --- a/nearcore/tests/sync_state_nodes.rs +++ b/integration-tests/tests/nearcore/sync_state_nodes.rs @@ -4,13 +4,13 @@ use std::time::Duration; use actix::{Actor, System}; use futures::{future, FutureExt}; +use integration_tests::test_helpers::heavy_test; use near_actix_test_utils::run_actix; use near_chain_configs::Genesis; use near_client::GetBlock; use near_logger_utils::init_integration_logger; use near_network::test_utils::{convert_boot_nodes, open_port, WaitOrTimeout}; use nearcore::{config::GenesisExt, load_test_config, start_with_config}; -use testlib::test_helpers::heavy_test; /// One client is in front, another must sync to it using state (fast) sync. #[test] diff --git a/nearcore/tests/track_shards.rs b/integration-tests/tests/nearcore/track_shards.rs similarity index 97% rename from nearcore/tests/track_shards.rs rename to integration-tests/tests/nearcore/track_shards.rs index ab286f10ccf..53ba3d96968 100644 --- a/nearcore/tests/track_shards.rs +++ b/integration-tests/tests/nearcore/track_shards.rs @@ -9,8 +9,7 @@ use near_logger_utils::init_integration_logger; use near_network::test_utils::WaitOrTimeout; use near_primitives::hash::CryptoHash; -mod node_cluster; -use node_cluster::NodeCluster; +use crate::node_cluster::NodeCluster; #[test] fn track_shards() { diff --git a/integration-tests/tests/runtime/main.rs b/integration-tests/tests/runtime/main.rs new file mode 100644 index 00000000000..122e294e8ba --- /dev/null +++ b/integration-tests/tests/runtime/main.rs @@ -0,0 +1,2 @@ +mod state_viewer; +mod test_evil_contracts; diff --git a/integration-tests/tests/runtime/state_viewer.rs b/integration-tests/tests/runtime/state_viewer.rs new file mode 100644 index 00000000000..e79c88772d4 --- /dev/null +++ b/integration-tests/tests/runtime/state_viewer.rs @@ -0,0 +1,209 @@ +use integration_tests::runtime_utils::{get_runtime_and_trie, get_test_trie_viewer}; +use near_primitives::{ + account::Account, + hash::CryptoHash, + views::{StateItem, ViewApplyState}, +}; +use near_primitives::{ + test_utils::MockEpochInfoProvider, + trie_key::TrieKey, + types::{EpochId, StateChangeCause}, + version::PROTOCOL_VERSION, +}; +use near_store::set_account; +use node_runtime::state_viewer::errors; +use node_runtime::state_viewer::*; +use testlib::runtime_utils::{alice_account, encode_int}; + +#[test] +fn test_view_call() { + let (viewer, root) = get_test_trie_viewer(); + + let mut logs = vec![]; + let view_state = ViewApplyState { + block_height: 1, + prev_block_hash: CryptoHash::default(), + block_hash: CryptoHash::default(), + epoch_id: EpochId::default(), + epoch_height: 0, + block_timestamp: 1, + current_protocol_version: PROTOCOL_VERSION, + cache: None, + }; + let result = viewer.call_function( + root, + view_state, + &"test.contract".parse().unwrap(), + "run_test", + &[], + &mut logs, + &MockEpochInfoProvider::default(), + ); + + assert_eq!(result.unwrap(), encode_int(10)); +} + +#[test] +fn test_view_call_try_changing_storage() { + let (viewer, root) = get_test_trie_viewer(); + + let mut logs = vec![]; + let view_state = ViewApplyState { + block_height: 1, + prev_block_hash: CryptoHash::default(), + block_hash: CryptoHash::default(), + epoch_id: EpochId::default(), + epoch_height: 0, + block_timestamp: 1, + current_protocol_version: PROTOCOL_VERSION, + cache: None, + }; + let result = viewer.call_function( + root, + view_state, + &"test.contract".parse().unwrap(), + "run_test_with_storage_change", + &[], + &mut logs, + &MockEpochInfoProvider::default(), + ); + let err = result.unwrap_err(); + assert!( + err.to_string().contains(r#"ProhibitedInView { method_name: "storage_write" }"#), + "Got different error that doesn't match: {}", + err + ); +} + +#[test] +fn test_view_call_with_args() { + let (viewer, root) = get_test_trie_viewer(); + let args: Vec<_> = [1u64, 2u64].iter().flat_map(|x| (*x).to_le_bytes().to_vec()).collect(); + let mut logs = vec![]; + let view_state = ViewApplyState { + block_height: 1, + prev_block_hash: CryptoHash::default(), + block_hash: CryptoHash::default(), + epoch_id: EpochId::default(), + epoch_height: 0, + block_timestamp: 1, + current_protocol_version: PROTOCOL_VERSION, + cache: None, + }; + let view_call_result = viewer.call_function( + root, + view_state, + &"test.contract".parse().unwrap(), + "sum_with_input", + &args, + &mut logs, + &MockEpochInfoProvider::default(), + ); + assert_eq!(view_call_result.unwrap(), 3u64.to_le_bytes().to_vec()); +} + +#[test] +fn test_view_state() { + let (_, tries, root) = get_runtime_and_trie(); + let mut state_update = tries.new_trie_update(0, root); + state_update.set( + TrieKey::ContractData { account_id: alice_account(), key: b"test123".to_vec() }, + b"123".to_vec(), + ); + state_update.set( + TrieKey::ContractData { account_id: alice_account(), key: b"test321".to_vec() }, + b"321".to_vec(), + ); + state_update.set( + TrieKey::ContractData { account_id: "alina".parse().unwrap(), key: b"qqq".to_vec() }, + b"321".to_vec(), + ); + state_update.set( + TrieKey::ContractData { account_id: "alex".parse().unwrap(), key: b"qqq".to_vec() }, + b"321".to_vec(), + ); + state_update.commit(StateChangeCause::InitialState); + let trie_changes = state_update.finalize().unwrap().0; + let (db_changes, new_root) = tries.apply_all(&trie_changes, 0).unwrap(); + db_changes.commit().unwrap(); + + let state_update = tries.new_trie_update(0, new_root); + let trie_viewer = TrieViewer::default(); + let result = trie_viewer.view_state(&state_update, &alice_account(), b"").unwrap(); + assert_eq!(result.proof, Vec::::new()); + assert_eq!( + result.values, + [ + StateItem { key: "dGVzdDEyMw==".to_string(), value: "MTIz".to_string(), proof: vec![] }, + StateItem { key: "dGVzdDMyMQ==".to_string(), value: "MzIx".to_string(), proof: vec![] } + ] + ); + let result = trie_viewer.view_state(&state_update, &alice_account(), b"xyz").unwrap(); + assert_eq!(result.values, []); + let result = trie_viewer.view_state(&state_update, &alice_account(), b"test123").unwrap(); + assert_eq!( + result.values, + [StateItem { key: "dGVzdDEyMw==".to_string(), value: "MTIz".to_string(), proof: vec![] }] + ); +} + +#[test] +fn test_view_state_too_large() { + let (_, tries, root) = get_runtime_and_trie(); + let mut state_update = tries.new_trie_update(0, root); + set_account( + &mut state_update, + alice_account(), + &Account::new(0, 0, CryptoHash::default(), 50_001), + ); + let trie_viewer = TrieViewer::new(Some(50_000), None); + let result = trie_viewer.view_state(&state_update, &alice_account(), b""); + assert!(matches!(result, Err(errors::ViewStateError::AccountStateTooLarge { .. }))); +} + +#[test] +fn test_view_state_with_large_contract() { + let (_, tries, root) = get_runtime_and_trie(); + let mut state_update = tries.new_trie_update(0, root); + set_account( + &mut state_update, + alice_account(), + &Account::new(0, 0, CryptoHash::default(), 50_001), + ); + state_update.set( + TrieKey::ContractCode { account_id: alice_account() }, + [0; Account::MAX_ACCOUNT_DELETION_STORAGE_USAGE as usize].to_vec(), + ); + let trie_viewer = TrieViewer::new(Some(50_000), None); + let result = trie_viewer.view_state(&state_update, &alice_account(), b""); + assert!(result.is_ok()); +} + +#[test] +fn test_log_when_panic() { + let (viewer, root) = get_test_trie_viewer(); + let view_state = ViewApplyState { + block_height: 1, + prev_block_hash: CryptoHash::default(), + block_hash: CryptoHash::default(), + epoch_id: EpochId::default(), + epoch_height: 0, + block_timestamp: 1, + current_protocol_version: PROTOCOL_VERSION, + cache: None, + }; + let mut logs = vec![]; + viewer + .call_function( + root, + view_state, + &"test.contract".parse().unwrap(), + "panic_after_logging", + &[], + &mut logs, + &MockEpochInfoProvider::default(), + ) + .unwrap_err(); + + assert_eq!(logs, vec!["hello".to_string()]); +} diff --git a/runtime/runtime/tests/test_evil_contracts.rs b/integration-tests/tests/runtime/test_evil_contracts.rs similarity index 98% rename from runtime/runtime/tests/test_evil_contracts.rs rename to integration-tests/tests/runtime/test_evil_contracts.rs index 55e86a1160a..192e1234447 100644 --- a/runtime/runtime/tests/test_evil_contracts.rs +++ b/integration-tests/tests/runtime/test_evil_contracts.rs @@ -1,10 +1,9 @@ +use integration_tests::node::{Node, RuntimeNode}; use near_primitives::errors::{ActionError, ActionErrorKind, ContractCallError}; use near_primitives::serialize::to_base64; use near_primitives::views::FinalExecutionStatus; use std::mem::size_of; -use testlib::node::{Node, RuntimeNode}; -#[cfg(test)] use assert_matches::assert_matches; /// Initial balance used in tests. diff --git a/integration-tests/tests/standard_cases/main.rs b/integration-tests/tests/standard_cases/main.rs index 46918abd6f9..37b500f9b1d 100644 --- a/integration-tests/tests/standard_cases/main.rs +++ b/integration-tests/tests/standard_cases/main.rs @@ -21,10 +21,10 @@ use near_primitives::views::{ use near_vm_errors::MethodResolveError; use nearcore::config::{NEAR_BASE, TESTING_INIT_BALANCE, TESTING_INIT_STAKE}; +use integration_tests::node::Node; +use integration_tests::user::User; use testlib::fees_utils::FeeHelper; -use testlib::node::Node; use testlib::runtime_utils::{alice_account, bob_account, eve_dot_alice_account}; -use testlib::user::User; /// The amount to send with function call. const FUNCTION_CALL_AMOUNT: Balance = TESTING_INIT_BALANCE / 10; diff --git a/integration-tests/tests/standard_cases/rpc.rs b/integration-tests/tests/standard_cases/rpc.rs index 2612571e496..42dffb570d3 100644 --- a/integration-tests/tests/standard_cases/rpc.rs +++ b/integration-tests/tests/standard_cases/rpc.rs @@ -6,10 +6,10 @@ mod test { use std::time::Duration; use crate::*; + use integration_tests::node::{create_nodes_from_seeds, Node, NodeConfig, ThreadNode}; + use integration_tests::test_helpers::heavy_test; use near_logger_utils::init_test_module_logger; - use testlib::node::{create_nodes_from_seeds, Node, NodeConfig, ThreadNode}; use testlib::runtime_utils::alice_account; - use testlib::test_helpers::heavy_test; fn create_thread_nodes_rpc() -> Vec { init_test_module_logger("runtime"); diff --git a/integration-tests/tests/standard_cases/runtime.rs b/integration-tests/tests/standard_cases/runtime.rs index 60c896ede93..2d5d381aba2 100644 --- a/integration-tests/tests/standard_cases/runtime.rs +++ b/integration-tests/tests/standard_cases/runtime.rs @@ -1,9 +1,9 @@ mod test { use crate::*; + use integration_tests::node::RuntimeNode; use near_chain_configs::Genesis; use near_primitives::state_record::StateRecord; use nearcore::config::{GenesisExt, TESTING_INIT_BALANCE}; - use testlib::node::RuntimeNode; use testlib::runtime_utils::{add_test_contract, alice_account, bob_account}; fn create_runtime_node() -> RuntimeNode { diff --git a/integration-tests/tests/test_catchup.rs b/integration-tests/tests/test_catchup.rs index 902c7472ca4..2948193fc3a 100644 --- a/integration-tests/tests/test_catchup.rs +++ b/integration-tests/tests/test_catchup.rs @@ -4,9 +4,9 @@ fn test_catchup() { use std::time::Duration; + use integration_tests::node::{create_nodes, Node}; + use integration_tests::test_helpers::{heavy_test, wait}; use std::sync::{Arc, RwLock}; - use testlib::node::{create_nodes, Node}; - use testlib::test_helpers::{heavy_test, wait}; /// Creates a network of `num_nodes` nodes, but starts only `num_nodes - 1`. After /// `num_blocks_to_wait` starts the last node and verifies that it can start validating within diff --git a/integration-tests/tests/test_errors.rs b/integration-tests/tests/test_errors.rs index b87396e9bc9..d96d0651624 100644 --- a/integration-tests/tests/test_errors.rs +++ b/integration-tests/tests/test_errors.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use integration_tests::node::{Node, ThreadNode}; use near_chain_configs::Genesis; use near_crypto::{InMemorySigner, KeyType}; use near_logger_utils::init_integration_logger; @@ -11,7 +12,6 @@ use near_primitives::transaction::{ }; use nearcore::config::{GenesisExt, TESTING_INIT_BALANCE, TESTING_INIT_STAKE}; use nearcore::load_test_config; -use testlib::node::{Node, ThreadNode}; use testlib::runtime_utils::{alice_account, bob_account}; fn start_node() -> ThreadNode { diff --git a/integration-tests/tests/test_rejoin.rs b/integration-tests/tests/test_rejoin.rs index 26ebe8e878f..1748c76fad6 100644 --- a/integration-tests/tests/test_rejoin.rs +++ b/integration-tests/tests/test_rejoin.rs @@ -7,12 +7,14 @@ mod test { use std::thread; use std::time::Duration; + use integration_tests::node::{ + create_nodes, sample_queryable_node, sample_two_nodes, Node, NodeConfig, + }; + use integration_tests::test_helpers::{heavy_test, wait, wait_for_catchup}; use near_chain_configs::Genesis; use near_primitives::runtime::config::RuntimeConfig; use near_primitives::transaction::SignedTransaction; use near_primitives::types::AccountId; - use testlib::node::{create_nodes, sample_queryable_node, sample_two_nodes, Node, NodeConfig}; - use testlib::test_helpers::{heavy_test, wait, wait_for_catchup}; fn warmup() { if let Err(_) = std::env::var("NIGHTLY_RUNNER") { diff --git a/integration-tests/tests/test_simple.rs b/integration-tests/tests/test_simple.rs index d6b086c3a7a..1812e651ccc 100644 --- a/integration-tests/tests/test_simple.rs +++ b/integration-tests/tests/test_simple.rs @@ -2,11 +2,11 @@ #[cfg(test)] #[cfg(feature = "expensive_tests")] mod test { + use integration_tests::node::{create_nodes, sample_two_nodes, Node}; + use integration_tests::test_helpers::{heavy_test, wait}; use near_logger_utils::init_integration_logger; use near_primitives::transaction::SignedTransaction; use std::time::{Duration, Instant}; - use testlib::node::{create_nodes, sample_two_nodes, Node}; - use testlib::test_helpers::{heavy_test, wait}; fn run_multiple_nodes(num_nodes: usize, num_trials: usize, test_prefix: &str) { init_integration_logger(); diff --git a/integration-tests/tests/test_tps_regression.rs b/integration-tests/tests/test_tps_regression.rs index 9d32c4e852a..6de53e4e102 100644 --- a/integration-tests/tests/test_tps_regression.rs +++ b/integration-tests/tests/test_tps_regression.rs @@ -10,10 +10,10 @@ mod test { use std::sync::{Arc, RwLock}; use std::thread; + use integration_tests::node::{create_nodes, sample_queryable_node, sample_two_nodes, Node}; + use integration_tests::test_helpers::heavy_test; use near_primitives::transaction::SignedTransaction; use std::time::{Duration, Instant}; - use testlib::node::{create_nodes, sample_queryable_node, sample_two_nodes, Node}; - use testlib::test_helpers::heavy_test; /// Creates and sends a random transaction. /// Args: diff --git a/runtime/runtime/src/state_viewer/mod.rs b/runtime/runtime/src/state_viewer/mod.rs index 0458e339128..1ffadc6bc5e 100644 --- a/runtime/runtime/src/state_viewer/mod.rs +++ b/runtime/runtime/src/state_viewer/mod.rs @@ -268,224 +268,3 @@ impl TrieViewer { } } } - -#[cfg(test)] -mod tests { - use near_primitives::{ - test_utils::MockEpochInfoProvider, - trie_key::TrieKey, - types::{EpochId, StateChangeCause}, - version::PROTOCOL_VERSION, - }; - use testlib::runtime_utils::{ - alice_account, encode_int, get_runtime_and_trie, get_test_trie_viewer, - }; - - use super::*; - use near_store::set_account; - - #[test] - fn test_view_call() { - let (viewer, root) = get_test_trie_viewer(); - - let mut logs = vec![]; - let view_state = ViewApplyState { - block_height: 1, - prev_block_hash: CryptoHash::default(), - block_hash: CryptoHash::default(), - epoch_id: EpochId::default(), - epoch_height: 0, - block_timestamp: 1, - current_protocol_version: PROTOCOL_VERSION, - cache: None, - }; - let result = viewer.call_function( - root, - view_state, - &"test.contract".parse().unwrap(), - "run_test", - &[], - &mut logs, - &MockEpochInfoProvider::default(), - ); - - assert_eq!(result.unwrap(), encode_int(10)); - } - - #[test] - fn test_view_call_try_changing_storage() { - let (viewer, root) = get_test_trie_viewer(); - - let mut logs = vec![]; - let view_state = ViewApplyState { - block_height: 1, - prev_block_hash: CryptoHash::default(), - block_hash: CryptoHash::default(), - epoch_id: EpochId::default(), - epoch_height: 0, - block_timestamp: 1, - current_protocol_version: PROTOCOL_VERSION, - cache: None, - }; - let result = viewer.call_function( - root, - view_state, - &"test.contract".parse().unwrap(), - "run_test_with_storage_change", - &[], - &mut logs, - &MockEpochInfoProvider::default(), - ); - let err = result.unwrap_err(); - assert!( - err.to_string().contains(r#"ProhibitedInView { method_name: "storage_write" }"#), - "Got different error that doesn't match: {}", - err - ); - } - - #[test] - fn test_view_call_with_args() { - let (viewer, root) = get_test_trie_viewer(); - let args: Vec<_> = [1u64, 2u64].iter().flat_map(|x| (*x).to_le_bytes().to_vec()).collect(); - let mut logs = vec![]; - let view_state = ViewApplyState { - block_height: 1, - prev_block_hash: CryptoHash::default(), - block_hash: CryptoHash::default(), - epoch_id: EpochId::default(), - epoch_height: 0, - block_timestamp: 1, - current_protocol_version: PROTOCOL_VERSION, - cache: None, - }; - let view_call_result = viewer.call_function( - root, - view_state, - &"test.contract".parse().unwrap(), - "sum_with_input", - &args, - &mut logs, - &MockEpochInfoProvider::default(), - ); - assert_eq!(view_call_result.unwrap(), 3u64.to_le_bytes().to_vec()); - } - - #[test] - fn test_view_state() { - let (_, tries, root) = get_runtime_and_trie(); - let mut state_update = tries.new_trie_update(0, root); - state_update.set( - TrieKey::ContractData { account_id: alice_account(), key: b"test123".to_vec() }, - b"123".to_vec(), - ); - state_update.set( - TrieKey::ContractData { account_id: alice_account(), key: b"test321".to_vec() }, - b"321".to_vec(), - ); - state_update.set( - TrieKey::ContractData { account_id: "alina".parse().unwrap(), key: b"qqq".to_vec() }, - b"321".to_vec(), - ); - state_update.set( - TrieKey::ContractData { account_id: "alex".parse().unwrap(), key: b"qqq".to_vec() }, - b"321".to_vec(), - ); - state_update.commit(StateChangeCause::InitialState); - let trie_changes = state_update.finalize().unwrap().0; - let (db_changes, new_root) = tries.apply_all(&trie_changes, 0).unwrap(); - db_changes.commit().unwrap(); - - let state_update = tries.new_trie_update(0, new_root); - let trie_viewer = TrieViewer::default(); - let result = trie_viewer.view_state(&state_update, &alice_account(), b"").unwrap(); - assert_eq!(result.proof, Vec::::new()); - assert_eq!( - result.values, - [ - StateItem { - key: "dGVzdDEyMw==".to_string(), - value: "MTIz".to_string(), - proof: vec![] - }, - StateItem { - key: "dGVzdDMyMQ==".to_string(), - value: "MzIx".to_string(), - proof: vec![] - } - ] - ); - let result = trie_viewer.view_state(&state_update, &alice_account(), b"xyz").unwrap(); - assert_eq!(result.values, []); - let result = trie_viewer.view_state(&state_update, &alice_account(), b"test123").unwrap(); - assert_eq!( - result.values, - [StateItem { - key: "dGVzdDEyMw==".to_string(), - value: "MTIz".to_string(), - proof: vec![] - }] - ); - } - - #[test] - fn test_view_state_too_large() { - let (_, tries, root) = get_runtime_and_trie(); - let mut state_update = tries.new_trie_update(0, root); - set_account( - &mut state_update, - alice_account(), - &Account::new(0, 0, CryptoHash::default(), 50_001), - ); - let trie_viewer = TrieViewer::new(Some(50_000), None); - let result = trie_viewer.view_state(&state_update, &alice_account(), b""); - assert!(matches!(result, Err(errors::ViewStateError::AccountStateTooLarge { .. }))); - } - - #[test] - fn test_view_state_with_large_contract() { - let (_, tries, root) = get_runtime_and_trie(); - let mut state_update = tries.new_trie_update(0, root); - set_account( - &mut state_update, - alice_account(), - &Account::new(0, 0, CryptoHash::default(), 50_001), - ); - state_update.set( - TrieKey::ContractCode { account_id: alice_account() }, - [0; Account::MAX_ACCOUNT_DELETION_STORAGE_USAGE as usize].to_vec(), - ); - let trie_viewer = TrieViewer::new(Some(50_000), None); - let result = trie_viewer.view_state(&state_update, &alice_account(), b""); - assert!(result.is_ok()); - } - - #[test] - fn test_log_when_panic() { - let (viewer, root) = get_test_trie_viewer(); - let view_state = ViewApplyState { - block_height: 1, - prev_block_hash: CryptoHash::default(), - block_hash: CryptoHash::default(), - epoch_id: EpochId::default(), - epoch_height: 0, - block_timestamp: 1, - current_protocol_version: PROTOCOL_VERSION, - cache: None, - }; - let mut logs = vec![]; - viewer - .call_function( - root, - view_state, - &"test.contract".parse().unwrap(), - "panic_after_logging", - &[], - &mut logs, - &MockEpochInfoProvider::default(), - ) - .unwrap_err(); - - assert_eq!(logs, vec!["hello".to_string()]); - } -} diff --git a/test-utils/loadtester/Cargo.toml b/test-utils/loadtester/Cargo.toml index 3cab8651335..968ada39778 100644 --- a/test-utils/loadtester/Cargo.toml +++ b/test-utils/loadtester/Cargo.toml @@ -19,6 +19,7 @@ byteorder = "1.2" borsh = "0.8.1" +integration-tests = { path = "../../integration-tests" } near-crypto = { path = "../../core/crypto" } near-primitives = { path = "../../core/primitives" } node-runtime = { path = "../../runtime/runtime" } diff --git a/test-utils/loadtester/src/remote_node.rs b/test-utils/loadtester/src/remote_node.rs index c20846aac4e..89c8d8047d6 100644 --- a/test-utils/loadtester/src/remote_node.rs +++ b/test-utils/loadtester/src/remote_node.rs @@ -10,6 +10,8 @@ use log::{debug, info}; use reqwest::blocking::Client as SyncClient; use reqwest::Client as AsyncClient; +use integration_tests::user::rpc_user::RpcUser; +use integration_tests::user::User; use near_crypto::{InMemorySigner, KeyType, PublicKey}; use near_jsonrpc_primitives::message::Message; use near_primitives::hash::CryptoHash; @@ -17,8 +19,6 @@ use near_primitives::serialize::to_base64; use near_primitives::transaction::SignedTransaction; use near_primitives::types::{AccountId, Nonce}; use near_primitives::views::AccessKeyView; -use testlib::user::rpc_user::RpcUser; -use testlib::user::User; const CONNECT_TIMEOUT: Duration = Duration::from_secs(10); /// Maximum number of blocks that can be fetched through a single RPC request. diff --git a/test-utils/testlib/Cargo.toml b/test-utils/testlib/Cargo.toml index 423d868f66a..57f61821a47 100644 --- a/test-utils/testlib/Cargo.toml +++ b/test-utils/testlib/Cargo.toml @@ -36,7 +36,6 @@ near-jsonrpc-primitives = { path = "../../chain/jsonrpc-primitives" } near-network = { path = "../../chain/network" } near-jsonrpc-client = { path = "../../chain/jsonrpc/client" } near-test-contracts = { path = "../../runtime/near-test-contracts" } -nearcore = { path = "../../nearcore" } [features] default = [] @@ -45,5 +44,3 @@ protocol_feature_alt_bn128 = [ "node-runtime/protocol_feature_alt_bn128", "near-vm-errors/protocol_feature_alt_bn128", ] -nightly_protocol_features = ["nightly_protocol", "nearcore/nightly_protocol_features"] -nightly_protocol = ["nearcore/nightly_protocol"] diff --git a/test-utils/testlib/src/lib.rs b/test-utils/testlib/src/lib.rs index ff01e942284..a388397f055 100644 --- a/test-utils/testlib/src/lib.rs +++ b/test-utils/testlib/src/lib.rs @@ -1,108 +1,2 @@ -use std::sync::Arc; - -use actix::Addr; -use actix_rt::ArbiterHandle; -use tempfile::{tempdir, TempDir}; - -use near_chain::{Chain, ChainGenesis, DoomslugThresholdMode}; -use near_chain_configs::Genesis; -use near_client::{ClientActor, ViewClientActor}; -use near_logger_utils::init_integration_logger; -use near_network::test_utils::{convert_boot_nodes, open_port}; -use near_primitives::block::{Block, BlockHeader}; -use near_primitives::hash::CryptoHash; -use near_primitives::types::{BlockHeight, BlockHeightDelta, NumSeats, NumShards, ShardId}; -use near_store::test_utils::create_test_store; -use nearcore::{config::GenesisExt, load_test_config, start_with_config, NightshadeRuntime}; - pub mod fees_utils; -pub mod node; pub mod runtime_utils; -pub mod test_helpers; -pub mod user; - -/// Compute genesis hash from genesis. -pub fn genesis_hash(genesis: &Genesis) -> CryptoHash { - *genesis_header(genesis).hash() -} - -/// Utility to generate genesis header from config for testing purposes. -pub fn genesis_header(genesis: &Genesis) -> BlockHeader { - let dir = tempdir().unwrap(); - let store = create_test_store(); - let chain_genesis = ChainGenesis::from(genesis); - let runtime = - Arc::new(NightshadeRuntime::new(dir.path(), store, genesis, vec![], vec![], None, None)); - let chain = Chain::new(runtime, &chain_genesis, DoomslugThresholdMode::TwoThirds).unwrap(); - chain.genesis().clone() -} - -/// Utility to generate genesis header from config for testing purposes. -pub fn genesis_block(genesis: &Genesis) -> Block { - let dir = tempdir().unwrap(); - let store = create_test_store(); - let chain_genesis = ChainGenesis::from(genesis); - let runtime = - Arc::new(NightshadeRuntime::new(dir.path(), store, genesis, vec![], vec![], None, None)); - let mut chain = Chain::new(runtime, &chain_genesis, DoomslugThresholdMode::TwoThirds).unwrap(); - chain.get_block(&chain.genesis().hash().clone()).unwrap().clone() -} - -pub fn start_nodes( - num_shards: NumShards, - dirs: &[TempDir], - num_validator_seats: NumSeats, - num_lightclient: usize, - epoch_length: BlockHeightDelta, - genesis_height: BlockHeight, -) -> (Genesis, Vec, Vec<(Addr, Addr, Vec)>) { - init_integration_logger(); - - let num_nodes = dirs.len(); - let num_tracking_nodes = num_nodes - num_lightclient; - let seeds = (0..num_nodes).map(|i| format!("near.{}", i)).collect::>(); - let mut genesis = Genesis::test_sharded( - seeds.iter().map(|s| s.parse().unwrap()).collect(), - num_validator_seats, - (0..num_shards).map(|_| num_validator_seats).collect(), - ); - genesis.config.epoch_length = epoch_length; - genesis.config.genesis_height = genesis_height; - - let validators = (0..num_validator_seats).map(|i| format!("near.{}", i)).collect::>(); - let mut near_configs = vec![]; - let first_node = open_port(); - let mut rpc_addrs = vec![]; - for i in 0..num_nodes { - let mut near_config = load_test_config( - if i < num_validator_seats as usize { &validators[i] } else { "" }, - if i == 0 { first_node } else { open_port() }, - genesis.clone(), - ); - rpc_addrs.push(near_config.rpc_addr().unwrap().clone()); - near_config.client_config.min_num_peers = num_nodes - 1; - if i > 0 { - near_config.network_config.boot_nodes = - convert_boot_nodes(vec![("near.0", first_node)]); - } - // if non validator, add some shards to track. - if i >= (num_validator_seats as usize) && i < num_tracking_nodes { - let shards_per_node = - num_shards as usize / (num_tracking_nodes - num_validator_seats as usize); - let (from, to) = ( - ((i - num_validator_seats as usize) * shards_per_node) as ShardId, - ((i - (num_validator_seats as usize) + 1) * shards_per_node) as ShardId, - ); - near_config.client_config.tracked_shards.extend(&(from..to).collect::>()); - } - near_config.client_config.epoch_sync_enabled = false; - near_configs.push(near_config); - } - - let mut res = vec![]; - for (i, near_config) in near_configs.into_iter().enumerate() { - let (client, view_client, arbiters) = start_with_config(dirs[i].path(), near_config); - res.push((client, view_client, arbiters)) - } - (genesis, rpc_addrs, res) -} diff --git a/test-utils/testlib/src/runtime_utils.rs b/test-utils/testlib/src/runtime_utils.rs index 8e7d335454b..5dbdba75734 100644 --- a/test-utils/testlib/src/runtime_utils.rs +++ b/test-utils/testlib/src/runtime_utils.rs @@ -6,9 +6,8 @@ use near_primitives::hash::{hash, CryptoHash}; use near_primitives::state_record::{state_record_to_account_id, StateRecord}; use near_primitives::types::{AccountId, StateRoot}; use near_store::test_utils::create_tries; -use near_store::{ShardTries, TrieUpdate}; -use nearcore::config::GenesisExt; -use node_runtime::{state_viewer::TrieViewer, Runtime}; +use near_store::ShardTries; +use node_runtime::Runtime; use std::collections::HashSet; @@ -77,20 +76,6 @@ pub fn get_runtime_and_trie_from_genesis(genesis: &Genesis) -> (Runtime, ShardTr (runtime, tries, genesis_root) } -pub fn get_runtime_and_trie() -> (Runtime, ShardTries, StateRoot) { - let mut genesis = - Genesis::test(vec![alice_account(), bob_account(), "carol.near".parse().unwrap()], 3); - add_test_contract(&mut genesis, &"test.contract".parse().unwrap()); - get_runtime_and_trie_from_genesis(&genesis) -} - -pub fn get_test_trie_viewer() -> (TrieViewer, TrieUpdate) { - let (_, tries, root) = get_runtime_and_trie(); - let trie_viewer = TrieViewer::default(); - let state_update = tries.new_trie_update(0, root); - (trie_viewer, state_update) -} - pub fn encode_int(val: i32) -> [u8; 4] { let mut tmp = [0u8; 4]; LittleEndian::write_i32(&mut tmp, val); diff --git a/tools/restaked/Cargo.toml b/tools/restaked/Cargo.toml index 4dca8898e03..4add10ca2a5 100644 --- a/tools/restaked/Cargo.toml +++ b/tools/restaked/Cargo.toml @@ -14,4 +14,4 @@ near-primitives = { path = "../../core/primitives" } near-jsonrpc-client = { path = "../../chain/jsonrpc/client" } nearcore = { path = "../../nearcore" } -testlib = { path = "../../test-utils/testlib" } +integration-tests = { path = "../../integration-tests" } diff --git a/tools/restaked/src/main.rs b/tools/restaked/src/main.rs index 0421113e690..b72f8a1ee87 100644 --- a/tools/restaked/src/main.rs +++ b/tools/restaked/src/main.rs @@ -10,7 +10,7 @@ use near_primitives::views::CurrentEpochValidatorInfo; use nearcore::config::{Config, BLOCK_PRODUCER_KICKOUT_THRESHOLD, CONFIG_FILENAME}; use nearcore::get_default_home; // TODO(1905): Move out RPC interface for transacting into separate production crate. -use testlib::user::{rpc_user::RpcUser, User}; +use integration_tests::user::{rpc_user::RpcUser, User}; const DEFAULT_WAIT_PERIOD_SEC: &str = "60"; const DEFAULT_RPC_URL: &str = "http://localhost:3030";