Skip to content

Commit

Permalink
feat: revert if timestamp in future
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Aug 29, 2024
1 parent da2eb7a commit 3388966
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
54 changes: 33 additions & 21 deletions l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -444,31 +444,10 @@ contract Rollup is Leonidas, IRollup, ITestRollup {
uint256 _currentTime,
DataStructures.ExecutionFlags memory _flags
) external view override(IRollup) {
// @note This is a convenience function to allow for easier testing of the header validation
// without having to go through the entire process of submitting a block.
// This is not used in production code.

HeaderLib.Header memory header = HeaderLib.decode(_header);
_validateHeader(header, _signatures, _digest, _currentTime, _flags);
}

function _validateHeader(
HeaderLib.Header memory _header,
SignatureLib.Signature[] memory _signatures,
bytes32 _digest,
uint256 _currentTime,
DataStructures.ExecutionFlags memory _flags
) internal view {
// @note This is a convenience function to allow for easier testing of the header validation
// without having to go through the entire process of submitting a block.
// This is not used in production code.

_validateHeaderForSubmissionBase(_header, _currentTime, _flags);
_validateHeaderForSubmissionSequencerSelection(
_header.globalVariables.slotNumber, _signatures, _digest, _currentTime, _flags
);
}

/**
* @notice Processes an incoming L2 block with signatures
*
Expand Down Expand Up @@ -589,6 +568,29 @@ contract Rollup is Leonidas, IRollup, ITestRollup {
}
}

/**
* @notice Validates the header for submission
*
* @param _header - The proposed block header
* @param _signatures - The signatures for the attestations
* @param _digest - The digest that signatures signed
* @param _currentTime - The time of execution
* @dev - This value is provided to allow for simple simulation of future
* @param _flags - Flags specific to the execution, whether certain checks should be skipped
*/
function _validateHeader(
HeaderLib.Header memory _header,
SignatureLib.Signature[] memory _signatures,
bytes32 _digest,
uint256 _currentTime,
DataStructures.ExecutionFlags memory _flags
) internal view {
_validateHeaderForSubmissionBase(_header, _currentTime, _flags);
_validateHeaderForSubmissionSequencerSelection(
_header.globalVariables.slotNumber, _signatures, _digest, _currentTime, _flags
);
}

/**
* @notice Validate a header for submission to the pending chain (sequencer selection checks)
*
Expand Down Expand Up @@ -703,6 +705,16 @@ contract Rollup is Leonidas, IRollup, ITestRollup {
revert Errors.Rollup__InvalidTimestamp(timestamp, _header.globalVariables.timestamp);
}

if (timestamp > _currentTime) {
// @note If you are hitting this error, it is likely because the chain you use have a blocktime that differs
// from the value that we have in the constants.
// When you are encountering this, it will likely be as the sequencer expects to be able to include
// an Aztec block in the "next" ethereum block based on a timestamp that is 12 seconds in the future
// from the last block. However, if the actual will only be 1 second in the future, you will end up
// expecting this value to be in the future.
revert Errors.Rollup__TimestampInFuture(_currentTime, timestamp);
}

// Check if the data is available using availability oracle (change availability oracle if you want a different DA layer)
if (
!_flags.ignoreDA && !AVAILABILITY_ORACLE.isAvailable(_header.contentCommitment.txsEffectsHash)
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ library Errors {
error Rollup__InvalidChainId(uint256 expected, uint256 actual); // 0x37b5bc12
error Rollup__InvalidVersion(uint256 expected, uint256 actual); // 0x9ef30794
error Rollup__InvalidTimestamp(uint256 expected, uint256 actual); // 0x3132e895
error Rollup__TimestampInFuture(); // 0xbc1ce916
error Rollup__TimestampInFuture(uint256 max, uint256 actual); // 0x89f30690
error Rollup__TimestampTooOld(); // 0x72ed9c81
error Rollup__UnavailableTxs(bytes32 txsHash); // 0x414906c3
error Rollup__NothingToPrune(); // 0x850defd3
Expand Down
16 changes: 16 additions & 0 deletions l1-contracts/test/Rollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ contract RollupTest is DecoderBase {
_;
}

function testTimestamp() public setUpFor("mixed_block_1") {
// Ensure that the timestamp of the current slot is never in the future.
for (uint256 i = 0; i < 100; i++) {
uint256 slot = rollup.getCurrentSlot();
uint256 ts = rollup.getTimestampForSlot(slot);

assertLe(ts, block.timestamp, "Invalid timestamp");

vm.warp(block.timestamp + 12);
vm.roll(block.number + 1);
}
}

function testRevertPrune() public setUpFor("mixed_block_1") {
if (rollup.isDevNet()) {
vm.expectRevert(abi.encodeWithSelector(Errors.DevNet__NoPruningAllowed.selector));
Expand Down Expand Up @@ -182,6 +195,9 @@ contract RollupTest is DecoderBase {

uint256 portalBalance = portalERC20.balanceOf(address(feeJuicePortal));

// We jump to the time of the block. (unless it is in the past)
vm.warp(max(block.timestamp, data.decodedHeader.globalVariables.timestamp));

vm.expectRevert(
abi.encodeWithSelector(
IERC20Errors.ERC20InsufficientBalance.selector,
Expand Down

0 comments on commit 3388966

Please sign in to comment.