Skip to content

Commit

Permalink
Adds post_adjustments to mock_process_instruction().
Browse files Browse the repository at this point in the history
Removes "solana_sbf_rust_external_spend" from assert_instruction_count() as it panics.
  • Loading branch information
Lichtso committed Apr 7, 2023
1 parent 0e2deaa commit 4048f59
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 36 deletions.
4 changes: 3 additions & 1 deletion program-runtime/src/invoke_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ macro_rules! with_mock_invoke_context {
};
}

pub fn mock_process_instruction<F: FnMut(&mut InvokeContext)>(
pub fn mock_process_instruction<F: FnMut(&mut InvokeContext), G: FnMut(&mut InvokeContext)>(
loader_id: &Pubkey,
mut program_indices: Vec<IndexOfAccount>,
instruction_data: &[u8],
Expand All @@ -933,6 +933,7 @@ pub fn mock_process_instruction<F: FnMut(&mut InvokeContext)>(
expected_result: Result<(), InstructionError>,
process_instruction: ProcessInstructionWithContext,
mut pre_adjustments: F,
mut post_adjustments: G,
) -> Vec<AccountSharedData> {
let mut instruction_accounts: Vec<InstructionAccount> =
Vec::with_capacity(instruction_account_metas.len());
Expand Down Expand Up @@ -976,6 +977,7 @@ pub fn mock_process_instruction<F: FnMut(&mut InvokeContext)>(
&mut ExecuteTimings::default(),
);
assert_eq!(result, expected_result);
post_adjustments(&mut invoke_context);
let mut transaction_accounts = transaction_context.deconstruct_without_keys().unwrap();
transaction_accounts.pop();
transaction_accounts
Expand Down
3 changes: 3 additions & 0 deletions programs/bpf_loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@ mod tests {
expected_result,
super::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
)
}

Expand Down Expand Up @@ -1982,6 +1983,7 @@ mod tests {
|invoke_context| {
invoke_context.mock_set_remaining(0);
},
|_invoke_context| {},
);

// Case: Account not a program
Expand Down Expand Up @@ -2524,6 +2526,7 @@ mod tests {
expected_result,
super::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
)
}

Expand Down
1 change: 1 addition & 0 deletions programs/config/src/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ mod tests {
expected_result,
super::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
)
}

Expand Down
1 change: 1 addition & 0 deletions programs/loader-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ mod tests {
expected_result,
super::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
)
}

