Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lightweight testlib #4622

Merged
merged 44 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
f0224db
Remove dependency of 'chain/client' on 'nearcore' by moving its tests…
nikurt Jul 26, 2021
49d328f
Remove dependency of 'chain/client' on 'nearcore' by moving its tests…
nikurt Jul 26, 2021
9e29ada
Remove dependency of 'chain/client' on 'nearcore' by moving its tests…
nikurt Jul 26, 2021
b5da0e7
rustfmt
nikurt Jul 26, 2021
f3d49f4
better layout of near-client integration tests
nikurt Jul 26, 2021
6166ca5
better layout of near-client integration tests
nikurt Jul 26, 2021
5073865
.
nikurt Jul 27, 2021
aa081b7
Merge branch 'master' into cycle-t4
nikurt Jul 28, 2021
4e6ecac
.
nikurt Jul 28, 2021
49eb3a5
update nightly features of integration tests
nikurt Jul 28, 2021
8d0f758
merged
nikurt Jul 28, 2021
1ae217a
Merged
nikurt Jul 29, 2021
110b417
metric_recorder feature
nikurt Jul 29, 2021
15e848b
Merge branch 'master' of https://github.com/near/nearcore into cycle-n
nikurt Jul 30, 2021
d923af6
Remove a 'dev-dependency' of 'near-client' on 'testlib' to prevent ci…
nikurt Jul 30, 2021
df0b1b4
Merge branch 'master' into cycle-n
nikurt Aug 2, 2021
0d2e2fa
Remove dependency of 'chain/client' on 'nearcore' by moving its tests…
nikurt Jul 26, 2021
4b8cd31
Remove dependency of 'chain/client' on 'nearcore' by moving its tests…
nikurt Jul 26, 2021
6179cba
Remove dependency of 'chain/client' on 'nearcore' by moving its tests…
nikurt Jul 26, 2021
2583ec3
merge
nikurt Aug 2, 2021
f31fafb
merge
nikurt Aug 2, 2021
e015d10
merge
nikurt Aug 2, 2021
a71fa25
merge
nikurt Aug 2, 2021
00b90b0
merge
nikurt Aug 2, 2021
5be00c2
feature protocol_feature_block_header_v3 is required to run the new i…
nikurt Aug 2, 2021
618006b
Move test_evil_contracts test into integration tests, as it's the onl…
nikurt Aug 2, 2021
327b601
Extract crate 'testlib::node' into 'integration_tests' and move tests…
nikurt Aug 2, 2021
ce8e9fd
Remove 'nearcore' dependency of 'testlib' by extracting functions and…
nikurt Aug 2, 2021
3af6c47
Add runtime_utils.rs
nikurt Aug 2, 2021
0448cf9
fixed import
nikurt Aug 3, 2021
d1418cb
imports
nikurt Aug 4, 2021
a0928a2
Move test_evil_contracts test into integration tests, as it's the onl…
nikurt Aug 2, 2021
51cebbd
Extract crate 'testlib::node' into 'integration_tests' and move tests…
nikurt Aug 2, 2021
456b836
Remove 'nearcore' dependency of 'testlib' by extracting functions and…
nikurt Aug 2, 2021
aa5e362
merge
nikurt Aug 9, 2021
0a6e1bd
Merge remote-tracking branch 'origin/master' into testlib-1
nikurt Aug 10, 2021
ab3014b
Merge
nikurt Aug 10, 2021
c1bdd06
Merge
nikurt Aug 10, 2021
cef5e2b
Merge
nikurt Aug 10, 2021
18ca98b
Merge branch 'master' into testlib-1
nikurt Aug 10, 2021
d2a9f65
Merge branch 'master' into testlib-1
nikurt Aug 11, 2021
2da17cf
Merge refs/heads/master into testlib-1
near-bulldozer[bot] Aug 11, 2021
4eafb84
Merge remote-tracking branch 'origin/master' into testlib-1
nikurt Aug 12, 2021
fe629db
Really remove run_nodes.rs
nikurt Aug 12, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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" }
Expand All @@ -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"]
37 changes: 37 additions & 0 deletions integration-tests/src/genesis_helpers.rs
Original file line number Diff line number Diff line change
@@ -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()
}
5 changes: 5 additions & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod genesis_helpers;
pub mod node;
pub mod runtime_utils;
pub mod test_helpers;
pub mod user;
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RwLock<MockClient>>,
Expand Down Expand Up @@ -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() {
Expand Down
22 changes: 22 additions & 0 deletions integration-tests/src/runtime_utils.rs
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion integration-tests/tests/client/chunks_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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).
Expand Down
8 changes: 8 additions & 0 deletions integration-tests/tests/nearcore/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<String>, Vec<(Addr<ClientActor>, Addr<ViewClientActor>, Vec<ArbiterHandle>)>) {
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::<Vec<_>>();
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::<Vec<_>>();
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::<Vec<_>>());
}
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ 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};
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]
Expand Down
Loading