karima_anchor_lang/
bpf_upgradeable_state.rs

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