Skip to content

Commit

Permalink
Merge pull request #316 from 0xPolygonHermez/feature/selecter-to-l1in…
Browse files Browse the repository at this point in the history
…fotree

Feature/selecter to l1infotree
  • Loading branch information
krlosMata authored Aug 30, 2024
2 parents c263786 + f8dc1aa commit f0f4d7c
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 68 deletions.
34 changes: 20 additions & 14 deletions contracts/v2/PolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,10 @@ contract PolygonRollupManager is

RollupData storage rollup = _rollupIDToRollupData[rollupID];

if (rollup.rollupVerifierType != VerifierType.StateTransition) {
revert OnlyStateTransitionChains();
}

// Update total sequence parameters
totalSequencedBatches += newSequencedBatches;

Expand Down Expand Up @@ -1033,14 +1037,14 @@ contract PolygonRollupManager is
/**
* @notice Allows a trusted aggregator to verify pessimistic proof
* @param rollupID Rollup identifier
* @param selectedGlobalExitRoot Selected global exit root to proof imported bridges
* @param l1InfoTreeLeafCount Count of the L1InfoTree leaf that will be used to verify imported bridge exits
* @param newLocalExitRoot New local exit root
* @param newPessimisticRoot New pessimistic information, Hash(localBalanceTreeRoot, nullifierTreeRoot)
* @param proof SP1 proof (Plonk)
*/
function verifyPessimisticTrustedAggregator(
uint32 rollupID,
bytes32 selectedGlobalExitRoot,
uint32 l1InfoTreeLeafCount,
bytes32 newLocalExitRoot,
bytes32 newPessimisticRoot,
bytes calldata proof
Expand All @@ -1052,17 +1056,19 @@ contract PolygonRollupManager is
revert OnlyChainsWithPessimisticProofs();
}

