Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Move KeyedAccounts to InvokeContext #15410

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fa482b6
Adds keyed_accounts property and getter to InvokeContext.
Lichtso Mar 16, 2021
bf35b5b
Adds keyed_accounts parameter to MockInvokeContext constructor.
Lichtso Mar 29, 2021
cd4c2f6
Adds keyed_accounts parameter to ThisInvokeContext constructor.
Lichtso Apr 5, 2021
871ece8
Replaces next_keyed_account() by keyed_account_at_index()
Lichtso Apr 5, 2021
7ba55f8
Adds "&keyed_accounts[1..]" expressions to invoke_context.
Lichtso Mar 31, 2021
cd70e40
Save and restore invoke_context.keyed_accounts in message_processor::…
Lichtso Feb 22, 2021
d66465b
Debug assert equality of old and new way of keyed_accounts.
Lichtso Feb 22, 2021
3f333dd
Changes bpf_loader::create_and_cache_executor() to use account indice…
Lichtso Apr 19, 2021
cd6b695
Splits off caching part from bpf_loader::create_and_cache_executor().
Lichtso Feb 24, 2021
907d19a
Changes bpf_loader::write_program_data() to use account indices not r…
Lichtso Mar 16, 2021
b872795
Removes callers_keyed_accounts from SyscallInvokeSigned.
Lichtso Feb 24, 2021
ecdf085
Changes message_processor::native_invoke() to use account indices not…
Lichtso Mar 31, 2021
690067c
Removed use of the keyed_accounts parameter in bpf_loader::process_lo…
Lichtso Mar 16, 2021
db7ef72
Removed use of the keyed_accounts parameter in bpf_loader::process_lo…
Lichtso Mar 16, 2021
7418f08
Removed use of the keyed_accounts parameter in bpf_loader::process_in…
Lichtso Feb 25, 2021
ed06314
Removes unused keyed_accounts parameter in bpf_loader.
Lichtso Feb 26, 2021
982ff93
Adds more assertions of invoke_context.get_keyed_accounts() being equ…
Lichtso Mar 16, 2021
843bbb1
Changes all native programs to use "invoke_context.get_keyed_accounts…
Lichtso Apr 5, 2021
071c541
Removes the "keyed_accounts" parameter from all "process_instruction"…
Lichtso Apr 5, 2021
20596f4
Fixes test of process_cross_program_instruction() to include keyed_ac…
Lichtso Apr 13, 2021
9c96908
Moves InvokeContext::set_keyed_accounts() into push() and pop().
Lichtso Mar 31, 2021
08c39b9
Renames pop_first_keyed_account => remove_first_keyed_account
Lichtso Mar 8, 2021
bdf27ef
Merges InvokeContext keyed_accounts and program_ids into invoke_stack.
Lichtso Apr 13, 2021
05deb6d
Removes unprotected integer arithmetic.
Lichtso Mar 31, 2021
63ce1c7
Replaces unwrap() by Result.
Lichtso Mar 18, 2021
fcbca01
Replaces tuple by struct.
Lichtso Apr 13, 2021
45069ad
Replaces slice reference by Vec.
Lichtso Apr 5, 2021
c859898
Use ranges instead of removing elements from Vec.
Lichtso Mar 31, 2021
c20b373
Moves MessageProcessor::create_keyed_accounts() and MessageProcessor:…
Lichtso Apr 13, 2021
b1b5639
Recycle existing keys in ThisInvokeContext::push()
Lichtso Apr 7, 2021
49e13c0
Unifies create_keyed_is_signer_accounts() and create_keyed_readonly_a…
Lichtso Apr 14, 2021
788ff6f
Makes unsafe transmute more obvious.
Lichtso Apr 19, 2021
0689763
Silence warnings about missing "dyn" before traits.
Lichtso Apr 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use {
hash::Hash,
instruction::Instruction,
instruction::InstructionError,
keyed_account::KeyedAccount,
message::Message,
native_token::sol_to_lamports,
process_instruction::{
Expand Down Expand Up @@ -99,12 +98,13 @@ fn get_invoke_context<'a>() -> &'a mut dyn InvokeContext {
pub fn builtin_process_instruction(
process_instruction: solana_sdk::entrypoint::ProcessInstruction,
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
input: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
set_invoke_context(invoke_context);

let keyed_accounts = invoke_context.get_keyed_accounts()?;

// Copy all the accounts into a HashMap to ensure there are no duplicates
let mut accounts: HashMap<Pubkey, Account> = keyed_accounts
.iter()
Expand Down Expand Up @@ -172,13 +172,11 @@ macro_rules! processor {
($process_instruction:expr) => {
Some(
|program_id: &Pubkey,
keyed_accounts: &[solana_sdk::keyed_account::KeyedAccount],
input: &[u8],
invoke_context: &mut dyn solana_sdk::process_instruction::InvokeContext| {
$crate::builtin_process_instruction(
$process_instruction,
program_id,
keyed_accounts,
input,
invoke_context,
)
Expand Down
18 changes: 9 additions & 9 deletions programs/bpf/benches/bpf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn bench_program_create_executable(bencher: &mut Bencher) {

bencher.iter(|| {
let _ =
Executable::<BpfError, ThisInstructionMeter>::from_elf(&elf, None, Config::default())
<dyn Executable::<BpfError, ThisInstructionMeter>>::from_elf(&elf, None, Config::default())
.unwrap();
});
}
Expand All @@ -91,11 +91,11 @@ fn bench_program_alu(bencher: &mut Bencher) {
.unwrap();
inner_iter.write_u64::<LittleEndian>(0).unwrap();
let loader_id = bpf_loader::id();
let mut invoke_context = MockInvokeContext::default();
let mut invoke_context = MockInvokeContext::new(vec![]);

let elf = load_elf("bench_alu").unwrap();
let mut executable =
Executable::<BpfError, ThisInstructionMeter>::from_elf(&elf, None, Config::default())
<dyn Executable::<BpfError, ThisInstructionMeter>>::from_elf(&elf, None, Config::default())
.unwrap();
executable.set_syscall_registry(register_syscalls(&mut invoke_context).unwrap());
executable.jit_compile().unwrap();
Expand All @@ -105,7 +105,6 @@ fn bench_program_alu(bencher: &mut Bencher) {
&loader_id,
executable.as_ref(),
&mut inner_iter,
&[],
&mut invoke_context,
)
.unwrap();
Expand Down Expand Up @@ -195,8 +194,6 @@ fn bench_program_execute_noop(bencher: &mut Bencher) {
fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
const BUDGET: u64 = 200_000;
let loader_id = bpf_loader::id();
let mut invoke_context = MockInvokeContext::default();
invoke_context.compute_meter.remaining = BUDGET;

let accounts = [RefCell::new(AccountSharedData::new(
1,
Expand All @@ -211,18 +208,22 @@ fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
.collect();
let instruction_data = vec![0u8];

let mut invoke_context = MockInvokeContext::new(keyed_accounts);
invoke_context.compute_meter.remaining = BUDGET;

// Serialize account data
let keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
let mut serialized = serialize_parameters(
&bpf_loader::id(),
&solana_sdk::pubkey::new_rand(),
&keyed_accounts,
keyed_accounts,
&instruction_data,
)
.unwrap();

let elf = load_elf("tuner").unwrap();
let mut executable =
Executable::<BpfError, ThisInstructionMeter>::from_elf(&elf, None, Config::default())
<dyn Executable::<BpfError, ThisInstructionMeter>>::from_elf(&elf, None, Config::default())
.unwrap();
executable.set_syscall_registry(register_syscalls(&mut invoke_context).unwrap());
let compute_meter = invoke_context.get_compute_meter();
Expand All @@ -231,7 +232,6 @@ fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
&loader_id,
executable.as_ref(),
&mut serialized,
&[],
&mut invoke_context,
)
.unwrap();
Expand Down
86 changes: 44 additions & 42 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn upgrade_bpf_program(
fn run_program(
name: &str,
program_id: &Pubkey,
parameter_accounts: &[KeyedAccount],
parameter_accounts: Vec<KeyedAccount>,
instruction_data: &[u8],
) -> Result<u64, InstructionError> {
let path = create_bpf_path(name);
Expand All @@ -188,14 +188,14 @@ fn run_program(
let mut data = vec![];
file.read_to_end(&mut data).unwrap();
let loader_id = bpf_loader::id();
let mut invoke_context = MockInvokeContext::default();
let parameter_bytes = serialize_parameters(
&bpf_loader::id(),
program_id,
parameter_accounts,
&parameter_accounts,
&instruction_data,
)
.unwrap();
let mut invoke_context = MockInvokeContext::new(parameter_accounts);
let compute_meter = invoke_context.get_compute_meter();
let mut instruction_meter = ThisInstructionMeter { compute_meter };

Expand All @@ -213,51 +213,53 @@ fn run_program(
let mut tracer = None;
for i in 0..2 {
let mut parameter_bytes = parameter_bytes.clone();
let mut vm = create_vm(
&loader_id,
executable.as_ref(),
&mut parameter_bytes,
parameter_accounts,
&mut invoke_context,
)
.unwrap();
let result = if i == 0 {
vm.execute_program_interpreted(&mut instruction_meter)
} else {
vm.execute_program_jit(&mut instruction_meter)
};
assert_eq!(SUCCESS, result.unwrap());
{
let mut vm = create_vm(
&loader_id,
executable.as_ref(),
&mut parameter_bytes,
&mut invoke_context,
)
.unwrap();
let result = if i == 0 {
vm.execute_program_interpreted(&mut instruction_meter)
} else {
vm.execute_program_jit(&mut instruction_meter)
};
assert_eq!(SUCCESS, result.unwrap());
if i == 1 {
assert_eq!(instruction_count, vm.get_total_instruction_count());
}
instruction_count = vm.get_total_instruction_count();
if config.enable_instruction_tracing {
if i == 1 {
if !Tracer::compare(tracer.as_ref().unwrap(), vm.get_tracer()) {
let mut tracer_display = String::new();
tracer
.as_ref()
.unwrap()
.write(&mut tracer_display, vm.get_program())
.unwrap();
println!("TRACE (interpreted): {}", tracer_display);
let mut tracer_display = String::new();
vm.get_tracer()
.write(&mut tracer_display, vm.get_program())
.unwrap();
println!("TRACE (jit): {}", tracer_display);
assert!(false);
}
}
tracer = Some(vm.get_tracer().clone());
}
}
let parameter_accounts = invoke_context.get_keyed_accounts().unwrap();
deserialize_parameters(
&bpf_loader::id(),
parameter_accounts,
&parameter_bytes,
true,
)
.unwrap();
if i == 1 {
assert_eq!(instruction_count, vm.get_total_instruction_count());
}
instruction_count = vm.get_total_instruction_count();
if config.enable_instruction_tracing {
if i == 1 {
if !Tracer::compare(tracer.as_ref().unwrap(), vm.get_tracer()) {
let mut tracer_display = String::new();
tracer
.as_ref()
.unwrap()
.write(&mut tracer_display, vm.get_program())
.unwrap();
println!("TRACE (interpreted): {}", tracer_display);
let mut tracer_display = String::new();
vm.get_tracer()
.write(&mut tracer_display, vm.get_program())
.unwrap();
println!("TRACE (jit): {}", tracer_display);
assert!(false);
}
}
tracer = Some(vm.get_tracer().clone());
}
}

Ok(instruction_count)
Expand Down Expand Up @@ -1263,7 +1265,7 @@ fn assert_instruction_count() {
let key = solana_sdk::pubkey::new_rand();
let mut account = RefCell::new(AccountSharedData::default());
let parameter_accounts = vec![KeyedAccount::new(&key, false, &mut account)];
let count = run_program(program.0, &program_id, &parameter_accounts[..], &[]).unwrap();
let count = run_program(program.0, &program_id, parameter_accounts, &[]).unwrap();
let diff: i64 = count as i64 - program.1 as i64;
println!(" {:30} {:8} {:6} {:+4}", program.0, program.1, count, diff);
if count > program.1 {
Expand Down
Loading