use thiserror::Error;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConsensusEmissionConfig {
pub proposer_reward_share: u64,
pub attester_reward_share: u64,
}
impl ConsensusEmissionConfig {
pub fn new(proposer_reward_share: u64, attester_reward_share: u64) -> Self {
Self {
proposer_reward_share,
attester_reward_share,
}
}
pub fn validate_for_attesters(&self, attesters_len: usize) -> Result<(), EmissionConfigError> {
if attesters_len == 0 && self.attester_reward_share > 0 {
return Err(EmissionConfigError::NonZeroAttesterShareWithNoAttesters);
}
Ok(())
}
}
#[derive(Debug, Error)]
pub enum EmissionConfigError {
#[error("non-zero attester share configured but no attesters provided")]
NonZeroAttesterShareWithNoAttesters,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_zero_attesters_policy() {
let cfg = ConsensusEmissionConfig::new(12, 0);
assert!(cfg.validate_for_attesters(0).is_ok());
let cfg_bad = ConsensusEmissionConfig::new(12, 1);
let err = cfg_bad.validate_for_attesters(0).unwrap_err();
match err {
EmissionConfigError::NonZeroAttesterShareWithNoAttesters => {}
}
}
#[test]
fn validate_with_attesters_ok() {
let cfg = ConsensusEmissionConfig::new(12, 88);
assert!(cfg.validate_for_attesters(3).is_ok());
}
}