|
@@ -2,8 +2,8 @@
|
|
|
|
|
|
use crate::error::ErrorCode;
|
|
use crate::error::ErrorCode;
|
|
use crate::{
|
|
use crate::{
|
|
- Accounts, AccountsClose, AccountsExit, Owner, ToAccountInfo, ToAccountInfos, ToAccountMetas,
|
|
|
|
- ZeroCopy,
|
|
|
|
|
|
+ Accounts, AccountsClose, AccountsExit, Bump, Owner, ToAccountInfo, ToAccountInfos,
|
|
|
|
+ ToAccountMetas, ZeroCopy,
|
|
};
|
|
};
|
|
use arrayref::array_ref;
|
|
use arrayref::array_ref;
|
|
use solana_program::account_info::AccountInfo;
|
|
use solana_program::account_info::AccountInfo;
|
|
@@ -126,7 +126,11 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
|
|
}
|
|
}
|
|
let data: &[u8] = &acc_info.try_borrow_data()?;
|
|
let data: &[u8] = &acc_info.try_borrow_data()?;
|
|
// Discriminator must match.
|
|
// Discriminator must match.
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
let disc_bytes = array_ref![data, 0, 8];
|
|
let disc_bytes = array_ref![data, 0, 8];
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ let disc_bytes = array_ref![data, 2, 4];
|
|
|
|
+
|
|
if disc_bytes != &T::discriminator() {
|
|
if disc_bytes != &T::discriminator() {
|
|
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
}
|
|
}
|
|
@@ -150,7 +154,11 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
|
|
pub fn load(&self) -> Result<Ref<T>, ProgramError> {
|
|
pub fn load(&self) -> Result<Ref<T>, ProgramError> {
|
|
let data = self.acc_info.try_borrow_data()?;
|
|
let data = self.acc_info.try_borrow_data()?;
|
|
|
|
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
let disc_bytes = array_ref![data, 0, 8];
|
|
let disc_bytes = array_ref![data, 0, 8];
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ let disc_bytes = array_ref![data, 2, 4];
|
|
|
|
+
|
|
if disc_bytes != &T::discriminator() {
|
|
if disc_bytes != &T::discriminator() {
|
|
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
}
|
|
}
|
|
@@ -170,7 +178,11 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
|
|
|
|
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
|
|
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
let disc_bytes = array_ref![data, 0, 8];
|
|
let disc_bytes = array_ref![data, 0, 8];
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ let disc_bytes = array_ref![data, 2, 4];
|
|
|
|
+
|
|
if disc_bytes != &T::discriminator() {
|
|
if disc_bytes != &T::discriminator() {
|
|
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
return Err(ErrorCode::AccountDiscriminatorMismatch.into());
|
|
}
|
|
}
|
|
@@ -192,9 +204,20 @@ impl<'info, T: ZeroCopy + Owner> AccountLoader<'info, T> {
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
let data = self.acc_info.try_borrow_mut_data()?;
|
|
|
|
|
|
// The discriminator should be zero, since we're initializing.
|
|
// The discriminator should be zero, since we're initializing.
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
let mut disc_bytes = [0u8; 8];
|
|
let mut disc_bytes = [0u8; 8];
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
disc_bytes.copy_from_slice(&data[..8]);
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
let discriminator = u64::from_le_bytes(disc_bytes);
|
|
let discriminator = u64::from_le_bytes(disc_bytes);
|
|
|
|
+
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ let mut disc_bytes = [0u8; 4];
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ disc_bytes.copy_from_slice(&data[2..6]);
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ let discriminator = u32::from_le_bytes(disc_bytes);
|
|
|
|
+
|
|
if discriminator != 0 {
|
|
if discriminator != 0 {
|
|
return Err(ErrorCode::AccountDiscriminatorAlreadySet.into());
|
|
return Err(ErrorCode::AccountDiscriminatorAlreadySet.into());
|
|
}
|
|
}
|
|
@@ -226,7 +249,12 @@ impl<'info, T: ZeroCopy + Owner> AccountsExit<'info> for AccountLoader<'info, T>
|
|
// The account *cannot* be loaded when this is called.
|
|
// The account *cannot* be loaded when this is called.
|
|
fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
|
|
fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
|
|
let mut data = self.acc_info.try_borrow_mut_data()?;
|
|
let mut data = self.acc_info.try_borrow_mut_data()?;
|
|
|
|
+
|
|
|
|
+ #[cfg(feature = "deprecated-layout")]
|
|
let dst: &mut [u8] = &mut data;
|
|
let dst: &mut [u8] = &mut data;
|
|
|
|
+ #[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+ let dst: &mut [u8] = &mut data[2..];
|
|
|
|
+
|
|
let mut cursor = std::io::Cursor::new(dst);
|
|
let mut cursor = std::io::Cursor::new(dst);
|
|
cursor.write_all(&T::discriminator()).unwrap();
|
|
cursor.write_all(&T::discriminator()).unwrap();
|
|
Ok(())
|
|
Ok(())
|
|
@@ -261,3 +289,13 @@ impl<'info, T: ZeroCopy + Owner> ToAccountInfos<'info> for AccountLoader<'info,
|
|
vec![self.acc_info.clone()]
|
|
vec![self.acc_info.clone()]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+#[cfg(not(feature = "deprecated-layout"))]
|
|
|
|
+impl<'info, T> Bump for T
|
|
|
|
+where
|
|
|
|
+ T: AsRef<AccountInfo<'info>>,
|
|
|
|
+{
|
|
|
|
+ fn seed(&self) -> u8 {
|
|
|
|
+ self.as_ref().data.borrow()[1]
|
|
|
|
+ }
|
|
|
|
+}
|