Skip to content

Commit

Permalink
Add config to pool program to track pool_authority
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Garfield committed Aug 31, 2022
1 parent ed05c4b commit 5d5cd06
Show file tree
Hide file tree
Showing 25 changed files with 263 additions and 86 deletions.
10 changes: 9 additions & 1 deletion cli/src/processor/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use {
crate::errors::CliError,
clockwork_client::{
crank::state::Config as CrankConfig, network::state::Config as NetworkConfig, Client,
crank::state::Config as CrankConfig, network::state::Config as NetworkConfig,
pool::state::Config as PoolConfig, Client,
},
solana_sdk::pubkey::Pubkey,
};
Expand All @@ -19,9 +20,16 @@ pub fn get(client: &Client) -> Result<(), CliError> {
.get::<NetworkConfig>(&network_config_pubkey)
.map_err(|_err| CliError::AccountNotFound(network_config_pubkey.to_string()))?;

// Get pool config
let pool_config_pubkey = PoolConfig::pubkey();
let pool_config = client
.get::<PoolConfig>(&pool_config_pubkey)
.map_err(|_err| CliError::AccountNotFound(pool_config_pubkey.to_string()))?;

// Print configs
println!("Crank {:#?}", crank_config);
println!("Network {:#?}", network_config);
println!("Pool {:#?}", pool_config);

Ok(())
}
Expand Down
9 changes: 4 additions & 5 deletions cli/src/processor/initialize.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use clockwork_client::pool::state::Pool;

use {
crate::errors::CliError,
clockwork_client::Client,
clockwork_client::{pool::state::Pool, Client},
solana_sdk::{native_token::LAMPORTS_PER_SOL, pubkey::Pubkey},
};

Expand All @@ -16,11 +14,12 @@ pub fn initialize(client: &Client, mint: Pubkey) -> Result<(), CliError> {
let ix_a = clockwork_client::crank::instruction::initialize(admin, pool);
let ix_b = clockwork_client::http::instruction::initialize(admin);
let ix_c = clockwork_client::network::instruction::initialize(admin, mint);
let ix_d = clockwork_client::network::instruction::pool_create(admin, pool_name.into(), 1);
let ix_d = clockwork_client::pool::instruction::initialize(admin);
let ix_e = clockwork_client::network::instruction::pool_create(admin, pool_name.into(), 1);

// Submit tx
client
.send_and_confirm(&[ix_a, ix_b, ix_c, ix_d], &[client.payer()])
.send_and_confirm(&[ix_a, ix_b, ix_c, ix_d, ix_e], &[client.payer()])
.unwrap();

// Airdrop some lamports to the network's snapshot queue
Expand Down
1 change: 1 addition & 0 deletions client/src/network/instruction/pool_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn pool_create(admin: Pubkey, name: String, size: usize) -> Instruction {
AccountMeta::new_readonly(clockwork_network::state::Config::pubkey(), false),
AccountMeta::new(clockwork_pool::state::Pool::pubkey(name.clone()), false),
AccountMeta::new_readonly(clockwork_pool::ID, false),
AccountMeta::new_readonly(clockwork_pool::state::Config::pubkey(), false),
AccountMeta::new(clockwork_network::state::Rotator::pubkey(), false),
AccountMeta::new_readonly(system_program::ID, false),
],
Expand Down
1 change: 1 addition & 0 deletions client/src/network/instruction/pools_rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn pools_rotate(
AccountMeta::new_readonly(entry, false),
AccountMeta::new_readonly(node, false),
AccountMeta::new_readonly(clockwork_pool::ID, false),
AccountMeta::new_readonly(clockwork_pool::state::Config::pubkey(), false),
AccountMeta::new(clockwork_network::state::Rotator::pubkey(), false),
AccountMeta::new(signer, true),
AccountMeta::new_readonly(snapshot, false),
Expand Down
23 changes: 23 additions & 0 deletions client/src/pool/instruction/initialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use anchor_lang::{
solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
system_program,
},
InstructionData,
};

pub fn initialize(admin: Pubkey) -> Instruction {
let config_pubkey = clockwork_pool::state::Config::pubkey();
let pool_authority = clockwork_network::state::Rotator::pubkey();

Instruction {
program_id: clockwork_pool::ID,
accounts: vec![
AccountMeta::new(admin, true),
AccountMeta::new(config_pubkey, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: clockwork_pool::instruction::Initialize { pool_authority }.data(),
}
}
3 changes: 3 additions & 0 deletions client/src/pool/instruction/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod initialize;

pub use initialize::*;
2 changes: 2 additions & 0 deletions client/src/pool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod instruction;

pub use clockwork_pool::state;
pub use clockwork_pool::ID;
4 changes: 2 additions & 2 deletions programs/crank/src/instructions/config_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use {crate::state::*, anchor_lang::prelude::*};
#[derive(Accounts)]
#[instruction(settings: ConfigSettings)]
pub struct ConfigUpdate<'info> {
#[account(mut, address = config.admin)]
#[account(mut)]
pub admin: Signer<'info>,

#[account(mut, seeds = [SEED_CONFIG], bump)]
#[account(mut, seeds = [SEED_CONFIG], bump, has_one = admin)]
pub config: Account<'info, Config>,
}

Expand Down
3 changes: 0 additions & 3 deletions programs/network/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ pub enum ClockworkError {
#[msg("This account has already been initialized")]
AccountAlreadyInitialized,

#[msg("This instruction requires admin authority")]
AdminAuthorityInvalid,

#[msg("The provided node is cannot be used for this operation")]
InvalidNode,

Expand Down
16 changes: 16 additions & 0 deletions programs/network/src/instructions/config_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use {crate::state::*, anchor_lang::prelude::*};

#[derive(Accounts)]
#[instruction(settings: ConfigSettings)]
pub struct ConfigUpdate<'info> {
#[account(mut)]
pub admin: Signer<'info>,

#[account(mut, seeds = [SEED_CONFIG], bump, has_one = admin)]
pub config: Account<'info, Config>,
}

pub fn handler(ctx: Context<ConfigUpdate>, settings: ConfigSettings) -> Result<()> {
let config = &mut ctx.accounts.config;
config.update(settings)
}
2 changes: 2 additions & 0 deletions programs/network/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config_update;
pub mod entry_close;
pub mod entry_create;
pub mod initialize;
Expand All @@ -14,6 +15,7 @@ pub mod snapshot_pause;
pub mod snapshot_resume;
pub mod snapshot_rotate;

pub use config_update::*;
pub use entry_close::*;
pub use entry_create::*;
pub use initialize::*;
Expand Down
21 changes: 13 additions & 8 deletions programs/network/src/instructions/pool_create.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use {
crate::state::*,
anchor_lang::{prelude::*, solana_program::system_program, system_program::{transfer, Transfer}},
clockwork_pool::{program::ClockworkPool, state::SEED_POOL}
anchor_lang::{
prelude::*,
solana_program::system_program,
system_program::{transfer, Transfer},
},
clockwork_pool::{program::ClockworkPool, state::SEED_POOL},
};

#[derive(Accounts)]
Expand All @@ -13,16 +17,15 @@ pub struct PoolCreate<'info> {
#[account(seeds = [SEED_CONFIG], bump, has_one = admin)]
pub config: Account<'info, Config>,

#[account(
seeds = [SEED_POOL, name.as_bytes()],
seeds::program = clockwork_pool::ID,
bump,
)]
#[account(seeds = [SEED_POOL, name.as_bytes()], seeds::program = clockwork_pool::ID, bump)]
pub pool: SystemAccount<'info>,

