Skip to content

Commit 20e2aa3

Browse files
authored
Merge pull request #16 from GenerationSoftware/gen-318-c4-issue-449
Optimized sloads
2 parents 6f88449 + 48a9f77 commit 20e2aa3

File tree

5 files changed

+63
-81
lines changed

5 files changed

+63
-81
lines changed

src/TwabController.sol

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ error PeriodOffsetInFuture(uint32 periodOffset);
2121
// The minimum period length
2222
uint32 constant MINIMUM_PERIOD_LENGTH = 1 hours;
2323

24+
address constant SPONSORSHIP_ADDRESS = address(1);
25+
2426
/**
2527
* @title Time-Weighted Average Balance Controller
2628
* @author PoolTogether Inc.
@@ -37,7 +39,6 @@ contract TwabController {
3739
using SafeCast for uint256;
3840

3941
/// @notice Allows users to revoke their chances to win by delegating to the sponsorship address.
40-
address public constant SPONSORSHIP_ADDRESS = address(1);
4142

4243
/// @notice Sets the minimum period length for Observations. When a period elapses, a new Observation is recorded, otherwise the most recent Observation is updated.
4344
uint32 public immutable PERIOD_LENGTH;
@@ -679,7 +680,8 @@ contract TwabController {
679680
(
680681
ObservationLib.Observation memory _observation,
681682
bool _isNewObservation,
682-
bool _isObservationRecorded
683+
bool _isObservationRecorded,
684+
TwabLib.AccountDetails memory accountDetails
683685
) = TwabLib.increaseBalances(PERIOD_LENGTH, PERIOD_OFFSET, _account, _amount, _delegateAmount);
684686

685687
// Always emit the balance change event
@@ -692,8 +694,8 @@ contract TwabController {
692694
emit ObservationRecorded(
693695
_vault,
694696
_user,
695-
_account.details.balance,
696-
_account.details.delegateBalance,
697+
accountDetails.balance,
698+
accountDetails.delegateBalance,
697699
_isNewObservation,
698700
_observation
699701
);
@@ -717,7 +719,8 @@ contract TwabController {
717719
(
718720
ObservationLib.Observation memory _observation,
719721
bool _isNewObservation,
720-
bool _isObservationRecorded
722+
bool _isObservationRecorded,
723+
TwabLib.AccountDetails memory accountDetails
721724
) = TwabLib.decreaseBalances(
722725
PERIOD_LENGTH,
723726
PERIOD_OFFSET,
@@ -737,8 +740,8 @@ contract TwabController {
737740
emit ObservationRecorded(
738741
_vault,
739742
_user,
740-
_account.details.balance,
741-
_account.details.delegateBalance,
743+
accountDetails.balance,
744+
accountDetails.delegateBalance,
742745
_isNewObservation,
743746
_observation
744747
);
@@ -761,7 +764,8 @@ contract TwabController {
761764
(
762765
ObservationLib.Observation memory _observation,
763766
bool _isNewObservation,
764-
bool _isObservationRecorded
767+
bool _isObservationRecorded,
768+
TwabLib.AccountDetails memory accountDetails
765769
) = TwabLib.decreaseBalances(
766770
PERIOD_LENGTH,
767771
PERIOD_OFFSET,
@@ -780,8 +784,8 @@ contract TwabController {
780784
if (_isObservationRecorded) {
781785
emit TotalSupplyObservationRecorded(
782786
_vault,
783-
_account.details.balance,
784-
_account.details.delegateBalance,
787+
accountDetails.balance,
788+
accountDetails.delegateBalance,
785789
_isNewObservation,
786790
_observation
787791
);
@@ -804,7 +808,8 @@ contract TwabController {
804808
(
805809
ObservationLib.Observation memory _observation,
806810
bool _isNewObservation,
807-
bool _isObservationRecorded
811+
bool _isObservationRecorded,
812+
TwabLib.AccountDetails memory accountDetails
808813
) = TwabLib.increaseBalances(PERIOD_LENGTH, PERIOD_OFFSET, _account, _amount, _delegateAmount);
809814

810815
// Always emit the balance change event
@@ -816,8 +821,8 @@ contract TwabController {
816821
if (_isObservationRecorded) {
817822
emit TotalSupplyObservationRecorded(
818823
_vault,
819-
_account.details.balance,
820-
_account.details.delegateBalance,
824+
accountDetails.balance,
825+
accountDetails.delegateBalance,
821826
_isNewObservation,
822827
_observation
823828
);

src/libraries/TwabLib.sol

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ library TwabLib {
9898
uint96 _delegateAmount
9999
)
100100
internal
101-
returns (ObservationLib.Observation memory observation, bool isNew, bool isObservationRecorded)
101+
returns (ObservationLib.Observation memory observation, bool isNew, bool isObservationRecorded, AccountDetails memory accountDetails)
102102
{
103-
AccountDetails memory accountDetails = _account.details;
103+
accountDetails = _account.details;
104104
isObservationRecorded = _delegateAmount != uint96(0);
105105

106106
accountDetails.balance += _amount;
@@ -136,9 +136,9 @@ library TwabLib {
136136
string memory _revertMessage
137137
)
138138
internal
139-
returns (ObservationLib.Observation memory observation, bool isNew, bool isObservationRecorded)
139+
returns (ObservationLib.Observation memory observation, bool isNew, bool isObservationRecorded, AccountDetails memory accountDetails)
140140
{
141-
AccountDetails memory accountDetails = _account.details;
141+
accountDetails = _account.details;
142142

143143
if (accountDetails.balance < _amount) {
144144
revert BalanceLTAmount(accountDetails.balance, _amount, _revertMessage);
@@ -549,15 +549,14 @@ library TwabLib {
549549
AccountDetails memory _accountDetails,
550550
uint32 _targetTime
551551
) private view returns (ObservationLib.Observation memory prevOrAtObservation) {
552-
uint32 currentTime = uint32(block.timestamp);
553-
554-
uint16 oldestTwabIndex;
555-
556552
// If there are no observations, return a zeroed observation
557553
if (_accountDetails.cardinality == 0) {
558554
return
559555
ObservationLib.Observation({ cumulativeBalance: 0, balance: 0, timestamp: PERIOD_OFFSET });
560556
}
557+
558+
uint32 currentTime = uint32(block.timestamp);
559+
uint16 oldestTwabIndex;
561560

562561
(oldestTwabIndex, prevOrAtObservation) = getOldestObservation(_observations, _accountDetails);
563562

test/TwabController.t.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
CannotTransferToSponsorshipAddress,
1111
MINIMUM_PERIOD_LENGTH,
1212
PeriodLengthTooShort,
13+
SPONSORSHIP_ADDRESS,
1314
PeriodOffsetInFuture
1415
} from "../src/TwabController.sol";
1516
import {
@@ -402,7 +403,7 @@ contract TwabControllerTest is BaseTest {
402403
assertEq(twabController.balanceOf(mockVault, alice), _amount);
403404
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
404405

405-
address _sponsorshipAddress = twabController.SPONSORSHIP_ADDRESS();
406+
address _sponsorshipAddress = SPONSORSHIP_ADDRESS;
406407
assertEq(twabController.balanceOf(mockVault, _sponsorshipAddress), 0);
407408
assertEq(twabController.delegateBalanceOf(mockVault, _sponsorshipAddress), 0);
408409

@@ -438,7 +439,7 @@ contract TwabControllerTest is BaseTest {
438439
assertEq(twabController.balanceOf(mockVault, bob), _amount);
439440
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
440441
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
441-
assertEq(twabController.delegateBalanceOf(mockVault, twabController.SPONSORSHIP_ADDRESS()), 0);
442+
assertEq(twabController.delegateBalanceOf(mockVault, SPONSORSHIP_ADDRESS), 0);
442443

443444
twabController.sponsor(bob);
444445
vm.stopPrank();
@@ -451,7 +452,7 @@ contract TwabControllerTest is BaseTest {
451452
// Delegate balances have changed
452453
assertEq(twabController.delegateBalanceOf(mockVault, alice), 0);
453454
assertEq(twabController.delegateBalanceOf(mockVault, bob), _amount);
454-
assertEq(twabController.delegateBalanceOf(mockVault, twabController.SPONSORSHIP_ADDRESS()), 0);
455+
assertEq(twabController.delegateBalanceOf(mockVault, SPONSORSHIP_ADDRESS), 0);
455456
}
456457

457458
function testMint() external {
@@ -657,7 +658,7 @@ contract TwabControllerTest is BaseTest {
657658

658659
function testTransfer_rejectSponsorshipAddress() public {
659660
twabController.mint(alice, 100e18);
660-
address sponsorship = twabController.SPONSORSHIP_ADDRESS();
661+
address sponsorship = SPONSORSHIP_ADDRESS;
661662
vm.expectRevert(abi.encodeWithSelector(CannotTransferToSponsorshipAddress.selector));
662663
twabController.transfer(alice, sponsorship, 100e18);
663664
}
@@ -735,7 +736,7 @@ contract TwabControllerTest is BaseTest {
735736
}
736737

737738
function testDelegateToSponsorship() external {
738-
address _sponsorshipAddress = twabController.SPONSORSHIP_ADDRESS();
739+
address _sponsorshipAddress = SPONSORSHIP_ADDRESS;
739740

740741
assertEq(twabController.delegateOf(mockVault, alice), alice);
741742

0 commit comments

Comments
 (0)