Refactor: Remove Message and CompiledInstruction from InvokeContext interfaces (#22102)

* Introduces InstructionAccount which is like AccountMeta but uses an index instead of a Pubkey

* Renames InvokeContext::create_message() to InvokeContext::prepare_instruction()

* Removes Message and CompiledInstruction from InvokeContext interfaces.

* Resolves TODOs of sol_invoke_signed() in program-test.

* Moves CompiledInstruction::visit_each_account() into invoke_context.rs
This commit is contained in:
Alexander Meißner
2021-12-24 16:17:55 +01:00
committed by GitHub
parent 214b561a28
commit 2ab4f34c02
7 changed files with 458 additions and 583 deletions

View File

@ -639,52 +639,4 @@ impl CompiledInstruction {
pub fn program_id<'a>(&self, program_ids: &'a [Pubkey]) -> &'a Pubkey {
&program_ids[self.program_id_index as usize]
}
/// Visit each unique instruction account index once
pub fn visit_each_account(
&self,
work: &mut dyn FnMut(usize, usize) -> Result<(), InstructionError>,
) -> Result<(), InstructionError> {
let mut unique_index = 0;
'root: for (i, account_index) in self.accounts.iter().enumerate() {
// Note: This is an O(n^2) algorithm,
// but performed on a very small slice and requires no heap allocations
for account_index_before in self.accounts[..i].iter() {
if account_index_before == account_index {
continue 'root; // skip dups
}
}
work(unique_index, *account_index as usize)?;
unique_index += 1;
}
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_visit_each_account() {
let do_work = |accounts: &[u8]| -> (usize, usize) {
let mut unique_total = 0;
let mut account_total = 0;
let mut work = |unique_index: usize, account_index: usize| {
unique_total += unique_index;
account_total += account_index;
Ok(())
};
let instruction = CompiledInstruction::new(0, &[0], accounts.to_vec());
instruction.visit_each_account(&mut work).unwrap();
(unique_total, account_total)
};
assert_eq!((6, 6), do_work(&[0, 1, 2, 3]));
assert_eq!((6, 6), do_work(&[0, 1, 1, 2, 3]));
assert_eq!((6, 6), do_work(&[0, 1, 2, 3, 3]));
assert_eq!((6, 6), do_work(&[0, 0, 1, 1, 2, 2, 3, 3]));
assert_eq!((0, 2), do_work(&[2, 2]));
}
}