Skip to content

Remove stakes in Mithril Aggregator pending certificate #258

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 7 commits into from
Jun 13, 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
4 changes: 2 additions & 2 deletions mithril-aggregator/src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ mod tests {
#[tokio::test]
async fn test_certificate_pending_get_ok() {
let fake_protocol_parameters = fake_data::protocol_parameters();
let fake_signers_with_stakes = fake_data::signers_with_stakes(5);
let fake_signers = fake_data::signers(5);
let method = Method::GET.as_str();
let path = "/certificate-pending";
let mut beacon_store = MockBeaconStore::new();
Expand All @@ -570,7 +570,7 @@ mod tests {
.return_once(|| Some(fake_protocol_parameters.into()));
mock_multi_signer
.expect_get_signers()
.return_once(|| Ok(fake_signers_with_stakes));
.return_once(|| Ok(fake_signers));
mock_multi_signer
.expect_get_multi_signature()
.return_once(|| Ok(None));
Expand Down
40 changes: 33 additions & 7 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,18 @@ pub trait MultiSigner: Sync + Send {
) -> Result<Option<ProtocolSignerVerificationKey>, ProtocolError>;

/// Get signers
async fn get_signers(&self) -> Result<Vec<entities::SignerWithStake>, ProtocolError>;
async fn get_signers(&self) -> Result<Vec<entities::Signer>, ProtocolError> {
Ok(self
.get_signers_with_stake()
.await?
.into_iter()
.map(|signer| signer.into())
.collect::<Vec<entities::Signer>>())
}

/// Get signers with stake
async fn get_signers_with_stake(&self)
-> Result<Vec<entities::SignerWithStake>, ProtocolError>;

/// Registers a single signature
async fn register_single_signature(
Expand Down Expand Up @@ -324,8 +335,10 @@ impl MultiSigner for MultiSignerImpl {
}
}

/// Get signers
async fn get_signers(&self) -> Result<Vec<entities::SignerWithStake>, ProtocolError> {
/// Get signers with stake
async fn get_signers_with_stake(
&self,
) -> Result<Vec<entities::SignerWithStake>, ProtocolError> {
#[allow(clippy::identity_op)]
let epoch = self
.beacon_store
Expand Down Expand Up @@ -516,7 +529,7 @@ impl MultiSigner for MultiSignerImpl {
let previous_hash = previous_hash;
let started_at = format!("{:?}", Utc::now());
let completed_at = started_at.clone();
let signers = self.get_signers().await?;
let signers = self.get_signers_with_stake().await?;
let aggregate_verification_key =
key_encode_hex(&self.avk.as_ref().unwrap()).map_err(ProtocolError::Codec)?;
let multi_signature =
Expand Down Expand Up @@ -646,21 +659,34 @@ mod tests {
.expect("register should have succeeded")
}

let mut signers_all_expected = Vec::new();
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).await;
assert!(verification_key.as_ref().unwrap().is_some());
assert_eq!(
*verification_key_expected,
verification_key.unwrap().unwrap()
);
signers_all_expected.push(entities::SignerWithStake::new(
signers_with_stake_all_expected.push(entities::SignerWithStake::new(
*party_id,
key_encode_hex(verification_key_expected).unwrap(),
*stake,
));
}
signers_all_expected.sort_by_key(|signer| signer.party_id);
signers_with_stake_all_expected.sort_by_key(|signer| signer.party_id);
let signers_all_expected = signers_with_stake_all_expected
.clone()
.into_iter()
.map(|signer| signer.into())
.collect::<Vec<entities::Signer>>();

let mut signers_with_stake_all = multi_signer
.get_signers_with_stake()
.await
.expect("get signers with stake should have been succeeded");
signers_with_stake_all.sort_by_key(|signer| signer.party_id);

assert_eq!(signers_with_stake_all_expected, signers_with_stake_all);

let mut signers_all = multi_signer
.get_signers()
Expand Down
4 changes: 2 additions & 2 deletions mithril-aggregator/src/store/pending_certificate_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod test {
beacon.clone(),
fake_data::protocol_parameters(),
ix.to_string(),
fake_data::signers_with_stakes(5),
fake_data::signers(5),
);
adapter
.store_record(&beacon, &certificate_pending)
Expand Down Expand Up @@ -112,7 +112,7 @@ mod test {
beacon,
fake_data::protocol_parameters(),
"0".to_string(),
fake_data::signers_with_stakes(1),
fake_data::signers(1),
);

assert!(store.save(certificate_pending).await.is_ok());
Expand Down
13 changes: 12 additions & 1 deletion mithril-common/src/crypto_helper/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl From<entities::ProtocolParameters> for types::ProtocolParameters {

impl From<entities::SignerWithStake> for entities::Signer {
fn from(other: entities::SignerWithStake) -> Self {
entities::Signer::new(other.party_id as types::ProtocolPartyId, "".to_string())
entities::Signer::new(other.party_id, other.verification_key)
}
}

Expand Down Expand Up @@ -80,4 +80,15 @@ pub mod tests {
let stake_expected_from = stake_expected.into();
assert_eq!(signer_with_stake_expected, stake_expected_from);
}

#[test]
fn test_stake_signers_from_into() {
let signer_expected = entities::Signer::new(1, "123456".to_string());
();
let signer_with_stake = entities::SignerWithStake::new(1, "123456".to_string(), 100);
();

let signer_into: entities::Signer = signer_with_stake.clone().into();
assert_eq!(signer_expected, signer_into);
}
}
7 changes: 3 additions & 4 deletions mithril-common/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ pub struct CertificatePending {
#[serde(rename = "previous_hash")]
pub previous_hash: String,

/// Current Signers with stakes
// TODO: Should return Vec<Signer> instead, will be updated when stake distribution is real
/// Current Signers
#[serde(rename = "signers")]
pub signers: Vec<SignerWithStake>,
pub signers: Vec<Signer>,
}

impl CertificatePending {
Expand All @@ -67,7 +66,7 @@ impl CertificatePending {
beacon: Beacon,
protocol_parameters: ProtocolParameters,
previous_hash: String,
signers: Vec<SignerWithStake>,
signers: Vec<Signer>,
) -> CertificatePending {
CertificatePending {
beacon,
Expand Down
4 changes: 2 additions & 2 deletions mithril-common/src/fake_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ pub fn certificate_pending() -> entities::CertificatePending {
// Previous hash
let previous_hash = "123".to_string();

// Signers with stakes
let signers = signers_with_stakes(5);
// Signers
let signers = signers(5);

// Certificate pending
entities::CertificatePending::new(beacon, protocol_parameters, previous_hash, signers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ data ProtocolParameters = ProtocolParameters
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)

data Signer = Signer
{ party_id :: Word64,
verification_key :: Text
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)

data SignerWithStake = SignerWithStake
{ party_id :: Word64,
verification_key :: Text,
Expand All @@ -48,7 +54,7 @@ data CertificatePending = CertificatePending
{ beacon :: Beacon,
protocol :: ProtocolParameters,
previous_hash :: Text,
signers :: [SignerWithStake]
signers :: [Signer]
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)

Expand Down
2 changes: 1 addition & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ components:
signers:
type: array
items:
$ref: "#/components/schemas/SignerWithStake"
$ref: "#/components/schemas/Signer"

Stake:
description: Stake represents the stakes of a participant in the Cardano chain
Expand Down