Skip to content

Commit

Permalink
Better globalExitRoot initialization (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
josojo authored Dec 30, 2023
1 parent 98773c5 commit a090458
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 17 deletions.
4 changes: 2 additions & 2 deletions contracts/PolygonZkEVMGlobalExitRootWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ contract PolygonZkEVMGlobalExitRootWrapper is PolygonZkEVMGlobalExitRoot {
* @param _rollupAddress Rollup contract address
* @param _bridgeAddress PolygonZkEVMBridge contract address
*/
function initialize(address _rollupAddress, address _bridgeAddress) public override initializer {
PolygonZkEVMGlobalExitRoot.initialize(_rollupAddress, _bridgeAddress);
function initialize(address _rollupAddress, address _bridgeAddress, bytes32 _lastMainnetExitRoot, bytes32 _lastRollupExitRoot) public override initializer {
PolygonZkEVMGlobalExitRoot.initialize(_rollupAddress, _bridgeAddress, _lastMainnetExitRoot, _lastRollupExitRoot);
}
}

16 changes: 15 additions & 1 deletion contracts/inheritedMainContracts/PolygonZkEVMGlobalExitRoot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ contract PolygonZkEVMGlobalExitRoot is IPolygonZkEVMGlobalExitRoot, Initializabl
* @param _rollupAddress Rollup contract address
* @param _bridgeAddress PolygonZkEVMBridge contract address
*/
function initialize(address _rollupAddress, address _bridgeAddress) public virtual onlyInitializing {
function initialize(address _rollupAddress, address _bridgeAddress, bytes32 _lastMainnetExitRoot, bytes32 _lastRollupExitRoot) public virtual onlyInitializing {
rollupAddress = _rollupAddress;
bridgeAddress = _bridgeAddress;
lastMainnetExitRoot = _lastMainnetExitRoot;
lastRollupExitRoot = _lastRollupExitRoot;
if(_lastMainnetExitRoot != bytes32(0) || _lastRollupExitRoot != bytes32(0)){
_updateGlobalExitRootHash(_lastMainnetExitRoot, _lastRollupExitRoot);
}
}

/**
Expand All @@ -61,6 +66,15 @@ contract PolygonZkEVMGlobalExitRoot is IPolygonZkEVMGlobalExitRoot, Initializabl
revert OnlyAllowedContracts();
}

_updateGlobalExitRootHash(cacheLastMainnetExitRoot, cacheLastRollupExitRoot);
}

/**
* @notice Update the global exit root
* @param cacheLastMainnetExitRoot last mainnet exit root
* @param cacheLastRollupExitRoot last rollup exit root
*/
function _updateGlobalExitRootHash(bytes32 cacheLastMainnetExitRoot, bytes32 cacheLastRollupExitRoot) internal {
bytes32 newGlobalExitRoot = GlobalExitRootLib.calculateGlobalExitRoot(
cacheLastMainnetExitRoot,
cacheLastRollupExitRoot
Expand Down
3 changes: 3 additions & 0 deletions contracts/interfaces/IPolygonZkEVMGlobalExitRoot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ import "./IBasePolygonZkEVMGlobalExitRoot.sol";

interface IPolygonZkEVMGlobalExitRoot is IBasePolygonZkEVMGlobalExitRoot {
function getLastGlobalExitRoot() external view returns (bytes32);

function lastMainnetExitRoot() external view returns (bytes32);
function lastRollupExitRoot() external view returns (bytes32);
}
14 changes: 12 additions & 2 deletions test/contracts/bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ describe('PolygonZkEVMBridge Contract - for L2', () => {
const PolygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootWrapper');
polygonZkEVMGlobalExitRoot = await upgrades.deployProxy(PolygonZkEVMGlobalExitRootFactory, [], { initializer: false });

await polygonZkEVMGlobalExitRoot.initialize(rollup.address, polygonZkEVMBridgeContract.address);
await polygonZkEVMGlobalExitRoot.initialize(
rollup.address,
polygonZkEVMBridgeContract.address,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

await polygonZkEVMBridgeContract.initialize(
networkIDMainnet,
Expand Down Expand Up @@ -1241,7 +1246,12 @@ describe('PolygonZkEVMBridge Contract - for L1', () => {
const PolygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootWrapper');
polygonZkEVMGlobalExitRoot = await upgrades.deployProxy(PolygonZkEVMGlobalExitRootFactory, [], { initializer: false });

await polygonZkEVMGlobalExitRoot.initialize(rollup.address, polygonZkEVMBridgeContract.address);
await polygonZkEVMGlobalExitRoot.initialize(
rollup.address,
polygonZkEVMBridgeContract.address,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

await polygonZkEVMBridgeContract.initialize(
networkIDMainnet,
Expand Down
7 changes: 6 additions & 1 deletion test/contracts/bridgeMock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ describe('PolygonZkEVMBridge Mock Contract', () => {
// deploy global exit root manager
const polygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootMock');
polygonZkEVMGlobalExitRoot = await upgrades.deployProxy(polygonZkEVMGlobalExitRootFactory, [], { initializer: false });
await polygonZkEVMGlobalExitRoot.initialize(rollup.address, polygonZkEVMBridgeContract.address);
await polygonZkEVMGlobalExitRoot.initialize(
rollup.address,
polygonZkEVMBridgeContract.address,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

await polygonZkEVMBridgeContract.initialize(
networkIDMainnet,
Expand Down
7 changes: 6 additions & 1 deletion test/contracts/bridge_metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ describe('PolygonZkEVMBridge Contract werid metadata', () => {
// deploy global exit root manager
const polygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootWrapper');
polygonZkEVMGlobalExitRoot = await upgrades.deployProxy(polygonZkEVMGlobalExitRootFactory, [], { initializer: false });
await polygonZkEVMGlobalExitRoot.initialize(rollup.address, polygonZkEVMBridgeContract.address);
await polygonZkEVMGlobalExitRoot.initialize(
rollup.address,
polygonZkEVMBridgeContract.address,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

await polygonZkEVMBridgeContract.initialize(
networkIDMainnet,
Expand Down
7 changes: 6 additions & 1 deletion test/contracts/bridge_permit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ describe('PolygonZkEVMBridge Contract Permit tests', () => {
// deploy global exit root manager
const polygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootWrapper');
polygonZkEVMGlobalExitRoot = await upgrades.deployProxy(polygonZkEVMGlobalExitRootFactory, [], { initializer: false });
await polygonZkEVMGlobalExitRoot.initialize(rollup.address, polygonZkEVMBridgeContract.address);
await polygonZkEVMGlobalExitRoot.initialize(
rollup.address,
polygonZkEVMBridgeContract.address,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

await polygonZkEVMBridgeContract.initialize(
networkIDMainnet,
Expand Down
7 changes: 6 additions & 1 deletion test/contracts/emergencyManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ describe('Emergency mode test', () => {
initializer: false,
constructorArgs: [],
});
await polygonZkEVMGlobalExitRoot.initialize(precalculateZkevmAddress, precalculateBridgeAddress);
await polygonZkEVMGlobalExitRoot.initialize(
precalculateZkevmAddress,
precalculateBridgeAddress,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

// deploy PolygonZkEVMBridge
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridgeWrapper');
Expand Down
13 changes: 7 additions & 6 deletions test/contracts/globalExitRootManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { ethers, upgrades } = require('hardhat');
function calculateGlobalExitRoot(mainnetExitRoot, rollupExitRoot) {
return ethers.utils.solidityKeccak256(['bytes32', 'bytes32'], [mainnetExitRoot, rollupExitRoot]);
}
const zero32bytes = '0x0000000000000000000000000000000000000000000000000000000000000000';
const one32bytes = '0x0000000000000000000000000000000000000000000000000000000000000001';
const two32bytes = '0x0000000000000000000000000000000000000000000000000000000000000002';

describe('Global Exit Root', () => {
let rollup;
Expand All @@ -19,15 +20,15 @@ describe('Global Exit Root', () => {
const PolygonZkEVMGlobalExitRootFactory = await ethers.getContractFactory('PolygonZkEVMGlobalExitRootWrapper');
polygonZkEVMGlobalExitRoot = await upgrades.deployProxy(PolygonZkEVMGlobalExitRootFactory, [], { initializer: false });

await polygonZkEVMGlobalExitRoot.initialize(rollup.address, PolygonZkEVMBridge.address);
await polygonZkEVMGlobalExitRoot.initialize(rollup.address, PolygonZkEVMBridge.address, one32bytes, two32bytes);
await polygonZkEVMGlobalExitRoot.deployed();
});

it('should check the constructor parameters', async () => {
expect(await polygonZkEVMGlobalExitRoot.rollupAddress()).to.be.equal(rollup.address);
expect(await polygonZkEVMGlobalExitRoot.bridgeAddress()).to.be.equal(PolygonZkEVMBridge.address);
expect(await polygonZkEVMGlobalExitRoot.lastRollupExitRoot()).to.be.equal(zero32bytes);
expect(await polygonZkEVMGlobalExitRoot.lastMainnetExitRoot()).to.be.equal(zero32bytes);
expect(await polygonZkEVMGlobalExitRoot.lastRollupExitRoot()).to.be.equal(two32bytes);
expect(await polygonZkEVMGlobalExitRoot.lastMainnetExitRoot()).to.be.equal(one32bytes);
});

it('should update root and check global exit root', async () => {
Expand All @@ -39,10 +40,10 @@ describe('Global Exit Root', () => {
// Update root from the rollup
await expect(polygonZkEVMGlobalExitRoot.connect(rollup).updateExitRoot(newRootRollup))
.to.emit(polygonZkEVMGlobalExitRoot, 'UpdateGlobalExitRoot')
.withArgs(zero32bytes, newRootRollup);
.withArgs(one32bytes, newRootRollup);

expect(await polygonZkEVMGlobalExitRoot.getLastGlobalExitRoot())
.to.be.equal(calculateGlobalExitRoot(zero32bytes, newRootRollup));
.to.be.equal(calculateGlobalExitRoot(one32bytes, newRootRollup));

// Update root from the PolygonZkEVMBridge
const newRootBridge = ethers.utils.hexlify(ethers.utils.randomBytes(32));
Expand Down
7 changes: 6 additions & 1 deletion test/contracts/polygonZkEVM.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ describe('Polygon ZK-EVM', () => {
initializer: false,
constructorArgs: [],
});
await polygonZkEVMGlobalExitRoot.initialize(precalculateZkevmAddress, precalculateBridgeAddress);
await polygonZkEVMGlobalExitRoot.initialize(
precalculateZkevmAddress,
precalculateBridgeAddress,
ethers.constants.HashZero,
ethers.constants.HashZero,
);

// deploy PolygonZkEVMBridge
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridgeWrapper');
Expand Down
7 changes: 6 additions & 1 deletion test/contracts/polygonZkEVMTestnetV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ describe('Polygon ZK-EVM TestnetV2', () => {
constructorArgs: [],
});

await polygonZkEVMGlobalExitRoot.initialize(precalculateZkevmAddress, precalculateBridgeAddress);
await polygonZkEVMGlobalExitRoot.initialize(
precalculateZkevmAddress,
precalculateBridgeAddress,
ethers.constants.HashZero,
ethers.constants.HashZero,
);
// deploy PolygonZkEVMBridge
const polygonZkEVMBridgeFactory = await ethers.getContractFactory('PolygonZkEVMBridgeWrapper');
polygonZkEVMBridgeContract = await upgrades.deployProxy(polygonZkEVMBridgeFactory, [], { initializer: false });
Expand Down

0 comments on commit a090458

Please sign in to comment.