-
Notifications
You must be signed in to change notification settings - Fork 4
feat: whitelist validator for program #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
[toolchain] | ||
channel = "1.75.0" | ||
channel = "1.79.0" | ||
components = [ "rustfmt", "rust-analyzer", "clippy"] | ||
targets = [ "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin"] | ||
profile = "minimal" |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,120 @@ | ||
use crate::consts::{ADMIN_PUBKEY, PROGRAM_CONFIG}; | ||
use crate::error::DlpError::Unauthorized; | ||
use crate::instruction::WhitelistValidatorForProgramArgs; | ||
use crate::state::WhitelistForProgram; | ||
use crate::utils::loaders::{load_pda, load_signer}; | ||
use crate::utils::utils_pda::{create_pda, resize_pda}; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use solana_program::bpf_loader_upgradeable::UpgradeableLoaderState; | ||
use solana_program::program_error::ProgramError; | ||
use solana_program::{ | ||
account_info::AccountInfo, | ||
bpf_loader_upgradeable, | ||
entrypoint::ProgramResult, | ||
pubkey::Pubkey, | ||
{self}, | ||
}; | ||
|
||
/// Whitelist a validator for a program | ||
/// | ||
pub fn process_whitelist_validator_for_program( | ||
_program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
data: &[u8], | ||
) -> ProgramResult { | ||
let args = WhitelistValidatorForProgramArgs::try_from_slice(data)?; | ||
|
||
// Load Accounts | ||
let [authority, validator_identity, program, program_data, program_config_account, system_program] = | ||
accounts | ||
else { | ||
return Err(ProgramError::NotEnoughAccountKeys); | ||
}; | ||
|
||
load_signer(authority)?; | ||
validate_authority(authority, program, program_data)?; | ||
GabrielePicco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let bump = load_pda( | ||
program_config_account, | ||
&[PROGRAM_CONFIG, program.key.as_ref()], | ||
&crate::id(), | ||
true, | ||
)?; | ||
|
||
// Get the program whitelist. If the account doesn't exist, create it | ||
let mut program_whitelist = if program_config_account.owner.eq(system_program.key) { | ||
create_pda( | ||
program_config_account, | ||
&crate::id(), | ||
8 + WhitelistForProgram::default().serialized_len(), | ||
&[PROGRAM_CONFIG, program.key.as_ref(), &[bump]], | ||
system_program, | ||
authority, | ||
)?; | ||
WhitelistForProgram::default() | ||
} else { | ||
let data = program_config_account.try_borrow_data()?; | ||
WhitelistForProgram::try_from_slice(&data)? | ||
}; | ||
if args.insert { | ||
program_whitelist | ||
.approved_validators | ||
.insert(*validator_identity.key); | ||
} else { | ||
program_whitelist | ||
.approved_validators | ||
.remove(validator_identity.key); | ||
} | ||
resize_pda( | ||
authority, | ||
program_config_account, | ||
system_program, | ||
program_whitelist.serialized_len(), | ||
)?; | ||
let mut data = program_config_account.try_borrow_mut_data()?; | ||
program_whitelist.serialize(&mut &mut data.as_mut())?; | ||
GabrielePicco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Ok(()) | ||
} | ||
|
||
/// Authority is valid if either the authority is the ADMIN_PUBKEY or the program upgrade authority | ||
fn validate_authority( | ||
authority: &AccountInfo, | ||
program: &AccountInfo, | ||
program_data: &AccountInfo, | ||
) -> Result<(), ProgramError> { | ||
if authority.key.eq(&ADMIN_PUBKEY) | ||
|| authority | ||
.key | ||
.eq(&get_program_upgrade_authority(program, program_data)?.ok_or(Unauthorized)?) | ||
{ | ||
Ok(()) | ||
} else { | ||
Err(Unauthorized.into()) | ||
} | ||
} | ||
|
||
/// Get the program upgrade authority for a given program | ||
fn get_program_upgrade_authority( | ||
program: &AccountInfo, | ||
program_data: &AccountInfo, | ||
) -> Result<Option<Pubkey>, ProgramError> { | ||
let program_data_address = | ||
Pubkey::find_program_address(&[program.key.as_ref()], &bpf_loader_upgradeable::id()).0; | ||
|
||
if !program_data_address.eq(program_data.key) { | ||
return Err(ProgramError::InvalidAccountData); | ||
} | ||
|
||
let program_account_data = program_data.try_borrow_data()?; | ||
if let UpgradeableLoaderState::ProgramData { | ||
GabrielePicco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
upgrade_authority_address, | ||
.. | ||
} = | ||
bincode::deserialize(&program_account_data).map_err(|_| ProgramError::InvalidAccountData)? | ||
{ | ||
Ok(upgrade_authority_address) | ||
} else { | ||
Err(ProgramError::InvalidAccountData) | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod commit_record; | ||
mod delegated_metadata; | ||
mod delegation_record; | ||
mod whitelist_for_program; | ||
|
||
pub use commit_record::*; | ||
pub use delegated_metadata::*; | ||
pub use delegation_record::*; | ||
pub use whitelist_for_program::*; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.