bpf_upgradeable_state.rs 2.2 KB

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