Przeglądaj źródła

chore : cleanup token crate (#193)

Nagaprasad V R 4 miesięcy temu
rodzic
commit
1b9a59162a

+ 0 - 1
programs/token/src/instructions/mint_to.rs

@@ -15,7 +15,6 @@ use crate::{write_bytes, UNINIT_BYTE};
 ///   0. `[WRITE]` The mint.
 ///   1. `[WRITE]` The account to mint tokens to.
 ///   2. `[SIGNER]` The mint's minting authority.
-///
 pub struct MintTo<'a> {
     /// Mint Account.
     pub mint: &'a AccountInfo,

+ 0 - 1
programs/token/src/instructions/mint_to_checked.rs

@@ -15,7 +15,6 @@ use crate::{write_bytes, UNINIT_BYTE};
 ///   0. `[WRITE]` The mint.
 ///   1. `[WRITE]` The account to mint tokens to.
 ///   2. `[SIGNER]` The mint's minting authority.
-///
 pub struct MintToChecked<'a> {
     /// Mint Account.
     pub mint: &'a AccountInfo,

+ 10 - 5
programs/token/src/state/mint.rs

@@ -51,7 +51,7 @@ impl Mint {
             return Err(ProgramError::InvalidAccountOwner);
         }
         Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe {
-            Self::from_bytes(data)
+            Self::from_bytes_unchecked(data)
         }))
     }
 
@@ -74,16 +74,21 @@ impl Mint {
         if account_info.owner() != &ID {
             return Err(ProgramError::InvalidAccountOwner);
         }
-        Ok(Self::from_bytes(account_info.borrow_data_unchecked()))
+        Ok(Self::from_bytes_unchecked(
+            account_info.borrow_data_unchecked(),
+        ))
     }
 
     /// Return a `Mint` from the given bytes.
     ///
     /// # Safety
     ///
-    /// The caller must ensure that `bytes` contains a valid representation of `Mint`.
+    /// The caller must ensure that `bytes` contains a valid representation of `Mint`, and
+    /// it is properly aligned to be interpreted as an instance of `Mint`.
+    /// At the moment `Mint` has an alignment of 1 byte.
+    /// This method does not perform a length validation.
     #[inline(always)]
-    pub unsafe fn from_bytes(bytes: &[u8]) -> &Self {
+    pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
         &*(bytes.as_ptr() as *const Mint)
     }
 
@@ -110,7 +115,7 @@ impl Mint {
     }
 
     pub fn supply(&self) -> u64 {
-        unsafe { core::ptr::read_unaligned(self.supply.as_ptr() as *const u64) }
+        u64::from_le_bytes(self.supply)
     }
 
     pub fn decimals(&self) -> u8 {

+ 12 - 7
programs/token/src/state/token.rs

@@ -66,7 +66,7 @@ impl TokenAccount {
             return Err(ProgramError::InvalidAccountData);
         }
         Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe {
-            Self::from_bytes(data)
+            Self::from_bytes_unchecked(data)
         }))
     }
 
@@ -89,16 +89,21 @@ impl TokenAccount {
         if account_info.owner() != &ID {
             return Err(ProgramError::InvalidAccountData);
         }
-        Ok(Self::from_bytes(account_info.borrow_data_unchecked()))
+        Ok(Self::from_bytes_unchecked(
+            account_info.borrow_data_unchecked(),
+        ))
     }
 
     /// Return a `TokenAccount` from the given bytes.
     ///
     /// # Safety
     ///
-    /// The caller must ensure that `bytes` contains a valid representation of `TokenAccount`.
+    /// The caller must ensure that `bytes` contains a valid representation of `TokenAccount`, and
+    /// it is properly aligned to be interpreted as an instance of `TokenAccount`.
+    /// At the moment `TokenAccount` has an alignment of 1 byte.
+    /// This method does not perform a length validation.
     #[inline(always)]
-    pub unsafe fn from_bytes(bytes: &[u8]) -> &Self {
+    pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self {
         &*(bytes.as_ptr() as *const TokenAccount)
     }
 
@@ -111,7 +116,7 @@ impl TokenAccount {
     }
 
     pub fn amount(&self) -> u64 {
-        unsafe { core::ptr::read_unaligned(self.amount.as_ptr() as *const u64) }
+        u64::from_le_bytes(self.amount)
     }
 
     #[inline(always)]
@@ -157,11 +162,11 @@ impl TokenAccount {
     /// skips the `Option` check.
     #[inline(always)]
     pub fn native_amount_unchecked(&self) -> u64 {
-        unsafe { core::ptr::read_unaligned(self.native_amount.as_ptr() as *const u64) }
+        u64::from_le_bytes(self.native_amount)
     }
 
     pub fn delegated_amount(&self) -> u64 {
-        unsafe { core::ptr::read_unaligned(self.delegated_amount.as_ptr() as *const u64) }
+        u64::from_le_bytes(self.delegated_amount)
     }
 
     #[inline(always)]