feat(contracts): add prover role in scroll chain (#559)

Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
This commit is contained in:
Xi Lin
2023-06-13 21:03:56 +08:00
committed by GitHub
parent 7118746e34
commit 87c81c6555
2 changed files with 67 additions and 3 deletions

View File

@@ -25,6 +25,11 @@ contract ScrollChain is OwnableUpgradeable, IScrollChain {
/// @param status The status of the account updated.
event UpdateSequencer(address indexed account, bool status);
/// @notice Emitted when owner updates the status of prover.
/// @param account The address of account updated.
/// @param status The status of the account updated.
event UpdateProver(address indexed account, bool status);
/// @notice Emitted when the address of rollup verifier is updated.
/// @param oldVerifier The address of old rollup verifier.
/// @param newVerifier The address of new rollup verifier.
@@ -58,6 +63,9 @@ contract ScrollChain is OwnableUpgradeable, IScrollChain {
/// @notice Whether an account is a sequencer.
mapping(address => bool) public isSequencer;
/// @notice Whether an account is a prover.
mapping(address => bool) public isProver;
/// @notice The latest finalized batch index.
uint256 public lastFinalizedBatchIndex;
@@ -80,6 +88,11 @@ contract ScrollChain is OwnableUpgradeable, IScrollChain {
_;
}
modifier OnlyProver() {
require(isProver[msg.sender], "caller not prover");
_;
}
/***************
* Constructor *
***************/
@@ -278,7 +291,7 @@ contract ScrollChain is OwnableUpgradeable, IScrollChain {
bytes32 _postStateRoot,
bytes32 _withdrawRoot,
bytes calldata _aggrProof
) external override OnlySequencer {
) external override OnlyProver {
require(_prevStateRoot != bytes32(0), "previous state root is zero");
require(_postStateRoot != bytes32(0), "new state root is zero");
@@ -352,6 +365,16 @@ contract ScrollChain is OwnableUpgradeable, IScrollChain {
emit UpdateSequencer(_account, _status);
}
/// @notice Update the status of prover.
/// @dev This function can only called by contract owner.
/// @param _account The address of account to update.
/// @param _status The status of the account to update.
function updateProver(address _account, bool _status) external onlyOwner {
isProver[_account] = _status;
emit UpdateProver(_account, _status);
}
/// @notice Update the address verifier contract.
/// @param _newVerifier The address of new verifier contract.
function updateVerifier(address _newVerifier) external onlyOwner {

View File

@@ -15,7 +15,9 @@ import {MockRollupVerifier} from "./mocks/MockRollupVerifier.sol";
contract ScrollChainTest is DSTestPlus {
// from ScrollChain
event UpdateSequencer(address indexed account, bool status);
event UpdateProver(address indexed account, bool status);
event UpdateVerifier(address oldVerifier, address newVerifier);
event UpdateMaxNumL2TxInChunk(uint256 oldMaxNumL2TxInChunk, uint256 newMaxNumL2TxInChunk);
event CommitBatch(bytes32 indexed batchHash);
event FinalizeBatch(bytes32 indexed batchHash, bytes32 stateRoot, bytes32 withdrawRoot);
@@ -121,10 +123,11 @@ contract ScrollChainTest is DSTestPlus {
}
function testFinalizeBatchWithProof() public {
// caller not sequencer, revert
hevm.expectRevert("caller not sequencer");
// caller not prover, revert
hevm.expectRevert("caller not prover");
rollup.finalizeBatchWithProof(new bytes(0), bytes32(0), bytes32(0), bytes32(0), new bytes(0));
rollup.updateProver(address(this), true);
rollup.updateSequencer(address(this), true);
bytes memory batchHeader0 = new bytes(89);
@@ -213,6 +216,7 @@ contract ScrollChainTest is DSTestPlus {
function testCommitAndFinalizeWithL1Messages() public {
rollup.updateSequencer(address(this), true);
rollup.updateProver(address(this), true);
// import 300 L1 messages
for (uint256 i = 0; i < 300; i++) {
@@ -514,6 +518,27 @@ contract ScrollChainTest is DSTestPlus {
assertBoolEq(rollup.isSequencer(_sequencer), false);
}
function testUpdateProver(address _prover) public {
// set by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("Ownable: caller is not the owner");
rollup.updateProver(_prover, true);
hevm.stopPrank();
// change to random operator
hevm.expectEmit(true, false, false, true);
emit UpdateProver(_prover, true);
assertBoolEq(rollup.isProver(_prover), false);
rollup.updateProver(_prover, true);
assertBoolEq(rollup.isProver(_prover), true);
hevm.expectEmit(true, false, false, true);
emit UpdateProver(_prover, false);
rollup.updateProver(_prover, false);
assertBoolEq(rollup.isProver(_prover), false);
}
function testUpdateVerifier(address _newVerifier) public {
// set by non-owner, should revert
hevm.startPrank(address(1));
@@ -530,6 +555,22 @@ contract ScrollChainTest is DSTestPlus {
assertEq(rollup.verifier(), _newVerifier);
}
function testUpdateMaxNumL2TxInChunk(uint256 _maxNumL2TxInChunk) public {
// set by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("Ownable: caller is not the owner");
rollup.updateMaxNumL2TxInChunk(_maxNumL2TxInChunk);
hevm.stopPrank();
// change to random operator
hevm.expectEmit(false, false, false, true);
emit UpdateMaxNumL2TxInChunk(100, _maxNumL2TxInChunk);
assertEq(rollup.maxNumL2TxInChunk(), 100);
rollup.updateMaxNumL2TxInChunk(_maxNumL2TxInChunk);
assertEq(rollup.maxNumL2TxInChunk(), _maxNumL2TxInChunk);
}
function testImportGenesisBlock() public {
bytes memory batchHeader;