Skip to content

Commit a2c6fde

Browse files
committed
feat: aggregator and signer get Mithril era from Cardano chain
1 parent a6acb44 commit a2c6fde

File tree

7 files changed

+180
-40
lines changed

7 files changed

+180
-40
lines changed

mithril-test-lab/mithril-end-to-end/src/assertions/exec.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use crate::{Aggregator, Devnet};
24
use mithril_common::entities::ProtocolParameters;
35
use mithril_common::StdResult;
@@ -16,6 +18,23 @@ pub async fn bootstrap_genesis_certificate(aggregator: &mut Aggregator) -> StdRe
1618
Ok(())
1719
}
1820

21+
pub async fn register_era_marker(aggregator: &mut Aggregator, devnet: &Devnet) -> StdResult<()> {
22+
info!("Register era marker");
23+
24+
info!("> generating era marker tx datum...");
25+
let tx_datum_file_path = devnet
26+
.artifacts_dir
27+
.join(PathBuf::from("era-tx-datum.txt".to_string()));
28+
aggregator
29+
.era_generate_tx_datum(&tx_datum_file_path)
30+
.await?;
31+
32+
info!("> writing era marker on the Cardano chain...");
33+
devnet.write_era_marker(&tx_datum_file_path).await?;
34+
35+
Ok(())
36+
}
37+
1938
pub async fn delegate_stakes_to_pools(devnet: &Devnet, delegation_round: u16) -> StdResult<()> {
2039
info!("Delegate stakes to the cardano pools");
2140

mithril-test-lab/mithril-end-to-end/src/devnet/runner.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use anyhow::{anyhow, Context};
22
use mithril_common::entities::PartyId;
33
use mithril_common::StdResult;
44
use slog_scope::info;
5-
use std::fs;
6-
use std::path::PathBuf;
5+
use std::fs::{self, File};
6+
use std::io::Read;
7+
use std::path::{Path, PathBuf};
78
use std::process::Stdio;
89
use tokio::process::Command;
910

1011
#[derive(Debug, Clone, Default)]
1112
pub struct Devnet {
12-
artifacts_dir: PathBuf,
13+
pub artifacts_dir: PathBuf,
1314
number_of_bft_nodes: u8,
1415
number_of_pool_nodes: u8,
1516
}
@@ -147,6 +148,22 @@ impl Devnet {
147148
}
148149
}
149150

151+
pub fn mithril_era_marker_address_path(&self) -> PathBuf {
152+
self.artifacts_dir
153+
.join("addresses")
154+
.join("mithril-era.addr")
155+
}
156+
157+
pub fn mithril_era_marker_address(&self) -> StdResult<String> {
158+
let mut mithril_era_marker_address_file =
159+
File::open(self.mithril_era_marker_address_path())?;
160+
let mut mithril_era_marker_address_buffer = Vec::new();
161+
mithril_era_marker_address_file.read_to_end(&mut mithril_era_marker_address_buffer)?;
162+
163+
String::from_utf8(mithril_era_marker_address_buffer)
164+
.with_context(|| "Failed to read mithril era marker address file")
165+
}
166+
150167
pub fn cardano_cli_path(&self) -> PathBuf {
151168
self.artifacts_dir.join("cardano-cli")
152169
}
@@ -252,6 +269,32 @@ impl Devnet {
252269
None => Err(anyhow!("Delegating stakes terminated by signal")),
253270
}
254271
}
272+
273+
pub async fn write_era_marker(&self, target_path: &Path) -> StdResult<()> {
274+
let run_script = "era-mithril.sh";
275+
let run_script_path = self.artifacts_dir.join(run_script);
276+
let mut run_command = Command::new(&run_script_path);
277+
run_command
278+
.current_dir(&self.artifacts_dir)
279+
.kill_on_drop(true);
280+
run_command.env("DATUM_FILE", target_path.to_str().unwrap());
281+
282+
info!("Writing era marker on chain"; "script" => &run_script_path.display());
283+
284+
let status = run_command
285+
.spawn()
286+
.with_context(|| "Failed to write era marker on chain")?
287+
.wait()
288+
.await
289+
.with_context(|| "Error while writin era marker on chain")?;
290+
match status.code() {
291+
Some(0) => Ok(()),
292+
Some(code) => Err(anyhow!(
293+
"Write era marker on chain exited with status code: {code}"
294+
)),
295+
None => Err(anyhow!("Write era marker on chain terminated by signal")),
296+
}
297+
}
255298
}
256299

257300
#[cfg(test)]

mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::devnet::BftNode;
22
use crate::utils::MithrilCommand;
3-
use crate::DEVNET_MAGIC_ID;
3+
use crate::{
4+
DEVNET_MAGIC_ID, ERA_MARKERS_SECRET_KEY, ERA_MARKERS_VERIFICATION_KEY, GENESIS_SECRET_KEY,
5+
GENESIS_VERIFICATION_KEY,
6+
};
47
use anyhow::{anyhow, Context};
58
use mithril_common::{entities, StdResult};
69
use std::collections::HashMap;
@@ -16,6 +19,7 @@ pub struct AggregatorConfig<'a> {
1619
pub work_dir: &'a Path,
1720
pub bin_dir: &'a Path,
1821
pub mithril_era: &'a str,
22+
pub mithril_era_marker_address: &'a str,
1923
pub signed_entity_types: &'a [String],
2024
pub chain_observer_type: &'a str,
2125
}
@@ -33,8 +37,8 @@ impl Aggregator {
3337
let magic_id = DEVNET_MAGIC_ID.to_string();
3438
let server_port_parameter = aggregator_config.server_port.to_string();
3539
let era_reader_adapter_params = format!(
36-
r#"{{"markers": [{{"name": "{}", "epoch": 0}}]}}"#,
37-
aggregator_config.mithril_era
40+
r#"{{"address": "{}", "verification_key": "{}"}}"#,
41+
aggregator_config.mithril_era_marker_address, ERA_MARKERS_VERIFICATION_KEY
3842
);
3943
let signed_entity_types = aggregator_config.signed_entity_types.join(",");
4044
let env = HashMap::from([
@@ -52,10 +56,13 @@ impl Aggregator {
5256
aggregator_config.bft_node.socket_path.to_str().unwrap(),
5357
),
5458
("STORE_RETENTION_LIMIT", "10"),
55-
("CARDANO_CLI_PATH", aggregator_config.cardano_cli_path.to_str().unwrap()),
56-
("GENESIS_VERIFICATION_KEY", "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d"),
57-
("GENESIS_SECRET_KEY", "5b3131382c3138342c3232342c3137332c3136302c3234312c36312c3134342c36342c39332c3130362c3232392c38332c3133342c3138392c34302c3138392c3231302c32352c3138342c3136302c3134312c3233372c32362c3136382c35342c3233392c3230342c3133392c3131392c31332c3139395d"),
58-
("ERA_READER_ADAPTER_TYPE", "dummy"),
59+
(
60+
"CARDANO_CLI_PATH",
61+
aggregator_config.cardano_cli_path.to_str().unwrap(),
62+
),
63+
("GENESIS_VERIFICATION_KEY", GENESIS_VERIFICATION_KEY),
64+
("GENESIS_SECRET_KEY", GENESIS_SECRET_KEY),
65+
("ERA_READER_ADAPTER_TYPE", "cardano-chain"),
5966
("ERA_READER_ADAPTER_PARAMS", &era_reader_adapter_params),
6067
("SIGNED_ENTITY_TYPES", &signed_entity_types),
6168
("CARDANO_NODE_VERSION", "8.7.3"),
@@ -137,6 +144,40 @@ impl Aggregator {
137144
Ok(())
138145
}
139146

147+
// TODO: This works only for the current epoch, and needs to be fixed
148+
pub async fn era_generate_tx_datum(&mut self, target_path: &Path) -> StdResult<()> {
149+
let exit_status = self
150+
.command
151+
.start(&[
152+
"era".to_string(),
153+
"generate-tx-datum".to_string(),
154+
"--current-era-epoch".to_string(),
155+
"0".to_string(),
156+
"--era-markers-secret-key".to_string(),
157+
ERA_MARKERS_SECRET_KEY.to_string(),
158+
"--target-path".to_string(),
159+
target_path.to_str().unwrap().to_string(),
160+
])?
161+
.wait()
162+
.await
163+
.with_context(|| "`mithril-aggregator era generate-tx-datum` crashed")?;
164+
165+
if exit_status.success() {
166+
Ok(())
167+
} else {
168+
Err(match exit_status.code() {
169+
Some(c) => {
170+
anyhow!("`mithril-aggregator era generate-tx-datum` exited with code: {c}")
171+
}
172+
None => {
173+
anyhow!(
174+
"`mithril-aggregator era generate-tx-datum` was terminated with a signal"
175+
)
176+
}
177+
})
178+
}
179+
}
180+
140181
pub fn set_protocol_parameters(&mut self, protocol_parameters: &entities::ProtocolParameters) {
141182
self.command.set_env_var(
142183
"PROTOCOL_PARAMETERS__K",

mithril-test-lab/mithril-end-to-end/src/mithril/infrastructure.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
Aggregator, AggregatorConfig, Client, Devnet, RelayAggregator, RelaySigner, Signer,
2+
assertions, Aggregator, AggregatorConfig, Client, Devnet, RelayAggregator, RelaySigner, Signer,
33
DEVNET_MAGIC_ID,
44
};
55
use anyhow::anyhow;
@@ -13,6 +13,8 @@ use std::borrow::BorrowMut;
1313
use std::path::PathBuf;
1414
use std::sync::Arc;
1515

16+
use super::signer::SignerConfig;
17+
1618
pub struct MithrilInfrastructureConfig {
1719
pub server_port: u64,
1820
pub devnet: Devnet,
@@ -52,6 +54,7 @@ impl MithrilInfrastructure {
5254
work_dir: &config.work_dir,
5355
bin_dir: &config.bin_dir,
5456
mithril_era: &config.mithril_era,
57+
mithril_era_marker_address: &config.devnet.mithril_era_marker_address()?,
5558
signed_entity_types: &config.signed_entity_types,
5659
chain_observer_type,
5760
})?;
@@ -60,6 +63,7 @@ impl MithrilInfrastructure {
6063
m: 100,
6164
phi_f: 0.95,
6265
});
66+
assertions::register_era_marker(&mut aggregator, &config.devnet).await?;
6367
aggregator.serve()?;
6468

6569
let mut relay_aggregators: Vec<RelayAggregator> = vec![];
@@ -104,15 +108,16 @@ impl MithrilInfrastructure {
104108
} else {
105109
aggregator.endpoint()
106110
};
107-
let mut signer = Signer::new(
111+
let mut signer = Signer::new(&SignerConfig {
108112
aggregator_endpoint,
109113
pool_node,
110-
&config.devnet.cardano_cli_path(),
111-
&config.work_dir,
112-
&config.bin_dir,
113-
&config.mithril_era,
114+
cardano_cli_path: &config.devnet.cardano_cli_path(),
115+
work_dir: &config.work_dir,
116+
bin_dir: &config.bin_dir,
117+
mithril_era: &config.mithril_era,
118+
mithril_era_marker_address: &config.devnet.mithril_era_marker_address()?,
114119
enable_certification,
115-
)?;
120+
})?;
116121
signer.start()?;
117122

118123
signers.push(signer);

mithril-test-lab/mithril-end-to-end/src/mithril/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ pub use relay_signer::RelaySigner;
1313
pub use signer::Signer;
1414

1515
pub const DEVNET_MAGIC_ID: mithril_common::MagicId = 42;
16+
17+
pub const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
18+
pub const GENESIS_SECRET_KEY: &str = "5b3131382c3138342c3232342c3137332c3136302c3234312c36312c3134342c36342c39332c3130362c3232392c38332c3133342c3138392c34302c3138392c3231302c32352c3138342c3136302c3134312c3233372c32362c3136382c35342c3233392c3230342c3133392c3131392c31332c3139395d";
19+
pub const ERA_MARKERS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
20+
pub const ERA_MARKERS_SECRET_KEY: &str = "5b3131382c3138342c3232342c3137332c3136302c3234312c36312c3134342c36342c39332c3130362c3232392c38332c3133342c3138392c34302c3138392c3231302c32352c3138342c3136302c3134312c3233372c32362c3136382c35342c3233392c3230342c3133392c3131392c31332c3139395d";

mithril-test-lab/mithril-end-to-end/src/mithril/signer.rs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
use crate::devnet::PoolNode;
22
use crate::utils::MithrilCommand;
3-
use crate::DEVNET_MAGIC_ID;
3+
use crate::{DEVNET_MAGIC_ID, ERA_MARKERS_VERIFICATION_KEY};
44
use mithril_common::entities::PartyId;
55
use mithril_common::StdResult;
66
use std::collections::HashMap;
77
use std::path::Path;
88
use tokio::process::Child;
99

10+
#[derive(Debug)]
11+
pub struct SignerConfig<'a> {
12+
pub aggregator_endpoint: String,
13+
pub pool_node: &'a PoolNode,
14+
pub cardano_cli_path: &'a Path,
15+
pub work_dir: &'a Path,
16+
pub bin_dir: &'a Path,
17+
pub mithril_era: &'a str,
18+
pub mithril_era_marker_address: &'a str,
19+
pub enable_certification: bool,
20+
}
21+
1022
#[derive(Debug)]
1123
pub struct Signer {
1224
party_id: PartyId,
@@ -15,50 +27,64 @@ pub struct Signer {
1527
}
1628

1729
impl Signer {
18-
pub fn new(
19-
aggregator_endpoint: String,
20-
pool_node: &PoolNode,
21-
cardano_cli_path: &Path,
22-
work_dir: &Path,
23-
bin_dir: &Path,
24-
mithril_era: &str,
25-
enable_certification: bool,
26-
) -> StdResult<Self> {
27-
let party_id = pool_node.party_id()?;
30+
pub fn new(signer_config: &SignerConfig) -> StdResult<Self> {
31+
let party_id = signer_config.pool_node.party_id()?;
2832
let magic_id = DEVNET_MAGIC_ID.to_string();
2933
let data_stores_path = format!("./stores/signer-{party_id}");
30-
let era_reader_adapater_params =
31-
format!(r#"{{"markers": [{{"name": "{mithril_era}", "epoch": 0}}]}}"#);
34+
let era_reader_adapter_params = format!(
35+
r#"{{"address": "{}", "verification_key": "{}"}}"#,
36+
signer_config.mithril_era_marker_address, ERA_MARKERS_VERIFICATION_KEY
37+
);
3238
let mut env = HashMap::from([
3339
("NETWORK", "devnet"),
3440
("RUN_INTERVAL", "100"),
35-
("AGGREGATOR_ENDPOINT", &aggregator_endpoint),
36-
("DB_DIRECTORY", pool_node.db_path.to_str().unwrap()),
41+
("AGGREGATOR_ENDPOINT", &signer_config.aggregator_endpoint),
42+
(
43+
"DB_DIRECTORY",
44+
signer_config.pool_node.db_path.to_str().unwrap(),
45+
),
3746
("DATA_STORES_DIRECTORY", &data_stores_path),
3847
("NETWORK_MAGIC", &magic_id),
3948
(
4049
"CARDANO_NODE_SOCKET_PATH",
41-
pool_node.socket_path.to_str().unwrap(),
50+
signer_config.pool_node.socket_path.to_str().unwrap(),
51+
),
52+
(
53+
"CARDANO_CLI_PATH",
54+
signer_config.cardano_cli_path.to_str().unwrap(),
4255
),
43-
("CARDANO_CLI_PATH", cardano_cli_path.to_str().unwrap()),
44-
("ERA_READER_ADAPTER_TYPE", "dummy"),
45-
("ERA_READER_ADAPTER_PARAMS", &era_reader_adapater_params),
56+
("ERA_READER_ADAPTER_TYPE", "cardano-chain"),
57+
("ERA_READER_ADAPTER_PARAMS", &era_reader_adapter_params),
4658
]);
47-
if enable_certification {
59+
if signer_config.enable_certification {
4860
env.insert(
4961
"KES_SECRET_KEY_PATH",
50-
pool_node.kes_secret_key_path.to_str().unwrap(),
62+
signer_config
63+
.pool_node
64+
.kes_secret_key_path
65+
.to_str()
66+
.unwrap(),
5167
);
5268
env.insert(
5369
"OPERATIONAL_CERTIFICATE_PATH",
54-
pool_node.operational_certificate_path.to_str().unwrap(),
70+
signer_config
71+
.pool_node
72+
.operational_certificate_path
73+
.to_str()
74+
.unwrap(),
5575
);
5676
} else {
5777
env.insert("PARTY_ID", &party_id);
5878
}
5979
let args = vec!["-vvv"];
6080

61-
let mut command = MithrilCommand::new("mithril-signer", work_dir, bin_dir, env, &args)?;
81+
let mut command = MithrilCommand::new(
82+
"mithril-signer",
83+
signer_config.work_dir,
84+
signer_config.bin_dir,
85+
env,
86+
&args,
87+
)?;
6288
command.set_log_name(format!("mithril-signer-{party_id}").as_str());
6389

6490
Ok(Self {

mithril-test-lab/mithril-end-to-end/src/stress_test/aggregator_helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub async fn bootstrap_aggregator(
2525
work_dir: &args.work_dir,
2626
bin_dir: &args.bin_dir,
2727
mithril_era: &args.mithril_era,
28+
mithril_era_marker_address: "",
2829
signed_entity_types: &signed_entity_types,
2930
chain_observer_type,
3031
})

0 commit comments

Comments
 (0)