bpf_upgradeable_state.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. use crate::{AccountDeserialize, AccountSerialize, Owner};
  2. use solana_program::{
  3. bpf_loader_upgradeable::UpgradeableLoaderState, program_error::ProgramError, pubkey::Pubkey,
  4. };
  5. #[derive(Clone)]
  6. pub struct ProgramData {
  7. pub slot: u64,
  8. pub upgrade_authority_address: Option<Pubkey>,
  9. }
  10. impl AccountDeserialize for ProgramData {
  11. fn try_deserialize(
  12. buf: &mut &[u8],
  13. ) -> Result<Self, solana_program::program_error::ProgramError> {
  14. ProgramData::try_deserialize_unchecked(buf)
  15. }
  16. fn try_deserialize_unchecked(
  17. buf: &mut &[u8],
  18. ) -> Result<Self, solana_program::program_error::ProgramError> {
  19. let program_state = AccountDeserialize::try_deserialize_unchecked(buf)?;
  20. match program_state {
  21. UpgradeableLoaderState::Uninitialized => {
  22. Err(anchor_lang::error::ErrorCode::AccountNotProgramData.into())
  23. }
  24. UpgradeableLoaderState::Buffer {
  25. authority_address: _,
  26. } => Err(anchor_lang::error::ErrorCode::AccountNotProgramData.into()),
  27. UpgradeableLoaderState::Program {
  28. programdata_address: _,
  29. } => Err(anchor_lang::error::ErrorCode::AccountNotProgramData.into()),
  30. UpgradeableLoaderState::ProgramData {
  31. slot,
  32. upgrade_authority_address,
  33. } => Ok(ProgramData {
  34. slot,
  35. upgrade_authority_address,
  36. }),
  37. }
  38. }
  39. }
  40. impl AccountSerialize for ProgramData {
  41. fn try_serialize<W: std::io::Write>(
  42. &self,
  43. _writer: &mut W,
  44. ) -> Result<(), solana_program::program_error::ProgramError> {
  45. // no-op
  46. Ok(())
  47. }
  48. }
  49. impl Owner for ProgramData {
  50. fn owner() -> solana_program::pubkey::Pubkey {
  51. anchor_lang::solana_program::bpf_loader_upgradeable::ID
  52. }
  53. }
  54. impl Owner for UpgradeableLoaderState {
  55. fn owner() -> Pubkey {
  56. anchor_lang::solana_program::bpf_loader_upgradeable::ID
  57. }
  58. }
  59. impl AccountSerialize for UpgradeableLoaderState {
  60. fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> Result<(), ProgramError> {
  61. // no-op
  62. Ok(())
  63. }
  64. }
  65. impl AccountDeserialize for UpgradeableLoaderState {
  66. fn try_deserialize(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  67. UpgradeableLoaderState::try_deserialize_unchecked(buf)
  68. }
  69. fn try_deserialize_unchecked(buf: &mut &[u8]) -> Result<Self, ProgramError> {
  70. bincode::deserialize(buf).map_err(|_| ProgramError::InvalidAccountData)
  71. }
  72. }