Skip to content

Commit

Permalink
audit: Optimize ActiveProposalLimiter storage reads (#226)
Browse files Browse the repository at this point in the history
* refactor: use PackedData struct in memory for reads

* chore: updated comments

---------

Co-authored-by: Orlando <[email protected]>
  • Loading branch information
Orland0x and Orlando authored Jun 27, 2023
1 parent f94a11f commit c3b5e3d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/utils/ActiveProposalsLimiter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.18;
/// @title Active Proposals Limiter Proposal Validation Module
/// @notice This module can be used to limit the number of active proposals per author.
abstract contract ActiveProposalsLimiter {
/// @dev Active proposal data stored for each author in a space.
struct PackedData {
uint32 activeProposals;
uint32 lastUpdate;
Expand All @@ -13,15 +14,15 @@ abstract contract ActiveProposalsLimiter {
/// @notice Thrown when the maximum number of active proposals per user is set to 0.
error MaxActiveProposalsCannotBeZero();

/// @dev Mapping that stores a data struct for each space.
/// @dev Mapping that stores a data struct for each author in a space.
mapping(address space => mapping(address author => PackedData)) private usersPackedData;

/// @dev Validates an author by checking if they have reached the maximum number of active proposals at the current timestamp.
function _validate(address author, uint256 cooldown, uint256 maxActiveProposals) internal returns (bool success) {
if (maxActiveProposals == 0) revert MaxActiveProposalsCannotBeZero();

// The space calls the proposal validation strategy, therefore msg.sender corresponds to the space address.
PackedData storage packedData = usersPackedData[msg.sender][author];
PackedData memory packedData = usersPackedData[msg.sender][author];

if (packedData.lastUpdate == 0) {
// First time the user proposes, activeProposals is 1 no matter what.
Expand All @@ -36,8 +37,8 @@ abstract contract ActiveProposalsLimiter {
// Cooldown has not passed, user has not reached maximum active proposals: increase counter.
packedData.activeProposals += 1;
}

packedData.lastUpdate = uint32(block.timestamp);
usersPackedData[msg.sender][author] = packedData;
return true;
}
}

0 comments on commit c3b5e3d

Please sign in to comment.