|
| 1 | +use { |
| 2 | + crate::{AccountSharedData, ReadableAccount, traits::debug_fmt}, |
| 3 | + solana_account_info::AccountInfo, |
| 4 | + solana_clock::Epoch, |
| 5 | + solana_pubkey::Pubkey, |
| 6 | + solana_sdk_ids::{bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable}, |
| 7 | + std::{cell::RefCell, fmt, rc::Rc}, |
| 8 | +}; |
| 9 | + |
| 10 | +/// An on-chain account with owned data and an explicit rent epoch. |
| 11 | +#[repr(C)] |
| 12 | +#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(rename_all = "camelCase"))] |
| 13 | +#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))] |
| 14 | +#[derive(PartialEq, Eq, Clone, Default)] |
| 15 | +pub struct Account { |
| 16 | + /// Lamports in the account. |
| 17 | + pub lamports: u64, |
| 18 | + /// Data held in the account. |
| 19 | + #[cfg_attr(feature = "serde", serde(with = "serde_bytes"))] |
| 20 | + pub data: Vec<u8>, |
| 21 | + /// The program that owns this account. |
| 22 | + pub owner: Pubkey, |
| 23 | + /// Whether the account contains executable program data. |
| 24 | + pub executable: bool, |
| 25 | + /// The epoch at which this account next owes rent. |
| 26 | + pub rent_epoch: Epoch, |
| 27 | +} |
| 28 | + |
| 29 | +#[cfg(feature = "serde")] |
| 30 | +mod account_serialize { |
| 31 | + use { |
| 32 | + crate::ReadableAccount, |
| 33 | + serde::{Serialize, ser::Serializer}, |
| 34 | + solana_clock::Epoch, |
| 35 | + solana_pubkey::Pubkey, |
| 36 | + }; |
| 37 | + |
| 38 | + #[repr(C)] |
| 39 | + #[derive(serde::Serialize)] |
| 40 | + #[serde(rename_all = "camelCase")] |
| 41 | + /// Serialization shape shared by `Account` and `AccountSharedData`. |
| 42 | + struct Account<'a> { |
| 43 | + lamports: u64, |
| 44 | + #[serde(with = "serde_bytes")] |
| 45 | + data: &'a [u8], |
| 46 | + owner: &'a Pubkey, |
| 47 | + executable: bool, |
| 48 | + rent_epoch: Epoch, |
| 49 | + } |
| 50 | + |
| 51 | + /// Serializes any readable account using the canonical `Account` layout. |
| 52 | + pub(crate) fn serialize_account<S>( |
| 53 | + account: &impl ReadableAccount, |
| 54 | + serializer: S, |
| 55 | + ) -> Result<S::Ok, S::Error> |
| 56 | + where |
| 57 | + S: Serializer, |
| 58 | + { |
| 59 | + let account = Account { |
| 60 | + lamports: account.lamports(), |
| 61 | + data: account.data(), |
| 62 | + owner: account.owner(), |
| 63 | + executable: account.executable(), |
| 64 | + rent_epoch: account.rent_epoch(), |
| 65 | + }; |
| 66 | + account.serialize(serializer) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(feature = "serde")] |
| 71 | +impl serde::ser::Serialize for Account { |
| 72 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 73 | + where |
| 74 | + S: serde::ser::Serializer, |
| 75 | + { |
| 76 | + account_serialize::serialize_account(self, serializer) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(feature = "serde")] |
| 81 | +impl serde::ser::Serialize for AccountSharedData { |
| 82 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 83 | + where |
| 84 | + S: serde::ser::Serializer, |
| 85 | + { |
| 86 | + account_serialize::serialize_account(self, serializer) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl From<AccountSharedData> for Account { |
| 91 | + fn from(other: AccountSharedData) -> Self { |
| 92 | + Self { |
| 93 | + lamports: other.lamports(), |
| 94 | + data: other.data().to_vec(), |
| 95 | + owner: *other.owner(), |
| 96 | + executable: other.executable(), |
| 97 | + rent_epoch: other.rent_epoch(), |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl fmt::Debug for Account { |
| 103 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 104 | + debug_fmt(self, f) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl fmt::Debug for AccountSharedData { |
| 109 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 110 | + debug_fmt(self, f) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl Account { |
| 115 | + /// Builds an account from its exact field set. |
| 116 | + /// |
| 117 | + /// Used by the constructors to keep the owned layout in one place. |
| 118 | + fn from_parts( |
| 119 | + lamports: u64, |
| 120 | + data: Vec<u8>, |
| 121 | + owner: Pubkey, |
| 122 | + executable: bool, |
| 123 | + rent_epoch: Epoch, |
| 124 | + ) -> Self { |
| 125 | + Self { |
| 126 | + lamports, |
| 127 | + data, |
| 128 | + owner, |
| 129 | + executable, |
| 130 | + rent_epoch, |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /// Creates a new account with zero-filled data. |
| 135 | + pub fn new(lamports: u64, space: usize, owner: &Pubkey) -> Self { |
| 136 | + Self::new_rent_epoch(lamports, space, owner, Epoch::default()) |
| 137 | + } |
| 138 | + |
| 139 | + /// Creates a new account wrapped in a `RefCell`. |
| 140 | + pub fn new_ref(lamports: u64, space: usize, owner: &Pubkey) -> Rc<RefCell<Self>> { |
| 141 | + Rc::new(RefCell::new(Self::new(lamports, space, owner))) |
| 142 | + } |
| 143 | + |
| 144 | + /// Creates a new account whose data is the serialized state. |
| 145 | + #[cfg(feature = "bincode")] |
| 146 | + pub fn new_data<T: serde::Serialize>( |
| 147 | + lamports: u64, |
| 148 | + state: &T, |
| 149 | + owner: &Pubkey, |
| 150 | + ) -> Result<Self, bincode::Error> { |
| 151 | + let data = bincode::serialize(state)?; |
| 152 | + Ok(Self::from_parts( |
| 153 | + lamports, |
| 154 | + data, |
| 155 | + *owner, |
| 156 | + false, |
| 157 | + Epoch::default(), |
| 158 | + )) |
| 159 | + } |
| 160 | + |
| 161 | + /// Creates a new serialized account wrapped in a `RefCell`. |
| 162 | + #[cfg(feature = "bincode")] |
| 163 | + pub fn new_ref_data<T: serde::Serialize>( |
| 164 | + lamports: u64, |
| 165 | + state: &T, |
| 166 | + owner: &Pubkey, |
| 167 | + ) -> Result<RefCell<Self>, bincode::Error> { |
| 168 | + Self::new_data(lamports, state, owner).map(RefCell::new) |
| 169 | + } |
| 170 | + |
| 171 | + /// Creates a new account with fixed space and serialized state. |
| 172 | + #[cfg(feature = "bincode")] |
| 173 | + pub fn new_data_with_space<T: serde::Serialize>( |
| 174 | + lamports: u64, |
| 175 | + state: &T, |
| 176 | + space: usize, |
| 177 | + owner: &Pubkey, |
| 178 | + ) -> Result<Self, bincode::Error> { |
| 179 | + let mut account = Self::new(lamports, space, owner); |
| 180 | + crate::codec::serialize_data(&mut account, state)?; |
| 181 | + Ok(account) |
| 182 | + } |
| 183 | + |
| 184 | + /// Creates a new fixed-size serialized account wrapped in a `RefCell`. |
| 185 | + #[cfg(feature = "bincode")] |
| 186 | + pub fn new_ref_data_with_space<T: serde::Serialize>( |
| 187 | + lamports: u64, |
| 188 | + state: &T, |
| 189 | + space: usize, |
| 190 | + owner: &Pubkey, |
| 191 | + ) -> Result<RefCell<Self>, bincode::Error> { |
| 192 | + Self::new_data_with_space(lamports, state, space, owner).map(RefCell::new) |
| 193 | + } |
| 194 | + |
| 195 | + /// Creates a new account with an explicit rent epoch. |
| 196 | + pub fn new_rent_epoch(lamports: u64, space: usize, owner: &Pubkey, rent_epoch: Epoch) -> Self { |
| 197 | + Self::from_parts(lamports, vec![0; space], *owner, false, rent_epoch) |
| 198 | + } |
| 199 | + |
| 200 | + /// Deserializes the account data as `T`. |
| 201 | + #[cfg(feature = "bincode")] |
| 202 | + pub fn deserialize_data<T: serde::de::DeserializeOwned>(&self) -> Result<T, bincode::Error> { |
| 203 | + crate::codec::deserialize_data(self) |
| 204 | + } |
| 205 | + |
| 206 | + /// Serializes `state` into the existing account data buffer. |
| 207 | + #[cfg(feature = "bincode")] |
| 208 | + pub fn serialize_data<T: serde::Serialize>(&mut self, state: &T) -> Result<(), bincode::Error> { |
| 209 | + crate::codec::serialize_data(self, state) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +impl solana_account_info::Account for Account { |
| 214 | + fn get(&mut self) -> (&mut u64, &mut [u8], &Pubkey, bool) { |
| 215 | + ( |
| 216 | + &mut self.lamports, |
| 217 | + &mut self.data, |
| 218 | + &self.owner, |
| 219 | + self.executable, |
| 220 | + ) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +/// Builds `AccountInfo` values for accounts and signer bits. |
| 225 | +/// |
| 226 | +/// The returned infos borrow the provided accounts directly. |
| 227 | +pub fn create_is_signer_account_infos<'a>( |
| 228 | + accounts: &'a mut [(&'a Pubkey, bool, &'a mut Account)], |
| 229 | +) -> Vec<AccountInfo<'a>> { |
| 230 | + accounts |
| 231 | + .iter_mut() |
| 232 | + .map(|(key, is_signer, account)| { |
| 233 | + AccountInfo::new( |
| 234 | + key, |
| 235 | + *is_signer, |
| 236 | + false, |
| 237 | + &mut account.lamports, |
| 238 | + &mut account.data, |
| 239 | + &account.owner, |
| 240 | + account.executable, |
| 241 | + ) |
| 242 | + }) |
| 243 | + .collect() |
| 244 | +} |
| 245 | + |
| 246 | +/// Owners that imply the account contains a loaded program. |
| 247 | +pub const PROGRAM_OWNERS: &[Pubkey] = |
| 248 | + &[bpf_loader_upgradeable::id(), bpf_loader::id(), bpf_loader_deprecated::id()]; |
0 commit comments