Skip to content

Commit

Permalink
Merge pull request #108 from zkLinkProtocol/issue_107
Browse files Browse the repository at this point in the history
replace preCommitBlockNumber with blockSequence
  • Loading branch information
zkbenny authored Dec 2, 2023
2 parents 07970b0 + cce77b1 commit 1217d7a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion contracts/Storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ contract Storage is ZkLinkAcceptor, Config {
/// @notice block stored data
struct StoredBlockInfo {
uint32 blockNumber; // Rollup block number
uint32 preCommittedBlockNumber; // The pre not empty block number committed
uint32 blockSequence; // The block commit sequence
uint64 priorityOperations; // Number of priority operations processed
bytes32 pendingOnchainOperationsHash; // Hash of all operations that must be processed after verify
bytes32 syncHash; // Used for cross chain block verify
Expand Down
14 changes: 6 additions & 8 deletions contracts/ZkLink.sol
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ contract ZkLink is ReentrancyGuard, Storage, Events, UpgradeableMaster {

return StoredBlockInfo(
_newBlock.blockNumber,
_previousBlock.blockNumber,
_previousBlock.blockSequence + 1,
priorityReqCommitted,
pendingOnchainOpsHash,
syncHash
Expand Down Expand Up @@ -603,25 +603,23 @@ contract ZkLink is ReentrancyGuard, Storage, Events, UpgradeableMaster {
/// @notice Execute blocks, completing priority operations and processing withdrawals.
/// @dev 1. Processes all pending operations (Send Exits, Complete priority requests)
/// 2. Finalizes block on Ethereum
function executeCompressedBlocks(StoredBlockInfo memory _latestExecutedBlockData, ExecuteBlockInfo[] memory _blocksData) external active onlyValidator nonReentrant {
function executeCompressedBlocks(ExecuteBlockInfo[] memory _blocksData) external active onlyValidator nonReentrant {
uint32 nBlocks = uint32(_blocksData.length);
require(nBlocks > 0, "d0");

uint32 _totalBlocksExecuted = totalBlocksExecuted;
require(storedBlockHashes[_totalBlocksExecuted] == hashStoredBlockInfo(_latestExecutedBlockData), "d1");
uint32 latestExecutedBlockNumber = _blocksData[nBlocks - 1].storedBlock.blockNumber;
require(latestExecutedBlockNumber <= totalBlocksSynchronized, "d1");

uint32 _totalBlocksExecuted = totalBlocksExecuted;
uint64 priorityRequestsExecuted = 0;
uint32 latestExecutedBlockNumber = _latestExecutedBlockData.blockNumber;
for (uint32 i = 0; i < nBlocks; ++i) {
uint32 _executedBlockIdx = _totalBlocksExecuted + i + 1;
ExecuteBlockInfo memory _blockExecuteData = _blocksData[i];
require(_blockExecuteData.storedBlock.preCommittedBlockNumber == latestExecutedBlockNumber, "d2");
require(_blockExecuteData.storedBlock.blockSequence == _executedBlockIdx, "d2");

executeOneBlock(_blockExecuteData, _executedBlockIdx);
priorityRequestsExecuted = priorityRequestsExecuted + _blockExecuteData.storedBlock.priorityOperations;
latestExecutedBlockNumber = _blockExecuteData.storedBlock.blockNumber;
}
require(latestExecutedBlockNumber <= totalBlocksSynchronized, "d3");

firstPriorityRequestId = firstPriorityRequestId + priorityRequestsExecuted;
totalCommittedPriorityRequests = totalCommittedPriorityRequests - priorityRequestsExecuted;
Expand Down
12 changes: 6 additions & 6 deletions contracts/ZkLinkPeriphery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -309,26 +309,26 @@ contract ZkLinkPeriphery is ReentrancyGuard, Storage, Events {
// #endif

// #if CHAIN_ID != MASTER_CHAIN_ID
function revertBlocks(StoredBlockInfo[] memory _blocksToRevert) external onlyValidator nonReentrant {
function revertBlocks(StoredBlockInfo memory _latestCommittedBlock, StoredBlockInfo[] memory _blocksToRevert) external onlyValidator nonReentrant {
uint32 blocksCommitted = totalBlocksCommitted;
uint32 blocksToRevert = Utils.minU32(SafeCast.toUint32(_blocksToRevert.length), blocksCommitted - totalBlocksExecuted);
uint64 revertedPriorityRequests = 0;

uint32 latestSynchronizedBlockNumber = totalBlocksSynchronized;
for (uint32 i = 0; i < blocksToRevert; ++i) {
StoredBlockInfo memory storedBlockInfo = _blocksToRevert[i];
require(storedBlockHashes[blocksCommitted] == hashStoredBlockInfo(storedBlockInfo), "c"); // incorrect stored block info
latestSynchronizedBlockNumber = storedBlockInfo.preCommittedBlockNumber;

delete storedBlockHashes[blocksCommitted];

--blocksCommitted;
revertedPriorityRequests = revertedPriorityRequests + storedBlockInfo.priorityOperations;
}
require(storedBlockHashes[blocksCommitted] == hashStoredBlockInfo(_latestCommittedBlock), "c1");

totalBlocksCommitted = blocksCommitted;
totalCommittedPriorityRequests = totalCommittedPriorityRequests - revertedPriorityRequests;
totalBlocksSynchronized = latestSynchronizedBlockNumber;
if (_latestCommittedBlock.blockNumber < totalBlocksSynchronized) {
totalBlocksSynchronized = _latestCommittedBlock.blockNumber;
}

emit BlocksRevert(totalBlocksExecuted, blocksCommitted);
}
Expand Down Expand Up @@ -378,7 +378,7 @@ contract ZkLinkPeriphery is ReentrancyGuard, Storage, Events {
/// @notice Send sync hash to master chain
function sendSyncHash(StoredBlockInfo memory _block) external onlyValidator payable {
require(_block.blockNumber > totalBlocksSynchronized, "j0");
require(hashStoredBlockInfo(_block) == storedBlockHashes[_block.blockNumber], "j1");
require(hashStoredBlockInfo(_block) == storedBlockHashes[_block.blockSequence], "j1");
syncService.sendSyncHash{value:msg.value}(_block.syncHash);
emit SendSyncHash(_block.syncHash);
}
Expand Down
4 changes: 2 additions & 2 deletions test/compressed_block_commit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Compressed block commit unit tests', function () {
describe('Commit one block', function () {
const preBlock = {
blockNumber:10,
preCommittedBlockNumber: 9,
blockSequence: 1,
priorityOperations:0,
pendingOnchainOperationsHash:"0x0000000000000000000000000000000000000000000000000000000000000001",
syncHash:"0x0100000000000000000000000000000000000000000000000000000000000000"
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('Compressed block commit unit tests', function () {
const r = await zkLink.testCommitOneBlock(preBlock, commitBlock);
const syncHash = hexlify(createSlaverChainSyncHash(preBlock.syncHash, commitBlock.blockNumber, commitBlock.newStateHash, expected.onchainOperationPubdataHash));
expect(r.blockNumber).to.eql(commitBlock.blockNumber);
expect(r.preCommittedBlockNumber).to.eql(preBlock.blockNumber);
expect(r.blockSequence).to.eql(preBlock.blockSequence + 1);
expect(r.priorityOperations).to.eql(BigNumber.from(expected.priorityOperationsProcessed));
expect(r.pendingOnchainOperationsHash).to.eql(expected.processableOpPubdataHash);
expect(r.syncHash).to.eql(syncHash);
Expand Down

0 comments on commit 1217d7a

Please sign in to comment.