Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Removes Rc from PreAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Dec 14, 2021
1 parent 033106e commit 6398a13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
6 changes: 3 additions & 3 deletions program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ impl<'a> InvokeContext<'a> {
self.pre_accounts = Vec::with_capacity(instruction.accounts.len());
let mut work = |_unique_index: usize, account_index: usize| {
if account_index < self.accounts.len() {
let account = self.accounts[account_index].1.borrow();
let account = self.accounts[account_index].1.borrow().clone();
self.pre_accounts
.push(PreAccount::new(&self.accounts[account_index].0, &account));
.push(PreAccount::new(&self.accounts[account_index].0, account));
return Ok(());
}
Err(InstructionError::MissingAccount)
Expand Down Expand Up @@ -461,7 +461,7 @@ impl<'a> InvokeContext<'a> {
.checked_add(u128::from(account.lamports()))
.ok_or(InstructionError::UnbalancedInstruction)?;
if is_writable && !pre_account.executable() {
pre_account.update(&account);
pre_account.update(account.clone());
}
return Ok(());
}
Expand Down
42 changes: 18 additions & 24 deletions program-runtime/src/pre_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ use {
system_instruction::MAX_PERMITTED_DATA_LENGTH,
system_program,
},
std::{
cell::{Ref, RefCell},
fmt::Debug,
rc::Rc,
},
std::fmt::Debug,
};

// The relevant state of an account before an Instruction executes, used
// to verify account integrity after the Instruction completes
#[derive(Clone, Debug, Default)]
pub struct PreAccount {
key: Pubkey,
account: Rc<RefCell<AccountSharedData>>,
account: AccountSharedData,
changed: bool,
}
impl PreAccount {
pub fn new(key: &Pubkey, account: &AccountSharedData) -> Self {
pub fn new(key: &Pubkey, account: AccountSharedData) -> Self {
Self {
key: *key,
account: Rc::new(RefCell::new(account.clone())),
account,
changed: false,
}
}
Expand All @@ -42,7 +38,7 @@ impl PreAccount {
outermost_call: bool,
do_support_realloc: bool,
) -> Result<(), InstructionError> {
let pre = self.account.borrow();
let pre = &self.account;

// Only the owner of the account may change owner and
// only if the account is writable and
Expand Down Expand Up @@ -157,11 +153,10 @@ impl PreAccount {
Ok(())
}

pub fn update(&mut self, account: &AccountSharedData) {
let mut pre = self.account.borrow_mut();
let rent_epoch = pre.rent_epoch();
*pre = account.clone();
pre.set_rent_epoch(rent_epoch);
pub fn update(&mut self, account: AccountSharedData) {
let rent_epoch = self.account.rent_epoch();
self.account = account;
self.account.set_rent_epoch(rent_epoch);

self.changed = true;
}
Expand All @@ -170,16 +165,16 @@ impl PreAccount {
&self.key
}

pub fn data(&self) -> Ref<[u8]> {
Ref::map(self.account.borrow(), |account| account.data())
pub fn data(&self) -> &[u8] {
self.account.data()
}

pub fn lamports(&self) -> u64 {
self.account.borrow().lamports()
self.account.lamports()
}

pub fn executable(&self) -> bool {
self.account.borrow().executable()
self.account.executable()
}

pub fn is_zeroed(buf: &[u8]) -> bool {
Expand Down Expand Up @@ -236,10 +231,9 @@ mod tests {
is_writable: true,
pre: PreAccount::new(
&solana_sdk::pubkey::new_rand(),
&AccountSharedData::from(Account {
AccountSharedData::from(Account {
owner: *owner,
lamports: std::u64::MAX,
data: vec![],
..Account::default()
}),
),
Expand All @@ -255,12 +249,12 @@ mod tests {
self
}
pub fn executable(mut self, pre: bool, post: bool) -> Self {
self.pre.account.borrow_mut().set_executable(pre);
self.pre.account.set_executable(pre);
self.post.set_executable(post);
self
}
pub fn lamports(mut self, pre: u64, post: u64) -> Self {
self.pre.account.borrow_mut().set_lamports(pre);
self.pre.account.set_lamports(pre);
self.post.set_lamports(post);
self
}
Expand All @@ -269,12 +263,12 @@ mod tests {
self
}
pub fn data(mut self, pre: Vec<u8>, post: Vec<u8>) -> Self {
self.pre.account.borrow_mut().set_data(pre);
self.pre.account.set_data(pre);
self.post.set_data(post);
self
}
pub fn rent_epoch(mut self, pre: u64, post: u64) -> Self {
self.pre.account.borrow_mut().set_rent_epoch(pre);
self.pre.account.set_rent_epoch(pre);
self.post.set_rent_epoch(post);
self
}
Expand Down

0 comments on commit 6398a13

Please sign in to comment.