Skip to content

Commit

Permalink
feat(validator): added manager stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ogmedia committed Nov 14, 2022
1 parent 332c29a commit de7f4fe
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions programs/validator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "validator"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "validator"

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.25.0"
squads-mpl = { path = "../squads-mpl", features = ["cpi"] }
2 changes: 2 additions & 0 deletions programs/validator/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
106 changes: 106 additions & 0 deletions programs/validator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use anchor_lang::prelude::*;
use state::validator::*;
use squads_mpl::state::*;
use squads_mpl::errors::*;
pub mod state;

declare_id!("");

#[program]
pub mod validator {
use anchor_lang::solana_program::{bpf_loader_upgradeable::upgrade};

use super::*;

pub fn create_validator_manager(ctx: Context<CreateManager>)-> Result<()>{
let validator_manager = &mut ctx.accounts.validator_manager;
validator_manager.init(
ctx.accounts.multisig.key(),
*ctx.bumps.get("validator_manager").unwrap(),
)
}

pub fn create_managed_validator(ctx: Context<CreateManagedValidator>, validator_address: Pubkey, name: String)->Result<()>{
let managed_validator = &mut ctx.accounts.managed_validator;
let validator_manager = &mut ctx.accounts.validator_manager;
let new_mvi = validator_manager.managed_validator_index.checked_add(1).unwrap();
managed_validator.init(
validator_address,
ctx.accounts.multisig.key(),
*ctx.bumps.get("managed_validator").unwrap(),
name,
new_mvi
)?;
validator_manager.managed_validator_index = new_mvi;
Ok(())
}



}

#[derive(Accounts)]
pub struct CreateManager<'info> {
#[account(
owner = squads_mpl::ID,
constraint = matches!(multisig.is_member(creator.key()), Some(..)) @MsError::KeyNotInMultisig,
)]
pub multisig: Account<'info, Ms>,

#[account(
init,
payer = creator,
space = ValidatorManager::MAXIMUM_SIZE,
seeds = [
b"squad",
multisig.key().as_ref(),
b"vmanage"
],
bump
)]
pub validator_manager: Account<'info,ValidatorManager>,

#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>
}


#[derive(Accounts)]
#[instruction(program_address: Pubkey, name: String)]
pub struct CreateManagedValidator<'info> {
#[account(
owner = squads_mpl::ID,
constraint = matches!(multisig.is_member(creator.key()), Some(..)) @MsError::KeyNotInMultisig,
)]
pub multisig: Account<'info, Ms>,

#[account(
mut,
seeds = [
b"squad",
multisig.key().as_ref(),
b"vmanage"
],
bump = validator_manager.bump
)]
pub validator_manager: Account<'info, ValidatorManager>,

#[account(
init,
payer = creator,
space = ManagedValidator::MINIMUM_SIZE + name.try_to_vec().unwrap().len() + 4,
seeds = [
b"squad",
validator_manager.key().as_ref(),
&validator_manager.managed_validator_index.checked_add(1).unwrap().to_le_bytes(),
b"validator"
],
bump
)]
pub managed_validator: Account<'info,ManagedValidator>,

#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>
}
2 changes: 2 additions & 0 deletions programs/validator/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub use validator::*;
pub mod validator;
50 changes: 50 additions & 0 deletions programs/validator/src/state/validator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use anchor_lang::{prelude::*, solana_program::instruction::Instruction};
use anchor_lang::solana_program::borsh::get_instance_packed_len;

#[account]
pub struct ValidatorManager {
pub multisig: Pubkey,
pub managed_validator_index: u32,
pub bump: u8,
}

impl ValidatorManager {
pub const MAXIMUM_SIZE: usize = 8 + // anchor discriminator
32 + // multisig key (used to derive as well)
4 + // to track the validators
1; // bump

pub fn init(&mut self, multisig: Pubkey, bump: u8) -> Result<()>{
self.multisig = multisig;
self.bump = bump;
self.managed_validator_index = 0;
Ok(())
}
}

#[account]
pub struct ManagedValidator {
pub managed_validator_index: u32,
pub validator_address: Pubkey,
pub multisig: Pubkey,
pub bump: u8,
pub name: String,
}

impl ManagedValidator {
// minimum size, as name will be dynamic
pub const MINIMUM_SIZE: usize = 8 + // anchor disrciminator
4 + // the managed validator index
32 + // the validator address
32 + // the multisig address
1; // the bump of the PDA deriv

pub fn init(&mut self, validator_address: Pubkey, multisig: Pubkey, bump: u8, name: String, managed_validator_index: u32) -> Result<()>{
self.managed_validator_index = managed_validator_index;
self.validator_address = validator_address;
self.multisig = multisig;
self.bump = bump;
self.name = name;
Ok(())
}
}

0 comments on commit de7f4fe

Please # to comment.