-
Notifications
You must be signed in to change notification settings - Fork 586
/
mod.rs
124 lines (110 loc) · 4.01 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use actix::Addr;
use futures::{future, future::LocalBoxFuture, FutureExt, TryFutureExt};
use serde_json::json;
use near_chain_configs::GenesisConfig;
use near_client::test_utils::setup_no_network_with_validity_period_and_no_epoch_sync;
use near_client::ViewClientActor;
use near_jsonrpc::{start_http, RpcConfig};
use near_network::test_utils::open_port;
use near_primitives::types::NumBlocks;
use near_jsonrpc_primitives::message::{from_slice, Message};
lazy_static::lazy_static! {
pub static ref TEST_GENESIS_CONFIG: GenesisConfig =
GenesisConfig::from_json(include_str!("../../../../neard/res/genesis_config.json"));
}
pub enum NodeType {
Validator,
NonValidator,
}
pub fn start_all(node_type: NodeType) -> (Addr<ViewClientActor>, String) {
start_all_with_validity_period_and_no_epoch_sync(node_type, 100, false)
}
pub fn start_all_with_validity_period_and_no_epoch_sync(
node_type: NodeType,
transaction_validity_period: NumBlocks,
enable_doomslug: bool,
) -> (Addr<ViewClientActor>, String) {
let (client_addr, view_client_addr) = setup_no_network_with_validity_period_and_no_epoch_sync(
vec!["test1", "test2"],
if let NodeType::Validator = node_type { "test1" } else { "other" },
true,
transaction_validity_period,
enable_doomslug,
);
let addr = format!("127.0.0.1:{}", open_port());
start_http(
RpcConfig::new(&addr),
TEST_GENESIS_CONFIG.clone(),
client_addr.clone(),
view_client_addr.clone(),
);
(view_client_addr, addr)
}
#[allow(unused_macros)] // Suppress Rustc warnings even though this macro is used.
macro_rules! test_with_client {
($node_type:expr, $client:ident, $block:expr) => {
init_test_logger();
near_actix_test_utils::run_actix_until_stop(async {
let (_view_client_addr, addr) = test_utils::start_all($node_type);
let $client = new_client(&format!("http://{}", addr));
actix::spawn(async move {
$block.await;
System::current().stop();
});
});
};
}
type RpcRequest<T> = LocalBoxFuture<'static, Result<T, near_jsonrpc_primitives::errors::RpcError>>;
/// Prepare a `RPCRequest` with a given client, server address, method and parameters.
pub fn call_method<R>(
client: &awc::Client,
server_addr: &str,
method: &str,
params: serde_json::Value,
) -> RpcRequest<R>
where
R: serde::de::DeserializeOwned + 'static,
{
let request = json!({
"jsonrpc": "2.0",
"method": method,
"id": "dontcare",
"params": params,
});
// TODO: simplify this.
client
.post(server_addr)
.insert_header(("Content-Type", "application/json"))
.send_json(&request)
.map_err(|err| {
near_jsonrpc_primitives::errors::RpcError::server_error(Some(format!("{:?}", err)))
})
.and_then(|mut response| {
response.body().map(|body| match body {
Ok(bytes) => from_slice(&bytes).map_err(|err| {
near_jsonrpc_primitives::errors::RpcError::parse_error(format!(
"Error {:?} in {:?}",
err, bytes
))
}),
Err(err) => Err(near_jsonrpc_primitives::errors::RpcError::parse_error(format!(
"Failed to retrieve payload: {:?}",
err
))),
})
})
.and_then(|message| {
future::ready(match message {
Message::Response(resp) => resp.result.and_then(|x| {
serde_json::from_value(x).map_err(|err| {
near_jsonrpc_primitives::errors::RpcError::parse_error(format!(
"Failed to parse: {:?}",
err
))
})
}),
_ => Err(near_jsonrpc_primitives::errors::RpcError::invalid_request()),
})
})
.boxed_local()
}