// Check selected global exit root exist
if (
globalExitRootManager.globalExitRootMap(selectedGlobalExitRoot) == 0
) {
revert GlobalExitRootNotExist();
// Check l1InfoTreeLeafCount has a valid l1InfoTreeRoot
bytes32 l1InfoRoot = globalExitRootManager.l1InfoRootMap(
l1InfoTreeLeafCount
);

if (l1InfoRoot == bytes32(0)) {
revert L1InfoTreeLeafCountInvalid();
}

bytes memory inputPessimisticBytes = _getInputPessimisticBytes(
rollupID,
rollup,
selectedGlobalExitRoot,
l1InfoRoot,
newLocalExitRoot,
newPessimisticRoot
);
Expand Down Expand Up @@ -1292,21 +1298,21 @@ contract PolygonRollupManager is
/**
* @notice Function to calculate the pessimistic input bytes
* @param rollupID Rollup id used to calculate the input snark bytes
* @param selectedGlobalExitRoot Selected global exit root to proof imported bridges
* @param l1InfoTreeRoot L1 Info tree root to proof imported bridges
* @param newLocalExitRoot New local exit root
* @param newPessimisticRoot New pessimistic information, Hash(localBalanceTreeRoot, nullifierTreeRoot)
*/
function getInputPessimisticBytes(
uint32 rollupID,
bytes32 selectedGlobalExitRoot,
bytes32 l1InfoTreeRoot,
bytes32 newLocalExitRoot,
bytes32 newPessimisticRoot
) public view returns (bytes memory) {
return
_getInputPessimisticBytes(
rollupID,
_rollupIDToRollupData[rollupID],
selectedGlobalExitRoot,
l1InfoTreeRoot,
newLocalExitRoot,
newPessimisticRoot
);
Expand All @@ -1316,14 +1322,14 @@ contract PolygonRollupManager is
* @notice Function to calculate the input snark bytes
* @param rollupID Rollup identifier
* @param rollup Rollup data storage pointer
* @param selectedGlobalExitRoot Selected global exit root to proof imported bridges
* @param l1InfoTreeRoot L1 Info tree root to proof imported bridges
* @param newLocalExitRoot New local exit root
* @param newPessimisticRoot New pessimistic information, Hash(localBalanceTreeRoot, nullifierTreeRoot)
*/
function _getInputPessimisticBytes(
uint32 rollupID,
RollupData storage rollup,
bytes32 selectedGlobalExitRoot,
bytes32 l1InfoTreeRoot,
bytes32 newLocalExitRoot,
bytes32 newPessimisticRoot
) internal view returns (bytes memory) {
Expand All @@ -1336,7 +1342,7 @@ contract PolygonRollupManager is
abi.encodePacked(
rollup.lastLocalExitRoot,
rollup.lastPessimisticRoot,
selectedGlobalExitRoot,
l1InfoTreeRoot,
rollupID,
consensusHash,
newLocalExitRoot,
Expand Down
2 changes: 1 addition & 1 deletion contracts/v2/consensus/validium/PolygonValidiumEtrog.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract PolygonValidiumEtrog is PolygonRollupBaseEtrog, IPolygonValidium {
);

if (l1InfoRoot == bytes32(0)) {
revert L1InfoRootIndexInvalid();
revert L1InfoTreeLeafCountInvalid();
}

// Store storage variables in memory, to save gas, because will be overrided multiple times
Expand Down
6 changes: 3 additions & 3 deletions contracts/v2/interfaces/IPolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ interface IPolygonRollupManager {
error InvalidRollup();

/**
* @dev Global exit root does not exists
* @dev Not valid L1 info tree leaf count
*/
error GlobalExitRootNotExist();
error L1InfoTreeLeafCountInvalid();

/**
* @dev Only State Transition Chains
Expand Down Expand Up @@ -378,7 +378,7 @@ interface IPolygonRollupManager {

function verifyPessimisticTrustedAggregator(
uint32 rollupID,
bytes32 selectedGlobalExitRoot,
uint32 l1InfoTreeLeafCount,
bytes32 newLocalExitRoot,
bytes32 newPessimisticRoot,
bytes calldata proof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity ^0.8.20;
import "../../interfaces/IPolygonZkEVMErrors.sol";

interface IPolygonZkEVMVEtrogErrors is IPolygonZkEVMErrors {
interface IPolygonZkEVMEtrogErrors is IPolygonZkEVMErrors {
/**
* @dev Thrown when the caller is not the trusted sequencer
*/
Expand Down Expand Up @@ -55,9 +55,9 @@ interface IPolygonZkEVMVEtrogErrors is IPolygonZkEVMErrors {
error MaxTimestampSequenceInvalid();

/**
* @dev Thrown when l1 info root does not exist
* @dev Thrown when l1 info tree leafCount does not exist
*/
error L1InfoRootIndexInvalid();
error L1InfoTreeLeafCountInvalid();

/**
* @dev Thrown when the acc input hash does not match the predicted by the sequencer
Expand Down
7 changes: 5 additions & 2 deletions contracts/v2/lib/PolygonConsensusBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeab
import "../interfaces/IPolygonZkEVMGlobalExitRootV2.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "../../interfaces/IPolygonZkEVMErrors.sol";
import "../interfaces/IPolygonZkEVMVEtrogErrors.sol";
import "../interfaces/IPolygonZkEVMEtrogErrors.sol";
import "../interfaces/IPolygonConsensusBase.sol";
import "../interfaces/IPolygonRollupBase.sol";
import "../interfaces/IPolygonZkEVMBridgeV2.sol";
Expand All @@ -24,7 +24,7 @@ import "../PolygonRollupManager.sol";
abstract contract PolygonConsensusBase is
Initializable,
IPolygonConsensusBase,
IPolygonZkEVMVEtrogErrors
IPolygonZkEVMEtrogErrors
{
// POL token address
IERC20Upgradeable public immutable pol;
Expand Down Expand Up @@ -125,6 +125,9 @@ abstract contract PolygonConsensusBase is
pol = _pol;
bridgeAddress = _bridgeAddress;
rollupManager = _rollupManager;

// Disable initalizers on the implementation following the best practices
_disableInitializers();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions contracts/v2/lib/PolygonRollupBaseEtrog.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ pragma solidity ^0.8.20;

import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "../interfaces/IPolygonZkEVMGlobalExitRootV2.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "../../interfaces/IPolygonZkEVMErrors.sol";
import "../interfaces/IPolygonZkEVMVEtrogErrors.sol";
import "../interfaces/IPolygonZkEVMEtrogErrors.sol";
import "../PolygonRollupManager.sol";
import "../interfaces/IPolygonRollupBase.sol";
import "../interfaces/IPolygonZkEVMBridgeV2.sol";
Expand Down Expand Up @@ -354,7 +353,7 @@ abstract contract PolygonRollupBaseEtrog is
);

if (l1InfoRoot == bytes32(0)) {
revert L1InfoRootIndexInvalid();
revert L1InfoTreeLeafCountInvalid();
}

// Store storage variables in memory, to save gas, because will be overrided multiple times
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeab
import "../interfaces/IPolygonZkEVMGlobalExitRootV2.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "../../interfaces/IPolygonZkEVMErrors.sol";
import "../interfaces/IPolygonZkEVMVEtrogErrors.sol";
import "../interfaces/IPolygonZkEVMEtrogErrors.sol";
import "../PolygonRollupManager.sol";
import "./IPolygonRollupBasePrevious.sol";
import "../interfaces/IPolygonZkEVMBridgeV2.sol";
Expand All @@ -23,7 +23,7 @@ import "../lib/PolygonConstantsBase.sol";
abstract contract PolygonRollupBaseEtrogPrevious is
Initializable,
PolygonConstantsBase,
IPolygonZkEVMVEtrogErrors,
IPolygonZkEVMEtrogErrors,
IPolygonRollupBasePrevious
{
using SafeERC20Upgradeable for IERC20Upgradeable;
Expand Down Expand Up @@ -283,6 +283,9 @@ abstract contract PolygonRollupBaseEtrogPrevious is
pol = _pol;
bridgeAddress = _bridgeAddress;
rollupManager = _rollupManager;

// Disable initalizers on the implementation following the best practices
_disableInitializers();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/pessimistic-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ConsensusTypes = {
* ) % FrSNARK
* @param {String} lastLocalExitRoot - old LER
* @param {String} lastPessimisticRoot - old pessimistic root. pessRoor = Poseidon(LBR # nullifierRoot)
* @param {String} selectedGlobalExitRoot - selected GER
* @param {String} l1InfoTreeRoot - L1 info tree root
* @param {Number} rollupID - rollup identifier (networkID = rollupID - 1)
* @param {String} consensusHash - consensus hash. consensusHash = Sha(consensusType # consensusPayload)
* @param {String} newLocalExitRoot - new LER
Expand All @@ -26,7 +26,7 @@ const ConsensusTypes = {
function computeInputPessimisticBytes(
lastLocalExitRoot,
lastPessimisticRoot,
selectedGlobalExitRoot,
l1InfoTreeRoot,
rollupID,
consensusHash,
newLocalExitRoot,
Expand All @@ -37,7 +37,7 @@ function computeInputPessimisticBytes(
[
lastLocalExitRoot,
lastPessimisticRoot,
selectedGlobalExitRoot,
l1InfoTreeRoot,
rollupID,
consensusHash,
newLocalExitRoot,
Expand Down
12 changes: 6 additions & 6 deletions test/contractsv2/PolygonPessimisticConsensus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ describe("PolygonPessimisticConsensus", () => {
// deploy consensus
// create polygonPessimisticConsensus implementation
const ppConsensusFactory = await ethers.getContractFactory("PolygonPessimisticConsensus");
PolygonPPConsensusContract = await ppConsensusFactory.deploy(
gerManagerAddress,
polTokenAddress,
bridgeAddress,
rollupManagerAddress
);
PolygonPPConsensusContract = await upgrades.deployProxy(ppConsensusFactory, [], {
initializer: false,
constructorArgs: [gerManagerAddress, polTokenAddress, bridgeAddress, rollupManagerAddress],
unsafeAllow: ["constructor", "state-variable-immutable"],
});

await PolygonPPConsensusContract.waitForDeployment();
});

Expand Down
42 changes: 31 additions & 11 deletions test/contractsv2/PolygonRollupManager-Pessimistic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
);

// select unexistent global exit root
const unexistentGER = "0xddff00000000000000000000000000000000000000000000000000000000ddff";
const unexistentL1InfoTreeCount = 2;
const newLER = "0x0000000000000000000000000000000000000000000000000000000000000001";
const newPPRoot = "0x0000000000000000000000000000000000000000000000000000000000000002";
const proofPP = "0x00";
Expand All @@ -838,7 +838,7 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
await expect(
rollupManagerContract.verifyPessimisticTrustedAggregator(
pessimisticRollupID,
unexistentGER,
unexistentL1InfoTreeCount,
newLER,
newPPRoot,
proofPP
Expand All @@ -849,10 +849,16 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
await expect(
rollupManagerContract
.connect(trustedAggregator)
.verifyPessimisticTrustedAggregator(pessimisticRollupID, unexistentGER, newLER, newPPRoot, proofPP)
).to.be.revertedWithCustomError(rollupManagerContract, "GlobalExitRootNotExist");
.verifyPessimisticTrustedAggregator(
pessimisticRollupID,
unexistentL1InfoTreeCount,
newLER,
newPPRoot,
proofPP
)
).to.be.revertedWithCustomError(rollupManagerContract, "L1InfoTreeLeafCountInvalid");

// create a bridge to generate a new GER
// create a bridge to generate a new GER and add another value in the l1IfoRootMap
const tokenAddress = ethers.ZeroAddress;
const amount = ethers.parseEther("1");
await polygonZkEVMBridgeContract.bridgeAsset(
Expand All @@ -867,12 +873,14 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
}
);

const existingGER = await polygonZkEVMGlobalExitRoot.getLastGlobalExitRoot();
// get last L1InfoTreeLeafCount
const lastL1InfoTreeLeafCount = await polygonZkEVMGlobalExitRoot.depositCount();
const lastL1InfoTreeRoot = await polygonZkEVMGlobalExitRoot.l1InfoRootMap(0);

// check JS function computeInputPessimisticBytes
const inputPessimisticBytes = await rollupManagerContract.getInputPessimisticBytes(
pessimisticRollupID,
existingGER,
lastL1InfoTreeRoot,
newLER,
newPPRoot
);
Expand All @@ -884,7 +892,7 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
const expectedInputPessimsiticBytes = computeInputPessimisticBytes(
infoRollup[4],
infoRollup[10],
existingGER,
lastL1InfoTreeRoot,
pessimisticRollupID,
consensusHash,
newLER,
Expand All @@ -897,7 +905,13 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
await expect(
rollupManagerContract
.connect(trustedAggregator)
.verifyPessimisticTrustedAggregator(pessimisticRollupID, existingGER, newLER, newPPRoot, proofPP)
.verifyPessimisticTrustedAggregator(
pessimisticRollupID,
lastL1InfoTreeLeafCount,
newLER,
newPPRoot,
proofPP
)
)
.to.emit(rollupManagerContract, "VerifyBatchesTrustedAggregator")
.withArgs(pessimisticRollupID, 0, ethers.ZeroHash, newLER, trustedAggregator.address);
Expand Down Expand Up @@ -1005,15 +1019,21 @@ describe("Polygon Rollup Manager with Polygon Pessimistic Consensus", () => {
);

// try to verify
const unexistentGER = "0xddff00000000000000000000000000000000000000000000000000000000ddff";
const unexistentL1InfoTreeLeafcount = 2;
const newLER = "0x0000000000000000000000000000000000000000000000000000000000000001";
const newPPRoot = "0x0000000000000000000000000000000000000000000000000000000000000002";
const proofPP = "0x00";

await expect(
rollupManagerContract
.connect(trustedAggregator)
.verifyPessimisticTrustedAggregator(stateTransistionRollupID, unexistentGER, newLER, newPPRoot, proofPP)
.verifyPessimisticTrustedAggregator(
stateTransistionRollupID,
unexistentL1InfoTreeLeafcount,
newLER,
newPPRoot,
proofPP
)
).to.be.revertedWithCustomError(rollupManagerContract, "OnlyChainsWithPessimisticProofs");
});
});
Loading

0 comments on commit f0f4d7c

Please sign in to comment.