-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub use validator::*; | ||
pub mod validator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |