Skip to content

Commit 165fbab

Browse files
committed
fix unit tests from ScrollChain
1 parent 1ed1d64 commit 165fbab

File tree

5 files changed

+751
-274
lines changed

5 files changed

+751
-274
lines changed

hardhat-test/ZkEvmVerifierV2.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ describe("ZkEvmVerifierV2", async () => {
9999
layer2ChainId,
100100
deployer.address,
101101
verifier.getAddress(),
102-
verifier.getAddress()
102+
verifier.getAddress(),
103+
0
103104
);
104105
await admin.upgrade(chainProxy.getAddress(), chainImpl.getAddress());
105106

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

src/L1/rollup/ScrollChain.sol

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
124124
/// @dev Thrown when update size for finalized batch.
125125
error ErrorUseFinalizedBatch();
126126

127-
/// @dev Thrown when batch index is smaller than previous `BundleSizeStruct.batchIndex`.
128-
error ErrorBatchIndexSmallerThanPreviousOne();
129-
130127
/// @dev Thrown when batch index delta is not multiple of previous `BundleSizeStruct.bundleSize`.
131128
error ErrorBatchIndexDeltaNotMultipleOfBundleSize();
132129

@@ -159,7 +156,7 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
159156

160157
/// @notice The duration to delay proof when we only has one proof type.
161158
/// @dev This is enabled after Euclid upgrade.
162-
uint256 public emergencyFinalizationDelay;
159+
uint256 public immutable emergencyFinalizationDelay;
163160

164161
/*********
165162
* Enums *
@@ -900,7 +897,8 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
900897
if (last.batchIndex > cachedLastTeeVerifiedBatchIndex && last.batchIndex > cachedLastZkpVerifiedBatchIndex) {
901898
// last is future batch index, we override the last one
902899
BundleSizeStruct memory prev = bundleSize[index - 1];
903-
if (batchIndex <= prev.batchIndex) revert ErrorBatchIndexSmallerThanPreviousOne();
900+
// since prev.batchIndex <= max(lastTeeVerifiedBatchIndex, lastZkpVerifiedBatchIndex)
901+
// we always have batchIndex > prev.batchIndex
904902
if ((batchIndex - prev.batchIndex) % prev.bundleSize != 0) {
905903
revert ErrorBatchIndexDeltaNotMultipleOfBundleSize();
906904
}
@@ -909,7 +907,9 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
909907
bundleSize[index] = last;
910908
} else {
911909
// last is past batch index, we append a new one
912-
if (batchIndex <= last.batchIndex) revert ErrorBatchIndexSmallerThanPreviousOne();
910+
// since batchIndex > max(lastTeeVerifiedBatchIndex, lastZkpVerifiedBatchIndex) and
911+
// last.batchIndex <= max(lastTeeVerifiedBatchIndex, lastZkpVerifiedBatchIndex)
912+
// we always have batchIndex > last.batchIndex
913913
if ((batchIndex - last.batchIndex) % last.bundleSize != 0) {
914914
revert ErrorBatchIndexDeltaNotMultipleOfBundleSize();
915915
}
@@ -1054,28 +1054,50 @@ contract ScrollChain is OwnableUpgradeable, PausableUpgradeable, IScrollChain {
10541054
bytes32 _withdrawRoot,
10551055
uint256 _totalL1MessagesPoppedOverall
10561056
) internal {
1057-
uint256 counterpartVerifiedBatchIndex = _proofType == ProofType.ZkProof
1058-
? lastTeeVerifiedBatchIndex
1059-
: lastZkpVerifiedBatchIndex;
1060-
if (_batchIndex <= counterpartVerifiedBatchIndex) {
1061-
// The zk proof is behind tee proof, we compare state roots here
1062-
if (_postStateRoot != finalizedStateRoots[_batchIndex] || withdrawRoots[_batchIndex] != _withdrawRoot) {
1063-
unresolvedState.proofType = _proofType;
1064-
unresolvedState.batchIndex = uint248(_batchIndex);
1065-
unresolvedState.stateRoot = _postStateRoot;
1066-
unresolvedState.withdrawRoot = _withdrawRoot;
1067-
emit StateMismatch(_batchIndex, _postStateRoot, _withdrawRoot);
1057+
bool counterpartProofEnabled;
1058+
{
1059+
uint256 mask = enabledProofTypeMask;
1060+
mask ^= 1 << uint256(uint8(_proofType));
1061+
counterpartProofEnabled = mask > 0;
1062+
}
1063+
bool overrideStateRoot = false;
1064+
bool finalizeBundle = false;
1065+
if (counterpartProofEnabled) {
1066+
uint256 counterpartVerifiedBatchIndex = _proofType == ProofType.ZkProof
1067+
? lastTeeVerifiedBatchIndex
1068+
: lastZkpVerifiedBatchIndex;
1069+
if (_batchIndex <= counterpartVerifiedBatchIndex) {
1070+
// The current proof is behind counterpart proof, we compare state roots here
1071+
if (_postStateRoot != finalizedStateRoots[_batchIndex] || withdrawRoots[_batchIndex] != _withdrawRoot) {
1072+
unresolvedState.proofType = _proofType;
1073+
unresolvedState.batchIndex = uint248(_batchIndex);
1074+
unresolvedState.stateRoot = _postStateRoot;
1075+
unresolvedState.withdrawRoot = _withdrawRoot;
1076+
emit StateMismatch(_batchIndex, _postStateRoot, _withdrawRoot);
1077+
} else {
1078+
finalizeBundle = true;
1079+
}
10681080
} else {
1069-
// state roots matched, mark bundle finalized.
1070-
_finalizePoppedL1Messages(_totalL1MessagesPoppedOverall);
1071-
emit FinalizeBatch(_batchIndex, _batchHash, _postStateRoot, _withdrawRoot);
1081+
overrideStateRoot = true;
10721082
}
10731083
} else {
1074-
// The current proof is ahead counterpart proof, we record state roots here.
1075-
// And we do not store intermediate finalized roots.
1084+
overrideStateRoot = true;
1085+
finalizeBundle = true;
1086+
}
1087+
// override state root, when
1088+
// 1. we only has this type proof enabled; or
1089+
// 2. current proof ahead counterpart proof.
1090+
if (overrideStateRoot) {
10761091
finalizedStateRoots[_batchIndex] = _postStateRoot;
10771092
withdrawRoots[_batchIndex] = _withdrawRoot;
10781093
}
1094+
// finalize bundle, when
1095+
// 1. we only has this type proof enabled; or
1096+
// 2. current proof behind counterpart proof and state root matches.
1097+
if (finalizeBundle) {
1098+
_finalizePoppedL1Messages(_totalL1MessagesPoppedOverall);
1099+
emit FinalizeBatch(_batchIndex, _batchHash, _postStateRoot, _withdrawRoot);
1100+
}
10791101
}
10801102

10811103
/* This function will never be used since we already upgrade to Darwin. We comment out the codes for reference.

src/mocks/ScrollChainMockBlob.sol

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ contract ScrollChainMockBlob is ScrollChain {
2424
uint64 _chainId,
2525
address _messageQueue,
2626
address _verifier,
27-
address _sgxVerifier
28-
) ScrollChain(_chainId, _messageQueue, _verifier, _sgxVerifier, 0) {}
27+
address _sgxVerifier,
28+
uint256 _delay
29+
) ScrollChain(_chainId, _messageQueue, _verifier, _sgxVerifier, _delay) {}
2930

3031
/**********************
3132
* Internal Functions *
@@ -39,6 +40,10 @@ contract ScrollChainMockBlob is ScrollChain {
3940
lastZkpVerifiedBatchIndex = index;
4041
}
4142

43+
function setLastTeeVerifiedBatchIndex(uint256 index) external {
44+
lastTeeVerifiedBatchIndex = index;
45+
}
46+
4247
function setFinalizedStateRoots(uint256 index, bytes32 value) external {
4348
finalizedStateRoots[index] = value;
4449
}
@@ -51,6 +56,14 @@ contract ScrollChainMockBlob is ScrollChain {
5156
overrideBatchHashCheck = status;
5257
}
5358

59+
function setEnabledProofTypeMask(uint256 mask) external {
60+
enabledProofTypeMask = mask;
61+
}
62+
63+
function setBatchCommittedTimestamp(uint256 index, uint256 timestamp) external {
64+
batchCommittedTimestamp[index] = timestamp;
65+
}
66+
5467
function _getBlobVersionedHash() internal virtual override returns (bytes32 _blobVersionedHash) {
5568
_blobVersionedHash = blobVersionedHash;
5669
}

src/test/L1GatewayTestBase.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ abstract contract L1GatewayTestBase is ScrollTestBase {
126126
// Upgrade the ScrollChain implementation and initialize
127127
admin.upgrade(
128128
ITransparentUpgradeableProxy(address(rollup)),
129-
address(new ScrollChainMockBlob(1233, address(messageQueue), address(zkpVerifier), address(teeVerifier)))
129+
address(new ScrollChainMockBlob(1233, address(messageQueue), address(zkpVerifier), address(teeVerifier), 0))
130130
);
131131
rollup.initialize(address(messageQueue), address(0), 44);
132132
rollup.initializeV2(1);

0 commit comments

Comments
 (0)