Skip to content

Commit

Permalink
Switch from using message.account_keys[i] to accounts[i].0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Jun 24, 2021
1 parent 0964dc7 commit 3fd3ec0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 71 deletions.
9 changes: 3 additions & 6 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,13 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
.map_err(|err| ProgramError::try_from(err).unwrap_or_else(|err| panic!("{}", err)))?;

// Copy writeable account modifications back into the caller's AccountInfos
for (i, account_pubkey) in message.account_keys.iter().enumerate() {
// REFACTOR: account_deps unification
for (i, (pubkey, account)) in accounts.iter().enumerate().take(message.account_keys.len()) {
if !message.is_writable(i, true) {
continue;
}

for account_info in account_infos {
if account_info.unsigned_key() == account_pubkey {
let (_key, account) = &accounts[i];
// REFACTOR: account_deps unification
assert_eq!(_key, account_pubkey);
if account_info.unsigned_key() == pubkey {
**account_info.try_borrow_mut_lamports().unwrap() = account.borrow().lamports();

let mut data = account_info.try_borrow_mut_data()?;
Expand Down
1 change: 0 additions & 1 deletion programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,6 @@ fn call<'a>(
let invoke_context = syscall.get_context()?;
for (i, ((_key, account), account_ref)) in accounts.iter().zip(account_refs).enumerate() {
// REFACTOR: account_deps unification
assert_eq!(message.account_keys[i], *_key);
let account = account.borrow();
if let Some(mut account_ref) = account_ref {
if message.is_writable(i, demote_sysvar_write_locks) && !account.executable() {
Expand Down
26 changes: 16 additions & 10 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3090,16 +3090,22 @@ pub mod rpc_full {
accounts.push(if result.is_err() {
None
} else {
transaction
.message
.account_keys
.iter()
.position(|pubkey| *pubkey == address)
.map(|i| post_simulation_accounts.get(i))
.flatten()
.map(|(_pubkey, account)| {
// REFACTOR: account_deps unification
UiAccount::encode(&address, account, accounts_encoding, None, None)
// REFACTOR: account_deps unification
(0..transaction.message.account_keys.len())
.position(|i| {
post_simulation_accounts
.get(i)
.map(|(key, _account)| *key == address)
.unwrap_or(false)
})
.map(|i| {
UiAccount::encode(
&address,
&post_simulation_accounts[i].1,
accounts_encoding,
None,
None,
)
})
});
}
Expand Down
10 changes: 3 additions & 7 deletions runtime/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,15 +993,11 @@ impl Accounts {
let message = &tx.message();
let loaded_transaction = raccs.as_mut().unwrap();
let mut fee_payer_index = None;
for ((i, key), (_key, account)) in message
.account_keys
.iter()
.enumerate()
for (i, (key, account)) in (0..message.account_keys.len())
.zip(loaded_transaction.accounts.iter_mut())
.filter(|((i, key), (_key, _account))| message.is_non_loader_key(key, *i))
.filter(|(i, (key, _account))| message.is_non_loader_key(key, *i))
{
// REFACTOR: account_deps unification
assert_eq!(key, _key);
let is_nonce_account = prepare_if_nonce_account(
account,
key,
Expand Down Expand Up @@ -1038,7 +1034,7 @@ impl Accounts {
.rent_debits
.push(key, rent, account.lamports());
}
accounts.push((key, &*account));
accounts.push((&*key, &*account));
}
}
}
Expand Down
43 changes: 17 additions & 26 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,30 +631,27 @@ impl NonceRollbackFull {
nonce_address,
nonce_account,
} = partial;
let fee_payer = message
.account_keys
.iter()
.enumerate()
.find(|(i, k)| message.is_non_loader_key(k, *i))
.and_then(|(i, k)| {
accounts.get(i).cloned().map(|(_k, a)| {
// REFACTOR: account_deps unification
assert_eq!(*k, _k);
(*k, a)
})
});
// REFACTOR: account_deps unification
let fee_payer = (0..message.account_keys.len()).find_map(|i| {
if let Some((k, a)) = &accounts.get(i) {
if message.is_non_loader_key(k, i) {
return Some((k, a));
}
}
None
});
if let Some((fee_pubkey, fee_account)) = fee_payer {
if fee_pubkey == nonce_address {
if *fee_pubkey == nonce_address {
Ok(Self {
nonce_address,
nonce_account: fee_account,
nonce_account: fee_account.clone(),
fee_account: None,
})
} else {
Ok(Self {
nonce_address,
nonce_account,
fee_account: Some(fee_account),
fee_account: Some(fee_account.clone()),
})
}
} else {
Expand Down Expand Up @@ -2958,14 +2955,11 @@ impl Bank {
TransactionAccountDepRefCells,
TransactionLoaderRefCells,
) {
let account_refcells: Vec<_> = message
.account_keys
.iter()
let account_refcells: Vec<_> = (0..message.account_keys.len())
.zip(accounts.drain(..))
.map(|(pubkey, (_pubkey, account))| {
.map(|(_i, (pubkey, account))| {
// REFACTOR: account_deps unification
assert_eq!(*pubkey, _pubkey);
(*pubkey, Rc::new(RefCell::new(account)))
(pubkey, Rc::new(RefCell::new(account)))
})
.collect();
let account_dep_refcells: Vec<_> = account_deps
Expand Down Expand Up @@ -4821,14 +4815,11 @@ impl Bank {
let message = &tx.message();
let loaded_transaction = raccs.as_ref().unwrap();

for (pubkey, (_pubkey, account)) in message
.account_keys
.iter()
for (_i, (pubkey, account)) in (0..message.account_keys.len())
.zip(loaded_transaction.accounts.iter())
.filter(|(_key, (_pubkey, account))| (Stakes::is_stake(account)))
.filter(|(_i, (_pubkey, account))| (Stakes::is_stake(account)))
{
// REFACTOR: account_deps unification
assert_eq!(pubkey, _pubkey);
if Stakes::is_stake(account) {
if let Some(old_vote_account) = self.stakes.write().unwrap().store(
pubkey,
Expand Down
35 changes: 14 additions & 21 deletions runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,10 @@ impl<'a> InvokeContext for ThisInvokeContext<'a> {
self.feature_set.is_active(feature_id)
}
fn get_account(&self, pubkey: &Pubkey) -> Option<Rc<RefCell<AccountSharedData>>> {
if let Some(index) = self
.message
.account_keys
.iter()
.position(|key| key == pubkey)
if let Some(index) =
(0..self.message.account_keys.len()).position(|index| self.accounts[index].0 == *pubkey)
{
// REFACTOR: account_deps unification
assert_eq!(*pubkey, self.accounts[index].0);
return Some(self.accounts[index].1.clone());
}
self.account_deps.iter().find_map(|(key, account)| {
Expand Down Expand Up @@ -636,11 +632,10 @@ impl MessageProcessor {
.chain(instruction.accounts.iter().map(|index| {
let index = *index as usize;
// REFACTOR: account_deps unification
assert_eq!(message.account_keys[index], accounts[index].0);
(
message.is_signer(index),
message.is_writable(index, demote_sysvar_write_locks),
&message.account_keys[index],
&accounts[index].0,
&accounts[index].1 as &RefCell<AccountSharedData>,
)
}))
Expand Down Expand Up @@ -990,12 +985,13 @@ impl MessageProcessor {
let mut pre_accounts = Vec::with_capacity(instruction.accounts.len());
{
let mut work = |_unique_index: usize, account_index: usize| {
let key = &message.account_keys[account_index];
let account = accounts[account_index].1.borrow();
// REFACTOR: account_deps unification
assert_eq!(*key, accounts[account_index].0);
pre_accounts.push(PreAccount::new(key, &account));
Ok(())
if account_index < message.account_keys.len() && account_index < accounts.len() {
let account = accounts[account_index].1.borrow();
// REFACTOR: account_deps unification
pre_accounts.push(PreAccount::new(&accounts[account_index].0, &account));
return Ok(());
}
Err(InstructionError::MissingAccount)
};
let _ = instruction.visit_each_account(&mut work);
}
Expand Down Expand Up @@ -1092,10 +1088,8 @@ impl MessageProcessor {
let (mut pre_sum, mut post_sum) = (0_u128, 0_u128);
let mut work = |_unique_index: usize, account_index: usize| {
if account_index < message.account_keys.len() && account_index < accounts.len() {
let key = &message.account_keys[account_index];
let (_key, account) = &accounts[account_index];
let (key, account) = &accounts[account_index];
// REFACTOR: account_deps unification
assert_eq!(key, _key);
let is_writable = if let Some(caller_write_privileges) = caller_write_privileges {
caller_write_privileges[account_index]
} else {
Expand Down Expand Up @@ -1165,11 +1159,10 @@ impl MessageProcessor {
// Fixup the special instructions key if present
// before the account pre-values are taken care of
if feature_set.is_active(&instructions_sysvar_enabled::id()) {
for (i, key) in message.account_keys.iter().enumerate() {
if instructions::check_id(key) {
for (pubkey, accont) in accounts.iter().take(message.account_keys.len()) {
if instructions::check_id(pubkey) {
// REFACTOR: account_deps unification
assert_eq!(*key, accounts[i].0);
let mut mut_account_ref = accounts[i].1.borrow_mut();
let mut mut_account_ref = accont.borrow_mut();
instructions::store_current_index(
mut_account_ref.data_as_mut_slice(),
instruction_index as u16,
Expand Down

0 comments on commit 3fd3ec0

Please sign in to comment.