Expand Down
48 changes: 18 additions & 30 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use {
parse_bpf_upgradeable_loader, BpfUpgradeableLoaderAccountType,
},
solana_ledger::token_balances::collect_token_balances,
solana_program_runtime::{
compute_budget::ComputeBudget, invoke_context::InvokeContext, timings::ExecuteTimings,
},
solana_program_runtime::{compute_budget::ComputeBudget, timings::ExecuteTimings},
solana_rbpf::vm::ContextObject,
solana_runtime::{
bank::{
Expand Down Expand Up @@ -86,7 +84,7 @@ use {
system_program,
transaction::{SanitizedTransaction, Transaction, TransactionError},
},
std::{str::FromStr, sync::Arc, time::Duration},
std::{cell::RefCell, str::FromStr, sync::Arc, time::Duration},
};

#[cfg(feature = "sbf_rust")]
Expand Down Expand Up @@ -1370,8 +1368,8 @@ fn assert_instruction_count() {
("noop++", 5),
("relative_call", 210),
("return_data", 980),
("sanity", 3259),
("sanity++", 3159),
("sanity", 2377),
("sanity++", 2277),
("secp256k1_recover", 25383),
("sha", 1355),
("struct_pass", 108),
Expand All @@ -1385,15 +1383,14 @@ fn assert_instruction_count() {
("solana_sbf_rust_alloc", 5067),
("solana_sbf_rust_custom_heap", 398),
("solana_sbf_rust_dep_crate", 2),
("solana_sbf_rust_external_spend", 288),
("solana_sbf_rust_iter", 1013),
("solana_sbf_rust_many_args", 1289),
("solana_sbf_rust_mem", 2067),
("solana_sbf_rust_membuiltins", 1539),
("solana_sbf_rust_noop", 275),
("solana_sbf_rust_param_passing", 146),
("solana_sbf_rust_rand", 378),
("solana_sbf_rust_sanity", 10759),
("solana_sbf_rust_sanity", 51931),
("solana_sbf_rust_secp256k1_recover", 91185),
("solana_sbf_rust_sha", 24059),
]);
Expand All @@ -1407,7 +1404,7 @@ fn assert_instruction_count() {
(program_key, AccountSharedData::new(0, 0, &loader_id)),
(
Pubkey::new_unique(),
AccountSharedData::new(0, 8, &program_key),
AccountSharedData::new(0, 0, &program_key),
),
];
let instruction_accounts = vec![AccountMeta {
Expand All @@ -1419,11 +1416,8 @@ fn assert_instruction_count() {
.1
.set_data_from_slice(&load_program_from_file(program_name));
transaction_accounts[0].1.set_executable(true);
transaction_accounts[1]
.1
.set_state(expected_consumption)
.unwrap();

let prev_compute_meter = RefCell::new(0);
print!(" {:36} {:8}", program_name, *expected_consumption);
mock_process_instruction(
&loader_id,
Expand All @@ -1432,29 +1426,23 @@ fn assert_instruction_count() {
transaction_accounts,
instruction_accounts,
Ok(()),
|invoke_context: &mut InvokeContext| {
let expected_consumption: u64 = invoke_context
.transaction_context
.get_current_instruction_context()
.unwrap()
.try_borrow_instruction_account(&invoke_context.transaction_context, 0)
.unwrap()
.get_state()
.unwrap();
let prev_compute_meter = invoke_context.get_remaining();
let _result = solana_bpf_loader_program::process_instruction(invoke_context);
let consumption = prev_compute_meter.saturating_sub(invoke_context.get_remaining());
let diff: i64 = consumption as i64 - expected_consumption as i64;
solana_bpf_loader_program::process_instruction,
|invoke_context| {
*prev_compute_meter.borrow_mut() = invoke_context.get_remaining();
},
|invoke_context| {
let consumption = prev_compute_meter
.borrow()
.saturating_sub(invoke_context.get_remaining());
let diff: i64 = consumption as i64 - *expected_consumption as i64;
println!(
"{:6} {:+5} ({:+3.0}%)",
consumption,
diff,
100.0_f64 * consumption as f64 / expected_consumption as f64 - 100.0_f64,
100.0_f64 * consumption as f64 / *expected_consumption as f64 - 100.0_f64,
);
assert_eq!(consumption, expected_consumption);
Ok(())
assert_eq!(consumption, *expected_consumption);
},
|_invoke_context| {},
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions programs/stake/src/stake_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ mod tests {
|invoke_context| {
invoke_context.feature_set = Arc::clone(&feature_set);
},
|_invoke_context| {},
)
}

Expand Down Expand Up @@ -6340,17 +6341,16 @@ mod tests {
transaction_accounts,
instruction_accounts,
Ok(()),
super::process_instruction,
|invoke_context| {
invoke_context.feature_set = Arc::clone(&feature_set);
},
|invoke_context| {
super::process_instruction(invoke_context)?;
let expected_minimum_delegation =
crate::get_minimum_delegation(&invoke_context.feature_set).to_le_bytes();
let actual_minimum_delegation =
invoke_context.transaction_context.get_return_data().1;
assert_eq!(expected_minimum_delegation, actual_minimum_delegation);
Ok(())
},
|invoke_context| {
invoke_context.feature_set = Arc::clone(&feature_set);
},
);
}
Expand Down
2 changes: 2 additions & 0 deletions programs/vote/src/vote_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ mod tests {
expected_result,
super::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
)
}

Expand All @@ -342,6 +343,7 @@ mod tests {
|invoke_context| {
invoke_context.feature_set = std::sync::Arc::new(FeatureSet::default());
},
|_invoke_context| {},
)
}

Expand Down
1 change: 1 addition & 0 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7988,6 +7988,7 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
Ok(()),
solana_bpf_loader_program::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
);

// Test initialized program account
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/system_instruction_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ mod tests {
expected_result,
super::process_instruction,
|_invoke_context| {},
|_invoke_context| {},
)
}

Expand Down Expand Up @@ -1831,6 +1832,7 @@ mod tests {
|invoke_context: &mut InvokeContext| {
invoke_context.blockhash = hash(&serialize(&0).unwrap());
},
|_invoke_context| {},
);
}

Expand Down Expand Up @@ -2181,6 +2183,7 @@ mod tests {
|invoke_context: &mut InvokeContext| {
invoke_context.blockhash = hash(&serialize(&0).unwrap());
},
|_invoke_context| {},
);
}

Expand Down

0 comments on commit 4048f59

Please sign in to comment.