use {
crate::{errors::*, state::*},
anchor_lang::prelude::*,
};
#[derive(Accounts)]
pub struct PoolRotate<'info> {
#[account(address = Config::pubkey())]
pub config: Account<'info, Config>,
#[account(
mut,
seeds = [
SEED_POOL,
pool.id.to_be_bytes().as_ref(),
],
bump,
)]
pub pool: Account<'info, Pool>,
#[account(address = Registry::pubkey())]
pub registry: Account<'info, Registry>,
#[account(mut)]
pub signatory: Signer<'info>,
#[account(
address = snapshot.pubkey(),
constraint = snapshot.id.eq(®istry.current_epoch)
)]
pub snapshot: Account<'info, Snapshot>,
#[account(
address = snapshot_frame.pubkey(),
has_one = snapshot,
has_one = worker
)]
pub snapshot_frame: Account<'info, SnapshotFrame>,
#[account(
address = worker.pubkey(),
has_one = signatory
)]
pub worker: Account<'info, Worker>,
}
pub fn handler(ctx: Context<PoolRotate>) -> Result<()> {
let pool = &mut ctx.accounts.pool;
let registry = &ctx.accounts.registry;
let snapshot = &ctx.accounts.snapshot;
let snapshot_frame = &ctx.accounts.snapshot_frame;
let worker = &ctx.accounts.worker;
require!(
pool.workers.len().lt(&pool.size)
|| is_rotation_window_open(®istry, &snapshot, &snapshot_frame).unwrap(),
ClockworkError::PoolFull
);
require!(
!pool.workers.contains(&worker.key()),
ClockworkError::AlreadyInPool
);
pool.rotate(worker.key())?;
Ok(())
}
fn is_rotation_window_open(
registry: &Account<Registry>,
snapshot: &Account<Snapshot>,
snapshot_frame: &Account<SnapshotFrame>,
) -> Result<bool> {
match registry.nonce.checked_rem(snapshot.total_stake) {
None => Ok(false),
Some(sample) => Ok(sample >= snapshot_frame.stake_offset
&& sample
< snapshot_frame
.stake_offset
.checked_add(snapshot_frame.stake_amount)
.unwrap()),
}
}