Skip to content
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

refactor: improved support for Conway genesis and protocol params #520

Closed
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
2 changes: 1 addition & 1 deletion pallas-applying/src/shelley_ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ fn check_pool_retirement(
pool_hash: &PoolKeyhash,
repoch: &Epoch,
cepoch: &Epoch,
emax: &u32,
emax: &Epoch,
ps: &mut PState,
) -> ValidationResult {
if !ps.pool_params.contains_key(pool_hash) {
Expand Down
16 changes: 8 additions & 8 deletions pallas-applying/src/utils/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pallas_primitives::{
Coin, CostMdls, ExUnitPrices, ExUnits, Nonce, ProtocolVersion, RationalNumber, UnitInterval,
},
babbage::CostMdls as BabbageCostMdls,
conway::CostMdls as ConwayCostMdls,
conway::{CostMdls as ConwayCostMdls, Epoch},
};

#[allow(clippy::large_enum_variant)]
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct ShelleyProtParams {
pub min_pool_cost: Coin,
pub expansion_rate: UnitInterval,
pub treasury_growth_rate: UnitInterval,
pub maximum_epoch: u32,
pub maximum_epoch: Epoch,
pub pool_pledge_influence: RationalNumber,
pub decentralization_constant: UnitInterval,
pub extra_entropy: Nonce,
Expand Down Expand Up @@ -109,7 +109,7 @@ pub struct AlonzoProtParams {
pub max_collateral_inputs: u32,
pub expansion_rate: UnitInterval,
pub treasury_growth_rate: UnitInterval,
pub maximum_epoch: u32,
pub maximum_epoch: Epoch,
pub pool_pledge_influence: RationalNumber,
pub decentralization_constant: UnitInterval,
pub extra_entropy: Nonce,
Expand Down Expand Up @@ -137,7 +137,7 @@ pub struct BabbageProtParams {
pub max_collateral_inputs: u32,
pub expansion_rate: UnitInterval,
pub treasury_growth_rate: UnitInterval,
pub maximum_epoch: u32,
pub maximum_epoch: Epoch,
pub pool_pledge_influence: RationalNumber,
pub decentralization_constant: UnitInterval,
pub extra_entropy: Nonce,
Expand Down Expand Up @@ -165,16 +165,16 @@ pub struct ConwayProtParams {
pub max_collateral_inputs: u32,
pub expansion_rate: UnitInterval,
pub treasury_growth_rate: UnitInterval,
pub maximum_epoch: u32,
pub maximum_epoch: Epoch,
pub pool_pledge_influence: RationalNumber,
pub pool_voting_thresholds: pallas_primitives::conway::PoolVotingThresholds,
pub drep_voting_thresholds: pallas_primitives::conway::DRepVotingThresholds,
pub min_committee_size: u64,
pub committee_term_limit: u32,
pub governance_action_validity_period: u32,
pub committee_term_limit: Epoch,
pub governance_action_validity_period: Epoch,
pub governance_action_deposit: Coin,
pub drep_deposit: Coin,
pub drep_inactivity_period: u32,
pub drep_inactivity_period: Epoch,
pub minfee_refscript_cost_per_byte: UnitInterval,
}

Expand Down
110 changes: 110 additions & 0 deletions pallas-configs/src/conway.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GenesisFile {
pub pool_voting_thresholds: PoolVotingThresholds,
pub d_rep_voting_thresholds: DRepVotingThresholds,
pub committee_min_size: u64,
pub committee_max_term_length: u32,
pub gov_action_lifetime: u32,
pub gov_action_deposit: u64,
pub d_rep_deposit: u64,
pub d_rep_activity: u32,
pub min_fee_ref_script_cost_per_byte: u64,
pub plutus_v3_cost_model: Vec<i64>,
pub constitution: Constitution,
pub committee: Committee,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PoolVotingThresholds {
pub committee_normal: f32,
pub committee_no_confidence: f32,
pub hard_fork_initiation: f32,
pub motion_no_confidence: f32,
pub pp_security_group: f32,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DRepVotingThresholds {
pub motion_no_confidence: f32,
pub committee_normal: f32,
pub committee_no_confidence: f32,
pub update_to_constitution: f32,
pub hard_fork_initiation: f32,
pub pp_network_group: f32,
pub pp_economic_group: f32,
pub pp_technical_group: f32,
pub pp_gov_group: f32,
pub treasury_withdrawal: f32,
}

#[derive(Debug, Deserialize)]
pub struct Constitution {
pub anchor: Anchor,
pub script: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Anchor {
pub data_hash: String,
pub url: String,
}

#[derive(Debug, Deserialize)]
pub struct Committee {
pub members: HashMap<String, u64>,
pub threshold: Fraction,
}

#[derive(Deserialize, Clone, Debug)]
pub struct Fraction {
pub numerator: u64,
pub denominator: u64,
}

impl From<Fraction> for pallas_primitives::conway::RationalNumber {
fn from(value: Fraction) -> Self {
Self {
numerator: value.numerator,
denominator: value.denominator,
}
}
}

pub fn from_file(path: &std::path::Path) -> Result<GenesisFile, std::io::Error> {
let file = std::fs::File::open(path)?;
let reader = std::io::BufReader::new(file);
let parsed: GenesisFile = serde_json::from_reader(reader)?;

Ok(parsed)
}

#[cfg(test)]
mod tests {
use super::*;

fn load_test_data_config(network: &str) -> GenesisFile {
let path = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..")
.join("test_data")
.join(format!("{network}-conway-genesis.json"));

from_file(&path).unwrap()
}

#[test]
fn test_preview_json_loads() {
load_test_data_config("preview");
}

#[test]
fn test_mainnet_json_loads() {
load_test_data_config("mainnet");
}
}
1 change: 1 addition & 0 deletions pallas-configs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
pub mod alonzo;
pub mod byron;
pub mod shelley;
pub mod conway;
4 changes: 2 additions & 2 deletions pallas-configs/src/shelley.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use num_rational::BigRational;
use pallas_crypto::hash::Hash;
use pallas_primitives::conway::RationalNumber;
use pallas_primitives::conway::{Epoch, RationalNumber};
use serde::{Deserialize, Deserializer};
use std::{collections::HashMap, str::FromStr};

Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct ProtocolParams {
pub pool_deposit: u64,
pub n_opt: u32,
pub min_pool_cost: u64,
pub e_max: u32,
pub e_max: Epoch,
pub extra_entropy: ExtraEntropy,

#[serde(deserialize_with = "deserialize_rational")]
Expand Down
9 changes: 9 additions & 0 deletions pallas-primitives/src/conway/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ pub struct ProtocolParamUpdate {
pub minfee_refscript_cost_per_byte: Option<UnitInterval>,
}

#[derive(Serialize, Deserialize, Encode, Decode, Debug, PartialEq, Eq, Clone)]
pub struct Update {
#[n(0)]
pub proposed_protocol_parameter_updates: KeyValuePairs<Genesishash, ProtocolParamUpdate>,

#[n(1)]
pub epoch: Epoch,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct PoolVotingThresholds {
pub motion_no_confidence: UnitInterval,
Expand Down
1 change: 1 addition & 0 deletions pallas-traverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub enum MultiEraUpdate<'b> {
Byron(u64, Box<Cow<'b, byron::UpProp>>),
AlonzoCompatible(Box<Cow<'b, alonzo::Update>>),
Babbage(Box<Cow<'b, babbage::Update>>),
Conway(Box<Cow<'b, conway::Update>>),
}

#[derive(Debug, Clone)]
Expand Down
45 changes: 38 additions & 7 deletions pallas-traverse/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use pallas_codec::minicbor;
use paste::paste;
use std::{borrow::Cow, ops::Deref};

use pallas_primitives::{alonzo, babbage, byron};
use pallas_primitives::{alonzo, babbage, byron, conway::{self, DRepVotingThresholds, PoolVotingThresholds}};

macro_rules! param_boilerplate {
($name:ident: $type_:ty, [$($variant:tt)*]) => {
paste! {
pub fn [<"first_proposed_" $name>](&self) -> Option<$type_> {
pub fn [<"first_proposed_" $name _ $($variant:lower)_*>](&self) -> Option<$type_> {
#[allow(unreachable_patterns)]
match self {
$(
Expand All @@ -23,7 +23,7 @@ macro_rules! param_boilerplate {
}

paste! {
pub fn [<"all_proposed_" $name>](&self) -> Vec<$type_> {
pub fn [<"all_proposed_" $name _ $($variant:lower)_*>](&self) -> Vec<$type_> {
#[allow(unreachable_patterns)]
match self {
$(
Expand All @@ -47,7 +47,9 @@ pub type UnitInterval = alonzo::UnitInterval;
pub type Nonce = alonzo::Nonce;
pub type ExUnitPrices = alonzo::ExUnitPrices;
pub type ExUnits = alonzo::ExUnits;
pub type CostMdls = alonzo::CostMdls;
pub type AlonzoCostMdls = alonzo::CostMdls;
pub type BabbageCostMdls = babbage::CostMdls;
pub type ConwayCostMdls = conway::CostMdls;
pub type ProtocolVersion = alonzo::ProtocolVersion;

use crate::{Era, MultiEraUpdate};
Expand All @@ -70,13 +72,18 @@ impl<'b> MultiEraUpdate<'b> {
let up = Box::new(Cow::Owned(up));
Ok(MultiEraUpdate::Babbage(up))
}
_ => unimplemented!("unimplemented era"),
Era::Conway => {
let up = minicbor::decode(cbor)?;
let up = Box::new(Cow::Owned(up));
Ok(MultiEraUpdate::Conway(up))
}
}
}

pub fn encode(&self) -> Vec<u8> {
// to_vec is infallible
match self {
MultiEraUpdate::Conway(x) => minicbor::to_vec(x).unwrap(),
MultiEraUpdate::AlonzoCompatible(x) => minicbor::to_vec(x).unwrap(),
MultiEraUpdate::Babbage(x) => minicbor::to_vec(x).unwrap(),
MultiEraUpdate::Byron(a, b) => minicbor::to_vec((a, b)).unwrap(),
Expand Down Expand Up @@ -121,6 +128,7 @@ impl<'b> MultiEraUpdate<'b> {
MultiEraUpdate::Byron(x, _) => *x,
MultiEraUpdate::AlonzoCompatible(x) => x.epoch,
MultiEraUpdate::Babbage(x) => x.epoch,
MultiEraUpdate::Conway(x) => x.epoch,
}
}

Expand Down Expand Up @@ -181,8 +189,11 @@ impl<'b> MultiEraUpdate<'b> {

param_boilerplate!(ada_per_utxo_byte: u64, [AlonzoCompatible Babbage]);

//param_boilerplate!(cost_models_for_script_languages: CostMdls,
// [AlonzoCompatible Babbage]);
param_boilerplate!(cost_models_for_script_languages: AlonzoCostMdls, [AlonzoCompatible]);

param_boilerplate!(cost_models_for_script_languages: BabbageCostMdls, [Babbage]);

param_boilerplate!(cost_models_for_script_languages: ConwayCostMdls, [Conway]);

param_boilerplate!(execution_costs: ExUnitPrices, [AlonzoCompatible Babbage]);

Expand All @@ -195,4 +206,24 @@ impl<'b> MultiEraUpdate<'b> {
param_boilerplate!(collateral_percentage: u32, [AlonzoCompatible Babbage]);

param_boilerplate!(max_collateral_inputs: u32, [AlonzoCompatible Babbage]);

param_boilerplate!(pool_voting_thresholds: PoolVotingThresholds, [Conway]);

param_boilerplate!(drep_voting_thresholds: DRepVotingThresholds, [Conway]);

param_boilerplate!(min_committee_size: u64, [Conway]);

param_boilerplate!(committee_term_limit: u64, [Conway]);

param_boilerplate!(governance_action_validity_period: u64, [Conway]);

param_boilerplate!(governance_action_deposit: u64, [Conway]);

param_boilerplate!(drep_deposit: u64, [Conway]);

param_boilerplate!(drep_inactivity_period: u64, [Conway]);

param_boilerplate!(minfee_refscript_cost_per_byte: UnitInterval, [Conway]);


}
Loading