Skip to content

Commit

Permalink
Fix - ProgramCache::assign_program() Closed => Loaded in same slot (s…
Browse files Browse the repository at this point in the history
…olana-labs#673)

* Refactors test_add_builtin() to use LoadedProgram::new_builtin() and ProgramCache::extract().

* Revert Closed => Loaded transition

* Adds more loaded programs deployment tests
  • Loading branch information
Lichtso authored and buffalojoec committed Apr 16, 2024
1 parent 16b2ade commit a546cbc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
13 changes: 2 additions & 11 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,17 +776,12 @@ impl<FG: ForkGraph> ProgramCache<FG> {
Ok(index) => {
let existing = slot_versions.get_mut(index).unwrap();
match (&existing.program, &entry.program) {
// Add test for Closed => Loaded transition in same slot
(LoadedProgramType::Builtin(_), LoadedProgramType::Builtin(_))
| (LoadedProgramType::Closed, LoadedProgramType::LegacyV0(_))
| (LoadedProgramType::Closed, LoadedProgramType::LegacyV1(_))
| (LoadedProgramType::Closed, LoadedProgramType::Typed(_))
| (LoadedProgramType::Unloaded(_), LoadedProgramType::LegacyV0(_))
| (LoadedProgramType::Unloaded(_), LoadedProgramType::LegacyV1(_))
| (LoadedProgramType::Unloaded(_), LoadedProgramType::Typed(_)) => {}
#[cfg(test)]
(LoadedProgramType::Closed, LoadedProgramType::TestLoaded(_))
| (LoadedProgramType::Unloaded(_), LoadedProgramType::TestLoaded(_)) => {}
(LoadedProgramType::Unloaded(_), LoadedProgramType::TestLoaded(_)) => {}
_ => {
// Something is wrong, I can feel it ...
error!("ProgramCache::assign_program() failed key={:?} existing={:?} entry={:?}", key, slot_versions, entry);
Expand Down Expand Up @@ -1684,6 +1679,7 @@ mod tests {

#[test_matrix(
(
LoadedProgramType::Closed,
LoadedProgramType::FailedVerification(Arc::new(BuiltinProgram::new_mock())),
LoadedProgramType::TestLoaded(Arc::new(BuiltinProgram::new_mock())),
),
Expand All @@ -1697,7 +1693,6 @@ mod tests {
)]
#[test_matrix(
(
LoadedProgramType::Closed,
LoadedProgramType::Unloaded(Arc::new(BuiltinProgram::new_mock())),
),
(
Expand Down Expand Up @@ -1746,10 +1741,6 @@ mod tests {
);
}

#[test_case(
LoadedProgramType::Closed,
LoadedProgramType::TestLoaded(Arc::new(BuiltinProgram::new_mock()))
)]
#[test_case(
LoadedProgramType::Unloaded(Arc::new(BuiltinProgram::new_mock())),
LoadedProgramType::TestLoaded(Arc::new(BuiltinProgram::new_mock()))
Expand Down
47 changes: 46 additions & 1 deletion runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7129,7 +7129,7 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
);
let upgrade_authority_keypair = Keypair::new();

// Invoke not yet deployed program
// Test nonexistent program invocation
let instruction = Instruction::new_with_bytes(program_keypair.pubkey(), &[], Vec::new());
let invocation_message = Message::new(&[instruction], Some(&mint_keypair.pubkey()));
let binding = mint_keypair.insecure_clone();
Expand Down Expand Up @@ -7196,6 +7196,32 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
&bpf_loader_upgradeable::id(),
);

// Test uninitialized program invocation
bank.store_account(&program_keypair.pubkey(), &program_account);
let transaction = Transaction::new(
&[&binding],
invocation_message.clone(),
bank.last_blockhash(),
);
assert_eq!(
bank.process_transaction(&transaction),
Err(TransactionError::InstructionError(
0,
InstructionError::InvalidAccountData
)),
);
{
let program_cache = bank.transaction_processor.program_cache.read().unwrap();
let slot_versions = program_cache.get_slot_versions_for_tests(&program_keypair.pubkey());
assert_eq!(slot_versions.len(), 1);
assert_eq!(slot_versions[0].deployment_slot, 0);
assert_eq!(slot_versions[0].effective_slot, 0);
assert!(matches!(
slot_versions[0].program,
LoadedProgramType::Closed,
));
}

// Test buffer invocation
bank.store_account(&buffer_address, &buffer_account);
let instruction = Instruction::new_with_bytes(buffer_address, &[], Vec::new());
Expand All @@ -7212,6 +7238,8 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
let program_cache = bank.transaction_processor.program_cache.read().unwrap();
let slot_versions = program_cache.get_slot_versions_for_tests(&buffer_address);
assert_eq!(slot_versions.len(), 1);
assert_eq!(slot_versions[0].deployment_slot, 0);
assert_eq!(slot_versions[0].effective_slot, 0);
assert!(matches!(
slot_versions[0].program,
LoadedProgramType::Closed,
Expand Down Expand Up @@ -7308,6 +7336,23 @@ fn test_bpf_loader_upgradeable_deploy_with_max_len() {
// Invoke the deployed program
let transaction = Transaction::new(&[&binding], invocation_message, bank.last_blockhash());
assert!(bank.process_transaction(&transaction).is_ok());
{
let program_cache = bank.transaction_processor.program_cache.read().unwrap();
let slot_versions = program_cache.get_slot_versions_for_tests(&program_keypair.pubkey());
assert_eq!(slot_versions.len(), 2);
assert_eq!(slot_versions[0].deployment_slot, 0);
assert_eq!(slot_versions[0].effective_slot, 0);
assert!(matches!(
slot_versions[0].program,
LoadedProgramType::Closed,
));
assert_eq!(slot_versions[1].deployment_slot, 0);
assert_eq!(slot_versions[1].effective_slot, 1);
assert!(matches!(
slot_versions[1].program,
LoadedProgramType::LegacyV1(_),
));
}

// Test initialized program account
bank.clear_signatures();
Expand Down

0 comments on commit a546cbc

Please sign in to comment.