Skip to content

Commit

Permalink
Separated maxTimeVariation into components
Browse files Browse the repository at this point in the history
  • Loading branch information
yahgwai committed Nov 17, 2023
1 parent 3646a78 commit 59b6dcf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 57 deletions.
10 changes: 9 additions & 1 deletion src/bridge/ISequencerInbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ interface ISequencerInbox is IDelayedMessageProvider {
}

/// @notice Returns the max time variation settings for this sequencer inbox
function maxTimeVariation() external view returns (ISequencerInbox.MaxTimeVariation memory);
function maxTimeVariation()
external
view
returns (
uint256 delayBlocks,
uint256 futureBlocks,
uint256 delaySeconds,
uint256 futureSeconds
);

function dasKeySetInfo(bytes32) external view returns (bool, uint64);

Expand Down
54 changes: 27 additions & 27 deletions src/bridge/SequencerInbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,35 +111,37 @@ contract SequencerInbox is GasRefundEnabled, ISequencerInbox {

function getTimeBounds() internal view virtual returns (IBridge.TimeBounds memory) {
IBridge.TimeBounds memory bounds;
ISequencerInbox.MaxTimeVariation memory maxTimeVariation_ = maxTimeVariation();
if (block.timestamp > maxTimeVariation_.delaySeconds) {
bounds.minTimestamp = uint64(block.timestamp - maxTimeVariation_.delaySeconds);
(
uint256 delayBlocks_,
uint256 futureBlocks_,
uint256 delaySeconds_,
uint256 futureSeconds_
) = maxTimeVariation();
if (block.timestamp > delaySeconds_) {
bounds.minTimestamp = uint64(block.timestamp - delaySeconds_);
}
bounds.maxTimestamp = uint64(block.timestamp + maxTimeVariation_.futureSeconds);
if (block.number > maxTimeVariation_.delayBlocks) {
bounds.minBlockNumber = uint64(block.number - maxTimeVariation_.delayBlocks);
bounds.maxTimestamp = uint64(block.timestamp + futureSeconds_);
if (block.number > delayBlocks_) {
bounds.minBlockNumber = uint64(block.number - delayBlocks_);
}
bounds.maxBlockNumber = uint64(block.number + maxTimeVariation_.futureBlocks);
bounds.maxBlockNumber = uint64(block.number + futureBlocks_);
return bounds;
}

function maxTimeVariation() public view returns (ISequencerInbox.MaxTimeVariation memory) {
function maxTimeVariation()
public
view
returns (
uint256,
uint256,
uint256,
uint256
)
{
if (_chainIdChanged()) {
return
ISequencerInbox.MaxTimeVariation({
delayBlocks: 1,
futureBlocks: 1,
delaySeconds: 1,
futureSeconds: 1
});
return (1, 1, 1, 1);
} else {
return
ISequencerInbox.MaxTimeVariation({
delayBlocks: delayBlocks,
futureBlocks: futureBlocks,
delaySeconds: delaySeconds,
futureSeconds: futureSeconds
});
return (delayBlocks, futureBlocks, delaySeconds, futureSeconds);
}
}

Expand All @@ -162,12 +164,10 @@ contract SequencerInbox is GasRefundEnabled, ISequencerInbox {
baseFeeL1,
messageDataHash
);
ISequencerInbox.MaxTimeVariation memory maxTimeVariation_ = maxTimeVariation();
(uint256 delayBlocks_, , uint256 delaySeconds_, ) = maxTimeVariation();
// Can only force-include after the Sequencer-only window has expired.
if (l1BlockAndTime[0] + maxTimeVariation_.delayBlocks >= block.number)
revert ForceIncludeBlockTooSoon();
if (l1BlockAndTime[1] + maxTimeVariation_.delaySeconds >= block.timestamp)
revert ForceIncludeTimeTooSoon();
if (l1BlockAndTime[0] + delayBlocks_ >= block.number) revert ForceIncludeBlockTooSoon();
if (l1BlockAndTime[1] + delaySeconds_ >= block.timestamp) revert ForceIncludeTimeTooSoon();

// Verify that message hash represents the last message sequence of delayed message to be included
bytes32 prevDelayedAcc = 0;
Expand Down
14 changes: 5 additions & 9 deletions src/rollup/BridgeCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ contract BridgeCreator is Ownable {
emit ERC20TemplatesUpdated();
}

function _createBridge(
address adminProxy,
BridgeTemplates storage templates,
ISequencerInbox.MaxTimeVariation calldata maxTimeVariation,
uint256 maxDataSize
) internal returns (BridgeContracts memory) {
function _createBridge(address adminProxy, BridgeTemplates storage templates)
internal
returns (BridgeContracts memory)
{
BridgeContracts memory frame;
frame.bridge = IBridge(
address(new TransparentUpgradeableProxy(address(templates.bridge), adminProxy, ""))
Expand Down Expand Up @@ -92,9 +90,7 @@ contract BridgeCreator is Ownable {
// create ETH-based bridge if address zero is provided for native token, otherwise create ERC20-based bridge
BridgeContracts memory frame = _createBridge(
adminProxy,
nativeToken == address(0) ? ethBasedTemplates : erc20BasedTemplates,
maxTimeVariation,
maxDataSize
nativeToken == address(0) ? ethBasedTemplates : erc20BasedTemplates
);

// init contracts
Expand Down
20 changes: 10 additions & 10 deletions test/contract/sequencerInboxForceInclude.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ describe('SequencerInboxForceInclude', async () => {
BigNumber.from(10),
'0x1010'
)
const maxTimeVariation = await sequencerInbox.maxTimeVariation()
const [delayBlocks] = await sequencerInbox.maxTimeVariation()

await mineBlocks(maxTimeVariation.delayBlocks.toNumber())
await mineBlocks(delayBlocks.toNumber())

await forceIncludeMessages(
sequencerInbox,
Expand Down Expand Up @@ -399,8 +399,8 @@ describe('SequencerInboxForceInclude', async () => {
'0xdeadface'
)

const maxTimeVariation = await sequencerInbox.maxTimeVariation()
await mineBlocks(maxTimeVariation.delayBlocks.toNumber())
const [delayBlocks] = await sequencerInbox.maxTimeVariation()
await mineBlocks(delayBlocks.toNumber())

await forceIncludeMessages(
sequencerInbox,
Expand Down Expand Up @@ -464,8 +464,8 @@ describe('SequencerInboxForceInclude', async () => {
'0x10101010'
)

const maxTimeVariation = await sequencerInbox.maxTimeVariation()
await mineBlocks(maxTimeVariation.delayBlocks.toNumber())
const [delayBlocks] = await sequencerInbox.maxTimeVariation()
await mineBlocks(delayBlocks.toNumber())

await forceIncludeMessages(
sequencerInbox,
Expand Down Expand Up @@ -495,8 +495,8 @@ describe('SequencerInboxForceInclude', async () => {
'0x1010'
)

const maxTimeVariation = await sequencerInbox.maxTimeVariation()
await mineBlocks(maxTimeVariation.delayBlocks.toNumber() - 1, 5)
const [delayBlocks] = await sequencerInbox.maxTimeVariation()
await mineBlocks(delayBlocks.toNumber() - 1, 5)

await forceIncludeMessages(
sequencerInbox,
Expand Down Expand Up @@ -527,10 +527,10 @@ describe('SequencerInboxForceInclude', async () => {
'0x1010'
)

const maxTimeVariation = await sequencerInbox.maxTimeVariation()
const [delayBlocks] = await sequencerInbox.maxTimeVariation()
// mine a lot of blocks - but use a short time per block
// this should mean enough blocks have passed, but not enough time
await mineBlocks(maxTimeVariation.delayBlocks.toNumber() + 1, 5)
await mineBlocks(delayBlocks.toNumber() + 1, 5)

await forceIncludeMessages(
sequencerInbox,
Expand Down
30 changes: 20 additions & 10 deletions test/foundry/BridgeCreator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ contract BridgeCreatorTest is Test {
// seqInbox
assertEq(address(seqInbox.bridge()), address(bridge), "Invalid bridge ref");
assertEq(address(seqInbox.rollup()), rollup, "Invalid seq rollup ref");
ISequencerInbox.MaxTimeVariation memory maxTimeVariation = seqInbox.maxTimeVariation();
assertEq(maxTimeVariation.delayBlocks, timeVars.delayBlocks, "Invalid delayBlocks");
assertEq(maxTimeVariation.futureBlocks, timeVars.futureBlocks, "Invalid futureBlocks");
assertEq(maxTimeVariation.delaySeconds, timeVars.delaySeconds, "Invalid delaySeconds");
assertEq(maxTimeVariation.futureSeconds, timeVars.futureSeconds, "Invalid futureSeconds");
(
uint256 delayBlocks,
uint256 futureBlocks,
uint256 delaySeconds,
uint256 futureSeconds
) = seqInbox.maxTimeVariation();
assertEq(delayBlocks, timeVars.delayBlocks, "Invalid delayBlocks");
assertEq(futureBlocks, timeVars.futureBlocks, "Invalid futureBlocks");
assertEq(delaySeconds, timeVars.delaySeconds, "Invalid delaySeconds");
assertEq(futureSeconds, timeVars.futureSeconds, "Invalid futureSeconds");

// inbox
assertEq(address(inbox.bridge()), address(bridge), "Invalid bridge ref");
Expand Down Expand Up @@ -219,11 +224,16 @@ contract BridgeCreatorTest is Test {
// seqInbox
assertEq(address(seqInbox.bridge()), address(bridge), "Invalid bridge ref");
assertEq(address(seqInbox.rollup()), rollup, "Invalid seq inbox rollup ref");
ISequencerInbox.MaxTimeVariation memory maxTimeVariation = seqInbox.maxTimeVariation();
assertEq(maxTimeVariation.delayBlocks, timeVars.delayBlocks, "Invalid delayBlocks");
assertEq(maxTimeVariation.futureBlocks, timeVars.futureBlocks, "Invalid futureBlocks");
assertEq(maxTimeVariation.delaySeconds, timeVars.delaySeconds, "Invalid delaySeconds");
assertEq(maxTimeVariation.futureSeconds, timeVars.futureSeconds, "Invalid futureSeconds");
(
uint256 delayBlocks,
uint256 futureBlocks,
uint256 delaySeconds,
uint256 futureSeconds
) = seqInbox.maxTimeVariation();
assertEq(delayBlocks, timeVars.delayBlocks, "Invalid delayBlocks");
assertEq(futureBlocks, timeVars.futureBlocks, "Invalid futureBlocks");
assertEq(delaySeconds, timeVars.delaySeconds, "Invalid delaySeconds");
assertEq(futureSeconds, timeVars.futureSeconds, "Invalid futureSeconds");

// inbox
assertEq(address(inbox.bridge()), address(bridge), "Invalid bridge ref");
Expand Down

0 comments on commit 59b6dcf

Please sign in to comment.