Skip to content

Commit 0c664d1

Browse files
authored
Add explorer to hersir (#4108)
* Refactor hersir: pull out all keys generation and allow to define external wallets or committess and voteplans, this will allow external clients to create their own keys which will be used for tally operations. * update tests with hersir changes * Refactor explorer module. Extract configuration struct for explorer. Added extension to multiaddr struct for better address formatting and finally make ExplorerProcess constructor return error * added simple explorer client which is capable to send simple sanity request to explorer server * update usages for ExplorerProcess new constructor impl * added possibility to start explorer by hersir
1 parent 7588dad commit 0c664d1

File tree

31 files changed

+389
-76
lines changed

31 files changed

+389
-76
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

explorer/src/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ pub fn explorer_test_context(
3333

3434
let params = ExplorerParams::new(test_config.query_complexity_limit, None, None);
3535

36-
(jormungandr.explorer(params), jormungandr)
36+
let explorer_process = jormungandr.explorer(params).unwrap();
37+
println!("{:?}", explorer_process.client().current_time());
38+
(explorer_process, jormungandr)
3739
}
3840

3941
pub fn get_invalid_block(explorer: &Explorer) {

testing/hersir/res/example.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ nodes:
1010
leadership_mode: leader
1111
persistence_mode: inmemory
1212

13+
explorer:
14+
connect_to: passive
15+
persistence_mode: inmemory
16+
17+
1318
blockchain:
1419
discrimination: test
1520
consensus: bft
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::{builder::NodeSetting, config::ExplorerTemplate};
2+
use jormungandr_automation::{
3+
jormungandr::{explorer::configuration::ExplorerConfiguration, get_available_port, NodeAlias},
4+
utils::MultiaddrExtension,
5+
};
6+
use std::{collections::HashMap, path::Path};
7+
8+
pub fn generate_explorer(
9+
nodes: &HashMap<NodeAlias, NodeSetting>,
10+
explorer_template: &ExplorerTemplate,
11+
) -> Result<ExplorerConfiguration, Error> {
12+
let settings = nodes
13+
.get(&explorer_template.connect_to)
14+
.ok_or_else(|| Error::CannotFindAlias(explorer_template.connect_to.clone()))?;
15+
16+
Ok(ExplorerConfiguration {
17+
explorer_port: get_available_port(),
18+
explorer_listen_address: "127.0.0.1".to_string(),
19+
node_address: settings.config.p2p.public_address.clone().to_http_addr(),
20+
logs_dir: Some(Path::new("C:\\work\\iohk\\logs.txt").to_path_buf()),
21+
storage_dir: None,
22+
params: explorer_template.to_explorer_params(),
23+
})
24+
}
25+
26+
#[derive(thiserror::Error, Debug)]
27+
pub enum Error {
28+
#[error("cannot find alias '{0}' for any defined node")]
29+
CannotFindAlias(NodeAlias),
30+
}

testing/hersir/src/builder/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod committee;
2+
mod explorer;
23
pub mod rng;
34
pub mod settings;
45
mod stake_pool;
@@ -9,7 +10,8 @@ pub mod wallet;
910
pub use crate::controller::Error as ControllerError;
1011
use crate::{
1112
config::{
12-
Blockchain, CommitteeTemplate, Config, SessionSettings, VotePlanTemplate, WalletTemplate,
13+
Blockchain, CommitteeTemplate, Config, ExplorerTemplate, SessionSettings, VotePlanTemplate,
14+
WalletTemplate,
1315
},
1416
controller::{Controller, Error},
1517
utils::Dotifier,
@@ -35,6 +37,7 @@ pub struct NetworkBuilder {
3537
topology: Topology,
3638
blockchain: Blockchain,
3739
session_settings: SessionSettings,
40+
explorer_template: Option<ExplorerTemplate>,
3841
wallet_templates: Vec<WalletTemplate>,
3942
committee_templates: Vec<CommitteeTemplate>,
4043
vote_plan_templates: Vec<VotePlanTemplate>,
@@ -72,6 +75,7 @@ impl NetworkBuilder {
7275
.wallet_templates(config.wallets)
7376
.vote_plan_templates(config.vote_plans)
7477
.committees(config.committees)
78+
.explorer(config.explorer)
7579
}
7680

7781
pub fn topology(mut self, topology: Topology) -> Self {
@@ -142,6 +146,7 @@ impl NetworkBuilder {
142146
&self.blockchain,
143147
&self.wallet_templates,
144148
&self.committee_templates,
149+
&self.explorer_template,
145150
&self.vote_plan_templates,
146151
&mut random,
147152
)?;
@@ -155,6 +160,11 @@ impl NetworkBuilder {
155160
self.finish_all();
156161
Controller::new(settings, self.session_settings.root)
157162
}
163+
164+
pub fn explorer(mut self, explorer: Option<ExplorerTemplate>) -> Self {
165+
self.explorer_template = explorer;
166+
self
167+
}
158168
}
159169

160170
fn document(path: &Path, settings: &Settings) -> Result<(), Error> {

testing/hersir/src/builder/settings/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ pub(crate) mod wallet;
55
pub use crate::{builder::settings::node::NodeSetting, config::Blockchain};
66
use crate::{
77
builder::{
8-
committee::generate_committee_data, stake_pool, vote::generate_vote_plans,
9-
wallet::generate, Node as NodeTemplate, Random, VotePlanKey, VotePlanSettings, Wallet,
8+
committee::generate_committee_data, explorer::generate_explorer, stake_pool,
9+
vote::generate_vote_plans, wallet::generate, Node as NodeTemplate, Random, VotePlanKey,
10+
VotePlanSettings, Wallet,
1011
},
11-
config::{CommitteeTemplate, VotePlanTemplate, WalletTemplate},
12+
config::{CommitteeTemplate, ExplorerTemplate, VotePlanTemplate, WalletTemplate},
1213
};
1314
use assert_fs::fixture::{ChildPath, PathChild};
1415
use chain_crypto::Ed25519;
1516
use chain_impl_mockchain::{chaintypes::ConsensusVersion, fee::LinearFee};
16-
use jormungandr_automation::jormungandr::NodeAlias;
17+
use jormungandr_automation::jormungandr::{
18+
explorer::configuration::ExplorerConfiguration, NodeAlias,
19+
};
1720
use jormungandr_lib::{
1821
crypto::key::SigningKey,
1922
interfaces::{
@@ -31,6 +34,7 @@ pub struct Settings {
3134
pub wallets: Vec<Wallet>,
3235
pub committees: Vec<CommitteeIdDef>,
3336
pub block0: Block0Configuration,
37+
pub explorer: Option<ExplorerConfiguration>,
3438
pub stake_pools: HashMap<NodeAlias, StakePool>,
3539
pub vote_plans: HashMap<VotePlanKey, VotePlanSettings>,
3640
}
@@ -41,6 +45,7 @@ impl Settings {
4145
blockchain: &Blockchain,
4246
wallets: &[WalletTemplate],
4347
committees: &[CommitteeTemplate],
48+
explorer: &Option<ExplorerTemplate>,
4449
vote_plans: &[VotePlanTemplate],
4550
rng: &mut Random<RNG>,
4651
) -> Result<Self, Error>
@@ -59,6 +64,7 @@ impl Settings {
5964
),
6065
initial: Vec::new(),
6166
},
67+
explorer: None,
6268
stake_pools: HashMap::new(),
6369
vote_plans: HashMap::new(),
6470
};
@@ -71,6 +77,11 @@ impl Settings {
7177

7278
settings.vote_plans = vote_plans;
7379
let discrimination = settings.block0.blockchain_configuration.discrimination;
80+
81+
if let Some(explorer) = explorer {
82+
settings.explorer = Some(generate_explorer(&settings.nodes, explorer)?);
83+
}
84+
7485
settings.block0.initial.extend(
7586
fragments
7687
.iter()
@@ -198,4 +209,6 @@ pub enum Error {
198209
Settings(#[from] wallet::Error),
199210
#[error(transparent)]
200211
Committee(#[from] crate::builder::committee::Error),
212+
#[error(transparent)]
213+
Explorer(#[from] crate::builder::explorer::Error),
201214
}

testing/hersir/src/config/mod.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use crate::{
1414
builder::{Node, NodeAlias, Topology},
1515
error::Error,
1616
};
17-
use jormungandr_automation::jormungandr::{LogLevel, TestingDirectory};
17+
use jormungandr_automation::jormungandr::{
18+
explorer::configuration::ExplorerParams, LogLevel, PersistenceMode, TestingDirectory,
19+
};
1820
use serde::Deserialize;
1921
use std::{collections::HashSet, path::PathBuf, str::FromStr};
2022
pub use vote_plan::VotePlanTemplate;
@@ -23,6 +25,7 @@ pub use vote_plan::VotePlanTemplate;
2325
pub struct Config {
2426
pub blockchain: Blockchain,
2527
pub nodes: Vec<NodeConfig>,
28+
pub explorer: Option<ExplorerTemplate>,
2629
#[serde(default)]
2730
pub session: SessionSettings,
2831
pub wallets: Vec<WalletTemplate>,
@@ -108,6 +111,30 @@ fn default_title() -> String {
108111
"unnamed_scenario".to_owned()
109112
}
110113

114+
#[derive(Debug, Deserialize, Clone)]
115+
pub struct ExplorerTemplate {
116+
pub connect_to: NodeAlias,
117+
#[serde(default = "default_persistence_mode")]
118+
pub persistence_mode: PersistenceMode,
119+
pub address_bech32_prefix: Option<String>,
120+
pub query_depth_limit: Option<u64>,
121+
pub query_complexity_limit: Option<u64>,
122+
}
123+
124+
fn default_persistence_mode() -> PersistenceMode {
125+
PersistenceMode::InMemory
126+
}
127+
128+
impl ExplorerTemplate {
129+
pub fn to_explorer_params(&self) -> ExplorerParams {
130+
ExplorerParams {
131+
address_bech32_prefix: self.address_bech32_prefix.clone(),
132+
query_complexity_limit: self.query_complexity_limit,
133+
query_depth_limit: self.query_depth_limit,
134+
}
135+
}
136+
}
137+
111138
impl Default for SessionSettings {
112139
fn default() -> Self {
113140
Self {

testing/hersir/src/controller/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ pub enum Error {
6464
SettingsWallet(#[from] crate::builder::settings::wallet::Error),
6565
#[error(transparent)]
6666
Settings(#[from] crate::builder::settings::Error),
67+
#[error("no explorer configuration defined")]
68+
NoExplorerConfigurationDefined,
6769
}

testing/hersir/src/controller/interactive/args/explorer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl ExplorerTip {
3434
})?;
3535
println!(
3636
"{:#?}",
37-
node.explorer(ExplorerParams::default())
37+
node.explorer(ExplorerParams::default())?
3838
.client()
3939
.last_block()?
4040
);

testing/hersir/src/controller/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use interactive::{
1919
UserInteractionController,
2020
};
2121
use jormungandr_automation::jormungandr::{
22-
ConfiguredStarter, JormungandrParams, JormungandrProcess, LegacyNodeConfig,
22+
ConfiguredStarter, ExplorerProcess, JormungandrParams, JormungandrProcess, LegacyNodeConfig,
2323
LegacyNodeConfigConverter, LogLevel, NodeAlias, PersistenceMode, Starter, TestingDirectory,
2424
Version,
2525
};
@@ -197,6 +197,16 @@ impl Controller {
197197
builder.build()
198198
}
199199

200+
pub fn spawn_explorer(&mut self) -> Result<ExplorerProcess, Error> {
201+
ExplorerProcess::new(
202+
self.settings
203+
.explorer
204+
.clone()
205+
.ok_or(Error::NoExplorerConfigurationDefined)?,
206+
)
207+
.map_err(Into::into)
208+
}
209+
200210
pub fn spawn_node_async(&mut self, alias: &str) -> Result<JormungandrProcess, Error> {
201211
let mut starter = self.make_starter_for(
202212
SpawnParams::new(alias).persistence_mode(PersistenceMode::InMemory),

0 commit comments

Comments
 (0)