-
Notifications
You must be signed in to change notification settings - Fork 274
/
samples.rs
142 lines (131 loc) · 4.71 KB
/
samples.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
//! This module contains the sample configurations used for testing and benchmarking throughout Iroha.
use std::{collections::HashSet, path::Path, str::FromStr, time::Duration};
use iroha_config::{
base::{HumanDuration, UnwrapPartial},
parameters::{
actual::Root as Config,
user::{CliContext, RootPartial as UserConfig},
},
snapshot::Mode as SnapshotMode,
};
use iroha_crypto::{KeyPair, PublicKey};
use iroha_data_model::{peer::PeerId, prelude::*, ChainId};
use iroha_primitives::{
addr::{socket_addr, SocketAddr},
unique_vec::UniqueVec,
};
// FIXME: move to a global test-related place, re-use everywhere else
const DEFAULT_P2P_ADDR: SocketAddr = socket_addr!(127.0.0.1:1337);
const DEFAULT_TORII_ADDR: SocketAddr = socket_addr!(127.0.0.1:8080);
/// Get sample trusted peers. The public key must be the same as `configuration.public_key`
///
/// # Panics
/// Never
pub fn get_trusted_peers(public_key: Option<&PublicKey>) -> HashSet<PeerId> {
let mut trusted_peers: HashSet<PeerId> = [
(
"localhost:1338",
"ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C1",
),
(
"195.162.0.1:23",
"ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C2",
),
(
"195.162.0.1:24",
"ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C3",
),
]
.iter()
.map(|(a, k)| PeerId::new(a.parse().expect("Valid"), PublicKey::from_str(k).unwrap()))
.collect();
if let Some(pubkey) = public_key {
trusted_peers.insert(PeerId {
address: DEFAULT_P2P_ADDR.clone(),
public_key: pubkey.clone(),
});
}
trusted_peers
}
#[allow(clippy::implicit_hasher)]
/// Get a sample Iroha configuration on user-layer level. Trusted peers must be
/// specified in this function, including the current peer. Use [`get_trusted_peers`]
/// to populate `trusted_peers` if in doubt. Almost equivalent to the [`get_config`]
/// function, except the proxy is left unbuilt.
///
/// # Panics
/// - when [`KeyPair`] generation fails (rare case).
pub fn get_user_config(
peers: &UniqueVec<PeerId>,
chain_id: Option<ChainId>,
key_pair: Option<KeyPair>,
) -> UserConfig {
let chain_id = chain_id.unwrap_or_else(|| ChainId::from("0"));
let (public_key, private_key) = key_pair.unwrap_or_else(KeyPair::random).into_parts();
iroha_logger::info!(%public_key);
let mut config = UserConfig::new();
config.chain_id.set(chain_id);
config.public_key.set(public_key.clone());
config.private_key.set(private_key.clone());
config.network.address.set(DEFAULT_P2P_ADDR);
config
.chain_wide
.max_transactions_in_block
.set(2.try_into().unwrap());
config.sumeragi.trusted_peers.set(peers.to_vec());
config.torii.address.set(DEFAULT_TORII_ADDR);
config
.network
.block_gossip_max_size
.set(1.try_into().unwrap());
config
.network
.block_gossip_period
.set(HumanDuration(Duration::from_millis(500)));
config.genesis.private_key.set(private_key);
config.genesis.public_key.set(public_key);
config.genesis.file.set("./genesis.json".into());
// There is no need in persistency in tests
// If required to should be set explicitly not to overlap with other existing tests
config.snapshot.mode.set(SnapshotMode::Disabled);
config
}
#[allow(clippy::implicit_hasher)]
/// Get a sample Iroha configuration. Trusted peers must either be
/// specified in this function, including the current peer. Use [`get_trusted_peers`]
/// to populate `trusted_peers` if in doubt.
///
/// # Panics
/// - when [`KeyPair`] generation fails (rare case).
pub fn get_config(
trusted_peers: &UniqueVec<PeerId>,
chain_id: Option<ChainId>,
key_pair: Option<KeyPair>,
) -> Config {
get_user_config(trusted_peers, chain_id, key_pair)
.unwrap_partial()
.expect("config should build as all required fields were provided")
.parse(CliContext {
submit_genesis: true,
})
.expect("config should finalize as the input is semantically valid (or there is a bug)")
}
/// Construct executor from path.
///
/// `relative_path` should be relative to `CARGO_MANIFEST_DIR`.
///
/// # Errors
///
/// - Failed to create temp dir for executor output
/// - Failed to build executor
/// - Failed to optimize executor
pub fn construct_executor<P>(relative_path: &P) -> color_eyre::Result<Executor>
where
P: AsRef<Path> + ?Sized,
{
let wasm_blob = iroha_wasm_builder::Builder::new(relative_path)
.build()?
.optimize()?
.into_bytes()?;
Ok(Executor::new(WasmSmartContract::from_compiled(wasm_blob)))
}