Skip to content

Commit

Permalink
bincode error
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Mar 26, 2024
1 parent 5604f31 commit 41cde30
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
5 changes: 4 additions & 1 deletion runtime/src/bank/builtins/core_bpf_migration/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use {solana_sdk::pubkey::Pubkey, thiserror::Error};

/// Errors returned by a Core BPF migration.
#[derive(Debug, Error, PartialEq)]
#[derive(Debug, Error)]
pub enum CoreBpfMigrationError {
/// Bincode serialization error
#[error("Bincode serialization error: {0:?}")]
BincodeError(#[from] bincode::Error),
/// Account not found
#[error("Account not found: {0:?}")]
AccountNotFound(Pubkey),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ impl SourceUpgradeableBpf {
// The program account should have a pointer to its data account.
if let UpgradeableLoaderState::Program {
programdata_address,
} = &self
.program_account
.deserialize_data()
.map_err::<CoreBpfMigrationError, _>(|_| {
CoreBpfMigrationError::InvalidProgramAccount(self.program_address)
})?
} = &self.program_account.deserialize_data()?
{
if programdata_address != &self.program_data_address {
return Err(CoreBpfMigrationError::InvalidProgramAccount(
Expand Down Expand Up @@ -66,8 +61,8 @@ impl SourceUpgradeableBpf {
// Length checked in previous block.
match bincode::deserialize::<UpgradeableLoaderState>(
&self.program_data_account.data()[..programdata_data_offset],
) {
Ok(UpgradeableLoaderState::ProgramData { .. }) => Ok(()),
)? {
UpgradeableLoaderState::ProgramData { .. } => Ok(()),
_ => Err(CoreBpfMigrationError::InvalidProgramDataAccount(
self.program_data_address,
)),
Expand Down Expand Up @@ -124,6 +119,7 @@ mod tests {
use {
super::*,
crate::bank::tests::create_simple_test_bank,
assert_matches::assert_matches,
solana_sdk::{account::Account, bpf_loader_upgradeable::ID as BPF_LOADER_UPGRADEABLE_ID},
};

Expand Down Expand Up @@ -159,9 +155,9 @@ mod tests {
let program_data_address = get_program_data_address(&program_id);

// Fail if the program account does not exist
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::AccountNotFound(program_id)
CoreBpfMigrationError::AccountNotFound(..)
);

// Store the proper program account
Expand All @@ -178,9 +174,9 @@ mod tests {
);

// Fail if the program data account does not exist
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::ProgramHasNoDataAccount(program_id)
CoreBpfMigrationError::ProgramHasNoDataAccount(..)
);

// Store the proper program data account
Expand Down Expand Up @@ -276,9 +272,9 @@ mod tests {
true,
&Pubkey::new_unique(), // Not the upgradeable loader
);
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::IncorrectOwner(program_id)
CoreBpfMigrationError::IncorrectOwner(..)
);

// Fail if the program account's state is not `UpgradeableLoaderState::Program`
Expand All @@ -290,9 +286,9 @@ mod tests {
true,
&BPF_LOADER_UPGRADEABLE_ID,
);
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::InvalidProgramAccount(program_id)
CoreBpfMigrationError::BincodeError(..)
);

// Fail if the program account's state is `UpgradeableLoaderState::Program`,
Expand All @@ -307,9 +303,9 @@ mod tests {
true,
&BPF_LOADER_UPGRADEABLE_ID,
);
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::InvalidProgramAccount(program_id)
CoreBpfMigrationError::InvalidProgramAccount(..)
);
}

Expand Down Expand Up @@ -344,9 +340,9 @@ mod tests {
false,
&Pubkey::new_unique(), // Not the upgradeable loader
);
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::IncorrectOwner(program_data_address)
CoreBpfMigrationError::IncorrectOwner(..)
);

// Fail if the program data account does not have the correct state
Expand All @@ -358,9 +354,9 @@ mod tests {
false,
&BPF_LOADER_UPGRADEABLE_ID,
);
assert_eq!(
assert_matches!(
SourceUpgradeableBpf::new_checked(&bank, &program_id).unwrap_err(),
CoreBpfMigrationError::InvalidProgramDataAccount(program_data_address)
CoreBpfMigrationError::BincodeError(..)
);
}
}
21 changes: 11 additions & 10 deletions runtime/src/bank/builtins/core_bpf_migration/target_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ mod tests {
use {
super::*,
crate::bank::{tests::create_simple_test_bank, ApplyFeatureActivationsCaller},
assert_matches::assert_matches,
solana_sdk::{
account::Account,
bpf_loader_upgradeable::{UpgradeableLoaderState, ID as BPF_LOADER_UPGRADEABLE_ID},
Expand Down Expand Up @@ -159,9 +160,9 @@ mod tests {
true,
&Pubkey::new_unique(), // Not the native loader
);
assert_eq!(
assert_matches!(
TargetBuiltin::new_checked(&bank, &program_address, &migration_target).unwrap_err(),
CoreBpfMigrationError::IncorrectOwner(program_address)
CoreBpfMigrationError::IncorrectOwner(..)
);

// Fail if the program data account exists
Expand All @@ -182,19 +183,19 @@ mod tests {
false,
&BPF_LOADER_UPGRADEABLE_ID,
);
assert_eq!(
assert_matches!(
TargetBuiltin::new_checked(&bank, &program_address, &migration_target).unwrap_err(),
CoreBpfMigrationError::ProgramHasDataAccount(program_address)
CoreBpfMigrationError::ProgramHasDataAccount(..)
);

// Fail if the program account does not exist
bank.store_account_and_update_capitalization(
&program_address,
&AccountSharedData::default(),
);
assert_eq!(
assert_matches!(
TargetBuiltin::new_checked(&bank, &program_address, &migration_target).unwrap_err(),
CoreBpfMigrationError::AccountNotFound(program_address)
CoreBpfMigrationError::AccountNotFound(..)
);
}

Expand Down Expand Up @@ -226,9 +227,9 @@ mod tests {
false,
&BPF_LOADER_UPGRADEABLE_ID,
);
assert_eq!(
assert_matches!(
TargetBuiltin::new_checked(&bank, &program_address, &migration_target).unwrap_err(),
CoreBpfMigrationError::ProgramHasDataAccount(program_address)
CoreBpfMigrationError::ProgramHasDataAccount(..)
);

// Fail if the program account exists
Expand All @@ -239,9 +240,9 @@ mod tests {
true,
&NATIVE_LOADER_ID,
);
assert_eq!(
assert_matches!(
TargetBuiltin::new_checked(&bank, &program_address, &migration_target).unwrap_err(),
CoreBpfMigrationError::AccountExists(program_address)
CoreBpfMigrationError::AccountExists(..)
);
}
}

0 comments on commit 41cde30

Please sign in to comment.