Răsfoiți Sursa

p-token: Simplify math operations (#83)

* Simplify math

* Lint

* Add note

* Update note
Fernando Otero 3 săptămâni în urmă
părinte
comite
9949dee861

+ 6 - 4
p-token/src/processor/close_account.rs

@@ -11,6 +11,7 @@ use {
 };
 };
 
 
 #[inline(always)]
 #[inline(always)]
+#[allow(clippy::arithmetic_side_effects)]
 pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
 pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
     let [source_account_info, destination_account_info, authority_info, remaining @ ..] = accounts
     let [source_account_info, destination_account_info, authority_info, remaining @ ..] = accounts
     else {
     else {
@@ -44,14 +45,15 @@ pub fn process_close_account(accounts: &[AccountInfo]) -> ProgramResult {
         }
         }
     }
     }
 
 
-    let destination_starting_lamports = destination_account_info.lamports();
     // SAFETY: single mutable borrow to `destination_account_info` lamports and
     // SAFETY: single mutable borrow to `destination_account_info` lamports and
     // there are no "active" borrows of `source_account_info` account data.
     // there are no "active" borrows of `source_account_info` account data.
     unsafe {
     unsafe {
         // Moves the lamports to the destination account.
         // Moves the lamports to the destination account.
-        *destination_account_info.borrow_mut_lamports_unchecked() = destination_starting_lamports
-            .checked_add(source_account_info.lamports())
-            .ok_or(TokenError::Overflow)?;
+        //
+        // Note: This is safe since the runtime checks for balanced instructions
+        // before and after each CPI and instruction, and the total lamports
+        // supply is bound to `u64::MAX`.
+        *destination_account_info.borrow_mut_lamports_unchecked() += source_account_info.lamports();
         // Closes the source account.
         // Closes the source account.
         source_account_info.close_unchecked();
         source_account_info.close_unchecked();
     }
     }

+ 2 - 2
p-token/src/processor/shared/burn.rs

@@ -8,6 +8,7 @@ use {
 };
 };
 
 
 #[inline(always)]
 #[inline(always)]
+#[allow(clippy::arithmetic_side_effects)]
 pub fn process_burn(
 pub fn process_burn(
     accounts: &[AccountInfo],
     accounts: &[AccountInfo],
     amount: u64,
     amount: u64,
@@ -83,8 +84,7 @@ pub fn process_burn(
         source_account.set_amount(updated_source_amount);
         source_account.set_amount(updated_source_amount);
         // Note: The amount of a token account is always within the range of the
         // Note: The amount of a token account is always within the range of the
         // mint supply (`u64`).
         // mint supply (`u64`).
-        let mint_supply = mint.supply().checked_sub(amount).unwrap();
-        mint.set_supply(mint_supply);
+        mint.set_supply(mint.supply() - amount);
     }
     }
 
 
     Ok(())
     Ok(())

+ 3 - 4
p-token/src/processor/withdraw_excess_lamports.rs

@@ -86,13 +86,12 @@ pub fn process_withdraw_excess_lamports(accounts: &[AccountInfo]) -> ProgramResu
             source_starting_lamports - transfer_amount;
             source_starting_lamports - transfer_amount;
     }
     }
 
 
-    let destination_starting_lamports = destination_info.lamports();
     // SAFETY: single mutable borrow to `destination_info` lamports.
     // SAFETY: single mutable borrow to `destination_info` lamports.
     unsafe {
     unsafe {
         // Moves the lamports to the destination account.
         // Moves the lamports to the destination account.
-        *destination_info.borrow_mut_lamports_unchecked() = destination_starting_lamports
-            .checked_add(transfer_amount)
-            .ok_or(TokenError::Overflow)?;
+        //
+        // Note: The total lamports supply is bound to `u64::MAX`.
+        *destination_info.borrow_mut_lamports_unchecked() += transfer_amount;
     }
     }
 
 
     Ok(())
     Ok(())