Parcourir la source

reuse amount validations (#82)

* reuse amount validations

* Add note on safety of source decrement

Co-authored-by: Fernando Otero <febo@anza.xyz>

* Add note on safety of dest increment

Co-authored-by: Fernando Otero <febo@anza.xyz>

* lint

---------

Co-authored-by: Fernando Otero <febo@anza.xyz>
cavemanloverboy il y a 1 mois
Parent
commit
27992e4513
1 fichiers modifiés avec 5 ajouts et 6 suppressions
  1. 5 6
      p-token/src/processor/shared/transfer.rs

+ 5 - 6
p-token/src/processor/shared/transfer.rs

@@ -172,18 +172,17 @@ pub fn process_transfer(
         if source_account.is_native() {
             // SAFETY: single mutable borrow to `source_account_info` lamports.
             let source_lamports = unsafe { source_account_info.borrow_mut_lamports_unchecked() };
-            *source_lamports = source_lamports
-                .checked_sub(amount)
-                .ok_or(TokenError::Overflow)?;
+            // Note: The amount of a source token account is already validated and the
+            // `lamports` on the account is always greater than `amount`.
+            *source_lamports -= amount;
 
             // SAFETY: single mutable borrow to `destination_account_info` lamports; the
             // account is already validated to be different from
             // `source_account_info`.
             let destination_lamports =
                 unsafe { destination_account_info.borrow_mut_lamports_unchecked() };
-            *destination_lamports = destination_lamports
-                .checked_add(amount)
-                .ok_or(TokenError::Overflow)?;
+            // Note: The total lamports supply is bound to `u64::MAX`.
+            *destination_lamports += amount;
         }
     }