Skip to content

Commit

Permalink
feat(applying): include constants in pparams
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Dec 22, 2024
1 parent 3a614d2 commit e111428
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 1,291 deletions.
1 change: 1 addition & 0 deletions pallas-applying/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pallas-primitives = { version = "=0.31.0", path = "../pallas-primitives" }
pallas-traverse = { version = "=0.31.0", path = "../pallas-traverse" }
rand = "0.8"
hex = "0.4"
chrono = "0.4.39"

[dev-dependencies]
hex = "0.4"
79 changes: 79 additions & 0 deletions pallas-applying/src/utils/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ pub enum MultiEraProtocolParameters {
}

impl MultiEraProtocolParameters {
pub fn system_start(&self) -> chrono::DateTime<chrono::FixedOffset> {
match self {
MultiEraProtocolParameters::Byron(ByronProtParams { start_time, .. }) => {
chrono::DateTime::from_timestamp(*start_time as i64, 0)
.expect("valid timestamp")
.fixed_offset()
}
MultiEraProtocolParameters::Shelley(ShelleyProtParams { system_start, .. }) => {
*system_start
}
MultiEraProtocolParameters::Alonzo(AlonzoProtParams { system_start, .. }) => {
*system_start
}
MultiEraProtocolParameters::Babbage(BabbageProtParams { system_start, .. }) => {
*system_start
}
MultiEraProtocolParameters::Conway(ConwayProtParams { system_start, .. }) => {
*system_start
}
}
}

pub fn protocol_version(&self) -> usize {
match self {
MultiEraProtocolParameters::Byron(ByronProtParams {
Expand All @@ -45,11 +67,56 @@ impl MultiEraProtocolParameters {
}) => *x as usize,
}
}

const FIVE_DAYS_IN_SECONDS: u64 = 5 * 24 * 60 * 60;

pub fn epoch_length(&self) -> u64 {
match self {
MultiEraProtocolParameters::Byron(ByronProtParams { slot_duration, .. }) => {
// TODO: research if Byron epoch length is actually hardcoded or if you can get
// it from genesis files somehow
Self::FIVE_DAYS_IN_SECONDS / (*slot_duration / 1000)
}
MultiEraProtocolParameters::Shelley(ShelleyProtParams { epoch_length, .. }) => {
*epoch_length
}
MultiEraProtocolParameters::Alonzo(AlonzoProtParams { epoch_length, .. }) => {
*epoch_length
}
MultiEraProtocolParameters::Babbage(BabbageProtParams { epoch_length, .. }) => {
*epoch_length
}
MultiEraProtocolParameters::Conway(ConwayProtParams { epoch_length, .. }) => {
*epoch_length
}
}
}

pub fn slot_length(&self) -> u64 {
match self {
MultiEraProtocolParameters::Byron(ByronProtParams { slot_duration, .. }) => {
*slot_duration / 1000
}
MultiEraProtocolParameters::Shelley(ShelleyProtParams { slot_length, .. }) => {
*slot_length
}
MultiEraProtocolParameters::Alonzo(AlonzoProtParams { slot_length, .. }) => {
*slot_length
}
MultiEraProtocolParameters::Babbage(BabbageProtParams { slot_length, .. }) => {
*slot_length
}
MultiEraProtocolParameters::Conway(ConwayProtParams { slot_length, .. }) => {
*slot_length
}
}
}
}

#[derive(Debug, Clone)]
pub struct ByronProtParams {
pub block_version: (u16, u16, u8),
pub start_time: u64,
pub script_version: u16,
pub slot_duration: u64,
pub max_block_size: u64,
Expand All @@ -69,6 +136,9 @@ pub struct ByronProtParams {

#[derive(Debug, Clone)]
pub struct ShelleyProtParams {
pub system_start: chrono::DateTime<chrono::FixedOffset>,
pub epoch_length: u64,
pub slot_length: u64,
pub minfee_a: u32,
pub minfee_b: u32,
pub max_block_body_size: u32,
Expand All @@ -90,6 +160,9 @@ pub struct ShelleyProtParams {

#[derive(Debug, Clone)]
pub struct AlonzoProtParams {
pub system_start: chrono::DateTime<chrono::FixedOffset>,
pub epoch_length: u64,
pub slot_length: u64,
pub minfee_a: u32,
pub minfee_b: u32,
pub max_block_body_size: u32,
Expand Down Expand Up @@ -118,6 +191,9 @@ pub struct AlonzoProtParams {

#[derive(Debug, Clone)]
pub struct BabbageProtParams {
pub system_start: chrono::DateTime<chrono::FixedOffset>,
pub epoch_length: u64,
pub slot_length: u64,
pub minfee_a: u32,
pub minfee_b: u32,
pub max_block_body_size: u32,
Expand Down Expand Up @@ -146,6 +222,9 @@ pub struct BabbageProtParams {

#[derive(Debug, Clone)]
pub struct ConwayProtParams {
pub system_start: chrono::DateTime<chrono::FixedOffset>,
pub epoch_length: u64,
pub slot_length: u64,
pub minfee_a: u32,
pub minfee_b: u32,
pub max_block_body_size: u32,
Expand Down
6 changes: 6 additions & 0 deletions pallas-applying/tests/alonzo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,9 @@ mod alonzo_tests {

fn mk_params_epoch_334() -> AlonzoProtParams {
AlonzoProtParams {
system_start: chrono::DateTime::parse_from_rfc3339("2017-09-23T21:44:51Z").unwrap(),
epoch_length: 432000,
slot_length: 1,
minfee_a: 44,
minfee_b: 155381,
max_block_body_size: 65536,
Expand Down Expand Up @@ -2633,6 +2636,9 @@ mod alonzo_tests {

fn mk_params_epoch_300() -> AlonzoProtParams {
AlonzoProtParams {
system_start: chrono::DateTime::parse_from_rfc3339("2017-09-23T21:44:51Z").unwrap(),
epoch_length: 432000,
slot_length: 1,
minfee_a: 44,
minfee_b: 155381,
max_block_body_size: 81920,
Expand Down
12 changes: 12 additions & 0 deletions pallas-applying/tests/babbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,9 @@ mod babbage_tests {

fn mk_mainnet_params_epoch_365() -> BabbageProtParams {
BabbageProtParams {
system_start: chrono::DateTime::parse_from_rfc3339("2017-09-23T21:44:51Z").unwrap(),
epoch_length: 432000,
slot_length: 1,
minfee_a: 44,
minfee_b: 155381,
max_block_body_size: 90112,
Expand Down Expand Up @@ -2445,6 +2448,9 @@ mod babbage_tests {

fn mk_mainnet_params_epoch_380() -> BabbageProtParams {
BabbageProtParams {
system_start: chrono::DateTime::parse_from_rfc3339("2022-10-25T00:00:00Z").unwrap(),
epoch_length: 432000,
slot_length: 1,
minfee_a: 44,
minfee_b: 155381,
max_block_body_size: 90112,
Expand Down Expand Up @@ -2697,6 +2703,9 @@ mod babbage_tests {

fn mk_preview_params_epoch_30() -> BabbageProtParams {
BabbageProtParams {
system_start: chrono::DateTime::parse_from_rfc3339("2022-10-25T00:00:00Z").unwrap(),
epoch_length: 432000,
slot_length: 1,
minfee_a: 44,
minfee_b: 155381,
max_block_body_size: 90112,
Expand Down Expand Up @@ -2787,6 +2796,9 @@ mod babbage_tests {

fn mk_preprod_params_epoch_100() -> BabbageProtParams {
BabbageProtParams {
system_start: chrono::DateTime::parse_from_rfc3339("2017-09-23T21:44:51Z").unwrap(),
epoch_length: 432000,
slot_length: 1,
minfee_a: 44,
minfee_b: 155381,
max_block_body_size: 90112,
Expand Down
Loading

0 comments on commit e111428

Please sign in to comment.