From 018ba1c9d2f8ae3678a8ed69a4ab2980e1f845a5 Mon Sep 17 00:00:00 2001 From: Sergei Tikhomirov Date: Thu, 3 Oct 2024 10:38:04 +0200 Subject: [PATCH] define period boundaries: start inclusive, end exclusive --- src/Membership.sol | 18 +++++++++--------- test/WakuRlnV2.t.sol | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Membership.sol b/src/Membership.sol index fd28711..afa5339 100644 --- a/src/Membership.sol +++ b/src/Membership.sol @@ -296,11 +296,11 @@ abstract contract MembershipUpgradeable is Initializable { } /// @dev Determine whether the current timestamp is within a given period - /// @param _start timestamp in which the period starts - /// @param _duration duration of the period + /// @param _start timestamp in which the period starts (inclusive) + /// @param _duration duration of the period (end timestamp exclusive) function _isInPeriod(uint256 _start, uint32 _duration) internal view returns (bool) { uint256 timeNow = block.timestamp; - return (_start <= timeNow && timeNow <= _start + uint256(_duration)); + return (_start <= timeNow && timeNow < _start + uint256(_duration)); } /// @notice Determine if a membership is expired @@ -311,11 +311,11 @@ abstract contract MembershipUpgradeable is Initializable { } /// @dev Determine whether the current timestamp is after a given period - /// @param _start timestamp in which the period starts - /// @param _duration duration of the period + /// @param _start timestamp in which the period starts (inclusive) + /// @param _duration duration of the period (end timestamp exclusive) function _isAfterPeriod(uint256 _start, uint32 _duration) internal view returns (bool) { uint256 timeNow = block.timestamp; - return (_start + uint256(_duration) < timeNow); + return (_timestampAfterPeriod(_start, _duration) <= timeNow); } /// @notice Returns the timestamp on which a membership can be considered expired (i.e. when its grace period ends) @@ -326,10 +326,10 @@ abstract contract MembershipUpgradeable is Initializable { } /// @dev Returns the first timestamp after a specified period - /// @param _start timestamp in which the period starts - /// @param _duration duration of the period + /// @param _start timestamp in which the period starts (inclusive) + /// @param _duration duration of the period (exclusive) function _timestampAfterPeriod(uint256 _start, uint32 _duration) internal pure returns (uint256) { - return _start + uint256(_duration) + 1; + return _start + uint256(_duration); } /// @dev Withdraw any available deposit balance in tokens after a membership is erased. diff --git a/test/WakuRlnV2.t.sol b/test/WakuRlnV2.t.sol index ed11595..ad3fc60 100644 --- a/test/WakuRlnV2.t.sol +++ b/test/WakuRlnV2.t.sol @@ -268,7 +268,7 @@ contract WakuRlnV2Test is Test { w.extendMemberships(commitmentsToExtend); (,, uint256 newGracePeriodStartTimestamp, uint32 newGracePeriodDuration,,,,) = w.memberships(idCommitment); - uint256 expectedExpirationTimestamp = newGracePeriodStartTimestamp + uint256(newGracePeriodDuration) + 1; + uint256 expectedExpirationTimestamp = newGracePeriodStartTimestamp + uint256(newGracePeriodDuration); uint256 membershipExpirationTimestamp = w.membershipExpirationTimestamp(idCommitment); assertEq(expectedExpirationTimestamp, membershipExpirationTimestamp); assertTrue(expectedExpirationTimestamp > ogExpirationTimestamp); @@ -289,7 +289,7 @@ contract WakuRlnV2Test is Test { (,, uint256 fetchedgracePeriodStartTimestamp, uint32 fetchedGracePeriod,,,,) = w.memberships(idCommitment); - uint256 expectedExpirationTimestamp = fetchedgracePeriodStartTimestamp + uint256(fetchedGracePeriod) + 1; + uint256 expectedExpirationTimestamp = fetchedgracePeriodStartTimestamp + uint256(fetchedGracePeriod); uint256 membershipExpirationTimestamp = w.membershipExpirationTimestamp(idCommitment); assertEq(expectedExpirationTimestamp, membershipExpirationTimestamp);