Skip to content

Commit a8fdf56

Browse files
committed
Use a mock stake distribution when calling cardano-cli
1 parent 39f7b1c commit a8fdf56

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

mithril-common/src/test_utils/mithril_fixture.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use std::{
2+
collections::HashMap,
23
path::{Path, PathBuf},
34
sync::Arc,
45
};
56

7+
use serde::{Deserialize, Serialize};
8+
69
use crate::{
710
certificate_chain::CertificateGenesisProducer,
811
crypto_helper::{
@@ -12,7 +15,7 @@ use crate::{
1215
},
1316
entities::{
1417
Beacon, Certificate, HexEncodedAgregateVerificationKey, PartyId, ProtocolMessage,
15-
ProtocolParameters, Signer, SignerWithStake, SingleSignatures, StakeDistribution,
18+
ProtocolParameters, Signer, SignerWithStake, SingleSignatures, Stake, StakeDistribution,
1619
},
1720
protocol::SignerBuilder,
1821
};
@@ -52,6 +55,25 @@ impl From<&SignerFixture> for SignerWithStake {
5255
}
5356
}
5457

58+
/// Represent the output of the `stake-snapshot` command of the cardano-cli
59+
#[derive(Debug, Clone, Serialize, Deserialize)]
60+
pub struct CardanoCliStakeDistribution {
61+
#[serde(rename = "pools")]
62+
pub signers: HashMap<String, CardanoCliSignerStake>,
63+
}
64+
65+
/// Represent the stakes of a party in the output of the `stake-snapshot`
66+
/// command of the cardano-cli
67+
#[derive(Debug, Clone, Serialize, Deserialize)]
68+
pub struct CardanoCliSignerStake {
69+
#[serde(rename = "stakeMark")]
70+
actual_stake: Stake,
71+
#[serde(rename = "stakeSet")]
72+
previous_stake: Stake,
73+
#[serde(rename = "stakeGo")]
74+
penultimate_stake: Stake,
75+
}
76+
5577
impl MithrilFixture {
5678
/// [MithrilFixture] factory.
5779
pub fn new(
@@ -104,6 +126,24 @@ impl MithrilFixture {
104126
self.stake_distribution.clone()
105127
}
106128

129+
/// Get the stake distribution formated as a cardano-cli `stake-snapshot` output.
130+
///
131+
/// Note: will fail if the signers certification was disabled
132+
pub fn cardano_cli_stake_distribution(&self) -> CardanoCliStakeDistribution {
133+
let signers = HashMap::from_iter(self.signers_fixture().into_iter().map(|signer| {
134+
(
135+
signer.compute_protocol_party_id_as_hash(),
136+
CardanoCliSignerStake {
137+
actual_stake: signer.signer_with_stake.stake,
138+
previous_stake: signer.signer_with_stake.stake,
139+
penultimate_stake: signer.signer_with_stake.stake,
140+
},
141+
)
142+
}));
143+
144+
CardanoCliStakeDistribution { signers }
145+
}
146+
107147
/// Compute the Aggregate Verification Key for this fixture.
108148
pub fn compute_avk(&self) -> ProtocolAggregateVerificationKey {
109149
SignerBuilder::new(&self.signers_with_stake(), &self.protocol_parameters)
@@ -167,6 +207,20 @@ impl SignerFixture {
167207
}
168208
}
169209

210+
/// Compute the party id hash
211+
///
212+
/// Note: will fail if the signers certification was disabled
213+
pub fn compute_protocol_party_id_as_hash(&self) -> String {
214+
self.operational_certificate()
215+
.unwrap()
216+
.compute_protocol_party_id_as_hash()
217+
}
218+
219+
/// Decode this signer verification key certificate
220+
pub fn verification_key(&self) -> ProtocolSignerVerificationKey {
221+
key_decode_hex(&self.signer_with_stake.verification_key).unwrap()
222+
}
223+
170224
/// Decode this signer verification key signature certificate if any
171225
pub fn verification_key_signature(&self) -> Option<ProtocolSignerVerificationKeySignature> {
172226
self.signer_with_stake

mithril-test-lab/mithril-end-to-end/script/mock-cardano-cli

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,7 @@ main () {
2020
done
2121

2222
case $command_to_run in
23-
stake-snapshot) cat <<EOF
24-
{
25-
"pools": {
26-
"d9899c574fd7a710732391706b59e878bfd416214c49d2b3841c5c8b": {
27-
"stakeGo": 300000000000,
28-
"stakeMark": 300000000001,
29-
"stakeSet": 300000000002
30-
}
31-
},
32-
"total": {
33-
"stakeGo": 300000000000,
34-
"stakeMark": 300000000001,
35-
"stakeSet": 300000000002
36-
}
37-
}
38-
EOF
39-
;;
23+
stake-snapshot) cat $STAKE_DISTRIBUTION_FILE ;;
4024
chain-tip) cat <<EOF
4125
{
4226
"era": "Babbage",

mithril-test-lab/mithril-end-to-end/src/bin/load-aggregator/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::PathBuf, sync::Arc, time::Duration};
1+
use std::{fs::File, path::PathBuf, sync::Arc, time::Duration};
22

33
use anyhow::Context;
44
use clap::Parser;
@@ -103,7 +103,7 @@ pub struct MainOpts {
103103
pub aggregator_dir: PathBuf,
104104

105105
/// Number of concurrent signers
106-
#[arg(long, default_value = "2")]
106+
#[arg(long, default_value = "100")]
107107
pub num_signers: usize,
108108

109109
/// Mithril technical Era
@@ -204,6 +204,7 @@ async fn main() -> StdResult<()> {
204204
let opts = MainOpts::parse();
205205
let _logger = init_logger(&opts);
206206
let args = AggregatorParameters::new(&opts)?;
207+
let mock_stake_distribution_file_path = args.work_dir.join("stake_distribution.json");
207208
info!(">> Starting stress test with options: {opts:?}");
208209

209210
info!(">> Creation of the Signer Key Registrations payloads");
@@ -221,6 +222,15 @@ async fn main() -> StdResult<()> {
221222
)
222223
.unwrap();
223224

225+
let mock_stake_distribution_file = File::create(&mock_stake_distribution_file_path).unwrap();
226+
serde_json::to_writer(
227+
&mock_stake_distribution_file,
228+
&signers_fixture.cardano_cli_stake_distribution(),
229+
)
230+
.expect("Writing the stake distribution into a file for the mock cardano cli failed");
231+
232+
aggregator.change_run_interval(Duration::from_secs(6));
233+
aggregator.set_mock_cardano_cli_stake_distribution_file(&mock_stake_distribution_file_path);
224234
aggregator.set_protocol_parameters(&ProtocolParameters::default());
225235
aggregator.serve().unwrap();
226236

@@ -270,7 +280,7 @@ async fn main() -> StdResult<()> {
270280
}
271281
}
272282

273-
assert_eq!(opts.num_signers - 1, errors);
283+
assert_eq!(0, errors);
274284

275285
// ensure POSTing payload gives 200
276286
aggregator.stop().await.unwrap();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::DEVNET_MAGIC_ID;
44
use mithril_common::entities;
55
use std::collections::HashMap;
66
use std::path::{Path, PathBuf};
7+
use std::time::Duration;
78
use tokio::process::Child;
89

910
#[derive(Debug)]
@@ -123,6 +124,17 @@ impl Aggregator {
123124
);
124125
}
125126

127+
pub fn set_mock_cardano_cli_stake_distribution_file(&mut self, file_path: &Path) {
128+
self.command
129+
.set_env_var("STAKE_DISTRIBUTION_FILE", file_path.to_str().unwrap())
130+
}
131+
132+
/// Change the run interval of the aggregator state machine (default: 400ms)
133+
pub fn change_run_interval(&mut self, interval: Duration) {
134+
self.command
135+
.set_env_var("RUN_INTERVAL", &format!("{}", interval.as_millis()))
136+
}
137+
126138
pub async fn tail_logs(&self, number_of_line: u64) -> Result<(), String> {
127139
self.command.tail_logs(None, number_of_line).await
128140
}

0 commit comments

Comments
 (0)