use anchor_lang::prelude::*;
use crate::crypto::constants::{
EXPECTED_EVMNET_CHAIN_HASH, EXPECTED_EVMNET_GENESIS_TIME, EXPECTED_EVMNET_PERIOD,
EXPECTED_EVMNET_PUBKEY,
};
use crate::errors::AleaError;
use crate::events::ConfigUpdated;
use crate::state::Config;
#[derive(Accounts)]
pub struct UpdateConfig<'info> {
#[account(
mut,
seeds = [b"config"],
bump = config.bump,
has_one = authority,
)]
pub config: Account<'info, Config>,
pub authority: Signer<'info>,
}
pub fn update_config_handler(
ctx: Context<UpdateConfig>,
pubkey_g2: [u8; 128],
genesis_time: u64,
period: u64,
chain_hash: [u8; 32],
) -> Result<()> {
require!(
chain_hash == EXPECTED_EVMNET_CHAIN_HASH,
AleaError::WrongChainHash
);
require!(pubkey_g2 == EXPECTED_EVMNET_PUBKEY, AleaError::WrongPubkey);
require!(
genesis_time == EXPECTED_EVMNET_GENESIS_TIME,
AleaError::InvalidGenesisTime
);
require!(period == EXPECTED_EVMNET_PERIOD, AleaError::InvalidPeriod);
let config = &mut ctx.accounts.config;
if config.pubkey_g2 == pubkey_g2
&& config.genesis_time == genesis_time
&& config.period == period
&& config.chain_hash == chain_hash
{
return Ok(());
}
config.pubkey_g2 = pubkey_g2;
config.genesis_time = genesis_time;
config.period = period;
config.chain_hash = chain_hash;
let pubkey_g2_hash = anchor_lang::solana_program::hash::hash(&config.pubkey_g2).to_bytes();
emit!(ConfigUpdated {
authority: config.authority,
chain_hash: config.chain_hash,
pubkey_g2_hash,
});
Ok(())
}