Skip to content

Commit

Permalink
fix unit tests from ScrollChain
Browse files Browse the repository at this point in the history
  • Loading branch information
zimpha committed Nov 20, 2024
1 parent 1ed1d64 commit 165fbab
Show file tree
Hide file tree
Showing 5 changed files with 751 additions and 274 deletions.
4 changes: 3 additions & 1 deletion hardhat-test/ZkEvmVerifierV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ describe("ZkEvmVerifierV2", async () => {
layer2ChainId,
deployer.address,
verifier.getAddress(),
verifier.getAddress()
verifier.getAddress(),
0
);
await admin.upgrade(chainProxy.getAddress(), chainImpl.getAddress());

chain = await ethers.getContractAt("ScrollChainMockBlob", await chainProxy.getAddress(), deployer);
await chain.initialize(deployer.address, deployer.address, 100);
await chain.initializeV2(Number(BigInt(hexlify(publicInputs.subarray(8, 12)))));
await chain.addProver(deployer.address);
});

Expand Down
66 changes: 44 additions & 22 deletions src/L1/rollup/ScrollChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
/// @dev Thrown when update size for finalized batch.
error ErrorUseFinalizedBatch();

/// @dev Thrown when batch index is smaller than previous `BundleSizeStruct.batchIndex`.
error ErrorBatchIndexSmallerThanPreviousOne();

/// @dev Thrown when batch index delta is not multiple of previous `BundleSizeStruct.bundleSize`.
error ErrorBatchIndexDeltaNotMultipleOfBundleSize();

