Skip to content

Commit

Permalink
Use seeds/bumps for account verification where needed (#70)
Browse files Browse the repository at this point in the history
* Reintroduce seeds and bumps where needed

* Resolve final instance of required seeds/bump check

Co-authored-by: Nick Garfield <[email protected]>
  • Loading branch information
nickgarfield and Nick Garfield authored Oct 4, 2022
1 parent 14adabf commit c0e4650
Show file tree
Hide file tree
Showing 34 changed files with 124 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cli/src/processor/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use {
};

pub fn initialize(client: &Client, mint: Pubkey) -> Result<(), CliError> {
// TODO Create a worker pool
// Create a worker pool
let pool_name = "crank";
let pool = Pool::pubkey(pool_name.into());

Expand Down
7 changes: 6 additions & 1 deletion programs/network/src/instructions/entry_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ pub struct EntryCreate<'info> {

#[account(
init,
address = SnapshotEntry::pubkey(snapshot.key(), snapshot.node_count),
seeds = [
SEED_SNAPSHOT_ENTRY,
snapshot.key().as_ref(),
snapshot.node_count.to_be_bytes().as_ref(),
],
bump,
payer = payer,
space = 8 + size_of::<SnapshotEntry>(),
)]
Expand Down
18 changes: 13 additions & 5 deletions programs/network/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub struct Initialize<'info> {

#[account(
init,
address = Authority::pubkey(),
seeds = [SEED_AUTHORITY],
bump,
payer = admin,
space = 8 + size_of::<Authority>(),
)]
Expand All @@ -27,15 +28,17 @@ pub struct Initialize<'info> {

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

#[account(
init,
address = Rotator::pubkey(),
seeds = [SEED_ROTATOR],
bump,
payer = admin,
space = 8 + size_of::<Rotator>(),
)]
Expand All @@ -46,15 +49,20 @@ pub struct Initialize<'info> {

#[account(
init,
address = Registry::pubkey(),
seeds = [SEED_REGISTRY],
bump,
payer = admin,
space = 8 + size_of::<Registry>(),
)]
pub registry: Account<'info, Registry>,

#[account(
init,
address = Snapshot::pubkey(0),
seeds = [
SEED_SNAPSHOT,
(0 as u64).to_be_bytes().as_ref(),
],
bump,
payer = admin,
space = 8 + size_of::<Snapshot>(),
)]
Expand Down
13 changes: 11 additions & 2 deletions programs/network/src/instructions/node_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ pub struct NodeRegister<'info> {

#[account(
init,
address = SnapshotEntry::pubkey(snapshot.key(), snapshot.node_count),
seeds = [
SEED_SNAPSHOT_ENTRY,
snapshot.key().as_ref(),
snapshot.id.to_be_bytes().as_ref(),
],
bump,
payer = authority,
space = 8 + size_of::<SnapshotEntry>(),
)]
Expand All @@ -35,7 +40,11 @@ pub struct NodeRegister<'info> {

#[account(
init,
address = Node::pubkey(registry.node_count),
seeds = [
SEED_NODE,
registry.node_count.to_be_bytes().as_ref(),
],
bump,
payer = authority,
space = 8 + size_of::<Node>(),
)]
Expand Down
8 changes: 7 additions & 1 deletion programs/network/src/instructions/node_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ pub struct NodeStake<'info> {
#[account(address = Config::pubkey())]
pub config: Account<'info, Config>,

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

#[account(
Expand Down
6 changes: 5 additions & 1 deletion programs/network/src/instructions/node_unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub struct NodeUnstake<'info> {
pub config: Account<'info, Config>,

#[account(
address = node.pubkey(),
seeds = [
SEED_NODE,
node.id.to_be_bytes().as_ref(),
],
bump,
has_one = authority,
)]
pub node: Account<'info, Node>,
Expand Down
6 changes: 3 additions & 3 deletions programs/network/src/instructions/pool_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct PoolCreate<'info> {
#[account(address = clockwork_pool_program::objects::Config::pubkey())]
pub pool_program_config: Account<'info, clockwork_pool_program::objects::Config>,

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

#[account(address = system_program::ID)]
Expand All @@ -43,7 +43,7 @@ pub fn handler(ctx: Context<PoolCreate>, name: String, size: usize) -> Result<()
let system_program = &ctx.accounts.system_program;

