Skip to content

Commit

Permalink
refactors scroll sequencer uptime feed to reduce gas and updates test…
Browse files Browse the repository at this point in the history
… suites
  • Loading branch information
chris-de-leon-cll committed Dec 10, 2023
1 parent 0a30bc9 commit ebbb791
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
32 changes: 18 additions & 14 deletions contracts/src/v0.8/l2ep/dev/scroll/ScrollSequencerUptimeFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ contract ScrollSequencerUptimeFeed is
/// @param timestamp The L1 block timestamp of status update
function _recordRound(uint80 roundId, bool status, uint64 timestamp) private {
s_feedState = FeedState(roundId, status, timestamp, uint64(block.timestamp));
s_rounds[roundId] = Round(status, timestamp, s_feedState.updatedAt);
s_rounds[roundId] = Round(status, timestamp, uint64(block.timestamp));

emit NewRound(roundId, msg.sender, timestamp);
emit AnswerUpdated(_getStatusAnswer(status), roundId, timestamp);
Expand All @@ -126,8 +126,8 @@ contract ScrollSequencerUptimeFeed is
/// @param status Sequencer status
function _updateRound(uint80 roundId, bool status) private {
s_feedState.updatedAt = uint64(block.timestamp);
s_rounds[roundId].updatedAt = s_feedState.updatedAt;
emit RoundUpdated(_getStatusAnswer(status), s_feedState.updatedAt);
s_rounds[roundId].updatedAt = uint64(block.timestamp);
emit RoundUpdated(_getStatusAnswer(status), uint64(block.timestamp));
}

/// @notice Record a new status and timestamp if it has changed since the last round.
Expand All @@ -136,23 +136,25 @@ contract ScrollSequencerUptimeFeed is
/// @param status Sequencer status
/// @param timestamp Block timestamp of status update
function updateStatus(bool status, uint64 timestamp) external override {
FeedState memory feedState = s_feedState;

if (
msg.sender != address(s_l2CrossDomainMessenger) || s_l2CrossDomainMessenger.xDomainMessageSender() != s_l1Sender
) {
revert InvalidSender();
}

// Ignore if latest recorded timestamp is newer
if (s_feedState.startedAt > timestamp) {
emit UpdateIgnored(s_feedState.latestStatus, s_feedState.startedAt, status, timestamp);
if (feedState.startedAt > timestamp) {
emit UpdateIgnored(feedState.latestStatus, feedState.startedAt, status, timestamp);
return;
}

if (s_feedState.latestStatus == status) {
_updateRound(s_feedState.latestRoundId, status);
if (feedState.latestStatus == status) {
_updateRound(feedState.latestRoundId, status);
} else {
s_feedState.latestRoundId += 1;
_recordRound(s_feedState.latestRoundId, status, timestamp);
feedState.latestRoundId += 1;
_recordRound(feedState.latestRoundId, status, timestamp);
}
}

Expand Down Expand Up @@ -216,12 +218,14 @@ contract ScrollSequencerUptimeFeed is
checkAccess
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
{
FeedState memory feedState = s_feedState;

return (
s_feedState.latestRoundId,
_getStatusAnswer(s_feedState.latestStatus),
s_feedState.startedAt,
s_feedState.updatedAt,
s_feedState.latestRoundId
feedState.latestRoundId,
_getStatusAnswer(feedState.latestStatus),
feedState.startedAt,
feedState.updatedAt,
feedState.latestRoundId
);
}
}
4 changes: 2 additions & 2 deletions contracts/test/v0.8/dev/ScrollSequencerUptimeFeed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('ScrollSequencerUptimeFeed', () => {
// Assert no update
expect(await scrollUptimeFeed.latestAnswer()).to.equal(0)
expect(noUpdateTx.cumulativeGasUsed.toNumber()).to.be.closeTo(
38694,
38594,
gasUsedDeviation,
)

Expand All @@ -317,7 +317,7 @@ describe('ScrollSequencerUptimeFeed', () => {
// Assert update
expect(await scrollUptimeFeed.latestAnswer()).to.equal(1)
expect(updateTx.cumulativeGasUsed.toNumber()).to.be.closeTo(
58995,
58458,
gasUsedDeviation,
)
})
Expand Down

0 comments on commit ebbb791

Please sign in to comment.