From 7151eaec833f4a37c2fec77ddb08c76c93a9963f Mon Sep 17 00:00:00 2001 From: LHerskind Date: Wed, 28 Aug 2024 20:19:42 +0000 Subject: [PATCH] feat: revert if timestamp in future --- l1-contracts/src/core/Rollup.sol | 44 +++++++++++++--------- l1-contracts/src/core/libraries/Errors.sol | 2 +- l1-contracts/test/Rollup.t.sol | 16 ++++++++ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/l1-contracts/src/core/Rollup.sol b/l1-contracts/src/core/Rollup.sol index 02a3bdda097..bb9722d87e1 100644 --- a/l1-contracts/src/core/Rollup.sol +++ b/l1-contracts/src/core/Rollup.sol @@ -452,23 +452,6 @@ contract Rollup is Leonidas, IRollup, ITestRollup { _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 * @@ -589,6 +572,23 @@ contract Rollup is Leonidas, IRollup, ITestRollup { } } + 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 Validate a header for submission to the pending chain (sequencer selection checks) * @@ -703,6 +703,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) diff --git a/l1-contracts/src/core/libraries/Errors.sol b/l1-contracts/src/core/libraries/Errors.sol index 257d7695707..b7ff5d9b4e6 100644 --- a/l1-contracts/src/core/libraries/Errors.sol +++ b/l1-contracts/src/core/libraries/Errors.sol @@ -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 diff --git a/l1-contracts/test/Rollup.t.sol b/l1-contracts/test/Rollup.t.sol index cd7de61110e..07c67385a80 100644 --- a/l1-contracts/test/Rollup.t.sol +++ b/l1-contracts/test/Rollup.t.sol @@ -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)); @@ -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,