1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use anchor_lang::prelude::*;

use crate::errors::RegistryError;

use super::state::{ProtocolConfig, ProtocolConfigPda};

#[derive(Accounts)]
pub struct UpdateProtocolConfig<'info> {
    pub fee_payer: Signer<'info>,
    pub authority: Signer<'info>,
    /// CHECK: authority is protocol config authority.
    #[account(mut, has_one=authority)]
    pub protocol_config_pda: Account<'info, ProtocolConfigPda>,
    /// CHECK: is signer to reduce risk of updating with a wrong authority.
    pub new_authority: Option<Signer<'info>>,
}

pub fn check_protocol_config(protocol_config: ProtocolConfig) -> Result<()> {
    if protocol_config.min_weight == 0 {
        msg!("Min weight cannot be zero.");
        return err!(RegistryError::InvalidConfigUpdate);
    }
    if protocol_config.active_phase_length < protocol_config.registration_phase_length {
        msg!(
            "Active phase length must be greater or equal than registration phase length. {} {}",
            protocol_config.active_phase_length,
            protocol_config.registration_phase_length
        );
        return err!(RegistryError::InvalidConfigUpdate);
    }
    if protocol_config.active_phase_length < protocol_config.report_work_phase_length {
        msg!(
            "Active phase length must be greater or equal than report work phase length. {} {}",
            protocol_config.active_phase_length,
            protocol_config.report_work_phase_length
        );
        return err!(RegistryError::InvalidConfigUpdate);
    }
    if protocol_config.active_phase_length < protocol_config.slot_length {
        msg!(
            "Active phase length is less than slot length, active phase length {} < slot length {}. (Active phase lenght must be greater than slot length.)",
            protocol_config.active_phase_length,
            protocol_config.slot_length
        );
        return err!(RegistryError::InvalidConfigUpdate);
    }
    Ok(())
}