// Rotate the worker into its supported pools
let rotator_bump = *ctx.bumps.get("rotator").unwrap();
let bump = *ctx.bumps.get("rotator").unwrap();
clockwork_pool_program::cpi::pool_create(
CpiContext::new_with_signer(
pool_program.to_account_info(),
Expand All @@ -54,7 +54,7 @@ pub fn handler(ctx: Context<PoolCreate>, name: String, size: usize) -> Result<()
pool_authority: rotator.to_account_info(),
system_program: system_program.to_account_info(),
},
&[&[SEED_ROTATOR, &[rotator_bump]]],
&[&[SEED_ROTATOR, &[bump]]],
),
name,
size,
Expand Down
7 changes: 4 additions & 3 deletions programs/network/src/instructions/pools_rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ pub struct PoolsRotate<'info> {

#[account(
mut,
address = Rotator::pubkey(),
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 Down Expand Up @@ -64,7 +65,7 @@ pub fn handler<'info>(ctx: Context<'_, '_, '_, 'info, PoolsRotate<'info>>) -> Re
require!(rotator.pool_pubkeys.len() == ctx.remaining_accounts.len(), ClockworkError::InvalidPool);

// Rotate the worker into its supported pools
let rotator_bump = *ctx.bumps.get("rotator").unwrap();
let bump = *ctx.bumps.get("rotator").unwrap();
for i in 0..ctx.remaining_accounts.len() {
match ctx.remaining_accounts.get(i) {
None => return Err(ClockworkError::InvalidPool.into()),
Expand All @@ -84,7 +85,7 @@ pub fn handler<'info>(ctx: Context<'_, '_, '_, 'info, PoolsRotate<'info>>) -> Re
pool_authority: rotator.to_account_info(),
worker: worker.to_account_info(),
},
&[&[SEED_ROTATOR, &[rotator_bump]]],
&[&[SEED_ROTATOR, &[bump]]],
),
)?;
}
Expand Down
6 changes: 5 additions & 1 deletion programs/network/src/instructions/snapshot_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ pub struct SnapshotCreate<'info> {

#[account(
init,
address = Snapshot::pubkey(registry.snapshot_count),
seeds = [
SEED_SNAPSHOT,
registry.snapshot_count.to_be_bytes().as_ref(),
],
bump,
space = 8 + size_of::<Snapshot>(),
payer = payer
)]
Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/instructions/snapshot_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct SnapshotPause<'info> {
#[account(mut)]
pub admin: Signer<'info>,

#[account(address = Authority::pubkey())]
#[account(seeds = [SEED_AUTHORITY], bump)]
pub authority: Account<'info, Authority>,

#[account(address = clockwork_queue_program::ID)]
Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/instructions/snapshot_resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct SnapshotResume<'info> {
#[account(mut)]
pub admin: Signer<'info>,

#[account(address = Authority::pubkey())]
#[account(seeds = [SEED_AUTHORITY], bump)]
pub authority: Account<'info, Authority>,

#[account(address = clockwork_queue_program::ID)]
Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/objects/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
std::convert::TryFrom,
};

const SEED_CONFIG: &[u8] = b"config";
pub const SEED_CONFIG: &[u8] = b"config";

static DEFAULT_SLOTS_PER_ROTATION: u64 = 10;

Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/objects/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
std::convert::TryFrom,
};

const SEED_REGISTRY: &[u8] = b"registry";
pub const SEED_REGISTRY: &[u8] = b"registry";

