Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update contracts tests #300

Merged
merged 18 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Main CI

on:
push:
branches: [feature/ongoingPP]
branches: [main, develop, feature/ongoingPP]
pull_request:
branches: [feature/ongoingPP]
branches: [main, develop, feature/ongoingPP]

jobs:
lint-and-test:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Main CI

on:
push:
branches: [main, develop]
branches: [main, develop, feature/ongoingPP]
pull_request:
branches: [main, develop]
branches: [main, develop, feature/ongoingPP]

jobs:
lint-and-test:
Expand Down
10 changes: 9 additions & 1 deletion contracts/mocks/VerifierRollupHelperMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
pragma solidity 0.8.20;

import "../interfaces/IVerifierRollup.sol";
import "../v2/interfaces/ISP1Verifier.sol";

contract VerifierRollupHelperMock is IVerifierRollup {
contract VerifierRollupHelperMock is IVerifierRollup, ISP1Verifier {
function verifyProof(
bytes32[24] calldata proof,
uint256[1] memory pubSignals
) public pure override returns (bool) {
return true;
}

// SP1 interface
function verifyProof(
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) public pure {}
}
172 changes: 140 additions & 32 deletions contracts/v2/PolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ contract PolygonRollupManager is
* @param rollupVerifierType Rollup compatibility ID, to check upgradability between rollup types
* @param obsolete Indicates if the rollup type is obsolete
* @param genesis Genesis block of the rollup, note that will only be used on creating new rollups, not upgrade them
* @param programVKey Hashed program that will be executed in case of using a "general porpuse ZK verifier" e.g SP1
*/
struct RollupType {
address consensusImplementation;
address verifier;
uint64 forkID;
/// @custom:oz-retyped-from uint8
VerifierType rollupVerifierType;
bool obsolete;
bytes32 genesis;
Expand Down Expand Up @@ -79,16 +81,75 @@ contract PolygonRollupManager is
uint64 forkID;
mapping(uint64 batchNum => bytes32) batchNumToStateRoot;
mapping(uint64 batchNum => SequencedBatchData) sequencedBatches;
/// @custom:oz-renamed-from pendingStateTransitions
mapping(uint256 pendingStateNum => PendingState) _legacyPendingStateTransitions;
bytes32 lastLocalExitRoot;
uint64 lastBatchSequenced;
uint64 lastVerifiedBatch;
uint128 _legacyPendingStateGap;
// uint64 _legacyLastPendingState;
// uint64 _legacyLastPendingStateConsolidated;
/// @custom:oz-renamed-from lastPendingState
uint64 _legacyLastPendingState;
/// @custom:oz-renamed-from lastPendingStateConsolidated
uint64 _legacyLastPendingStateConsolidated;
uint64 lastVerifiedBatchBeforeUpgrade;
uint64 rollupTypeID;
/// @custom:oz-retyped-from uint8
VerifierType rollupVerifierType;
bytes32 lastPessimisticRoot;
bytes32 programVKey;
}

/**
* @notice Struct to return all the necessary rollup info: VerifierType StateTransition
* @param rollupContract Rollup consensus contract, which manages everything
* related to sequencing transactions
* @param chainID Chain ID of the rollup
* @param verifier Verifier contract
* @param forkID ForkID of the rollup
* @param lastLocalExitRoot Last exit root verified, used for compute the rollupExitRoot
* @param lastBatchSequenced Last batch sent by the consensus contract
* @param lastVerifiedBatch Last batch verified
* @param _legacyLastPendingState Last pending state (deprecated)
* @param _legacyLastPendingStateConsolidated Last pending state consolidated (deprecated)
* @param lastVerifiedBatchBeforeUpgrade Last batch verified before the last upgrade
* @param rollupTypeID Rollup type ID, can be 0 if it was added as an existing rollup
* @param rollupVerifierType Rollup ID used for compatibility checks when upgrading
*/
struct RollupDataReturnStateTransistion {
IPolygonRollupBase rollupContract;
uint64 chainID;
address verifier;
uint64 forkID;
bytes32 lastLocalExitRoot;
uint64 lastBatchSequenced;
uint64 lastVerifiedBatch;
uint64 _legacyLastPendingState;
uint64 _legacyLastPendingStateConsolidated;
uint64 lastVerifiedBatchBeforeUpgrade;
uint64 rollupTypeID;
VerifierType rollupVerifierType;
}

/**
* @notice Struct to return all the necessary rollup info: VerifierType Pessimistic
* @param rollupContract Rollup consensus contract, which manages everything
* related to sequencing transactions
* @param chainID Chain ID of the rollup
* @param verifier Verifier contract
* @param forkID ForkID of the rollup
* @param lastLocalExitRoot Last exit root verified, used for compute the rollupExitRoot
* @param rollupTypeID Rollup type ID, can be 0 if it was added as an existing rollup
* @param rollupVerifierType Rollup ID used for compatibility checks when upgrading
* @param lastPessimisticRoot Pessimistic info, currently contains the local balance tree and the local nullifier tree hashed
* @param programVKey Hashed program that will be executed in case of using a "general porpuse ZK verifier" e.g SP1
*/
struct RollupDataReturnPessimistic {
IPolygonRollupBase rollupContract;
uint64 chainID;
address verifier;
uint64 forkID;
bytes32 lastLocalExitRoot;
uint64 rollupTypeID;
VerifierType rollupVerifierType;
bytes32 lastPessimisticRoot;
bytes32 programVKey;
}
Expand Down Expand Up @@ -175,13 +236,15 @@ contract PolygonRollupManager is
uint32 public rollupTypeCount;

// Rollup type mapping
// @custom:oz-retyped-from PolygonRollupManagerPrevious.RollupType
mapping(uint32 rollupTypeID => RollupType) public rollupTypeMap;

// Number of rollups added, every new rollup will be assigned sequencially a new ID
uint32 public rollupCount;

// Rollups ID mapping
mapping(uint32 rollupID => RollupData) public rollupIDToRollupData;
/// @custom:oz-renamed-from rollupIDToRollupData
mapping(uint32 rollupID => RollupData) internal _rollupIDToRollupData;

// Rollups address mapping
mapping(address rollupAddress => uint32 rollupID) public rollupAddressToID;
Expand All @@ -201,16 +264,20 @@ contract PolygonRollupManager is

// Trusted aggregator timeout, if a sequence is not verified in this time frame,
// everyone can verify that sequence
/// @custom:oz-renamed-from trustedAggregatorTimeout
uint64 internal __legacyTrustedAggregatorTimeout;

// Once a pending state exceeds this timeout it can be consolidated (deprecated)
/// @custom:oz-renamed-from pendingStateTimeout
uint64 internal __legacyPendingStateTimeout;

// Time target of the verification of a batch
// Adaptively the batchFee will be updated to achieve this target
/// @custom:oz-renamed-from verifyBatchTimeTarget
uint64 internal __legacyVerifyBatchTimeTarget;

// Batch fee multiplier with 3 decimals that goes from 1000 - 1023
/// @custom:oz-renamed-from multiplierBatchFee
uint16 internal __legacyMultiplierBatchFee;

// Current POL fee per batch sequenced
Expand Down Expand Up @@ -461,7 +528,7 @@ contract PolygonRollupManager is
// Store rollup data
rollupAddressToID[rollupAddress] = rollupID;

RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];

