Browse Source

Move adds on total earlier to enable the use of unchecked (#3527)

Co-authored-by: Francisco <frangio.1@gmail.com>
Linhai Song 3 years ago
parent
commit
8b778fa20d
1 changed files with 11 additions and 2 deletions
  1. 11 2
      contracts/finance/PaymentSplitter.sol

+ 11 - 2
contracts/finance/PaymentSplitter.sol

@@ -149,8 +149,12 @@ contract PaymentSplitter is Context {
 
 
         require(payment != 0, "PaymentSplitter: account is not due payment");
         require(payment != 0, "PaymentSplitter: account is not due payment");
 
 
-        _released[account] += payment;
+        // _totalReleased is the sum of all values in _released.
+        // If "_totalReleased += payment" does not overflow, then "_released[account] += payment" cannot overflow.
         _totalReleased += payment;
         _totalReleased += payment;
+        unchecked {
+            _released[account] += payment;
+        }
 
 
         Address.sendValue(account, payment);
         Address.sendValue(account, payment);
         emit PaymentReleased(account, payment);
         emit PaymentReleased(account, payment);
@@ -168,8 +172,13 @@ contract PaymentSplitter is Context {
 
 
         require(payment != 0, "PaymentSplitter: account is not due payment");
         require(payment != 0, "PaymentSplitter: account is not due payment");
 
 
-        _erc20Released[token][account] += payment;
+        // _erc20TotalReleased[token] is the sum of all values in _erc20Released[token].
+        // If "_erc20TotalReleased[token] += payment" does not overflow, then "_erc20Released[token][account] += payment"
+        // cannot overflow.
         _erc20TotalReleased[token] += payment;
         _erc20TotalReleased[token] += payment;
+        unchecked {
+            _erc20Released[token][account] += payment;
+        }
 
 
         SafeERC20.safeTransfer(token, account, payment);
         SafeERC20.safeTransfer(token, account, payment);
         emit ERC20PaymentReleased(token, account, payment);
         emit ERC20PaymentReleased(token, account, payment);