From de42a695258d0c2acf167626bd720d118959d35b Mon Sep 17 00:00:00 2001 From: Samuel Vanderwaal Date: Sun, 6 Nov 2022 07:25:27 -0900 Subject: [PATCH] remove redundant CPI; clean up account closing (#880) * remove redundant CPI; clean up account closing * fix runtime borrow error * remove account info reference * fix double mut borrow issue * fix cargo release --- auction-house/program/src/execute_sale/mod.rs | 143 +++++------------- auction-house/program/src/utils.rs | 24 ++- release.toml | 1 - 3 files changed, 61 insertions(+), 107 deletions(-) diff --git a/auction-house/program/src/execute_sale/mod.rs b/auction-house/program/src/execute_sale/mod.rs index c0737f0f60..0a8376f5db 100644 --- a/auction-house/program/src/execute_sale/mod.rs +++ b/auction-house/program/src/execute_sale/mod.rs @@ -1,10 +1,5 @@ use crate::{constants::*, errors::*, utils::*, AuctionHouse, Auctioneer, AuthorityScope, *}; -use anchor_lang::{ - prelude::*, - solana_program::{program::invoke, program_pack::Pack}, - AnchorDeserialize, -}; -use solana_program::program_memory::sol_memset; +use anchor_lang::{prelude::*, solana_program::program_pack::Pack, AnchorDeserialize}; use spl_token::state::Account as SplAccount; /// Accounts for the [`execute_sale` handler](auction_house/fn.execute_sale.html). @@ -1047,16 +1042,17 @@ fn auctioneer_execute_sale_logic<'c, 'info>( msg!("No delegate detected on token account."); return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into()); } - let buyer_ts_data = &mut buyer_trade_state.try_borrow_mut_data()?; - let seller_ts_data = &mut seller_trade_state.try_borrow_mut_data()?; - let ts_bump = if buyer_ts_data.len() > 0 { - buyer_ts_data[0] + let ts_bump = if buyer_trade_state.data_len() > 0 { + buyer_trade_state.try_borrow_data()?[0] } else { return Err(AuctionHouseError::BuyerTradeStateNotValid.into()); }; - if ts_bump == 0 || seller_ts_data.len() == 0 || seller_ts_data[0] == 0 { + if ts_bump == 0 + || seller_trade_state.data_len() == 0 + || seller_trade_state.try_borrow_data()?[0] == 0 + { return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into()); } @@ -1342,55 +1338,23 @@ fn auctioneer_execute_sale_logic<'c, 'info>( )?; // Close the buyer trade state account if the rest of execute sale was successful. - let curr_buyer_lamp = buyer_trade_state.lamports(); - **buyer_trade_state.lamports.borrow_mut() = 0; - sol_memset(&mut *buyer_ts_data, 0, TRADE_STATE_SIZE); - **fee_payer.lamports.borrow_mut() = fee_payer - .lamports() - .checked_add(curr_buyer_lamp) - .ok_or(AuctionHouseError::NumericalOverflow)?; + close_account( + &buyer_trade_state.to_account_info(), + &fee_payer.to_account_info(), + )?; let token_account_data = SplAccount::unpack(&token_account.data.borrow())?; if token_account_data.delegated_amount == 0 { - if seller.to_account_info().is_signer { - invoke( - &revoke( - &token_program.key(), - &token_account.key(), - &seller.key(), - &[], - ) - .unwrap(), - &[ - token_program.to_account_info(), - token_account.to_account_info(), - seller.to_account_info(), - ], - )?; - } - - let curr_seller_lamp = seller_trade_state.lamports(); - **seller_trade_state.lamports.borrow_mut() = 0; - sol_memset(&mut *seller_ts_data, 0, TRADE_STATE_SIZE); - - **fee_payer.lamports.borrow_mut() = fee_payer - .lamports() - .checked_add(curr_seller_lamp) - .ok_or(AuctionHouseError::NumericalOverflow)?; + close_account( + &seller_trade_state.to_account_info(), + &fee_payer.to_account_info(), + )?; if free_trade_state.lamports() > 0 { - let curr_buyer_lamp = free_trade_state.lamports(); - **free_trade_state.lamports.borrow_mut() = 0; - - **fee_payer.lamports.borrow_mut() = fee_payer - .lamports() - .checked_add(curr_buyer_lamp) - .ok_or(AuctionHouseError::NumericalOverflow)?; - sol_memset( - *free_trade_state.try_borrow_mut_data()?, - 0, - TRADE_STATE_SIZE, - ); + close_account( + &free_trade_state.to_account_info(), + &fee_payer.to_account_info(), + )?; } } Ok(()) @@ -1462,16 +1426,16 @@ fn execute_sale_logic<'c, 'info>( return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into()); }; - let buyer_ts_data = &mut buyer_trade_state.try_borrow_mut_data()?; - let seller_ts_data = &mut seller_trade_state.try_borrow_mut_data()?; - - let ts_bump = if buyer_ts_data.len() > 0 { - buyer_ts_data[0] + let ts_bump = if buyer_trade_state.data_len() > 0 { + buyer_trade_state.try_borrow_data()?[0] } else { return Err(AuctionHouseError::BuyerTradeStateNotValid.into()); }; - if ts_bump == 0 || seller_ts_data.len() == 0 || seller_ts_data[0] == 0 { + if ts_bump == 0 + || seller_trade_state.data_len() == 0 + || seller_trade_state.try_borrow_data()?[0] == 0 + { return Err(AuctionHouseError::BothPartiesNeedToAgreeToSale.into()); } @@ -1750,56 +1714,25 @@ fn execute_sale_logic<'c, 'info>( )?; // Close the buyer trade state account if the rest of execute sale was successful. - let curr_buyer_lamp = buyer_trade_state.lamports(); - **buyer_trade_state.lamports.borrow_mut() = 0; - sol_memset(&mut *buyer_ts_data, 0, TRADE_STATE_SIZE); - **fee_payer.lamports.borrow_mut() = fee_payer - .lamports() - .checked_add(curr_buyer_lamp) - .ok_or(AuctionHouseError::NumericalOverflow)?; + close_account( + &buyer_trade_state.to_account_info(), + &fee_payer.to_account_info(), + )?; let token_account_data = SplAccount::unpack(&token_account.data.borrow())?; if token_account_data.delegated_amount == 0 { - if seller.to_account_info().is_signer { - invoke( - &revoke( - &token_program.key(), - &token_account.key(), - &seller.key(), - &[], - ) - .unwrap(), - &[ - token_program.to_account_info(), - token_account.to_account_info(), - seller.to_account_info(), - ], - )?; - } - let curr_seller_lamp = seller_trade_state.lamports(); - **seller_trade_state.lamports.borrow_mut() = 0; - sol_memset(&mut *seller_ts_data, 0, TRADE_STATE_SIZE); - - **fee_payer.lamports.borrow_mut() = fee_payer - .lamports() - .checked_add(curr_seller_lamp) - .ok_or(AuctionHouseError::NumericalOverflow)?; + close_account( + &seller_trade_state.to_account_info(), + &fee_payer.to_account_info(), + )?; if free_trade_state.lamports() > 0 { - let curr_buyer_lamp = free_trade_state.lamports(); - **free_trade_state.lamports.borrow_mut() = 0; - - **fee_payer.lamports.borrow_mut() = fee_payer - .lamports() - .checked_add(curr_buyer_lamp) - .ok_or(AuctionHouseError::NumericalOverflow)?; - sol_memset( - *free_trade_state.try_borrow_mut_data()?, - 0, - TRADE_STATE_SIZE, - ); + close_account( + &free_trade_state.to_account_info(), + &fee_payer.to_account_info(), + )?; } - }; + } Ok(()) } diff --git a/auction-house/program/src/utils.rs b/auction-house/program/src/utils.rs index d546a63bf6..818559ce0e 100644 --- a/auction-house/program/src/utils.rs +++ b/auction-house/program/src/utils.rs @@ -6,7 +6,7 @@ use anchor_lang::{ prelude::*, solana_program::{ program::invoke_signed, - program_memory::sol_memcmp, + program_memory::{sol_memcmp, sol_memset}, program_option::COption, program_pack::{IsInitialized, Pack}, pubkey::PUBKEY_BYTES, @@ -685,3 +685,25 @@ pub fn assert_scopes_eq( Ok(()) } + +pub fn close_account<'a>( + source_account: &AccountInfo<'a>, + receiver_account: &AccountInfo<'a>, +) -> Result<()> { + let current_lamports = source_account.lamports(); + let account_data_size = source_account.data_len(); + + **source_account.lamports.borrow_mut() = 0; + **receiver_account.lamports.borrow_mut() = receiver_account + .lamports() + .checked_add(current_lamports) + .ok_or(AuctionHouseError::NumericalOverflow)?; + + sol_memset( + &mut *source_account.try_borrow_mut_data()?, + 0, + account_data_size, + ); + + Ok(()) +} diff --git a/release.toml b/release.toml index 4ee89e4d88..47a3767561 100644 --- a/release.toml +++ b/release.toml @@ -1,4 +1,3 @@ consolidate-commits = false -consolidate-pushes = false dependent-version = "ignore" pre-release-commit-message = "(cargo-release) {{crate_name}} v{{version}}" \ No newline at end of file