Skip to content

Commit

Permalink
refactor: DRY time check
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
MaxMustermann2 committed Aug 5, 2024
1 parent ca7d77c commit 3eb235a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion script/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async function updateGenesisFile() {
}

// Set spawn time
const spawnTime = await myContract.methods.exocoreSpawnTime().call();
const spawnTime = await myContract.methods.spawnTime().call();
const spawnTimeInSeconds = spawnTime.toString();
const spawnDate = new Date(spawnTimeInSeconds * 1000).toISOString();
genesisJSON.genesis_time = spawnDate;
Expand Down
62 changes: 29 additions & 33 deletions src/core/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,15 @@ contract Bootstrap is
if (owner == address(0)) {
revert Errors.ZeroAddress();
}
if (spawnTime_ <= block.timestamp) {
revert Errors.BootstrapSpawnTimeAlreadyPast();
}
if (offsetDuration_ == 0) {
revert Errors.ZeroValue();
}
if (spawnTime_ <= offsetDuration_) {
revert Errors.BootstrapSpawnTimeLessThanDuration();
}
uint256 lockTime = spawnTime_ - offsetDuration_;
if (lockTime <= block.timestamp) {
revert Errors.BootstrapLockTimeAlreadyPast();
}

_validateSpawnTimeAndOffsetDuration(spawnTime_, offsetDuration_);
spawnTime = spawnTime_;
offsetDuration = offsetDuration_;

if (customProxyAdmin_ == address(0)) {
revert Errors.ZeroAddress();
}

exocoreSpawnTime = spawnTime_;
offsetDuration = offsetDuration_;

_addWhitelistTokens(whitelistTokens_);

_whiteListFunctionSelectors[Action.REQUEST_MARK_BOOTSTRAP] = this.markBootstrapped.selector;
Expand All @@ -109,7 +98,7 @@ contract Bootstrap is
/// @dev Returns true if the contract is locked, false otherwise.
/// @return bool Returns `true` if the contract is locked, `false` otherwise.
function isLocked() public view returns (bool) {
return block.timestamp >= exocoreSpawnTime - offsetDuration;
return block.timestamp >= spawnTime - offsetDuration;
}

/// @dev Modifier to restrict operations based on the contract's defined timeline, that is,
Expand Down Expand Up @@ -138,18 +127,9 @@ contract Bootstrap is
/// be called before the currently set lock time has started.
/// @param _spawnTime The new spawn time in seconds.
function setSpawnTime(uint256 _spawnTime) external onlyOwner beforeLocked {
if (_spawnTime <= block.timestamp) {
revert Errors.BootstrapSpawnTimeAlreadyPast();
}
if (_spawnTime <= offsetDuration) {
revert Errors.BootstrapSpawnTimeLessThanDuration();
}
uint256 lockTime = _spawnTime - offsetDuration;
if (lockTime <= block.timestamp) {
revert Errors.BootstrapLockTimeAlreadyPast();
}
_validateSpawnTimeAndOffsetDuration(_spawnTime, offsetDuration);
// technically the spawn time can be moved backwards in time as well.
exocoreSpawnTime = _spawnTime;
spawnTime = _spawnTime;
emit SpawnTimeUpdated(_spawnTime);
}

Expand All @@ -159,15 +139,31 @@ contract Bootstrap is
/// before the currently set lock time has started.
/// @param _offsetDuration The new offset duration in seconds.
function setOffsetDuration(uint256 _offsetDuration) external onlyOwner beforeLocked {
if (exocoreSpawnTime <= _offsetDuration) {
_validateSpawnTimeAndOffsetDuration(spawnTime, _offsetDuration);
offsetDuration = _offsetDuration;
emit OffsetDurationUpdated(_offsetDuration);
}

/// @dev Validates the spawn time and offset duration.
/// The spawn time must be in the future and greater than the offset duration.
/// The difference of the two must be greater than the current time.
/// @param _spawnTime The new spawn time of the Exocore chain.
/// @param _offsetDuration The new offset duration before the spawn time.
function _validateSpawnTimeAndOffsetDuration(uint256 _spawnTime, uint256 _offsetDuration) internal view {
if (_offsetDuration == 0) {
revert Errors.ZeroValue();
}
// _spawnTime == 0 is included in the below check.
if (_spawnTime <= block.timestamp) {
revert Errors.BootstrapSpawnTimeAlreadyPast();
}
if (_spawnTime <= _offsetDuration) {
revert Errors.BootstrapSpawnTimeLessThanDuration();
}
uint256 lockTime = exocoreSpawnTime - _offsetDuration;
uint256 lockTime = _spawnTime - _offsetDuration;
if (lockTime <= block.timestamp) {
revert Errors.BootstrapLockTimeAlreadyPast();
}
offsetDuration = _offsetDuration;
emit OffsetDurationUpdated(_offsetDuration);
}

/// @inheritdoc ITokenWhitelister
Expand Down Expand Up @@ -529,7 +525,7 @@ contract Bootstrap is
// nonce match, which requires that inbound nonce is uint64(1).
// TSS checks are not super clear since they can be set by anyone
// but at this point that does not matter since it is not fully implemented anyway.
if (block.timestamp < exocoreSpawnTime) {
if (block.timestamp < spawnTime) {
revert Errors.BootstrapNotSpawnTime();
}
if (bootstrapped) {
Expand Down
5 changes: 3 additions & 2 deletions src/core/ClientChainGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ contract ClientChainGateway is
delete clientChainGatewayLogic;
delete clientChainInitializationData;
// no risk keeping these but they are cheap to clear.
delete exocoreSpawnTime;
delete spawnTime;
delete offsetDuration;
// previously, we tried clearing the loops but it is too expensive.
// previously, we tried clearing the contents of these in loops but it is too expensive.
delete depositors;
delete registeredValidators;
// mappings cannot be deleted
}

/// @notice Pauses the contract.
Expand Down
4 changes: 2 additions & 2 deletions src/storage/BootstrapStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ contract BootstrapStorage is GatewayStorage {
// time and duration
/// @notice A timestamp representing the scheduled spawn time of the Exocore chain, which influences the contract's
/// operational restrictions.
/// @dev `offsetDuration` before `exocoreSpawnTime`, the contract freezes and most actions are prohibited.
uint256 public exocoreSpawnTime;
/// @dev `offsetDuration` before `spawnTime`, the contract freezes and most actions are prohibited.
uint256 public spawnTime;

/// @notice The amount of time before the Exocore spawn time during which operations are restricted.
/// @dev The duration before the Exocore spawn time during which most contract operations are locked.
Expand Down
2 changes: 1 addition & 1 deletion test/foundry/unit/Bootstrap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ contract BootstrapTest is Test {
function test16_SetSpawnTime() public {
vm.startPrank(deployer);
bootstrap.setSpawnTime(block.timestamp + 35 minutes);
assertTrue(bootstrap.exocoreSpawnTime() == block.timestamp + 35 minutes);
assertTrue(bootstrap.spawnTime() == block.timestamp + 35 minutes);
}

function test16_SetSpawnTime_NotInFuture() public {
Expand Down

0 comments on commit 3eb235a

Please sign in to comment.