Skip to content

Commit

Permalink
feat: sequencer inbox optimization
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Buckland <[email protected]>
  • Loading branch information
gzeoneth and yahgwai committed Jan 17, 2024
1 parent b716da9 commit cffafc5
Show file tree
Hide file tree
Showing 26 changed files with 540 additions and 581 deletions.
2 changes: 1 addition & 1 deletion deploy/BridgeStubCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = async hre => {
const { deploy } = deployments
const { deployer } = await getNamedAccounts()

await deploy('BridgeStub', { from: deployer, args: [] })
await deploy('BridgeStub', { from: deployer, args: [deployer] })
}

module.exports.tags = ['BridgeStub', 'test']
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"test:e2e": "hardhat test test/e2e/*.ts",
"postinstall": "patch-package",
"deploy-factory": "hardhat run scripts/deployment.ts",
"deploy-rollup": "hardhat run scripts/rollupCreation.ts",
"deploy-eth-rollup": "hardhat run scripts/createEthRollup.ts",
"deploy-erc20-rollup": "hardhat run scripts/createERC20Rollup.ts"
},
Expand Down
8 changes: 4 additions & 4 deletions scripts/config.ts.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const config = {
'{"chainId":13331370,"homesteadBlock":0,"daoForkBlock":null,"daoForkSupport":true,"eip150Block":0,"eip150Hash":"0x0000000000000000000000000000000000000000000000000000000000000000","eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"muirGlacierBlock":0,"berlinBlock":0,"londonBlock":0,"clique":{"period":0,"epoch":0},"arbitrum":{"EnableArbOS":true,"AllowDebugPrecompiles":false,"DataAvailabilityCommittee":false,"InitialArbOSVersion":10,"InitialChainOwner":"0x1234123412341234123412341234123412341234","GenesisBlockNum":0}}',
genesisBlockNum: ethers.BigNumber.from('0'),
sequencerInboxMaxTimeVariation: {
delayBlocks: ethers.BigNumber.from('5760'),
futureBlocks: ethers.BigNumber.from('12'),
delaySeconds: ethers.BigNumber.from('86400'),
futureSeconds: ethers.BigNumber.from('3600'),
delayBlocks: 5760,
futureBlocks: 12,
delaySeconds: 86400,
futureSeconds: 3600,
},
},
validators: [
Expand Down
34 changes: 31 additions & 3 deletions src/bridge/AbsBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import {
NotSequencerInbox,
NotOutbox,
InvalidOutboxSet,
BadSequencerMessageNumber
BadSequencerMessageNumber,
EmptyDelayedMessagesRead,
DelayedBackwards,
DelayedTooFar
} from "../libraries/Error.sol";
import "./IBridge.sol";
import "./Messages.sol";
import "../libraries/DelegateCallAware.sol";
import "./ISequencerInbox.sol";

import {L1MessageType_batchPostingReport} from "../libraries/MessageTypes.sol";

Expand Down Expand Up @@ -55,8 +59,15 @@ abstract contract AbsBridge is Initializable, DelegateCallAware, IBridge {

uint256 public override sequencerReportedSubMessageCount;

uint256 public totalDelayedMessagesRead;

address internal constant EMPTY_ACTIVEOUTBOX = address(type(uint160).max);

function postUpgradeInit() external onlyDelegated onlyProxyOwner {
totalDelayedMessagesRead = ISequencerInbox(sequencerInbox).totalDelayedMessagesRead();
if (totalDelayedMessagesRead == 0) revert EmptyDelayedMessagesRead();
}

modifier onlyRollupOrOwner() {
if (msg.sender != address(rollup)) {
address rollupOwner = rollup.owner();
Expand Down Expand Up @@ -101,7 +112,9 @@ abstract contract AbsBridge is Initializable, DelegateCallAware, IBridge {
bytes32 dataHash,
uint256 afterDelayedMessagesRead,
uint256 prevMessageCount,
uint256 newMessageCount
uint256 newMessageCount,
TimeBounds memory timeBounds,
BatchDataLocation batchDataLocation
)
external
onlySequencerInbox
Expand All @@ -119,6 +132,9 @@ abstract contract AbsBridge is Initializable, DelegateCallAware, IBridge {
) {
revert BadSequencerMessageNumber(sequencerReportedSubMessageCount, prevMessageCount);
}
if (afterDelayedMessagesRead > delayedInboxAccs.length) revert DelayedTooFar();
if (afterDelayedMessagesRead < totalDelayedMessagesRead) revert DelayedBackwards();

sequencerReportedSubMessageCount = newMessageCount;
seqMessageIndex = sequencerInboxAccs.length;
if (sequencerInboxAccs.length > 0) {
Expand All @@ -129,6 +145,18 @@ abstract contract AbsBridge is Initializable, DelegateCallAware, IBridge {
}
acc = keccak256(abi.encodePacked(beforeAcc, dataHash, delayedAcc));
sequencerInboxAccs.push(acc);
totalDelayedMessagesRead = afterDelayedMessagesRead;

emit SequencerBatchDelivered(
seqMessageIndex,
beforeAcc,
acc,
delayedAcc,
afterDelayedMessagesRead,
timeBounds,
batchDataLocation,
msg.sender
);
}

/// @inheritdoc IBridge
Expand Down Expand Up @@ -304,5 +332,5 @@ abstract contract AbsBridge is Initializable, DelegateCallAware, IBridge {
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[40] private __gap;
uint256[39] private __gap;
}
1 change: 1 addition & 0 deletions src/bridge/AbsOutbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ abstract contract AbsOutbox is DelegateCallAware, IOutbox {
});
bridge = _bridge;
rollup = address(_bridge.rollup());
if (address(rollup) == address(0)) revert RollupNotChanged();
}

function postUpgradeInit() external onlyDelegated onlyProxyOwner {
Expand Down
18 changes: 17 additions & 1 deletion src/bridge/IBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// solhint-disable-next-line compiler-version
pragma solidity >=0.6.9 <0.9.0;
pragma experimental ABIEncoderV2;

import "./IOwnable.sol";

Expand Down Expand Up @@ -48,6 +49,17 @@ interface IBridge {
bytes data
);

event SequencerBatchDelivered(
uint256 indexed batchSequenceNumber,
bytes32 indexed beforeAcc,
bytes32 indexed afterAcc,
bytes32 delayedAcc,
uint256 afterDelayedMessagesRead,
TimeBounds timeBounds,
BatchDataLocation dataLocation,
address sequencerInbox
);

event InboxToggle(address indexed inbox, bool enabled);

event OutboxToggle(address indexed outbox, bool enabled);
Expand Down Expand Up @@ -88,13 +100,17 @@ interface IBridge {

function sequencerMessageCount() external view returns (uint256);

function totalDelayedMessagesRead() external view returns (uint256);

// ---------- onlySequencerInbox functions ----------

function enqueueSequencerMessage(
bytes32 dataHash,
uint256 afterDelayedMessagesRead,
uint256 prevMessageCount,
uint256 newMessageCount
uint256 newMessageCount,
TimeBounds memory timeBounds,
BatchDataLocation dataLocation
)
external
returns (
Expand Down
35 changes: 11 additions & 24 deletions src/bridge/ISequencerInbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,18 @@ import "./IDelayedMessageProvider.sol";
import "./IBridge.sol";

interface ISequencerInbox is IDelayedMessageProvider {
/// @notice The maximum amount of time variatin between a message being posted on the L1 and being executed on the L2
/// @param delayBlocks The max amount of blocks in the past that a message can be received on L2
/// @param futureBlocks The max amount of blocks in the future that a message can be received on L2
/// @param delaySeconds The max amount of seconds in the past that a message can be received on L2
/// @param futureSeconds The max amount of seconds in the future that a message can be received on L2
struct MaxTimeVariation {
uint64 delayBlocks;
uint64 futureBlocks;
uint64 delaySeconds;
uint64 futureSeconds;
}

event SequencerBatchDelivered(
uint256 indexed batchSequenceNumber,
bytes32 indexed beforeAcc,
bytes32 indexed afterAcc,
bytes32 delayedAcc,
uint256 afterDelayedMessagesRead,
IBridge.TimeBounds timeBounds,
IBridge.BatchDataLocation dataLocation
);

event OwnerFunctionCalled(uint256 indexed id);

/// @dev a separate event that emits batch data when this isn't easily accessible in the tx.input
Expand All @@ -39,6 +34,8 @@ interface ISequencerInbox is IDelayedMessageProvider {
/// @dev a keyset was invalidated
event InvalidateKeyset(bytes32 indexed keysetHash);

/// @notice The total number of delated messages read in the bridge
/// @dev We surface this here for backwards compatibility
function totalDelayedMessagesRead() external view returns (uint256);

function bridge() external view returns (IBridge);
Expand Down Expand Up @@ -103,9 +100,6 @@ interface ISequencerInbox is IDelayedMessageProvider {

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

/// @notice Remove force inclusion delay after a L1 chainId fork
function removeDelayAfterFork() external;

/// @notice Force messages from the delayed inbox to be included in the chain
/// Callable by any address, but message can only be force-included after maxTimeVariation.delayBlocks and
/// maxTimeVariation.delaySeconds has elapsed. As part of normal behaviour the sequencer will include these
Expand Down Expand Up @@ -140,7 +134,9 @@ interface ISequencerInbox is IDelayedMessageProvider {
uint256 sequenceNumber,
bytes calldata data,
uint256 afterDelayedMessagesRead,
IGasRefunder gasRefunder
IGasRefunder gasRefunder,
uint256 prevMessageCount,
uint256 newMessageCount
) external;

function addSequencerL2Batch(
Expand All @@ -154,12 +150,6 @@ interface ISequencerInbox is IDelayedMessageProvider {

// ---------- onlyRollupOrOwner functions ----------

/**
* @notice Set max delay for sequencer inbox
* @param maxTimeVariation_ the maximum time variation parameters
*/
function setMaxTimeVariation(MaxTimeVariation memory maxTimeVariation_) external;

/**
* @notice Updates whether an address is authorized to be a batch poster at the sequencer inbox
* @param addr the address
Expand Down Expand Up @@ -187,10 +177,7 @@ interface ISequencerInbox is IDelayedMessageProvider {
*/
function setIsSequencer(address addr, bool isSequencer_) external;

// ---------- initializer ----------

function initialize(IBridge bridge_, MaxTimeVariation calldata maxTimeVariation_) external;

/// @notice Allows the rollup owner to sync the rollup address
function updateRollupAddress() external;
}

Expand Down
Loading

0 comments on commit cffafc5

Please sign in to comment.