rollup.rollupContract = IPolygonRollupBase(rollupAddress);
rollup.forkID = rollupType.forkID;
Expand Down Expand Up @@ -536,7 +603,7 @@ contract PolygonRollupManager is
// Store rollup data
rollupAddressToID[address(rollupAddress)] = rollupID;

RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];
rollup.rollupContract = rollupAddress;
rollup.forkID = forkID;
rollup.verifier = verifier;
Expand All @@ -550,6 +617,7 @@ contract PolygonRollupManager is
} else {
rollup.batchNumToStateRoot[0] = initRoot;
}

// rollup type is 0, since it does not follow any rollup type
emit AddExistingRollup(
rollupID,
Expand Down Expand Up @@ -578,7 +646,7 @@ contract PolygonRollupManager is
}

// Check all sequences are verified before upgrading
RollupData storage rollup = rollupIDToRollupData[
RollupData storage rollup = _rollupIDToRollupData[
rollupAddressToID[address(rollupContract)]
];

Expand All @@ -592,13 +660,6 @@ contract PolygonRollupManager is
revert UpdateToOldRollupTypeID();
}

if (
rollup.rollupVerifierType !=
rollupTypeMap[newRollupTypeID].rollupVerifierType
) {
revert UpdateNotCompatible();
}

_updateRollup(rollupContract, newRollupTypeID, new bytes(0));
}

Expand Down Expand Up @@ -638,7 +699,7 @@ contract PolygonRollupManager is
revert RollupMustExist();
}

RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];

// The update must be to a new rollup type
if (rollup.rollupTypeID == newRollupTypeID) {
Expand All @@ -654,13 +715,7 @@ contract PolygonRollupManager is

// Check rollup types
if (rollup.rollupVerifierType != newRollupType.rollupVerifierType) {
// Currently the transition from pessimistic to state transition is not allowed
if (rollup.rollupVerifierType == VerifierType.Pessimistic) {
revert RollupTypeObsolete();
}

// Update rollup verifier type
rollup.rollupVerifierType = newRollupType.rollupVerifierType;
revert UpdateNotCompatible();
}

// Update rollup parameters
Expand Down Expand Up @@ -706,7 +761,7 @@ contract PolygonRollupManager is
}

// Load rollup
RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];

if (rollup.rollupVerifierType != VerifierType.StateTransition) {
revert OnlyStateTransitionChains();
Expand Down Expand Up @@ -786,7 +841,7 @@ contract PolygonRollupManager is
revert MustSequenceSomeBatch();
}

RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];

// Update total sequence parameters
totalSequencedBatches += newSequencedBatches;
Expand Down Expand Up @@ -835,7 +890,7 @@ contract PolygonRollupManager is
revert PendingStateNumExist();
}

RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];

