Skip to content

Commit

Permalink
Merge pull request #650 from input-output-hk/jpraynaud/627-fix-protoc…
Browse files Browse the repository at this point in the history
…ol-parameters-transition

Fix protocol parameters transition
  • Loading branch information
jpraynaud authored Dec 14, 2022
2 parents d1c3eeb + 15493f2 commit d66c0ca
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 33 deletions.
33 changes: 20 additions & 13 deletions mithril-aggregator/src/http_server/routes/epoch_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,31 @@ mod handlers {
debug!("⇄ HTTP SERVER: epoch_settings");

match multi_signer.read().await.get_current_beacon().await {
Some(beacon) => match protocol_parameters_store
.get_protocol_parameters(beacon.epoch)
.await
{
Ok(Some(protocol_parameters)) => Ok(reply::json(
&EpochSettings {
epoch: beacon.epoch,
protocol_parameters,
},
StatusCode::OK,
)),
Ok(None) => {
Some(beacon) => match (
protocol_parameters_store
.get_protocol_parameters(beacon.epoch)
.await,
protocol_parameters_store
.get_protocol_parameters(beacon.epoch + 1)
.await,
) {
(Ok(Some(protocol_parameters)), Ok(Some(next_protocol_parameters))) => {
Ok(reply::json(
&EpochSettings {
epoch: beacon.epoch,
protocol_parameters,
next_protocol_parameters,
},
StatusCode::OK,
))
}
(Ok(None), Ok(Some(_))) | (Ok(Some(_)), Ok(None)) | (Ok(None), Ok(None)) => {
warn!("epoch_settings::could_not_retrieve_protocol_parameters");
Ok(reply::internal_server_error(
"could_not_retrieve_protocol_parameters".to_string(),
))
}
Err(err) => {
(Err(err), _) | (_, Err(err)) => {
warn!("epoch_settings::error"; "error" => ?err);
Ok(reply::internal_server_error(err.to_string()))
}
Expand Down
4 changes: 4 additions & 0 deletions mithril-common/src/entities/epoch_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ pub struct EpochSettings {
/// Current Protocol parameters
#[serde(rename = "protocol")]
pub protocol_parameters: ProtocolParameters,

/// Next Protocol parameters
#[serde(rename = "next_protocol")]
pub next_protocol_parameters: ProtocolParameters,
}
2 changes: 2 additions & 0 deletions mithril-common/src/fake_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ pub fn epoch_settings() -> entities::EpochSettings {

// Protocol parameters
let protocol_parameters = protocol_parameters();
let next_protocol_parameters = protocol_parameters.clone();

// Epoch settings
entities::EpochSettings {
epoch: beacon.epoch,
protocol_parameters,
next_protocol_parameters,
}
}

Expand Down
2 changes: 1 addition & 1 deletion mithril-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ pub const SIGNER_EPOCH_RECORDING_OFFSET: i64 = 1;
/// this is the same as the one in openapi.yml file.
/// If you want to update this version to reflect changes in the protocol,
/// please also update the entry in the openapi.yml
pub const MITHRIL_API_VERSION: &str = "0.0.1";
pub const MITHRIL_API_VERSION: &str = "0.1.0";
12 changes: 7 additions & 5 deletions mithril-explorer/components/EpochSettings/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useEffect, useState} from 'react';
import {Card, ListGroup} from "react-bootstrap";
import React, { useEffect, useState } from 'react';
import { Card, ListGroup } from "react-bootstrap";
import RawJsonButton from "../RawJsonButton";
import {useSelector} from "react-redux";
import { useSelector } from "react-redux";
import ProtocolParameters from "../ProtocolParameters";

export default function EpochSettings(props) {
Expand Down Expand Up @@ -36,7 +36,7 @@ export default function EpochSettings(props) {
<div>
<h2>
Epoch Settings
<RawJsonButton href={`${aggregator}/epoch-settings`} variant="outline-light" size="sm"/>
<RawJsonButton href={`${aggregator}/epoch-settings`} variant="outline-light" size="sm" />
</h2>

<Card>
Expand All @@ -46,7 +46,9 @@ export default function EpochSettings(props) {
<ListGroup.Item>{epochSettings.epoch}</ListGroup.Item>
</ListGroup>
<Card.Title>Protocol Parameters</Card.Title>
<ProtocolParameters protocolParameters={epochSettings.protocol}/>
<ProtocolParameters protocolParameters={epochSettings.protocol} />
<Card.Title>Next Protocol Parameters</Card.Title>
<ProtocolParameters protocolParameters={epochSettings.next_protocol} />
</Card.Body>
</Card>
</div>
Expand Down
3 changes: 2 additions & 1 deletion mithril-signer/src/runtime/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl StateMachine {
let beacon = self.runner.get_current_beacon().await?;
self.runner.update_stake_distribution(beacon.epoch).await?;
self.runner
.register_signer_to_aggregator(beacon.epoch, &epoch_settings.protocol_parameters)
.register_signer_to_aggregator(beacon.epoch, &epoch_settings.next_protocol_parameters)
.await?;

Ok(RegisteredState { beacon })
Expand Down Expand Up @@ -304,6 +304,7 @@ mod tests {
let epoch_settings = EpochSettings {
epoch: Epoch(3),
protocol_parameters: fake_data::protocol_parameters(),
next_protocol_parameters: fake_data::protocol_parameters(),
};
let known_epoch = Epoch(4);
runner
Expand Down
45 changes: 38 additions & 7 deletions mithril-test-lab/mithril-end-to-end/src/end_to_end_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::utils::AttemptResult;
use crate::{attempt, Aggregator, Client, ClientCommand, Devnet, MithrilInfrastructure};
use mithril_common::chain_observer::{CardanoCliChainObserver, ChainObserver};
use mithril_common::digesters::ImmutableFile;
use mithril_common::entities::{Certificate, Epoch, EpochSettings, Snapshot};
use mithril_common::entities::{Certificate, Epoch, EpochSettings, ProtocolParameters, Snapshot};
use reqwest::StatusCode;
use slog_scope::{info, warn};
use std::error::Error;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl Spec {
bootstrap_genesis_certificate(self.infrastructure.aggregator_mut()).await?;
wait_for_epoch_settings(&aggregator_endpoint).await?;

// Wait 2 epochs before changing stake distribution, so that we use at least once original stake distribution
// Wait 2 epochs before changing stake distribution, so that we use at least one original stake distribution
target_epoch += 2;
wait_for_target_epoch(
self.infrastructure.chain_observer(),
Expand All @@ -51,12 +51,22 @@ impl Spec {
.await?;
delegate_stakes_to_pools(self.infrastructure.devnet()).await?;

// Wait 5 epochs after stake delegation, so that we make sure that we use new stake distribution a few times
target_epoch += 5;
// Wait 2 epochs before changing protocol parameters
target_epoch += 2;
wait_for_target_epoch(
self.infrastructure.chain_observer(),
target_epoch,
"epoch after which the certificate chain will be long enough to catch most common troubles".to_string(),
"epoch after which the protocol parameters will change".to_string(),
)
.await?;
update_protocol_parameters(self.infrastructure.aggregator_mut()).await?;

// Wait 4 epochs after protocol parameters update, so that we make sure that we use new protocol parameters as well as new stake distribution a few times
target_epoch += 4;
wait_for_target_epoch(
self.infrastructure.chain_observer(),
target_epoch,
"epoch after which the certificate chain will be long enough to catch most common troubles with stake distribution and protocol parameters".to_string(),
)
.await?;

Expand Down Expand Up @@ -177,7 +187,7 @@ async fn wait_for_target_epoch(
}
}) {
AttemptResult::Ok(_) => {
info!("Target epoch reached !"; "target_epoch" => ?target_epoch);
info!("Target epoch reached!"; "target_epoch" => ?target_epoch);
Ok(())
}
AttemptResult::Err(error) => Err(error),
Expand All @@ -194,7 +204,7 @@ async fn bootstrap_genesis_certificate(aggregator: &mut Aggregator) -> Result<()

info!("> stopping aggregator");
aggregator.stop().await?;
info!("> bootstrapping genesis using signers registered two epochs ago ...");
info!("> bootstrapping genesis using signers registered two epochs ago...");
aggregator.bootstrap_genesis().await?;
info!("> done, restarting aggregator");
aggregator.serve()?;
Expand All @@ -210,6 +220,27 @@ async fn delegate_stakes_to_pools(devnet: &Devnet) -> Result<(), String> {
Ok(())
}

async fn update_protocol_parameters(aggregator: &mut Aggregator) -> Result<(), String> {
info!("Update protocol parameters");

info!("> stopping aggregator");
aggregator.stop().await?;
let protocol_parameters_new = ProtocolParameters {
k: 150,
m: 200,
phi_f: 0.95,
};
info!(
"> updating protocol parameters to {:?}...",
protocol_parameters_new
);
aggregator.set_protocol_parameters(&protocol_parameters_new);
info!("> done, restarting aggregator");
aggregator.serve()?;

Ok(())
}

async fn assert_node_producing_snapshot(aggregator_endpoint: &str) -> Result<String, String> {
let url = format!("{}/snapshots", aggregator_endpoint);
info!("Waiting for the aggregator to produce a snapshot");
Expand Down
19 changes: 16 additions & 3 deletions mithril-test-lab/mithril-end-to-end/src/mithril/aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::devnet::BftNode;
use crate::utils::MithrilCommand;
use crate::DEVNET_MAGIC_ID;
use mithril_common::entities;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::process::Child;
Expand All @@ -25,9 +26,6 @@ impl Aggregator {
let server_port_parameter = server_port.to_string();
let env = HashMap::from([
("NETWORK", "devnet"),
("PROTOCOL_PARAMETERS__K", "75"),
("PROTOCOL_PARAMETERS__M", "100"),
("PROTOCOL_PARAMETERS__PHI_F", "0.95"),
("RUN_INTERVAL", "800"),
("SERVER_IP", "0.0.0.0"),
("SERVER_PORT", &server_port_parameter),
Expand Down Expand Up @@ -105,6 +103,21 @@ impl Aggregator {
Ok(())
}

pub fn set_protocol_parameters(&mut self, protocol_parameters: &entities::ProtocolParameters) {
self.command.set_env_var(
"PROTOCOL_PARAMETERS__K",
&format!("{}", protocol_parameters.k),
);
self.command.set_env_var(
"PROTOCOL_PARAMETERS__M",
&format!("{}", protocol_parameters.m),
);
self.command.set_env_var(
"PROTOCOL_PARAMETERS__PHI_F",
&format!("{}", protocol_parameters.phi_f),
);
}

pub async fn tail_logs(&self, number_of_line: u64) -> Result<(), String> {
self.command.tail_logs(None, number_of_line).await
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{Aggregator, Client, Devnet, Signer, DEVNET_MAGIC_ID};
use mithril_common::chain_observer::{CardanoCliChainObserver, CardanoCliRunner};
use mithril_common::entities::ProtocolParameters;
use mithril_common::CardanoNetwork;
use std::borrow::BorrowMut;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -35,6 +36,11 @@ impl MithrilInfrastructure {
work_dir,
bin_dir,
)?;
aggregator.set_protocol_parameters(&ProtocolParameters {
k: 75,
m: 100,
phi_f: 0.95,
});
aggregator.serve()?;

let mut signers: Vec<Signer> = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl MithrilCommand {
self.log_path = self.work_dir.join(format!("{}.log", name));
}

pub fn set_env_var(&mut self, name: &str, value: &str) {
self.env_vars.insert(name.to_string(), value.to_string());
}

pub fn start(&mut self, args: &[String]) -> Result<Child, String> {
let args = [&self.default_args, args].concat();

Expand Down
11 changes: 8 additions & 3 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
# `mithril-aggregator/src/http_server/mod.rs` file. If you plan to update it
# here to reflect changes in the API, please also update the constant in the
# Rust file.
version: 0.0.1
version: 0.1.0
title: Mithril Aggregator Server
description: |
The REST API provided by a Mithril Aggregator Node in a Mithril network.
Expand All @@ -28,7 +28,8 @@ paths:
summary: Get current epoch settings
description: |
Returns the information related to the current epoch:
* protocol parameters for current epoch (to setup cryptography, allowing signers to register)
* protocol parameters for current epoch
* protocol parameters for next epoch (to setup cryptography, allowing signers to register)
responses:
"200":
description: epoch settings found
Expand Down Expand Up @@ -238,17 +239,21 @@ components:
required:
- epoch
- protocol
- next_protocol
properties:
epoch:
description: Cardano chain epoch number
type: integer
format: int64
protocol:
$ref: "#/components/schemas/ProtocolParameters"
next_protocol:
$ref: "#/components/schemas/ProtocolParameters"
example:
{
"epoch": 329,
"protocol": { "k": 857, "m": 6172, "phi_f": 0.2 }
"protocol": { "k": 857, "m": 6172, "phi_f": 0.2 },
"next_protocol": { "k": 2422, "m": 20973, "phi_f": 0.2 }
}
ProtocolParameters:
description: Protocol cryptographic parameters
Expand Down

0 comments on commit d66c0ca

Please sign in to comment.