From 03e6514472a357f8cdb0ca494a48059d7677d25f Mon Sep 17 00:00:00 2001 From: Pierrick Turelier Date: Tue, 5 Oct 2021 21:09:54 -0500 Subject: [PATCH 1/3] fix(RingBufferLib): remove unecessary cast --- contracts/libraries/RingBufferLib.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/libraries/RingBufferLib.sol b/contracts/libraries/RingBufferLib.sol index eb8f73d9..917bd0e8 100644 --- a/contracts/libraries/RingBufferLib.sol +++ b/contracts/libraries/RingBufferLib.sol @@ -33,7 +33,7 @@ library RingBufferLib { return 0; } - return (_nextAvailableIndex + uint256(_cardinality) - 1) % _cardinality; + return (_nextAvailableIndex + _cardinality - 1) % _cardinality; } function nextIndex(uint256 _currentIndex, uint256 _cardinality) From aa9c4bdcb4313e49ee992dba712f4b6f6ca7f1c2 Mon Sep 17 00:00:00 2001 From: Pierrick Turelier Date: Tue, 5 Oct 2021 21:13:00 -0500 Subject: [PATCH 2/3] fix(RingBufferLib): use wrap helper function --- contracts/libraries/RingBufferLib.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/libraries/RingBufferLib.sol b/contracts/libraries/RingBufferLib.sol index 917bd0e8..70a76265 100644 --- a/contracts/libraries/RingBufferLib.sol +++ b/contracts/libraries/RingBufferLib.sol @@ -17,7 +17,7 @@ library RingBufferLib { uint256 _amount, uint256 _cardinality ) internal pure returns (uint256) { - return (_index + _cardinality - _amount) % _cardinality; + return wrap(_index + _cardinality - _amount, _cardinality); } /// @notice Returns the index of the last recorded TWAB @@ -33,7 +33,7 @@ library RingBufferLib { return 0; } - return (_nextAvailableIndex + _cardinality - 1) % _cardinality; + return wrap(_nextAvailableIndex + _cardinality - 1, _cardinality); } function nextIndex(uint256 _currentIndex, uint256 _cardinality) @@ -41,6 +41,6 @@ library RingBufferLib { pure returns (uint256) { - return (_currentIndex + 1) % _cardinality; + return wrap(_currentIndex + 1, _cardinality); } } From 78790fa75cae89a07884c0d39a937e2924f35a25 Mon Sep 17 00:00:00 2001 From: Pierrick Turelier Date: Tue, 5 Oct 2021 21:34:27 -0500 Subject: [PATCH 3/3] fix(RingBufferLib): add Natspec doc. --- contracts/libraries/RingBufferLib.sol | 38 ++++++++++++++++++++------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/contracts/libraries/RingBufferLib.sol b/contracts/libraries/RingBufferLib.sol index 70a76265..9f4dbba9 100644 --- a/contracts/libraries/RingBufferLib.sol +++ b/contracts/libraries/RingBufferLib.sol @@ -3,15 +3,27 @@ pragma solidity 0.8.6; library RingBufferLib { - /// @notice Returns TWAB index. - /// @dev `twabs` is a circular buffer of `MAX_CARDINALITY` size equal to 32. So the array goes from 0 to 31. - /// @dev In order to navigate the circular buffer, we need to use the modulo operator. - /// @dev For example, if `_index` is equal to 32, `_index % MAX_CARDINALITY` will return 0 and will point to the first element of the array. - /// @param _index Index used to navigate through `twabs` circular buffer. + /** + * @notice Returns wrapped TWAB index. + * @dev In order to navigate the TWAB circular buffer, we need to use the modulo operator. + * @dev For example, if `_index` is equal to 32 and the TWAB circular buffer is of `_cardinality` 32, + * it will return 0 and will point to the first element of the array. + * @param _index Index used to navigate through the TWAB circular buffer. + * @param _cardinality TWAB buffer cardinality. + * @return TWAB index. + */ function wrap(uint256 _index, uint256 _cardinality) internal pure returns (uint256) { return _index % _cardinality; } + /** + * @notice Returns the `_index` offsetted by `_amount`. + * @dev We add `_cardinality` to `_index` to be able to offset event if `_amount` is superior to `_cardinality`. + * @param _index Index to offset. + * @param _amount Amount we want to offset the `_index` by. + * @param _cardinality TWAB buffer cardinality. + * @return Offsetted index. + */ function offset( uint256 _index, uint256 _amount, @@ -20,10 +32,12 @@ library RingBufferLib { return wrap(_index + _cardinality - _amount, _cardinality); } - /// @notice Returns the index of the last recorded TWAB - /// @param _nextAvailableIndex The next available twab index. This will be recorded to next. - /// @param _cardinality The cardinality of the TWAB history. - /// @return The index of the last recorded TWAB + /** + * @notice Returns index of the last recorded TWAB. + * @param _nextAvailableIndex Next available twab index to which will be recorded the next TWAB. + * @param _cardinality TWAB buffer cardinality. + * @return Index of the last recorded TWAB. + */ function mostRecentIndex(uint256 _nextAvailableIndex, uint256 _cardinality) internal pure @@ -36,6 +50,12 @@ library RingBufferLib { return wrap(_nextAvailableIndex + _cardinality - 1, _cardinality); } + /** + * @notice Returns the next available TWAB index. + * @param _currentIndex Current TWAB buffer index. + * @param _cardinality TWAB buffer cardinality. + * @return Next available TWAB index. + */ function nextIndex(uint256 _currentIndex, uint256 _cardinality) internal pure