Skip to content

Commit

Permalink
fix(TwabLib): prevent account.cardianlity from exceeding max cardinality
Browse files Browse the repository at this point in the history
  • Loading branch information
kamescg committed Oct 5, 2021
1 parent 4c9f635 commit e3af77f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions contracts/libraries/TwabLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,15 @@ library TwabLib {
_accountDetails.nextTwabIndex = uint24(
RingBufferLib.nextIndex(_accountDetails.nextTwabIndex, MAX_CARDINALITY)
);

_accountDetails.cardinality += 1;

// Prevent the Account specific cardinality from exceeding the MAX_CARDINALITY.
// The ring buffer length is limited by MAX_CARDINALITY. IF the account.cardinality
// exceeds the max cardinality, new observations would be incorrectly set or the
// observation would be out of "bounds" of the ring buffer. Once reached the
// AccountDetails.cardinality will continue to be equal to max cardinality.
if(_accountDetails.cardinality < MAX_CARDINALITY) {
_accountDetails.cardinality += 1;
}

return (_accountDetails, newTwab, true);
}
Expand Down

0 comments on commit e3af77f

Please sign in to comment.