Skip to content

Commit

Permalink
feat: add support for Conway config and params traverse (#521)
Browse files Browse the repository at this point in the history
Co-authored-by: Clark Alesna <clark_alesna@hotmail.com>
Co-authored-by: johnquinnvictaboada <quinnvictaboada@gmail.com>
  • Loading branch information
3 people authored Oct 17, 2024
1 parent 4d0f9fc commit 21af5b7
Show file tree
Hide file tree
Showing 10 changed files with 800 additions and 19 deletions.
7 changes: 4 additions & 3 deletions pallas-applying/src/shelley_ma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ fn compute_script_hash(script: &NativeScript) -> PolicyId {
pallas_crypto::hash::Hasher::<224>::hash(&payload)
}

// Checks all certificates in order, and counts the relevant ones for computing deposits.
// Checks all certificates in order, and counts the relevant ones for computing
// deposits.
#[allow(clippy::too_many_arguments)]
fn check_certificates(
cert_opt: &Option<Vec<Certificate>>,
Expand Down Expand Up @@ -585,13 +586,13 @@ 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) {
return Err(ShelleyMA(PoolNotRegistered));
}
if (*cepoch < *repoch) & (*repoch <= *cepoch + *emax as u64) {
if (*cepoch < *repoch) & (*repoch <= *cepoch + *emax) {
ps.retiring.insert(*pool_hash, *repoch);
Ok(())
} else {
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 @@ -2,4 +2,5 @@
pub mod alonzo;
pub mod byron;
pub mod conway;
pub mod shelley;
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
71 changes: 65 additions & 6 deletions pallas-traverse/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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};

macro_rules! param_boilerplate {
($name:ident: $type_:ty, [$($variant:tt)*]) => {
Expand Down Expand Up @@ -47,8 +47,12 @@ 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;
pub type PoolVotingThresholds = conway::PoolVotingThresholds;
pub type DRepVotingThresholds = conway::DRepVotingThresholds;

use crate::{Era, MultiEraUpdate};

Expand All @@ -70,13 +74,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 +130,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 All @@ -147,6 +157,40 @@ impl<'b> MultiEraUpdate<'b> {
}
}

pub fn alonzo_first_proposed_cost_models_for_script_languages(&self) -> Option<AlonzoCostMdls> {
match self {
MultiEraUpdate::AlonzoCompatible(x) => x
.proposed_protocol_parameter_updates
.first()
.and_then(|x| x.1.cost_models_for_script_languages.clone()),
_ => None,
}
}

pub fn babbage_first_proposed_cost_models_for_script_languages(
&self,
) -> Option<BabbageCostMdls> {
match self {
MultiEraUpdate::Babbage(x) => x
.proposed_protocol_parameter_updates
.first()
.and_then(|x| x.1.cost_models_for_script_languages.clone()),
_ => None,
}
}

pub fn conway_first_proposed_cost_models_for_script_languages(&self) -> Option<ConwayCostMdls> {
match self {
MultiEraUpdate::Conway(x) => x
.proposed_protocol_parameter_updates
.first()
.and_then(|x| x.1.cost_models_for_script_languages.clone()),
_ => None,
}
}

// remaining params are mostly boilerplate code, so we can just generate them

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

param_boilerplate!(minfee_b: u32, [AlonzoCompatible Babbage]);
Expand Down Expand Up @@ -181,9 +225,6 @@ 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!(execution_costs: ExUnitPrices, [AlonzoCompatible Babbage]);

param_boilerplate!(max_tx_ex_units: ExUnits, [AlonzoCompatible Babbage]);
Expand All @@ -195,4 +236,22 @@ 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

0 comments on commit 21af5b7

Please sign in to comment.