deregister fixes

This commit is contained in:
Hardhat Chad
2024-05-13 15:06:14 +00:00
parent 56e20bf78e
commit a71024cfa1
3 changed files with 26 additions and 36 deletions

View File

@@ -258,6 +258,20 @@ pub fn register(signer: Pubkey) -> Instruction {
}
}
/// Builds a deregister instruction.
pub fn deregister(signer: Pubkey) -> Instruction {
let proof_pda = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id());
Instruction {
program_id: crate::id(),
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(proof_pda.0, false),
AccountMeta::new_readonly(solana_program::system_program::id(), false),
],
data: OreInstruction::Deregister.to_vec(),
}
}
/// Builds a mine instruction.
pub fn mine(signer: Pubkey, bus: Pubkey, nonce: u64) -> Instruction {
let proof = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()).0;

View File

@@ -1,14 +1,13 @@
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed,
program_error::ProgramError, pubkey::Pubkey, system_program,
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey, system_program,
};
use crate::{loaders::*, state::Proof, utils::AccountDeserialize, PROOF};
use crate::{loaders::*, state::Proof, utils::AccountDeserialize};
/// Deregister closes a proof account and returns the rent to the owner. Its responsibilities include:
/// 1. Realloc proof account size to 0.
/// 2. Transfer lamports to the owner.
/// 3. Reassign the account owner back to the system program.
///
/// Safety requirements:
/// - Deregister is a permissionless instruction and can be invoked by any singer.
@@ -33,31 +32,14 @@ pub fn process_deregister<'a, 'info>(
if proof.balance.gt(&0) {
return Err(ProgramError::InvalidAccountData);
}
// Generate bump
let bump = Pubkey::find_program_address(&[PROOF, signer.key.as_ref()], &crate::id()).1;
drop(proof_data);
// Realloc data to zero
drop(proof_data);
proof_info.realloc(0, false)?;
proof_info.realloc(0, true)?;
// Send lamports to signer
invoke_signed(
&solana_program::system_instruction::transfer(
proof_info.key,
signer.key,
proof_info.lamports(),
),
&[proof_info.clone(), signer.clone(), system_program.clone()],
&[&[PROOF, signer.key.as_ref(), &[bump]]],
)?;
// Reassign back to system program
solana_program::program::invoke_signed(
&solana_program::system_instruction::assign(proof_info.key, &system_program::id()),
&[proof_info.clone(), system_program.clone()],
&[&[PROOF, signer.key.as_ref(), &[bump]]],
)?;
**signer.lamports.borrow_mut() = signer.lamports().saturating_add(proof_info.lamports());
**proof_info.lamports.borrow_mut() = 0;
Ok(())
}

View File

@@ -48,9 +48,9 @@ pub(crate) fn create_pda<'a, 'info>(
rent_exempt_balance,
),
&[
payer.as_ref().clone(),
target_account.as_ref().clone(),
system_program.as_ref().clone(),
payer.clone(),
target_account.clone(),
system_program.clone(),
],
)?;
}
@@ -58,20 +58,14 @@ pub(crate) fn create_pda<'a, 'info>(
// 2) allocate space for the account
solana_program::program::invoke_signed(
&solana_program::system_instruction::allocate(target_account.key, space as u64),
&[
target_account.as_ref().clone(),
system_program.as_ref().clone(),
],
&[target_account.clone(), system_program.clone()],
&[pda_seeds],
)?;
// 3) assign our program as the owner
solana_program::program::invoke_signed(
&solana_program::system_instruction::assign(target_account.key, owner),
&[
target_account.as_ref().clone(),
system_program.as_ref().clone(),
],
&[target_account.clone(), system_program.clone()],
&[pda_seeds],
)?;
}