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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use {crate::objects::*, 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(),
constraint = is_rotation_window_open(®istry, &snapshot, &snapshot_frame).unwrap(),
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 worker = &ctx.accounts.worker;
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()),
}
}