Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audit: Optimize ActiveProposalLimiter storage reads #226

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}