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

Fixing expensive tests #1157

Closed
wants to merge 10 commits into from
6 changes: 3 additions & 3 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ impl Handler<Status> for ClientActor {

fn handle(&mut self, msg: Status, _: &mut Context<Self>) -> Self::Result {
let head = self.client.chain.head().map_err(|err| err.to_string())?;
let prev_header = self
let header = self
.client
.chain
.get_block_header(&head.last_block_hash)
.map_err(|err| err.to_string())?;
let latest_block_time = prev_header.inner_lite.timestamp.clone();
let latest_block_time = header.inner_lite.timestamp.clone();
if msg.is_health_check {
let elapsed = (Utc::now() - from_timestamp(latest_block_time)).to_std().unwrap();
if elapsed
Expand Down Expand Up @@ -507,7 +507,7 @@ impl Handler<Status> for ClientActor {
sync_info: StatusSyncInfo {
latest_block_hash: head.last_block_hash.into(),
latest_block_height: head.height,
latest_state_root: prev_header.inner_lite.prev_state_root.clone().into(),
latest_state_root: header.inner_lite.prev_state_root.clone().into(),
latest_block_time: from_timestamp(latest_block_time),
syncing: self.client.sync_status.is_syncing(),
},
Expand Down
24 changes: 13 additions & 11 deletions chain/jsonrpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use near_primitives::views::{
StatusResponse,
};

use crate::message::{from_slice, Message};
use crate::message::{from_slice, Message, RpcError};

pub mod message;

Expand All @@ -34,7 +34,7 @@ pub enum ChunkId {
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

type HttpRequest<T> = Box<dyn Future<Item = T, Error = String>>;
type RpcRequest<T> = Box<dyn Future<Item = T, Error = String>>;
type RpcRequest<T> = Box<dyn Future<Item = T, Error = RpcError>>;

/// Prepare a `RPCRequest` with a given client, server address, method and parameters.
fn call_method<P, R>(client: &Client, server_addr: &str, method: &str, params: P) -> RpcRequest<R>
Expand All @@ -50,21 +50,23 @@ where
.post(server_addr)
.header("Content-Type", "application/json")
.send_json(&request)
.map_err(|err| err.to_string())
.map_err(|_| RpcError::invalid_request())
.and_then(|mut response| {
response.body().then(|body| match body {
Ok(bytes) => {
from_slice(&bytes).map_err(|err| format!("Error {:?} in {:?}", err, bytes))
Ok(bytes) => from_slice(&bytes).map_err(|err| {
RpcError::parse_error(format!("Error {:?} in {:?}", err, bytes))
}),
Err(err) => {
Err(RpcError::parse_error(format!("Failed to retrieve payload: {:?}", err)))
}
Err(_) => Err("Payload error: {:?}".to_string()),
})
})
.and_then(|message| match message {
Message::Response(resp) => resp
.result
.map_err(|x| format!("{:?}", x))
.and_then(|x| serde_json::from_value(x).map_err(|x| x.to_string())),
_ => Err("Invalid message type".to_string()),
Message::Response(resp) => resp.result.and_then(|x| {
serde_json::from_value(x)
.map_err(|err| RpcError::parse_error(format!("Failed to parse: {:?}", err)))
}),
_ => Err(RpcError::invalid_request()),
}),
)
}
Expand Down
6 changes: 6 additions & 0 deletions chain/jsonrpc/client/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ impl RpcError {
}
}

impl std::string::ToString for RpcError {
fn to_string(&self) -> String {
format!("{:?}", self)
}
}

/// A response to an RPC.
///
/// It is created by the methods on [Request](struct.Request.html).
Expand Down
2 changes: 2 additions & 0 deletions near/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@ pub fn create_testnet_configs_from_seeds(
let first_node_port = open_port();
for i in 0..seeds.len() {
let mut config = Config::default();
config.consensus.min_block_production_delay = Duration::from_millis(100);
config.consensus.max_block_production_delay = Duration::from_millis(200);
if local_ports {
config.network.addr =
format!("127.0.0.1:{}", if i == 0 { first_node_port } else { open_port() });
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-logic/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use near_vm_logic::types::Gas;
use near_vm_logic::{ExtCosts, VMLogic, EXT_COSTS_COUNTER};
use std::collections::HashMap;

#[allow(dead_code)]
type Result<T> = ::std::result::Result<T, HostErrorOrStorageError>;

#[allow(dead_code)]
pub fn promise_create(
logic: &mut VMLogic,
account_id: &[u8],
Expand All @@ -25,6 +27,7 @@ pub fn promise_create(
)
}

#[allow(dead_code)]
pub fn promise_batch_action_function_call(
logic: &mut VMLogic,
promise_index: u64,
Expand All @@ -45,6 +48,7 @@ pub fn promise_batch_action_function_call(
)
}

#[allow(dead_code)]
pub fn promise_batch_action_add_key_with_function_call(
logic: &mut VMLogic,
promise_index: u64,
Expand Down Expand Up @@ -92,6 +96,7 @@ pub fn reset_costs_counter() {
EXT_COSTS_COUNTER.with(|f| f.borrow_mut().clear());
}

#[allow(dead_code)]
pub fn assert_costs(expected: HashMap<ExtCosts, u64>) {
EXT_COSTS_COUNTER.with(|f| {
assert_eq!(f.borrow().clone(), expected);
Expand Down
10 changes: 5 additions & 5 deletions runtime/near-vm-logic/tests/test_miscs.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod fixtures;
mod vm_logic_builder;

use fixtures::get_context;
use helpers::*;
use near_vm_errors::HostError;
use near_vm_logic::ExtCosts;
use std::collections::HashMap;
use vm_logic_builder::VMLogicBuilder;

mod fixtures;
mod vm_logic_builder;

mod helpers;
use helpers::*;

#[test]
fn test_valid_utf8() {
Expand Down
2 changes: 0 additions & 2 deletions runtime/runtime-params-estimator/src/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ use node_runtime::config::RuntimeConfig;

/// How much gas there is in a nanosecond worth of computation.
const GAS_IN_NANOS: f64 = 1_000_000f64;
/// The block limit is
const GAS_IN_BLOCK: u64 = 1_000_000_000_000_000u64;

fn measure_function(
metric: Metric,
Expand Down
25 changes: 12 additions & 13 deletions runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ use std::sync::Arc;
use borsh::{BorshDeserialize, BorshSerialize};
use kvdb::DBValue;

use crate::actions::*;
use crate::balance_checker::check_balance;
use crate::config::{
exec_fee, safe_add_balance, safe_add_gas, safe_gas_to_balance, total_deposit, total_exec_fees,
total_prepaid_gas, tx_cost, RuntimeConfig,
};
pub use crate::store::StateRecord;
use near_crypto::PublicKey;
use near_primitives::account::{AccessKey, AccessKeyPermission, Account};
use near_primitives::contract::ContractCode;
Expand All @@ -41,14 +34,23 @@ use near_primitives::utils::{
key_for_pending_data_count, key_for_postponed_receipt, key_for_postponed_receipt_id,
key_for_received_data, system_account, ACCOUNT_DATA_SEPARATOR,
};
use near_runtime_fees::RuntimeFeesConfig;
use near_store::{
get, get_access_key, get_account, get_receipt, get_received_data, set, set_access_key,
set_account, set_code, set_receipt, set_received_data, PrefixKeyValueChanges, StorageError,
StoreUpdate, Trie, TrieChanges, TrieUpdate,
};
use near_vm_logic::types::PromiseResult;
use near_vm_logic::ReturnData;
#[cfg(feature = "costs_counting")]
pub use near_vm_runner::EXT_COSTS_COUNTER;

use crate::actions::*;
use crate::balance_checker::check_balance;
use crate::config::{
exec_fee, safe_add_balance, safe_add_gas, safe_gas_to_balance, total_deposit, total_exec_fees,
total_prepaid_gas, tx_cost, RuntimeConfig,
};
pub use crate::store::StateRecord;

mod actions;
pub mod adapter;
Expand All @@ -60,9 +62,6 @@ mod metrics;
pub mod state_viewer;
mod store;

#[cfg(feature = "costs_counting")]
pub use near_vm_runner::EXT_COSTS_COUNTER;

#[derive(Debug)]
pub struct ApplyState {
/// Currently building block index.
Expand Down Expand Up @@ -1262,15 +1261,15 @@ impl Runtime {

#[cfg(test)]
mod tests {
use super::*;

use near_crypto::KeyType;
use near_primitives::hash::hash;
use near_primitives::transaction::TransferAction;
use near_primitives::types::MerkleHash;
use near_store::test_utils::create_trie;
use testlib::runtime_utils::{alice_account, bob_account};

use super::*;

const GAS_PRICE: Balance = 100;

#[test]
Expand Down
6 changes: 4 additions & 2 deletions test-utils/testlib/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fs;
use std::panic;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::RwLock;
use std::{fs, panic};

use near::config::{
create_testnet_configs, create_testnet_configs_from_seeds, Config, GenesisConfig,
Expand All @@ -11,13 +12,13 @@ use near_crypto::{InMemorySigner, Signer};
use near_primitives::serialize::to_base64;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::{AccountId, Balance};
use near_primitives::views::AccountView;
use node_runtime::StateRecord;

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::views::AccountView;

mod process_node;
mod runtime_node;
Expand Down Expand Up @@ -150,6 +151,7 @@ pub fn create_nodes_from_seeds(seeds: Vec<String>) -> Vec<NodeConfig> {
let code = to_base64(&fs::read(path).unwrap());
let (configs, signers, network_signers, mut genesis_config) =
create_testnet_configs_from_seeds(seeds.clone(), 1, 0, true);
genesis_config.gas_price_adjustment_rate = 0;
for seed in seeds {
genesis_config.records.push(StateRecord::Contract { account_id: seed, code: code.clone() });
}
Expand Down
16 changes: 11 additions & 5 deletions test-utils/testlib/src/node/thread_node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::PathBuf;
use std::sync::Arc;

use near::{start_with_config, GenesisConfig, NearConfig};
Expand All @@ -18,12 +19,12 @@ pub struct ThreadNode {
pub config: NearConfig,
pub state: ThreadNodeState,
pub signer: Arc<InMemorySigner>,
pub dir: tempdir::TempDir,
}

fn start_thread(config: NearConfig) -> ShutdownableThread {
fn start_thread(config: NearConfig, path: PathBuf) -> ShutdownableThread {
ShutdownableThread::start("test", move || {
let tmp_dir = tempdir::TempDir::new("thread_node").unwrap();
start_with_config(tmp_dir.path(), config);
start_with_config(&path, config);
})
}

Expand All @@ -40,7 +41,7 @@ impl Node for ThreadNode {
}

fn start(&mut self) {
let handle = start_thread(self.config.clone());
let handle = start_thread(self.config.clone(), self.dir.path().to_path_buf());
self.state = ThreadNodeState::Running(handle);
}

Expand Down Expand Up @@ -87,6 +88,11 @@ impl ThreadNode {
KeyType::ED25519,
&config.block_producer.clone().unwrap().account_id,
));
ThreadNode { config, state: ThreadNodeState::Stopped, signer }
ThreadNode {
config,
state: ThreadNodeState::Stopped,
signer,
dir: tempdir::TempDir::new("thread_node").unwrap(),
}
}
}
1 change: 0 additions & 1 deletion test-utils/testlib/src/standard_test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ pub fn test_add_access_key_with_allowance(node: impl Node) {
let initial_balance = account.amount;
let fee_helper = fee_helper(&node);
let add_access_key_cost = fee_helper.add_key_cost(0);
println!("{}", add_access_key_cost);
add_access_key(&node, node_user.as_ref(), &access_key, &signer2);

let account = node_user.view_account(account_id).unwrap();
Expand Down
30 changes: 23 additions & 7 deletions test-utils/testlib/src/user/rpc_user.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use std::convert::TryInto;
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;

use actix::System;
use borsh::BorshSerialize;

use near_client::StatusResponse;
use near_crypto::{PublicKey, Signer};
use near_jsonrpc::client::{new_client, JsonRpcClient};
use near_jsonrpc_client::BlockId;
use near_primitives::hash::CryptoHash;
use near_primitives::receipt::Receipt;
use near_primitives::serialize::{to_base, to_base64};
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::AccountId;
use near_primitives::views::{
AccessKeyView, AccountView, BlockView, ExecutionOutcomeView, FinalExecutionOutcomeView,
QueryResponse, ViewStateResult,
AccessKeyView, AccountView, BlockView, ExecutionErrorView, ExecutionOutcomeView,
FinalExecutionOutcomeView, QueryResponse, ViewStateResult,
};

use crate::user::User;
use near_jsonrpc_client::BlockId;

pub struct RpcUser {
account_id: AccountId,
Expand All @@ -36,7 +38,9 @@ impl RpcUser {
}

pub fn query(&self, path: String, data: &[u8]) -> Result<QueryResponse, String> {
System::new("actix").block_on(self.client.write().unwrap().query(path, to_base(data)))
System::new("actix")
.block_on(self.client.write().unwrap().query(path, to_base(data)))
.map_err(|err| err.to_string())
}
}

Expand All @@ -52,7 +56,8 @@ impl User for RpcUser {
fn add_transaction(&self, transaction: SignedTransaction) -> Result<(), String> {
let bytes = transaction.try_to_vec().unwrap();
let _ = System::new("actix")
.block_on(self.client.write().unwrap().broadcast_tx_async(to_base64(&bytes)))?;
.block_on(self.client.write().unwrap().broadcast_tx_async(to_base64(&bytes)))
.map_err(|err| err.to_string())?;
Ok(())
}

Expand All @@ -61,8 +66,19 @@ impl User for RpcUser {
transaction: SignedTransaction,
) -> Result<FinalExecutionOutcomeView, String> {
let bytes = transaction.try_to_vec().unwrap();
System::new("actix")
.block_on(self.client.write().unwrap().broadcast_tx_commit(to_base64(&bytes)))
let result = System::new("actix")
.block_on(self.client.write().unwrap().broadcast_tx_commit(to_base64(&bytes)));
// Wait for one more block, to make sure all nodes actually apply the state transition.
let height = self.get_best_block_index().unwrap();
while height == self.get_best_block_index().unwrap() {
thread::sleep(Duration::from_millis(50));
}
match result {
Ok(outcome) => Ok(outcome),
Err(err) => Err(serde_json::from_value::<ExecutionErrorView>(err.data.unwrap())
.unwrap()
.error_message),
}
}

fn add_receipt(&self, _receipt: Receipt) -> Result<(), String> {
Expand Down
Loading