Skip to content

Use offsetted epochs in Mithril Aggregator/Signer #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mithril-aggregator/src/beacon_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ impl DumbImmutableFileObserver {
}
}

impl Default for DumbImmutableFileObserver {
fn default() -> Self {
Self::new()
}
}

#[async_trait]
impl ImmutableFileObserver for DumbImmutableFileObserver {
async fn get_last_immutable_number(&self) -> Result<u64, Box<dyn Error + Sync + Send>> {
Expand Down
49 changes: 25 additions & 24 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use chrono::prelude::*;
use hex::ToHex;
use slog_scope::{debug, warn};
use slog_scope::debug;
use thiserror::Error;

use mithril_common::crypto_helper::{
Expand All @@ -12,6 +12,7 @@ use mithril_common::crypto_helper::{
};
use mithril_common::entities;
use mithril_common::store::stake_store::{StakeStoreError, StakeStorer};
use mithril_common::SIGNER_EPOCH_RETRIEVAL_OFFSET;

use super::beacon_store::BeaconStoreError;
use super::dependency::{
Expand Down Expand Up @@ -259,7 +260,6 @@ impl MultiSigner for MultiSignerImpl {
/// Get stake distribution
async fn get_stake_distribution(&self) -> Result<ProtocolStakeDistribution, ProtocolError> {
debug!("Get stake distribution");
#[allow(unused_variables, clippy::identity_op)]
let epoch = self
.beacon_store
.read()
Expand All @@ -268,11 +268,7 @@ impl MultiSigner for MultiSignerImpl {
.await?
.ok_or_else(ProtocolError::UnavailableBeacon)?
.epoch
- 0; // TODO: Should be -1 or -2
warn!(
"Epoch computation is not final and needs to be fixed: {}",
epoch
);
- SIGNER_EPOCH_RETRIEVAL_OFFSET;
let signers = self
.stake_store
.read()
Expand All @@ -297,7 +293,6 @@ impl MultiSigner for MultiSignerImpl {
stakes: &ProtocolStakeDistribution,
) -> Result<(), ProtocolError> {
debug!("Update stake distribution to {:?}", stakes);
#[allow(unused_variables)]
let epoch = self
.beacon_store
.read()
Expand Down Expand Up @@ -325,7 +320,6 @@ impl MultiSigner for MultiSignerImpl {
party_id: ProtocolPartyId,
) -> Result<Option<ProtocolSignerVerificationKey>, ProtocolError> {
debug!("Get signer {}", party_id);
#[allow(clippy::identity_op)]
let epoch = self
.beacon_store
.read()
Expand All @@ -334,11 +328,7 @@ impl MultiSigner for MultiSignerImpl {
.await?
.ok_or_else(ProtocolError::UnavailableBeacon)?
.epoch
- 0; // TODO: Should be -1 or -2
warn!(
"Epoch computation is not final and needs to be fixed: {}",
epoch
);
- SIGNER_EPOCH_RETRIEVAL_OFFSET;
let signers = self
.verification_key_store
.read()
Expand All @@ -359,7 +349,6 @@ impl MultiSigner for MultiSignerImpl {
&self,
) -> Result<Vec<entities::SignerWithStake>, ProtocolError> {
debug!("Get signers with stake");
#[allow(clippy::identity_op)]
let epoch = self
.beacon_store
.read()
Expand All @@ -368,11 +357,7 @@ impl MultiSigner for MultiSignerImpl {
.await?
.ok_or_else(ProtocolError::UnavailableBeacon)?
.epoch
- 0; // TODO: Should be -1 or -2
warn!(
"Epoch computation is not final and needs to be fixed: {}",
epoch
);
- SIGNER_EPOCH_RETRIEVAL_OFFSET;
let signers = self
.verification_key_store
.read()
Expand Down Expand Up @@ -428,10 +413,7 @@ impl MultiSigner for MultiSignerImpl {
Some(_) => Err(ProtocolError::ExistingSigner()),
None => Ok(()),
};
// TODO: to remove once epoch offset is activated
if result.as_ref().ok().is_some() {
self.clerk = self.create_clerk().await?;
}

result
}

Expand All @@ -447,6 +429,11 @@ impl MultiSigner for MultiSignerImpl {
party_id, index
);

// TODO: to remove once epoch offset is activated
if self.clerk.as_ref().is_none() {
self.clerk = self.create_clerk().await?;
}

let message = &self
.get_current_message()
.await
Expand Down Expand Up @@ -641,6 +628,14 @@ mod tests {
)
}

async fn offset_epoch(multi_signer: &MultiSignerImpl, offset: i64) {
let mut beacon_store = multi_signer.beacon_store.write().await;
let mut beacon = beacon_store.get_current_beacon().await.unwrap().unwrap();
let epoch_new = beacon.epoch as i64 + offset;
beacon.epoch = epoch_new as u64;
beacon_store.set_current_beacon(beacon).await.unwrap();
}

#[tokio::test]
async fn test_multi_signer_current_message_ok() {
let mut multi_signer = setup_multi_signer().await;
Expand Down Expand Up @@ -689,6 +684,8 @@ mod tests {
.await
.expect("update stake distribution failed");

offset_epoch(&multi_signer, SIGNER_EPOCH_RETRIEVAL_OFFSET as i64).await;

let mut stake_distribution = multi_signer
.get_stake_distribution()
.await
Expand Down Expand Up @@ -725,6 +722,8 @@ mod tests {
.expect("register should have succeeded")
}

offset_epoch(&multi_signer, SIGNER_EPOCH_RETRIEVAL_OFFSET as i64).await;

let mut signers_with_stake_all_expected = Vec::new();
for (party_id, stake, verification_key_expected, _, _) in &signers {
let verification_key = multi_signer.get_signer(party_id.to_owned()).await;
Expand Down Expand Up @@ -798,6 +797,8 @@ mod tests {
.expect("register should have succeeded")
}

offset_epoch(&multi_signer, SIGNER_EPOCH_RETRIEVAL_OFFSET as i64).await;

let mut signatures = Vec::new();
for (party_id, _, _, protocol_signer, _) in &signers {
for i in 1..=protocol_parameters.m {
Expand Down
3 changes: 3 additions & 0 deletions mithril-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ pub mod fake_data;
pub mod store;

pub use entities::{CardanoNetwork, MagicId};

/// The epoch offset used for signers stake distribution and verification keys retrieval
pub const SIGNER_EPOCH_RETRIEVAL_OFFSET: u64 = 0; // TODO: Reactivate proper epoch offset with deployment of new certificate chain
10 changes: 3 additions & 7 deletions mithril-signer/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use slog_scope::{error, info, warn};
use slog_scope::{error, info};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -11,6 +11,7 @@ use mithril_common::crypto_helper::{key_encode_hex, Bytes};
use mithril_common::digesters::{Digester, DigesterError};
use mithril_common::entities::{self, Beacon, CertificatePending, Epoch, PartyId, SignerWithStake};
use mithril_common::store::stake_store::{StakeStore, StakeStoreError, StakeStorer};
use mithril_common::SIGNER_EPOCH_RETRIEVAL_OFFSET;

use super::certificate_handler::CertificateHandler;
use super::single_signer::SingleSigner;
Expand Down Expand Up @@ -201,12 +202,7 @@ impl Runtime {
.iter()
.map(|signer| (signer.party_id.to_owned(), signer.verification_key.as_str()))
.collect::<HashMap<PartyId, &str>>();
#[allow(clippy::identity_op)]
let epoch = pending_certificate.beacon.epoch - 0; // TODO: Should be -1 or -2
warn!(
"Epoch computation is not final and needs to be fixed: {}",
epoch
);
let epoch = pending_certificate.beacon.epoch - SIGNER_EPOCH_RETRIEVAL_OFFSET;
let stake_store = self.stake_store.read().await;
let stake_distribution = stake_store
.get_stakes(epoch)
Expand Down