/**
* Registry
Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/objects/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
std::convert::TryFrom,
};

const SEED_SNAPSHOT: &[u8] = b"snapshot";
pub const SEED_SNAPSHOT: &[u8] = b"snapshot";

/**
* Snapshot
Expand Down
2 changes: 1 addition & 1 deletion programs/network/src/objects/snapshot_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
std::convert::TryFrom,
};

const SEED_SNAPSHOT_ENTRY: &[u8] = b"snapshot_entry";
pub const SEED_SNAPSHOT_ENTRY: &[u8] = b"snapshot_entry";

/**
* SnapshotEntry
Expand Down
3 changes: 2 additions & 1 deletion programs/pool/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct Initialize<'info> {

#[account(
init,
address = Config::pubkey(),
seeds = [SEED_CONFIG],
bump,
payer = admin,
space = 8 + size_of::<Config>(),
)]
Expand Down
6 changes: 5 additions & 1 deletion programs/pool/src/instructions/pool_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub struct PoolCreate<'info> {

#[account(
init,
address = Pool::pubkey(name),
seeds = [
SEED_POOL,
name.as_bytes(),
],
bump,
payer = payer,
space = 8 + size_of::<Pool>() + (size_of::<Pubkey>() * size) + name.as_bytes().len(),
)]
Expand Down
2 changes: 1 addition & 1 deletion programs/pool/src/objects/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anchor_lang::{prelude::*, AnchorDeserialize};

const SEED_CONFIG: &[u8] = b"config";
pub const SEED_CONFIG: &[u8] = b"config";

/**
* Config
Expand Down
2 changes: 1 addition & 1 deletion programs/pool/src/objects/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
std::{collections::VecDeque, convert::TryFrom},
};

const SEED_POOL: &[u8] = b"pool";
pub const SEED_POOL: &[u8] = b"pool";

/**
* Pool
Expand Down
3 changes: 2 additions & 1 deletion programs/queue/src/instructions/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub struct Initialize<'info> {
/// The program config account.
#[account(
init,
address = Config::pubkey(),
seeds = [SEED_CONFIG],
bump,
payer = admin,
space = 8 + size_of::<Config>(),
)]
Expand Down
13 changes: 11 additions & 2 deletions programs/queue/src/instructions/queue_crank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ pub struct QueueCrank<'info> {
/// The worker's fee account.
#[account(
init_if_needed,
address = Fee::pubkey(worker.key()),
seeds = [
SEED_FEE,
worker.key().as_ref(),
],
bump,
payer = worker,
space = 8 + size_of::<Fee>(),
)]
Expand All @@ -39,7 +43,12 @@ pub struct QueueCrank<'info> {
/// The queue to crank.
#[account(
mut,
address = queue.pubkey(),
seeds = [
SEED_QUEUE,
queue.authority.as_ref(),
queue.id.as_bytes(),
],
bump,
constraint = !queue.paused @ ClockworkError::PausedQueue
)]
pub queue: Box<Account<'info, Queue>>,
Expand Down
7 changes: 6 additions & 1 deletion programs/queue/src/instructions/queue_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ pub struct QueueCreate<'info> {
/// The queue to be created.
#[account(
init,
address = Queue::pubkey(authority.key(), id),
seeds = [
SEED_QUEUE,
authority.key().as_ref(),
id.as_bytes(),
],
bump,
payer = payer,
space = vec![
8,
Expand Down
2 changes: 1 addition & 1 deletion programs/queue/src/objects/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
std::convert::TryFrom,
};

const SEED_CONFIG: &[u8] = b"config";
pub const SEED_CONFIG: &[u8] = b"config";

static DEFAULT_CRANK_FEE: u64 = 1_000;

Expand Down
2 changes: 1 addition & 1 deletion programs/queue/src/objects/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
std::convert::TryFrom,
};

const SEED_FEE: &[u8] = b"fee";
pub const SEED_FEE: &[u8] = b"fee";

/// Escrows the lamport balance owed to a particular worker.
#[account]
Expand Down
2 changes: 1 addition & 1 deletion programs/queue/src/objects/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
},
};

const SEED_QUEUE: &[u8] = b"queue";
pub const SEED_QUEUE: &[u8] = b"queue";

const DEFAULT_RATE_LIMIT: u64 = 10;

Expand Down
9 changes: 7 additions & 2 deletions programs/webhook/src/instructions/api_new.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::objects::{Api, ApiAccount},
crate::objects::{Api, ApiAccount, SEED_API},
anchor_lang::{prelude::*, system_program},
std::mem::size_of,
};
Expand All @@ -12,7 +12,12 @@ pub struct ApiNew<'info> {

#[account(
init,
address = Api::pubkey(authority.key(), base_url),
seeds = [
SEED_API,
authority.key().as_ref(),
base_url.as_bytes(),
],
bump,
payer = payer,
space = 8 + size_of::<Api>() + base_url.len(),
)]
Expand Down
Loading

0 comments on commit c0e4650

Please sign in to comment.