bpf_upgradeable_state.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  65. #[cfg(feature = "idl-build")]
  66. mod idl_build {
  67. use super::*;
  68. impl crate::IdlBuild for ProgramData {}
  69. impl crate::Discriminator for ProgramData {
  70. const DISCRIMINATOR: [u8; 8] = [u8::MAX; 8];
  71. }
  72. }