#[account(address = clockwork_pool::ID)]
pub pool_program: Program<'info, ClockworkPool>,

#[account(seeds = [SEED_CONFIG], seeds::program = clockwork_pool::ID, bump)]
pub pool_program_config: Account<'info, clockwork_pool::state::Config>,

#[account(mut, seeds = [SEED_ROTATOR], bump)]
pub rotator: Account<'info, Rotator>,

Expand All @@ -35,6 +38,7 @@ pub fn handler(ctx: Context<PoolCreate>, name: String, size: usize) -> Result<()
let admin = &ctx.accounts.admin;
let pool = &ctx.accounts.pool;
let pool_program = &ctx.accounts.pool_program;
let pool_program_config = &ctx.accounts.pool_program_config;
let rotator = &mut ctx.accounts.rotator;
let system_program = &ctx.accounts.system_program;

Expand All @@ -44,9 +48,10 @@ pub fn handler(ctx: Context<PoolCreate>, name: String, size: usize) -> Result<()
CpiContext::new_with_signer(
pool_program.to_account_info(),
clockwork_pool::cpi::accounts::PoolCreate {
authority: rotator.to_account_info(),
config: pool_program_config.to_account_info(),
payer: admin.to_account_info(),
pool: pool.to_account_info(),
pool_authority: rotator.to_account_info(),
system_program: system_program.to_account_info(),
},
&[&[SEED_ROTATOR, &[rotator_bump]]],
Expand Down
26 changes: 9 additions & 17 deletions programs/network/src/instructions/pools_rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,17 @@ pub struct PoolsRotate<'info> {
)]
pub entry: Account<'info, SnapshotEntry>,

#[account(
seeds = [
SEED_NODE,
node.id.to_be_bytes().as_ref()
],
bump,
constraint = node.id == entry.id
)]
#[account(seeds = [SEED_NODE, node.id.to_be_bytes().as_ref()], bump, constraint = node.id == entry.id)]
pub node: Account<'info, Node>,

#[account(address = clockwork_pool::ID)]
pub pool_program: Program<'info, clockwork_pool::program::ClockworkPool>,

#[account(seeds = [SEED_CONFIG], bump, seeds::program = clockwork_pool::ID)]
pub pool_program_config: Account<'info, clockwork_pool::state::Config>,

