Skip to content

Commit

Permalink
refactor: simplify registry
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Aug 13, 2024
1 parent 7168290 commit d09bc02
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 80 deletions.
7 changes: 7 additions & 0 deletions l1-contracts/src/core/interfaces/IRollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// Copyright 2023 Aztec Labs.
pragma solidity >=0.8.18;

import {IInbox} from "../interfaces/messagebridge/IInbox.sol";
import {IOutbox} from "../interfaces/messagebridge/IOutbox.sol";

import {SignatureLib} from "../sequencer_selection/SignatureLib.sol";

interface ITestRollup {
Expand All @@ -15,6 +18,10 @@ interface IRollup {
event L2ProofVerified(uint256 indexed blockNumber, bytes32 indexed proverId);
event ProgressedState(uint256 provenBlockCount, uint256 pendingBlockCount);

function INBOX() external view returns (IInbox);

function OUTBOX() external view returns (IOutbox);

function publishAndProcess(
bytes calldata _header,
bytes32 _archive,
Expand Down
12 changes: 1 addition & 11 deletions l1-contracts/src/core/interfaces/messagebridge/IRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ pragma solidity >=0.8.18;

import {DataStructures} from "../../libraries/DataStructures.sol";
import {IRollup} from "../IRollup.sol";
import {IInbox} from "./IInbox.sol";
import {IOutbox} from "./IOutbox.sol";

interface IRegistry {
// docs:start:registry_upgrade
function upgrade(address _rollup, address _inbox, address _outbox) external returns (uint256);
function upgrade(address _rollup) external returns (uint256);
// docs:end:registry_upgrade

// docs:start:registry_get_rollup
Expand All @@ -19,14 +17,6 @@ interface IRegistry {
function getVersionFor(address _rollup) external view returns (uint256);
// docs:end:registry_get_version_for

// docs:start:registry_get_inbox
function getInbox() external view returns (IInbox);
// docs:end:registry_get_inbox

// docs:start:registry_get_outbox
function getOutbox() external view returns (IOutbox);
// docs:end:registry_get_outbox

// docs:start:registry_get_snapshot
function getSnapshot(uint256 _version)
external
Expand Down
4 changes: 0 additions & 4 deletions l1-contracts/src/core/libraries/DataStructures.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,10 @@ library DataStructures {
/**
* @notice Struct for storing address of cross communication components and the block number when it was updated
* @param rollup - The address of the rollup contract
* @param inbox - The address of the inbox contract
* @param outbox - The address of the outbox contract
* @param blockNumber - The block number of the snapshot
*/
struct RegistrySnapshot {
address rollup;
address inbox;
address outbox;
uint256 blockNumber;
}
// docs:end:registry_snapshot
Expand Down
37 changes: 5 additions & 32 deletions l1-contracts/src/core/messagebridge/Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {Ownable} from "@oz/access/Ownable.sol";
// Interfaces
import {IRegistry} from "../interfaces/messagebridge/IRegistry.sol";
import {IRollup} from "../interfaces/IRollup.sol";
import {IInbox} from "../interfaces/messagebridge/IInbox.sol";
import {IOutbox} from "../interfaces/messagebridge/IOutbox.sol";

// Libraries
import {DataStructures} from "../libraries/DataStructures.sol";
Expand All @@ -17,9 +15,7 @@ import {Errors} from "../libraries/Errors.sol";
/**
* @title Registry
* @author Aztec Labs
* @notice Keeps track of addresses of rollup, inbox and outbox as well as historical addresses.
* Used as the source of truth for finding the "head" of the rollup chain. Very important information
* for L1<->L2 communication.
* @notice Keeps track of addresses of current rollup and historical addresses.
*/
contract Registry is IRegistry, Ownable {
uint256 public override(IRegistry) numberOfVersions;
Expand All @@ -29,9 +25,9 @@ contract Registry is IRegistry, Ownable {
mapping(address rollup => uint256 version) internal rollupToVersion;

constructor() Ownable(msg.sender) {
// Inserts a "dead" rollup and message boxes at version 0
// Inserts a "dead" rollup at version 0
// This is simply done to make first version 1, which fits better with the rest of the system
upgrade(address(0xdead), address(0xdead), address(0xdead));
upgrade(address(0xdead));
}

/**
Expand All @@ -53,22 +49,6 @@ contract Registry is IRegistry, Ownable {
return version;
}

/**
* @notice Returns the inbox contract
* @return The inbox contract (of type IInbox)
*/
function getInbox() external view override(IRegistry) returns (IInbox) {
return IInbox(currentSnapshot.inbox);
}

/**
* @notice Returns the outbox contract
* @return The outbox contract (of type IOutbox)
*/
function getOutbox() external view override(IRegistry) returns (IOutbox) {
return IOutbox(currentSnapshot.outbox);
}

/**
* @notice Fetches a snapshot of the registry indicated by `version`
* @dev the version is 0 indexed, so the first snapshot is version 0.
Expand Down Expand Up @@ -104,21 +84,14 @@ contract Registry is IRegistry, Ownable {
* @dev Reverts if the rollup is already registered
*
* @param _rollup - The address of the rollup contract
* @param _inbox - The address of the inbox contract
* @param _outbox - The address of the outbox contract
* @return The version of the new snapshot
*/
function upgrade(address _rollup, address _inbox, address _outbox)
public
override(IRegistry)
onlyOwner
returns (uint256)
{
function upgrade(address _rollup) public override(IRegistry) onlyOwner returns (uint256) {
(, bool exists) = _getVersionFor(_rollup);
if (exists) revert Errors.Registry__RollupAlreadyRegistered(_rollup);

DataStructures.RegistrySnapshot memory newSnapshot =
DataStructures.RegistrySnapshot(_rollup, _inbox, _outbox, block.number);
DataStructures.RegistrySnapshot(_rollup, block.number);
currentSnapshot = newSnapshot;
uint256 version = numberOfVersions++;
snapshots[version] = newSnapshot;
Expand Down
18 changes: 4 additions & 14 deletions l1-contracts/test/Registry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,30 @@ contract RegistryTest is Test {
assertEq(registry.numberOfVersions(), 1, "should have 1 version");
DataStructures.RegistrySnapshot memory snapshot = registry.getCurrentSnapshot();
assertEq(snapshot.rollup, DEAD, "should have dead rollup");
assertEq(snapshot.inbox, DEAD, "should have dead inbox");
assertEq(snapshot.outbox, DEAD, "should have dead outbox");
assertEq(address(registry.getRollup()), DEAD);
assertEq(address(registry.getInbox()), DEAD);
assertEq(address(registry.getOutbox()), DEAD);

vm.expectRevert(abi.encodeWithSelector(Errors.Registry__RollupNotRegistered.selector, DEAD));
registry.getVersionFor(DEAD);
}

function testUpgrade() public {
address newRollup = address(0xbeef1);
address inbox = address(0xbeef2);
address newOutbox = address(0xbeef3);
uint256 newVersion = registry.upgrade(newRollup, inbox, newOutbox);
uint256 newVersion = registry.upgrade(newRollup);

assertEq(registry.numberOfVersions(), 2, "should have 2 versions");
DataStructures.RegistrySnapshot memory snapshot = registry.getCurrentSnapshot();
assertEq(snapshot.rollup, newRollup, "should have newRollup");
assertEq(snapshot.inbox, inbox, "should have inbox");
assertEq(snapshot.outbox, newOutbox, "should have newOutbox");

assertEq(address(registry.getRollup()), newRollup);
assertEq(address(registry.getInbox()), inbox);
assertEq(address(registry.getOutbox()), newOutbox);
assertEq(
registry.getVersionFor(newRollup), newVersion, "should have version newVersion for newRollup"
);
}

function testRevertUpgradeToSame() public {
registry.upgrade(DEAD, DEAD, DEAD);
registry.upgrade(DEAD);
vm.expectRevert(abi.encodeWithSelector(Errors.Registry__RollupAlreadyRegistered.selector, DEAD));
registry.upgrade(DEAD, DEAD, DEAD);
registry.upgrade(DEAD);
}

function testRevertGetVersionForNonExistent() public {
Expand All @@ -68,6 +58,6 @@ contract RegistryTest is Test {
vm.assume(_owner != registry.owner());
vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, _owner));
vm.prank(_owner);
registry.upgrade(DEAD, DEAD, DEAD);
registry.upgrade(DEAD);
}
}
2 changes: 1 addition & 1 deletion l1-contracts/test/Rollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ contract RollupTest is DecoderBase {
inbox = Inbox(address(rollup.INBOX()));
outbox = Outbox(address(rollup.OUTBOX()));

registry.upgrade(address(rollup), address(inbox), address(outbox));
registry.upgrade(address(rollup));

// mint some tokens to the rollup
portalERC20.mint(address(rollup), 1000000);
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/portals/FeeJuicePortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract FeeJuicePortal {
returns (bytes32)
{
// Preamble
IInbox inbox = registry.getInbox();
IInbox inbox = registry.getRollup().INBOX();
DataStructures.L2Actor memory actor = DataStructures.L2Actor(l2TokenAddress, 1);

// Hash the message content to be reconstructed in the receiving contract
Expand Down
6 changes: 3 additions & 3 deletions l1-contracts/test/portals/TokenPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract TokenPortal {
returns (bytes32)
{
// Preamble
IInbox inbox = registry.getInbox();
IInbox inbox = registry.getRollup().INBOX();
DataStructures.L2Actor memory actor = DataStructures.L2Actor(l2Bridge, 1);

// Hash the message content to be reconstructed in the receiving contract
Expand Down Expand Up @@ -69,7 +69,7 @@ contract TokenPortal {
bytes32 _secretHashForL2MessageConsumption
) external returns (bytes32) {
// Preamble
IInbox inbox = registry.getInbox();
IInbox inbox = registry.getRollup().INBOX();
DataStructures.L2Actor memory actor = DataStructures.L2Actor(l2Bridge, 1);

// Hash the message content to be reconstructed in the receiving contract
Expand Down Expand Up @@ -120,7 +120,7 @@ contract TokenPortal {
)
});

IOutbox outbox = registry.getOutbox();
IOutbox outbox = registry.getRollup().OUTBOX();

outbox.consume(message, _l2BlockNumber, _leafIndex, _path);

Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/portals/TokenPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract TokenPortalTest is Test {
inbox = rollup.INBOX();
outbox = rollup.OUTBOX();

registry.upgrade(address(rollup), address(inbox), address(outbox));
registry.upgrade(address(rollup));

portalERC20.mint(address(rollup), 1000000);
tokenPortal = new TokenPortal();
Expand Down
4 changes: 2 additions & 2 deletions l1-contracts/test/portals/UniswapPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ contract UniswapPortal {

// Consume the message from the outbox
{
IOutbox outbox = registry.getOutbox();
IOutbox outbox = registry.getRollup().OUTBOX();

outbox.consume(
DataStructures.L2ToL1Msg({
Expand Down Expand Up @@ -209,7 +209,7 @@ contract UniswapPortal {

// Consume the message from the outbox
{
IOutbox outbox = registry.getOutbox();
IOutbox outbox = registry.getRollup().OUTBOX();

outbox.consume(
DataStructures.L2ToL1Msg({
Expand Down
10 changes: 5 additions & 5 deletions l1-contracts/test/portals/UniswapPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ contract UniswapPortalTest is Test {

Rollup internal rollup;
Registry internal registry;

IOutbox internal outbox;
bytes32 internal l2TokenAddress = bytes32(uint256(0x1));
bytes32 internal l2UniswapAddress = bytes32(uint256(0x2));

Expand All @@ -54,7 +56,7 @@ contract UniswapPortalTest is Test {
PortalERC20 portalERC20 = new PortalERC20();
rollup =
new Rollup(registry, new AvailabilityOracle(), IERC20(address(portalERC20)), bytes32(0));
registry.upgrade(address(rollup), address(rollup.INBOX()), address(rollup.OUTBOX()));
registry.upgrade(address(rollup));
portalERC20.mint(address(rollup), 1000000);

daiTokenPortal = new TokenPortal();
Expand All @@ -68,6 +70,8 @@ contract UniswapPortalTest is Test {

// have DAI locked in portal that can be moved when funds are withdrawn
deal(address(DAI), address(daiTokenPortal), amount);

outbox = rollup.OUTBOX();
}

/**
Expand Down Expand Up @@ -169,8 +173,6 @@ contract UniswapPortalTest is Test {
(bytes32[] memory withdrawSiblingPath,) = tree.computeSiblingPath(0);
(bytes32[] memory swapSiblingPath,) = tree.computeSiblingPath(1);

IOutbox outbox = registry.getOutbox();

vm.prank(address(rollup));
outbox.insert(_l2BlockNumber, treeRoot, treeHeight);

Expand Down Expand Up @@ -392,7 +394,6 @@ contract UniswapPortalTest is Test {
// there should be some weth in the weth portal
assertGt(WETH9.balanceOf(address(wethTokenPortal)), 0);
// there the message should be nullified at index 0 and 1
IOutbox outbox = registry.getOutbox();
assertTrue(outbox.hasMessageBeenConsumedAtBlockAndIndex(l2BlockNumber, 0));
assertTrue(outbox.hasMessageBeenConsumedAtBlockAndIndex(l2BlockNumber, 1));
}
Expand Down Expand Up @@ -440,7 +441,6 @@ contract UniswapPortalTest is Test {
// there should be some weth in the weth portal
assertGt(WETH9.balanceOf(address(wethTokenPortal)), 0);
// there should be no message in the outbox:
IOutbox outbox = registry.getOutbox();
assertTrue(outbox.hasMessageBeenConsumedAtBlockAndIndex(l2BlockNumber, 0));
assertTrue(outbox.hasMessageBeenConsumedAtBlockAndIndex(l2BlockNumber, 1));
}
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/sparta/DevNet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract DevNetTest is DecoderBase {
inbox = Inbox(address(rollup.INBOX()));
outbox = Outbox(address(rollup.OUTBOX()));

registry.upgrade(address(rollup), address(inbox), address(outbox));
registry.upgrade(address(rollup));

// mint some tokens to the rollup
portalERC20.mint(address(rollup), 1000000);
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/sparta/Sparta.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract SpartaTest is DecoderBase {
inbox = Inbox(address(rollup.INBOX()));
outbox = Outbox(address(rollup.OUTBOX()));

registry.upgrade(address(rollup), address(inbox), address(outbox));
registry.upgrade(address(rollup));

// mint some tokens to the rollup
portalERC20.mint(address(rollup), 1000000);
Expand Down
5 changes: 1 addition & 4 deletions yarn-project/ethereum/src/deploy_l1_contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ export const deployL1Contracts = async (
abi: contractsToDeploy.registry.contractAbi,
client: walletClient,
});
await registryContract.write.upgrade(
[getAddress(rollupAddress.toString()), getAddress(inboxAddress.toString()), getAddress(outboxAddress.toString())],
{ account },
);
await registryContract.write.upgrade([getAddress(rollupAddress.toString())], { account });

// this contract remains uninitialized because at this point we don't know the address of the Fee Juice on L2
const feeJuicePortalAddress = await deployL1Contract(
Expand Down

0 comments on commit d09bc02

Please sign in to comment.