Expand Down Expand Up @@ -159,7 +156,7 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {

/// @notice The duration to delay proof when we only has one proof type.
/// @dev This is enabled after Euclid upgrade.
uint256 public emergencyFinalizationDelay;
uint256 public immutable emergencyFinalizationDelay;

/*********
* Enums *
Expand Down Expand Up @@ -900,7 +897,8 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
if (last.batchIndex > cachedLastTeeVerifiedBatchIndex && last.batchIndex > cachedLastZkpVerifiedBatchIndex) {
// last is future batch index, we override the last one
BundleSizeStruct memory prev = bundleSize[index - 1];
if (batchIndex <= prev.batchIndex) revert ErrorBatchIndexSmallerThanPreviousOne();
// since prev.batchIndex <= max(lastTeeVerifiedBatchIndex, lastZkpVerifiedBatchIndex)
// we always have batchIndex > prev.batchIndex
if ((batchIndex - prev.batchIndex) % prev.bundleSize != 0) {
revert ErrorBatchIndexDeltaNotMultipleOfBundleSize();
}
Expand All @@ -909,7 +907,9 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
bundleSize[index] = last;
} else {
// last is past batch index, we append a new one
if (batchIndex <= last.batchIndex) revert ErrorBatchIndexSmallerThanPreviousOne();
// since batchIndex > max(lastTeeVerifiedBatchIndex, lastZkpVerifiedBatchIndex) and
// last.batchIndex <= max(lastTeeVerifiedBatchIndex, lastZkpVerifiedBatchIndex)
// we always have batchIndex > last.batchIndex
if ((batchIndex - last.batchIndex) % last.bundleSize != 0) {
revert ErrorBatchIndexDeltaNotMultipleOfBundleSize();
}
Expand Down Expand Up @@ -1054,28 +1054,50 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
bytes32 _withdrawRoot,
uint256 _totalL1MessagesPoppedOverall
) internal {
uint256 counterpartVerifiedBatchIndex = _proofType == ProofType.ZkProof
? lastTeeVerifiedBatchIndex
: lastZkpVerifiedBatchIndex;
if (_batchIndex <= counterpartVerifiedBatchIndex) {
// The zk proof is behind tee proof, we compare state roots here
if (_postStateRoot != finalizedStateRoots[_batchIndex] || withdrawRoots[_batchIndex] != _withdrawRoot) {
unresolvedState.proofType = _proofType;
unresolvedState.batchIndex = uint248(_batchIndex);
unresolvedState.stateRoot = _postStateRoot;
unresolvedState.withdrawRoot = _withdrawRoot;
emit StateMismatch(_batchIndex, _postStateRoot, _withdrawRoot);
bool counterpartProofEnabled;
{
uint256 mask = enabledProofTypeMask;
mask ^= 1 << uint256(uint8(_proofType));
counterpartProofEnabled = mask > 0;
}
bool overrideStateRoot = false;
bool finalizeBundle = false;
if (counterpartProofEnabled) {
uint256 counterpartVerifiedBatchIndex = _proofType == ProofType.ZkProof
? lastTeeVerifiedBatchIndex
: lastZkpVerifiedBatchIndex;
if (_batchIndex <= counterpartVerifiedBatchIndex) {
// The current proof is behind counterpart proof, we compare state roots here
if (_postStateRoot != finalizedStateRoots[_batchIndex] || withdrawRoots[_batchIndex] != _withdrawRoot) {
unresolvedState.proofType = _proofType;
unresolvedState.batchIndex = uint248(_batchIndex);
unresolvedState.stateRoot = _postStateRoot;
unresolvedState.withdrawRoot = _withdrawRoot;
emit StateMismatch(_batchIndex, _postStateRoot, _withdrawRoot);
} else {
finalizeBundle = true;
}
} else {
// state roots matched, mark bundle finalized.
_finalizePoppedL1Messages(_totalL1MessagesPoppedOverall);
emit FinalizeBatch(_batchIndex, _batchHash, _postStateRoot, _withdrawRoot);
overrideStateRoot = true;
}
} else {
// The current proof is ahead counterpart proof, we record state roots here.
// And we do not store intermediate finalized roots.
overrideStateRoot = true;
finalizeBundle = true;
}
// override state root, when
// 1. we only has this type proof enabled; or
// 2. current proof ahead counterpart proof.
if (overrideStateRoot) {
finalizedStateRoots[_batchIndex] = _postStateRoot;
withdrawRoots[_batchIndex] = _withdrawRoot;
}
// finalize bundle, when
// 1. we only has this type proof enabled; or
// 2. current proof behind counterpart proof and state root matches.
if (finalizeBundle) {
_finalizePoppedL1Messages(_totalL1MessagesPoppedOverall);
emit FinalizeBatch(_batchIndex, _batchHash, _postStateRoot, _withdrawRoot);
}
}

/* This function will never be used since we already upgrade to Darwin. We comment out the codes for reference.
Expand Down
17 changes: 15 additions & 2 deletions src/mocks/ScrollChainMockBlob.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ contract ScrollChainMockBlob is ScrollChain {
uint64 _chainId,
address _messageQueue,
address _verifier,
address _sgxVerifier
) ScrollChain(_chainId, _messageQueue, _verifier, _sgxVerifier, 0) {}
address _sgxVerifier,
uint256 _delay
) ScrollChain(_chainId, _messageQueue, _verifier, _sgxVerifier, _delay) {}

/**********************
* Internal Functions *
Expand All @@ -39,6 +40,10 @@ contract ScrollChainMockBlob is ScrollChain {
lastZkpVerifiedBatchIndex = index;
}

function setLastTeeVerifiedBatchIndex(uint256 index) external {
lastTeeVerifiedBatchIndex = index;
}

function setFinalizedStateRoots(uint256 index, bytes32 value) external {
finalizedStateRoots[index] = value;
}
Expand All @@ -51,6 +56,14 @@ contract ScrollChainMockBlob is ScrollChain {
overrideBatchHashCheck = status;
}

function setEnabledProofTypeMask(uint256 mask) external {
enabledProofTypeMask = mask;
}

function setBatchCommittedTimestamp(uint256 index, uint256 timestamp) external {
batchCommittedTimestamp[index] = timestamp;
}

function _getBlobVersionedHash() internal virtual override returns (bytes32 _blobVersionedHash) {
_blobVersionedHash = blobVersionedHash;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/L1GatewayTestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ abstract contract L1GatewayTestBase is ScrollTestBase {
// Upgrade the ScrollChain implementation and initialize
admin.upgrade(
ITransparentUpgradeableProxy(address(rollup)),
address(new ScrollChainMockBlob(1233, address(messageQueue), address(zkpVerifier), address(teeVerifier)))
address(new ScrollChainMockBlob(1233, address(messageQueue), address(zkpVerifier), address(teeVerifier), 0))
);
rollup.initialize(address(messageQueue), address(0), 44);
rollup.initializeV2(1);
Expand Down
Loading

0 comments on commit 165fbab

Please sign in to comment.