use std::{path::Path, sync::Arc};
use datasize::DataSize;
use serde::{Deserialize, Serialize};
use casper_types::{Chainspec, PublicKey, SecretKey};
use crate::{
components::consensus::{
era_supervisor::PAST_EVIDENCE_ERAS,
protocols::{highway::config::Config as HighwayConfig, zug::config::Config as ZugConfig},
EraId,
},
utils::{External, LoadError, Loadable},
};
const DEFAULT_MAX_EXECUTION_DELAY: u64 = 3;
#[derive(DataSize, Debug, Serialize, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub secret_key_path: External,
pub max_execution_delay: u64,
#[serde(default)]
pub highway: HighwayConfig,
#[serde(default)]
pub zug: ZugConfig,
}
impl Default for Config {
fn default() -> Self {
Config {
secret_key_path: External::Missing,
max_execution_delay: DEFAULT_MAX_EXECUTION_DELAY,
highway: HighwayConfig::default(),
zug: ZugConfig::default(),
}
}
}
type LoadKeyError = LoadError<<Arc<SecretKey> as Loadable>::Error>;
impl Config {
pub(crate) fn load_keys<P: AsRef<Path>>(
&self,
root: P,
) -> Result<(Arc<SecretKey>, PublicKey), LoadKeyError> {
let secret_signing_key: Arc<SecretKey> = self.secret_key_path.clone().load(root)?;
let public_key: PublicKey = PublicKey::from(secret_signing_key.as_ref());
Ok((secret_signing_key, public_key))
}
}
pub trait ChainspecConsensusExt {
fn activation_era(&self) -> EraId;
fn earliest_relevant_era(&self, current_era: EraId) -> EraId;
fn earliest_switch_block_needed(&self, era_id: EraId) -> EraId;
fn number_of_past_switch_blocks_needed(&self) -> u64;
}
impl ChainspecConsensusExt for Chainspec {
fn activation_era(&self) -> EraId {
self.protocol_config.activation_point.era_id()
}
fn earliest_relevant_era(&self, current_era: EraId) -> EraId {
self.activation_era()
.successor()
.max(current_era.saturating_sub(PAST_EVIDENCE_ERAS))
}
fn earliest_switch_block_needed(&self, era_id: EraId) -> EraId {
self.activation_era().max(
era_id
.saturating_sub(1)
.saturating_sub(self.core_config.auction_delay),
)
}
fn number_of_past_switch_blocks_needed(&self) -> u64 {
self.core_config
.auction_delay
.saturating_add(PAST_EVIDENCE_ERAS)
.saturating_add(1)
}
}