if (rollup.rollupVerifierType != VerifierType.StateTransition) {
revert OnlyStateTransitionChains();
Expand Down Expand Up @@ -883,7 +938,7 @@ contract PolygonRollupManager is
bytes32 newPessimisticRoot,
bytes calldata proof
) external onlyRole(_TRUSTED_AGGREGATOR_ROLE) {
RollupData storage rollup = rollupIDToRollupData[rollupID];
RollupData storage rollup = _rollupIDToRollupData[rollupID];

// Only for pessimistic verifiers
if (rollup.rollupVerifierType != VerifierType.Pessimistic) {
Expand Down Expand Up @@ -1113,7 +1168,7 @@ contract PolygonRollupManager is
// In the first iteration the nodes will be the leafs which are the local exit roots of each network
for (uint256 i = 0; i < currentNodes; i++) {
// The first rollup ID starts on 1
tmpTree[i] = rollupIDToRollupData[uint32(i + 1)].lastLocalExitRoot;
tmpTree[i] = _rollupIDToRollupData[uint32(i + 1)].lastLocalExitRoot;
}

// This variable will keep track of the zero hashes
Expand Down Expand Up @@ -1168,7 +1223,7 @@ contract PolygonRollupManager is
function getLastVerifiedBatch(
uint32 rollupID
) public view returns (uint64) {
return _getLastVerifiedBatch(rollupIDToRollupData[rollupID]);
return _getLastVerifiedBatch(_rollupIDToRollupData[rollupID]);
}

/**
Expand Down Expand Up @@ -1225,7 +1280,7 @@ contract PolygonRollupManager is
) public view returns (bytes memory) {
return
_getInputPessimisticBytes(
rollupIDToRollupData[rollupID],
_rollupIDToRollupData[rollupID],
selectedGlobalExitRoot,
newLocalExitRoot,
newPessimisticRoot
Expand Down Expand Up @@ -1280,7 +1335,7 @@ contract PolygonRollupManager is
) public view returns (bytes memory) {
return
_getInputSnarkBytes(
rollupIDToRollupData[rollupID],
_rollupIDToRollupData[rollupID],
initNumBatch,
finalNewBatch,
newLocalExitRoot,
Expand Down Expand Up @@ -1373,7 +1428,7 @@ contract PolygonRollupManager is
uint32 rollupID,
uint64 batchNum
) public view returns (bytes32) {
return rollupIDToRollupData[rollupID].batchNumToStateRoot[batchNum];
return _rollupIDToRollupData[rollupID].batchNumToStateRoot[batchNum];
}

/**
Expand All @@ -1385,6 +1440,59 @@ contract PolygonRollupManager is
uint32 rollupID,
uint64 batchNum
) public view returns (SequencedBatchData memory) {
return rollupIDToRollupData[rollupID].sequencedBatches[batchNum];
return _rollupIDToRollupData[rollupID].sequencedBatches[batchNum];
}

/**
* @notice Get rollup data: VerifierType StateTransition
* @param rollupID Rollup identifier
*/
function rollupIDToRollupData(
uint32 rollupID
) public view returns (RollupDataReturnStateTransistion memory rollupData) {
RollupData storage rollup = _rollupIDToRollupData[rollupID];

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

rollupData.rollupContract = rollup.rollupContract;
rollupData.chainID = rollup.chainID;
rollupData.verifier = rollup.verifier;
rollupData.forkID = rollup.forkID;
rollupData.lastLocalExitRoot = rollup.lastLocalExitRoot;
rollupData.lastBatchSequenced = rollup.lastBatchSequenced;
rollupData.lastVerifiedBatch = rollup.lastVerifiedBatch;
rollupData._legacyLastPendingState = rollup._legacyLastPendingState;
rollupData._legacyLastPendingStateConsolidated = rollup
._legacyLastPendingStateConsolidated;
rollupData.lastVerifiedBatchBeforeUpgrade = rollup
.lastVerifiedBatchBeforeUpgrade;
rollupData.rollupTypeID = rollup.rollupTypeID;
rollupData.rollupVerifierType = rollup.rollupVerifierType;
}

/**
* @notice Get rollup data: VerifierType Pessimistic
* @param rollupID Rollup identifier
*/
function rollupIDToRollupDataPessimistic(
uint32 rollupID
) public view returns (RollupDataReturnPessimistic memory rollupData) {
RollupData storage rollup = _rollupIDToRollupData[rollupID];

if (rollup.rollupVerifierType != VerifierType.Pessimistic) {
revert InvalidVerifierType();
}

rollupData.rollupContract = rollup.rollupContract;
rollupData.chainID = rollup.chainID;
rollupData.verifier = rollup.verifier;
rollupData.forkID = rollup.forkID;
rollupData.lastLocalExitRoot = rollup.lastLocalExitRoot;
rollupData.rollupTypeID = rollup.rollupTypeID;
rollupData.rollupVerifierType = rollup.rollupVerifierType;
rollupData.lastPessimisticRoot = rollup.lastPessimisticRoot;
rollupData.programVKey = rollup.programVKey;
}
}
Loading
Loading