-
Notifications
You must be signed in to change notification settings - Fork 52
Add aggregator CLI commands for configuration parameters #3438
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
base: main
Are you sure you want to change the base?
Changes from all commits
df3a63e
2cb269e
c5e8377
07a6c66
5bb377c
0365148
36d4973
fed05bd
ca410c7
38c8373
e2f8473
0beba25
d09e351
17e90e4
0f67a96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| //! Messages representing Protocol Configurations converted into CBOR format, for Cardano chain datum | ||
|
|
||
| use anyhow::Context; | ||
| use fixed::types::U8F24; | ||
| use hex::FromHex; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::collections::BTreeSet; | ||
| use thiserror::Error; | ||
|
|
||
| use mithril_common::{ | ||
| StdError, | ||
| entities::{ | ||
| BlockNumber, BlockNumberOffset, CardanoBlocksTransactionsSigningConfig, | ||
| CardanoTransactionsSigningConfig, Epoch, ProtocolParameters, | ||
| }, | ||
| messages::SignedEntityTypeDiscriminantsMessage, | ||
| }; | ||
|
|
||
| /// The cbor representation of a [ProtocolConfigurationForEpochMessage] | ||
| pub type CborProtocolConfigurationForEpochMessage = String; | ||
|
|
||
| /// Value object that represents a tag of Protocol Configuration. | ||
| #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| pub struct ProtocolConfigurationMarker { | ||
| /// Epoch | ||
| pub epoch: Epoch, | ||
|
|
||
| /// Protocol parameters | ||
| pub configuration: CborProtocolConfigurationForEpochMessage, | ||
| } | ||
|
|
||
| impl ProtocolConfigurationMarker { | ||
| /// instantiate a new [ProtocolConfigurationMarker]. | ||
| pub fn new( | ||
| epoch: Epoch, | ||
| protocol_configuration: CborProtocolConfigurationForEpochMessage, | ||
| ) -> Self { | ||
| ProtocolConfigurationMarker { | ||
| epoch, | ||
| configuration: protocol_configuration, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Parse error | ||
| #[derive(Error, Debug)] | ||
| #[error("Codec parse error")] | ||
| pub struct ProtocolConfigurationForEpochMessageParseError(#[source] StdError); | ||
|
|
||
| /// Protocol cryptographic parameters Message | ||
| /// | ||
| /// used for the CBOR representation of [ProtocolConfigurationForEpochMessage] | ||
| #[derive(Clone, Debug, Serialize, Deserialize)] | ||
| pub struct ProtocolParametersMessage { | ||
| /// Quorum parameter | ||
| pub k: u64, | ||
|
|
||
| /// Security parameter (number of lotteries) | ||
| pub m: u64, | ||
|
|
||
| /// f in phi(w) = 1 - (1 - f)^w, where w is the stake of a participant | ||
| pub phi_f: f64, | ||
| } | ||
|
|
||
| impl ProtocolParametersMessage { | ||
| /// phi_f_fixed is a fixed decimal representation of phi_f | ||
| /// used for PartialEq and Hash implementation | ||
| pub fn phi_f_fixed(&self) -> U8F24 { | ||
| U8F24::from_num(self.phi_f) | ||
| } | ||
| } | ||
|
|
||
| impl PartialEq<ProtocolParametersMessage> for ProtocolParametersMessage { | ||
| fn eq(&self, other: &ProtocolParametersMessage) -> bool { | ||
| self.k == other.k && self.m == other.m && self.phi_f_fixed() == other.phi_f_fixed() | ||
| } | ||
| } | ||
|
|
||
| impl From<ProtocolParameters> for ProtocolParametersMessage { | ||
| fn from(params: ProtocolParameters) -> Self { | ||
| ProtocolParametersMessage { | ||
| k: params.k, | ||
| m: params.m, | ||
| phi_f: params.phi_f, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Configuration for the signing of Cardano transactions | ||
| /// | ||
| /// used for the CBOR representation of [ProtocolConfigurationForEpochMessage] | ||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
| pub struct CardanoTransactionsSigningConfigMessage { | ||
| /// Number of blocks to discard from the tip of the chain when importing transactions. | ||
| pub security_parameter: BlockNumberOffset, | ||
|
|
||
| /// The number of blocks between signature of the transactions. | ||
| pub step: BlockNumber, | ||
| } | ||
|
|
||
| impl From<CardanoTransactionsSigningConfig> for CardanoTransactionsSigningConfigMessage { | ||
| fn from(config: CardanoTransactionsSigningConfig) -> Self { | ||
| CardanoTransactionsSigningConfigMessage { | ||
| security_parameter: config.security_parameter, | ||
| step: config.step, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Configuration for the signing of Cardano blocks and transactions | ||
| /// | ||
| /// used for the CBOR representation of [ProtocolConfigurationForEpochMessage] | ||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
| pub struct CardanoBlocksTransactionsSigningConfigMessage { | ||
| /// Number of blocks to discard from the tip of the chain when importing blocks and transactions. | ||
| pub security_parameter: BlockNumberOffset, | ||
|
|
||
| /// The number of blocks between signature of the blocks and transactions. | ||
| pub step: BlockNumber, | ||
| } | ||
|
|
||
| impl From<CardanoBlocksTransactionsSigningConfig> | ||
| for CardanoBlocksTransactionsSigningConfigMessage | ||
| { | ||
| fn from(config: CardanoBlocksTransactionsSigningConfig) -> Self { | ||
| CardanoBlocksTransactionsSigningConfigMessage { | ||
| security_parameter: config.security_parameter, | ||
| step: config.step, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //A epoch configuration used for the CBOR representation in the [ProtocolConfigurationMarker] | ||
| #[derive(PartialEq, Clone, Debug, Serialize, Deserialize)] | ||
| /// A network configuration available for an epoch | ||
| pub struct ProtocolConfigurationForEpochMessage { | ||
| /// Cryptographic protocol parameters (`k`, `m` and `phi_f`) | ||
| pub protocol_parameters: ProtocolParametersMessage, | ||
|
|
||
| /// List of available types of certifications | ||
| pub enabled_signed_entity_types: BTreeSet<SignedEntityTypeDiscriminantsMessage>, | ||
|
|
||
| /// Signing configuration for Cardano transactions | ||
| pub cardano_transactions: Option<CardanoTransactionsSigningConfigMessage>, | ||
|
|
||
| /// Signing configuration for Cardano blocks and transactions | ||
| pub cardano_blocks_transactions: Option<CardanoBlocksTransactionsSigningConfigMessage>, | ||
| } | ||
|
|
||
| impl ProtocolConfigurationForEpochMessage { | ||
| /// Serialize the structure to a CBOR bytes representation. | ||
| fn to_cbor_bytes(&self) -> Result<Vec<u8>, ProtocolConfigurationForEpochMessageParseError> { | ||
| let mut cursor = std::io::Cursor::new(Vec::new()); | ||
| ciborium::ser::into_writer(&self, &mut cursor) | ||
| .with_context(|| "ProtocolConfigurationForEpoch can not serialize data to cbor") | ||
| .map_err(ProtocolConfigurationForEpochMessageParseError)?; | ||
|
|
||
| Ok(cursor.into_inner()) | ||
| } | ||
|
|
||
| /// Serialize the structure to a CBOR hex representation. | ||
| pub fn to_cbor_hex(&self) -> Result<String, ProtocolConfigurationForEpochMessageParseError> { | ||
| Ok(hex::encode(self.to_cbor_bytes()?)) | ||
| } | ||
|
|
||
| /// Deserialize a type `T: Serialize + DeserializeOwned` from CBOR bytes representation. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment and the one below probably need to be adjusted. |
||
| fn from_cbor_bytes( | ||
| bytes: &[u8], | ||
| ) -> Result<Self, ProtocolConfigurationForEpochMessageParseError> { | ||
| let mut cursor = std::io::Cursor::new(&bytes); | ||
| let a: Self = ciborium::de::from_reader(&mut cursor) | ||
| .with_context(|| "ProtocolConfigurationForEpoch can not unserialize cbor data") | ||
| .map_err(ProtocolConfigurationForEpochMessageParseError)?; | ||
|
|
||
| Ok(a) | ||
| } | ||
|
|
||
| /// Deserialize a type `T: Serialize + DeserializeOwned` from CBOR hex representation. | ||
| pub fn from_cbor_hex( | ||
| hex: &str, | ||
| ) -> Result<Self, ProtocolConfigurationForEpochMessageParseError> { | ||
| let hex_vector = Vec::from_hex(hex) | ||
| .with_context(|| "ProtocolConfigurationForEpochMessage can not unserialize hex data") | ||
| .map_err(ProtocolConfigurationForEpochMessageParseError)?; | ||
|
|
||
| Self::from_cbor_bytes(&hex_vector) | ||
| .with_context(|| "ProtocolConfigurationForEpochMessage can not unserialize cbor data") | ||
| .map_err(ProtocolConfigurationForEpochMessageParseError) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Golden tests are also welcome here. |
||
| use mithril_common::test::double::Dummy; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn to_cbor_from_cbor_conversion() { | ||
| let mithril_network_configuration_for_epoch = ProtocolConfigurationForEpochMessage::dummy(); | ||
| let cbor = mithril_network_configuration_for_epoch.to_cbor_hex().unwrap(); | ||
| let mithril_network_configuration_for_epoch_from_cbor = | ||
| ProtocolConfigurationForEpochMessage::from_cbor_hex(&cbor).unwrap(); | ||
| assert_eq!( | ||
| mithril_network_configuration_for_epoch, | ||
| mithril_network_configuration_for_epoch_from_cbor | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| //! Cardano Chain module to read protocol configuration markers | ||
|
|
||
| pub mod message; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| pub mod payload; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| pub mod protocol_configuration_reader; | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| pub use payload::{ | ||
| ProtocolConfigurationMarkersPayload as ProtocolConfigurationMarkersPayloadCardanoChain, | ||
| SignedProtocolConfigurationMarkersPayload as SignedProtocolConfigurationMarkersPayloadCardanoChain, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //! Payload structures and signing utilitaries for Protocol Configuration Datum | ||
|
|
||
| use anyhow::Context; | ||
| use serde::{Deserialize, Serialize}; | ||
| use thiserror::Error; | ||
|
|
||
| use mithril_common::crypto_helper::{ | ||
| ProtocolConfigurationMarkersSigner, ProtocolConfigurationMarkersVerifierSignature, | ||
| key_encode_hex, | ||
| }; | ||
| use mithril_common::{StdError, StdResult}; | ||
|
|
||
| use crate::cardano_chain::message::ProtocolConfigurationMarker; | ||
|
|
||
| /// [ProtocolConfigurationMarkersPayload] related errors. | ||
| #[derive(Debug, Error)] | ||
| pub enum ProtocolConfigurationMarkersPayloadError { | ||
| /// Error raised when the message serialization fails | ||
| #[error("could not serialize message")] | ||
| SerializeMessage(#[source] StdError), | ||
|
|
||
| /// Error raised when the signature deserialization fails | ||
| #[error("could not deserialize signature")] | ||
| DeserializeSignature(#[source] StdError), | ||
|
|
||
| /// Error raised when the signature is missing | ||
| #[error("could not verify signature: signature is missing")] | ||
| MissingSignature, | ||
|
|
||
| /// Error raised when the signature is invalid | ||
| #[error("could not verify signature")] | ||
| VerifySignature(#[source] StdError), | ||
|
|
||
| /// Error raised when the signing the markers | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not very clear. |
||
| #[error("could not create signature")] | ||
| CreateSignature(#[source] StdError), | ||
| } | ||
|
|
||
| /// Protocol Configuration markers payload | ||
| #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| pub struct ProtocolConfigurationMarkersPayload { | ||
| /// List of protocol configuration markers | ||
| pub markers: Vec<ProtocolConfigurationMarker>, | ||
| } | ||
|
|
||
| /// Signed Protocol Configuration markers payload | ||
| #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
| pub struct SignedProtocolConfigurationMarkersPayload { | ||
| /// List of protocol configuration markers | ||
| pub markers: Vec<ProtocolConfigurationMarker>, | ||
|
|
||
| /// Protocol Configuration markers signature | ||
| pub signature: ProtocolConfigurationMarkersVerifierSignature, | ||
| } | ||
|
|
||
| impl SignedProtocolConfigurationMarkersPayload { | ||
| /// Instanciate a new SignedProtocolConfigurationMarkersPayload with markers and signature | ||
| pub fn new( | ||
| markers: Vec<ProtocolConfigurationMarker>, | ||
| signature: ProtocolConfigurationMarkersVerifierSignature, | ||
| ) -> Self { | ||
| Self { markers, signature } | ||
| } | ||
|
|
||
| /// Encode this payload to a json hex string | ||
| pub fn to_json_hex(&self) -> StdResult<String> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably miss some golden tests here? |
||
| key_encode_hex(self).with_context( | ||
| || "SignedProtocolConfigurationMarkersPayload could not be json hex encoded", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl ProtocolConfigurationMarkersPayload { | ||
| /// Instanciate a new ProtocolConfigurationMarkersPayload with markers | ||
| pub fn new(markers: Vec<ProtocolConfigurationMarker>) -> Self { | ||
| Self { markers } | ||
| } | ||
|
|
||
| fn message_to_bytes(&self) -> Result<Vec<u8>, ProtocolConfigurationMarkersPayloadError> { | ||
| serde_json::to_vec(&self.markers) | ||
| .map_err(|e| ProtocolConfigurationMarkersPayloadError::SerializeMessage(e.into())) | ||
| } | ||
|
|
||
| /// Sign an protocol configuration markers payload | ||
| pub fn sign( | ||
| self, | ||
| signer: &ProtocolConfigurationMarkersSigner, | ||
| ) -> Result<SignedProtocolConfigurationMarkersPayload, ProtocolConfigurationMarkersPayloadError> | ||
| { | ||
| let signature = | ||
| signer.sign(&self.message_to_bytes().map_err(|e| { | ||
| ProtocolConfigurationMarkersPayloadError::CreateSignature(e.into()) | ||
| })?); | ||
|
|
||
| Ok(SignedProtocolConfigurationMarkersPayload { | ||
| markers: self.markers, | ||
| signature, | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more CborHex tha Cbor.