Skip to content

Commit

Permalink
add unit tests for L1MessageQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
zimpha committed Jul 3, 2024
1 parent 7ba07fe commit 48eaab0
Show file tree
Hide file tree
Showing 6 changed files with 667 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/L1/rollup/IL1MessageQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ interface IL1MessageQueue {
/// @param skippedBitmap A bitmap indicates whether a message is skipped.
event DequeueTransaction(uint256 startIndex, uint256 count, uint256 skippedBitmap);

event ResetDequeuedTransaction(uint256 startIndex, uint256 count);
/// @notice Emitted when dequeued transactions are reset.
/// @param startIndex The start index of messages.
event ResetDequeuedTransaction(uint256 startIndex);

/// @notice Emitted when some L1 => L2 transactions are finalized in L1.
/// @param finalizedIndex The last index of messages finalized.
event FinalizedDequeuedTransaction(uint256 finalizedIndex);

/// @notice Emitted when a message is dropped from L1.
Expand Down Expand Up @@ -153,8 +157,7 @@ interface IL1MessageQueue {
/// @dev We can only reset unfinalized popped messages.
///
/// @param startIndex The start index to reset.
/// @param count The number of messages to reset.
function resetPoppedCrossDomainMessage(uint256 startIndex, uint256 count) external;
function resetPoppedCrossDomainMessage(uint256 startIndex) external;

/// @notice Finalize status of popped messages.
/// @param newFinalizedQueueIndexPlusOne The index of message to finalize plus one.
Expand Down
15 changes: 9 additions & 6 deletions src/L1/rollup/L1MessageQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -374,18 +374,21 @@ contract L1MessageQueue is OwnableUpgradeable, IL1MessageQueue {
}

/// @inheritdoc IL1MessageQueue
/// @dev Caller should make sure `_count > 0` to reduce unnecessary contract call.
function resetPoppedCrossDomainMessage(uint256 _startIndex, uint256 _count) external override onlyScrollChain {
if (_count == 0) return;
/// @dev Caller should make sure `_startIndex < pendingQueueIndex` to reduce unnecessary contract call.
function resetPoppedCrossDomainMessage(uint256 _startIndex) external override onlyScrollChain {
uint256 cachedPendingQueueIndex = pendingQueueIndex;
if (_startIndex == cachedPendingQueueIndex) return;

require(_startIndex >= finalizedQueueIndexPlusOne, "reset finalized messages");
require(_startIndex + _count <= pendingQueueIndex, "reset non-popped messages");
require(_startIndex < cachedPendingQueueIndex, "reset pending messages");

unchecked {
uint256 count = cachedPendingQueueIndex - _startIndex;
uint256 bucket = _startIndex >> 8;
uint256 offset = _startIndex & 0xff;
skippedMessageBitmap[bucket] &= (1 << offset) - 1;
uint256 numResetMessages = 256 - offset;
while (numResetMessages < _count) {
while (numResetMessages < count) {
bucket += 1;
uint256 bitmap = skippedMessageBitmap[bucket];
if (bitmap > 0) skippedMessageBitmap[bucket] = 0;
Expand All @@ -394,7 +397,7 @@ contract L1MessageQueue is OwnableUpgradeable, IL1MessageQueue {
}

pendingQueueIndex = _startIndex;
emit ResetDequeuedTransaction(_startIndex, _count);
emit ResetDequeuedTransaction(_startIndex);
}

/// @inheritdoc IL1MessageQueue
Expand Down
12 changes: 4 additions & 8 deletions src/L1/rollup/ScrollChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,7 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
uint256 _firstBatchIndex,
uint256 _totalL1MessagesPoppedOverallFirstBatch
) = _loadBatchHeader(_firstBatchHeader);
(, , uint256 _lastBatchIndex, uint256 _totalL1MessagesPoppedOverallLastBatch) = _loadBatchHeader(
_lastBatchHeader
);
(, , uint256 _lastBatchIndex, ) = _loadBatchHeader(_lastBatchHeader);
if (_firstBatchIndex > _lastBatchIndex) revert ErrorRevertZeroBatches();

// make sure no gap is left when reverting from the ending to the beginning.
Expand All @@ -456,11 +454,9 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
// `getL1MessagePopped` codes are the same in V0, V1, V2, V3
uint256 l1MessagePoppedFirstBatch = BatchHeaderV0Codec.getL1MessagePopped(firstBatchPtr);
unchecked {
uint256 startQueueIndex = _totalL1MessagesPoppedOverallFirstBatch - l1MessagePoppedFirstBatch;
uint256 numMessages = _totalL1MessagesPoppedOverallLastBatch - startQueueIndex;
if (numMessages > 0) {
IL1MessageQueue(messageQueue).resetPoppedCrossDomainMessage(startQueueIndex, numMessages);
}
IL1MessageQueue(messageQueue).resetPoppedCrossDomainMessage(
_totalL1MessagesPoppedOverallFirstBatch - l1MessagePoppedFirstBatch
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mocks/ScrollChainMockBlob.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity =0.8.24;

import {ScrollChain} from "../L1/rollup/ScrollChain.sol";

contract ScrollChainMockFinalize is ScrollChain {
contract ScrollChainMockBlob is ScrollChain {
bytes32 blobVersionedHash;

/***************
Expand Down
Loading

0 comments on commit 48eaab0

Please sign in to comment.