#[account(
mut,
seeds = [SEED_ROTATOR],
bump,
mut, seeds = [SEED_ROTATOR], bump,
constraint = Clock::get().unwrap().slot >= rotator.last_rotation_at.checked_add(config.slots_per_rotation).unwrap()
)]
pub rotator: Account<'info, Rotator>,
Expand All @@ -50,11 +44,7 @@ pub struct PoolsRotate<'info> {
pub signer: Signer<'info>,

#[account(
seeds = [
SEED_SNAPSHOT,
snapshot.id.to_be_bytes().as_ref()
],
bump,
seeds = [SEED_SNAPSHOT, snapshot.id.to_be_bytes().as_ref()], bump,
constraint = snapshot.status == SnapshotStatus::Current @ ClockworkError::SnapshotNotCurrent
)]
pub snapshot: Account<'info, Snapshot>,
Expand All @@ -67,6 +57,7 @@ pub fn handler<'info>(ctx: Context<'_, '_, '_, 'info, PoolsRotate<'info>>) -> Re
// Get accounts
let node = &ctx.accounts.node;
let pool_program = &ctx.accounts.pool_program;
let pool_program_config = &ctx.accounts.pool_program_config;
let rotator = &mut ctx.accounts.rotator;
let worker = &ctx.accounts.worker;

Expand All @@ -89,8 +80,9 @@ pub fn handler<'info>(ctx: Context<'_, '_, '_, 'info, PoolsRotate<'info>>) -> Re
CpiContext::new_with_signer(
pool_program.to_account_info(),
PoolRotate {
authority: rotator.to_account_info(),
config: pool_program_config.to_account_info(),
pool: pool_acc_info.clone(),
pool_authority: rotator.to_account_info(),
worker: worker.to_account_info(),
},
&[&[SEED_ROTATOR, &[rotator_bump]]],
Expand Down
4 changes: 4 additions & 0 deletions programs/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use state::*;
pub mod clockwork_network {
use super::*;

pub fn config_update(ctx: Context<ConfigUpdate>, settings: ConfigSettings) -> Result<()> {
config_update::handler(ctx, settings)
}

pub fn entry_close(ctx: Context<EntryClose>) -> Result<CrankResponse> {
entry_close::handler(ctx)
}
Expand Down
11 changes: 4 additions & 7 deletions programs/network/src/state/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use {
crate::errors::ClockworkError,
anchor_lang::{prelude::*, AnchorDeserialize},
std::convert::TryFrom,
};
Expand Down Expand Up @@ -51,7 +50,7 @@ pub struct ConfigSettings {
pub trait ConfigAccount {
fn init(&mut self, admin: Pubkey, mint: Pubkey) -> Result<()>;

fn update(&mut self, admin: &Signer, settings: ConfigSettings) -> Result<()>;
fn update(&mut self, settings: ConfigSettings) -> Result<()>;
}

impl ConfigAccount for Account<'_, Config> {
Expand All @@ -62,12 +61,10 @@ impl ConfigAccount for Account<'_, Config> {
Ok(())
}

fn update(&mut self, admin: &Signer, settings: ConfigSettings) -> Result<()> {
require!(
self.admin == admin.key(),
ClockworkError::AdminAuthorityInvalid
);
fn update(&mut self, settings: ConfigSettings) -> Result<()> {
self.admin = settings.admin;
self.mint = settings.mint;
self.slots_per_rotation = settings.slots_per_rotation;
Ok(())
}
}
16 changes: 16 additions & 0 deletions programs/pool/src/instructions/config_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use {crate::state::*, anchor_lang::prelude::*};

#[derive(Accounts)]
#[instruction(settings: ConfigSettings)]
pub struct ConfigUpdate<'info> {
#[account(mut)]
pub admin: Signer<'info>,

#[account(mut, seeds = [SEED_CONFIG], bump, has_one = admin)]
pub config: Account<'info, Config>,
}

pub fn handler(ctx: Context<ConfigUpdate>, settings: ConfigSettings) -> Result<()> {
let config = &mut ctx.accounts.config;
config.update(settings)
}
33 changes: 33 additions & 0 deletions programs/pool/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use {
crate::state::*,
anchor_lang::{prelude::*, solana_program::system_program},
std::mem::size_of,
};

#[derive(Accounts)]
#[instruction(pool_authority: Pubkey)]
pub struct Initialize<'info> {
#[account(mut)]
pub admin: Signer<'info>,

#[account(
init,
seeds = [SEED_CONFIG],
bump,
payer = admin,
space = 8 + size_of::<Config>(),
)]
pub config: Account<'info, Config>,

#[account(address = system_program::ID)]
pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<Initialize>, pool_authority: Pubkey) -> Result<()> {
let admin = &ctx.accounts.admin;
let config = &mut ctx.accounts.config;

config.init(admin.key(), pool_authority)?;

Ok(())
}
4 changes: 4 additions & 0 deletions programs/pool/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
pub mod config_update;
pub mod initialize;
pub mod pool_create;
pub mod pool_rotate;
pub mod pool_update;

pub use config_update::*;
pub use initialize::*;
pub use pool_create::*;
pub use pool_rotate::*;
pub use pool_update::*;
Loading

0 comments on commit 5d5cd06

Please sign in to comment.