Skip to content

Commit

Permalink
refactor: remove universal period (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
pegahcarter authored Nov 21, 2024
1 parent 1ff6513 commit a25e8a3
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 264 deletions.
9 changes: 0 additions & 9 deletions contracts/globalParameters/GlobalParameters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.20;

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "./IGlobalParameters.sol";
import {TimeLibrary} from "../libraries/TimeLibrary.sol";

contract GlobalParameters is IGlobalParameters, OwnableUpgradeable {
// slot 1
Expand Down Expand Up @@ -33,7 +32,6 @@ contract GlobalParameters is IGlobalParameters, OwnableUpgradeable {
// Slot 3
uint64 public credibleNeutrality6ExpExpiresAt;

uint32 public period0Start;
uint128 public penaltyFactor;
uint128 public constraintFactor;

Expand All @@ -56,9 +54,6 @@ contract GlobalParameters is IGlobalParameters, OwnableUpgradeable {

/// @dev fill with default values
function initialize() external initializer {
period0Start = TimeLibrary.periodStart({timestamp: uint32(block.timestamp)});
periodDuration = TimeLibrary.FOUR_WEEKS;

constraintFactor = 4e17; // 40%
penaltyFactor = 4e17; // 40%

Expand All @@ -69,10 +64,6 @@ contract GlobalParameters is IGlobalParameters, OwnableUpgradeable {
credibleNeutrality6Exp = 1_300_000;
}

function currentPeriodId() external view returns (uint32) {
return TimeLibrary.periodId({period0Start: period0Start, timestamp: uint32(block.timestamp)});
}

function stagePeriodDuration(uint32 nextPeriodDuration) external {
_checkOwner();
if (periodDurationExpiresAt != 0) {
Expand Down
2 changes: 0 additions & 2 deletions contracts/globalParameters/IGlobalParameters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ interface IGlobalParameters {
event PrestigeForPeriod0Unstaged();
event PrestiveForPeriod0Committed();

function period0Start() external view returns (uint32);
function currentPeriodId() external view returns (uint32);
function constraintFactor() external view returns (uint128);
function penaltyFactor() external view returns (uint128);

Expand Down
29 changes: 5 additions & 24 deletions contracts/hub/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {IGlobalParameters} from "../globalParameters/IGlobalParameters.sol";
import {IMembership} from "../membership/IMembership.sol";
// import {OnboardingModule} from "../modules/onboarding/OnboardingModule.sol";
import {HubUtils} from "./HubUtils.sol";
import {PeriodUtils} from "../utils/PeriodUtils.sol";
import {IHub} from "./interfaces/IHub.sol";
import {ITaskManager} from "../tasks/interfaces/ITaskManager.sol";
import {Domain, IHubDomainsRegistry} from "./interfaces/IHubDomainsRegistry.sol";

contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
contract Hub is IHub, HubUtils, PeriodUtils, OwnableUpgradeable, HubUpgradeable {
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;

Expand All @@ -40,9 +41,6 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
uint256 archetype;
uint256 market;
string uri;
uint32 initTimestamp;
uint32 initPeriodId;
uint32 period0Start;
EnumerableSet.AddressSet admins;
EnumerableSet.UintSet roles;
string[] urls;
Expand Down Expand Up @@ -94,9 +92,7 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
_setCommitment(_commitment);
_setUri(_uri);

$.initTimestamp = uint32(block.timestamp);
$.period0Start = IGlobalParameters(_globalParameters).period0Start();
$.initPeriodId = TimeLibrary.periodId({period0Start: $.period0Start, timestamp: uint32(block.timestamp)});
_init_PeriodUtils();
}

/// @dev contracts specific to this hub
Expand Down Expand Up @@ -242,21 +238,6 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
return $.uri;
}

function initTimestamp() external view returns (uint32) {
HubStorage storage $ = _getHubStorage();
return $.initTimestamp;
}

function initPeriodId() external view returns (uint32) {
HubStorage storage $ = _getHubStorage();
return $.initPeriodId;
}

function period0Start() external view returns (uint32) {
HubStorage storage $ = _getHubStorage();
return $.period0Start;
}

/// @inheritdoc IHub
function membersCount() external view returns (uint256) {
return IMembership(membership()).membersCount();
Expand All @@ -283,11 +264,11 @@ contract Hub is IHub, HubUtils, OwnableUpgradeable, HubUpgradeable {
return currentRole(who) == role;
}

function hadRole(address who, uint256 role, uint32 periodId) external view returns (bool) {
function hadRole(address who, uint256 role, uint32 period) external view returns (bool) {
// TODO
}

function roleAtPeriod(address who, uint32 periodId) public view returns (uint256) {
function roleAtPeriod(address who, uint32 period) public view returns (uint256) {
// TODO
}

Expand Down
14 changes: 1 addition & 13 deletions contracts/hub/HubRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradea
$.initialContributionManager = _initialContributionManager;
}

/// @inheritdoc IHubRegistry
function currentPeriodId() public view returns (uint32) {
HubRegistryStorage storage $ = _getHubRegistryStorage();
return IGlobalParameters($.globalParameters).currentPeriodId();
}

/// @inheritdoc IHubRegistry
function period0Start() public view returns (uint32) {
HubRegistryStorage storage $ = _getHubRegistryStorage();
return IGlobalParameters($.globalParameters).period0Start();
}

/// @inheritdoc IHubRegistry
function deployHub(
uint256[] calldata roles,
Expand Down Expand Up @@ -134,7 +122,7 @@ contract HubRegistry is IHubRegistry, ERC2771ContextUpgradeable, OwnableUpgradea
hub = address(new BeaconProxy(address($.upgradeableBeacon), data));

// data for all hub-owned modules
data = abi.encodeCall(IHubModule.initialize, (hub, period0Start(), currentPeriodId()));
data = abi.encodeCall(IHubModule.initialize, (hub));

// deploy taskFactory
address taskFactory = address(new AutProxy($.taskFactoryImplementation, _msgSender(), data));
Expand Down
2 changes: 1 addition & 1 deletion contracts/hub/interfaces/IHubModule.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pragma solidity >=0.8.0;

interface IHubModule {
function initialize(address, uint32, uint32) external;
function initialize(address) external;
}
6 changes: 0 additions & 6 deletions contracts/hub/interfaces/IHubRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ interface IHubRegistry {
/// @dev only callable by Hub owner
function setInitialContributionManager(address _initialContributionManager) external;

/// @notice return the current period id (as stored globally)
function currentPeriodId() external view returns (uint32);

/// @notice return the timestamp of when period id 0 begins (as stored globally)
function period0Start() external view returns (uint32);

/// @notice return the array of hubs created through the registry
/// @dev supports subgraph queries
function hubs() external view returns (address[] memory);
Expand Down
24 changes: 11 additions & 13 deletions contracts/libraries/TimeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ library TimeLibrary {
uint32 internal constant FOUR_WEEKS = 28 days;

/// @notice Round down the timestamp passed to the start of the period
function periodStart(uint32 timestamp) internal pure returns (uint32) {
unchecked {
return timestamp - (timestamp % FOUR_WEEKS);
}
function periodStart(uint32 currentTimestamp, uint32 initTimestamp) internal pure returns (uint32) {
return initTimestamp + numPeriodsCompleted(currentTimestamp, initTimestamp) * FOUR_WEEKS;
}

/// @notice Round up the timestamp passed to the end of the period
function periodEnd(uint32 timestamp) internal pure returns (uint32) {
unchecked {
return periodStart({timestamp: timestamp}) + FOUR_WEEKS;
}
function periodEnd(uint32 currentTimestamp, uint32 initTimestamp) internal pure returns (uint32) {
return periodStart(currentTimestamp, initTimestamp) + FOUR_WEEKS;
}

/// @dev returns the number of full periods betwen the init and current time
function numPeriodsCompleted(uint32 currentTimestamp, uint32 initTimestamp) internal pure returns (uint32) {
return (currentTimestamp - initTimestamp) / FOUR_WEEKS;
}

/// @dev returns the id of the current period, starting at 1
function periodId(uint32 period0Start, uint32 timestamp) internal pure returns (uint32) {
unchecked {
return (((periodStart({timestamp: timestamp}) - period0Start) / FOUR_WEEKS) + 1);
}
function periodId(uint32 currentTimestamp, uint32 initTimestamp) internal pure returns (uint32) {
return numPeriodsCompleted(currentTimestamp, initTimestamp) + 1;
}
}
16 changes: 7 additions & 9 deletions contracts/membership/IMembership.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface IMembership {
error TooLateCommitmentChange();
error SameCommitment();
error InvalidCommitment();
error InvalidPeriodId();

event ChangeCommitment(address indexed who, uint8 oldCommitment, uint8 newCommitment);

Expand All @@ -33,25 +34,22 @@ interface IMembership {
function membersCount() external view returns (uint256);

/// @notice Get the period ID of when the member joined the hub
function getPeriodIdJoined(address who) external view returns (uint32 periodId);
function getPeriodJoined(address who) external view returns (uint32 period);

/// @notice Get the period ID of when an array of members joined the hub
function getPeriodIdsJoined(address[] calldata whos) external view returns (uint32[] memory);
function getPeriodsJoined(address[] calldata whos) external view returns (uint32[] memory);

/// @notice get the commitment level of a member at a particular period id
function getCommitment(address who, uint32 periodId) external view returns (uint8 commitment);
function getCommitment(address who, uint32 period) external view returns (uint8 commitment);

/// @notice get the commitment level of an array of members at a particular period id
function getCommitments(
address[] calldata whos,
uint32[] calldata periodIds
) external view returns (uint8[] memory);
function getCommitments(address[] calldata whos, uint32[] calldata periods) external view returns (uint8[] memory);

/// @notice get the cumulative commitment by members at a given period id
function getCommitmentSum(uint32 periodId) external view returns (uint128 commitmentSum);
function getCommitmentSum(uint32 period) external view returns (uint128 commitmentSum);

/// @notice get the cumulative commitment by members at an array of given period ids
function getCommitmentSums(uint32[] calldata periodIds) external view returns (uint128[] memory);
function getCommitmentSums(uint32[] calldata periods) external view returns (uint128[] memory);

/// @notice Join the hub
/// @dev Is called through the hub
Expand Down
Loading

0 comments on commit a25e8a3

Please sign in to comment.