From a71024cfa1e62ad251daaee3f96ab90090316702 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Mon, 13 May 2024 15:06:14 +0000 Subject: [PATCH] deregister fixes --- src/instruction.rs | 14 ++++++++++++++ src/processor/deregister.rs | 32 +++++++------------------------- src/utils.rs | 16 +++++----------- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/instruction.rs b/src/instruction.rs index 949e87a..1d94c13 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -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; diff --git a/src/processor/deregister.rs b/src/processor/deregister.rs index 8ec8d4a..ea69397 100644 --- a/src/processor/deregister.rs +++ b/src/processor/deregister.rs @@ -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(()) } diff --git a/src/utils.rs b/src/utils.rs index ed4398e..cdb9779 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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], )?; }