-
Notifications
You must be signed in to change notification settings - Fork 581
/
mod.rs
192 lines (163 loc) · 6.35 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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};
use near_primitives::validator_signer::InMemoryValidatorSigner;
use near_primitives::views::AccountView;
use nearcore::config::{
create_testnet_configs, create_testnet_configs_from_seeds, Config, GenesisExt,
};
use nearcore::NearConfig;
use testlib::runtime_utils::{alice_account, bob_account};
mod process_node;
mod runtime_node;
mod thread_node;
pub const TEST_BLOCK_FETCH_LIMIT: u64 = 5;
pub const TEST_BLOCK_MAX_SIZE: u32 = 1000;
pub fn configure_chain_spec() -> Genesis {
Genesis::test(vec![alice_account(), bob_account()], 2)
}
/// Config that can be used to start a node or connect to an existing node.
#[allow(clippy::large_enum_variant)]
pub enum NodeConfig {
/// A node with only runtime and state that is used to run runtime tests.
Runtime { account_id: AccountId },
/// A complete node with network, RPC, client, consensus and all tasks running in a thead.
/// Should be the default choice for the tests, since it provides the most control through the
/// internal access.
Thread(NearConfig),
/// A complete noe running in a subprocess. Can be started and stopped, but besides that all
/// interactions are limited to what is exposed through RPC.
Process(NearConfig),
}
pub trait Node: Send + Sync {
fn genesis(&self) -> &Genesis;
fn account_id(&self) -> Option<AccountId>;
fn start(&mut self);
fn kill(&mut self);
fn view_account(&self, account_id: &AccountId) -> Result<AccountView, String> {
self.user().view_account(account_id)
}
fn get_access_key_nonce_for_signer(&self, account_id: &AccountId) -> Result<u64, String> {
self.user().get_access_key_nonce_for_signer(account_id)
}
fn view_balance(&self, account_id: &AccountId) -> Result<Balance, String> {
self.user().view_balance(account_id)
}
fn add_transaction(&self, transaction: SignedTransaction) -> Result<(), ServerError> {
self.user().add_transaction(transaction)
}
fn signer(&self) -> Arc<dyn Signer>;
fn block_signer(&self) -> Arc<dyn Signer> {
unimplemented!()
}
fn is_running(&self) -> bool;
fn user(&self) -> Box<dyn User>;
fn async_user(&self) -> Box<dyn AsyncUser> {
unimplemented!()
}
fn as_thread_ref(&self) -> &ThreadNode {
unimplemented!()
}
fn as_thread_mut(&mut self) -> &mut ThreadNode {
unimplemented!()
}
fn as_process_ref(&self) -> &ProcessNode {
unimplemented!()
}
fn as_process_mut(&mut self) -> &mut ProcessNode {
unimplemented!()
}
}
impl dyn Node {
pub fn new_sharable(config: NodeConfig) -> Arc<RwLock<dyn Node>> {
match config {
NodeConfig::Runtime { account_id } => {
Arc::new(RwLock::new(RuntimeNode::new(&account_id)))
}
NodeConfig::Thread(config) => Arc::new(RwLock::new(ThreadNode::new(config))),
NodeConfig::Process(config) => Arc::new(RwLock::new(ProcessNode::new(config))),
}
}
pub fn new(config: NodeConfig) -> Box<dyn Node> {
match config {
NodeConfig::Runtime { account_id } => Box::new(RuntimeNode::new(&account_id)),
NodeConfig::Thread(config) => Box::new(ThreadNode::new(config)),
NodeConfig::Process(config) => Box::new(ProcessNode::new(config)),
}
}
}
fn near_configs_to_node_configs(
configs: Vec<Config>,
validator_signers: Vec<InMemoryValidatorSigner>,
network_signers: Vec<InMemorySigner>,
genesis: Genesis,
) -> Vec<NodeConfig> {
let mut result = vec![];
for i in 0..configs.len() {
result.push(NodeConfig::Thread(NearConfig::new(
configs[i].clone(),
genesis.clone(),
(&network_signers[i]).into(),
Some(Arc::new(validator_signers[i].clone())),
)))
}
result
}
pub fn create_nodes(num_nodes: usize, prefix: &str) -> Vec<NodeConfig> {
let (configs, validator_signers, network_signers, genesis) =
create_testnet_configs(1, num_nodes as NumSeats, 0, prefix, true, false);
near_configs_to_node_configs(configs, validator_signers, network_signers, genesis)
}
pub fn create_nodes_from_seeds(seeds: Vec<String>) -> Vec<NodeConfig> {
let code = near_test_contracts::rs_contract();
let (configs, validator_signers, network_signers, mut genesis) =
create_testnet_configs_from_seeds(seeds.clone(), 1, 0, true, false);
genesis.config.gas_price_adjustment_rate = Rational::from_integer(0);
for seed in seeds {
let mut is_account_record_found = false;
for record in genesis.records.as_mut() {
if let StateRecord::Account { account_id: record_account_id, ref mut account } = record
{
if record_account_id.as_ref() == &seed {
is_account_record_found = true;
account.set_code_hash(ContractCode::new(code.to_vec(), None).hash().clone());
}
}
}
assert!(is_account_record_found);
genesis
.records
.as_mut()
.push(StateRecord::Contract { account_id: seed.parse().unwrap(), code: code.to_vec() });
}
near_configs_to_node_configs(configs, validator_signers, network_signers, genesis)
}
pub fn sample_two_nodes(num_nodes: usize) -> (usize, usize) {
let i = rand::random::<usize>() % num_nodes;
// Should be a different node.
let mut j = rand::random::<usize>() % (num_nodes - 1);
if j >= i {
j += 1;
}
(i, j)
}
/// Sample a node for sending a transaction/checking balance
pub fn sample_queryable_node(nodes: &[Arc<RwLock<dyn Node>>]) -> usize {
let num_nodes = nodes.len();
let mut k = rand::random::<usize>() % num_nodes;
while !nodes[k].read().unwrap().is_running() {
k = rand::random::<usize>() % num_nodes;
}
k
}