Skip to content

Commit 49f2cc4

Browse files
committed
🪵 Introduce L2LogLocator for word-packing zkSync log indices
1 parent 86a7159 commit 49f2cc4

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

zksync/IZkSync.sol

+2-21
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,13 @@
22

33
pragma solidity ^0.8.0;
44

5+
import {L2Log} from "./L2Log.sol";
6+
57
enum TxStatus {
68
Failure,
79
Success
810
}
911

10-
/**
11-
* @dev The log passed from L2
12-
* @param l2ShardId The shard identifier, 0 - rollup, 1 - porter. All other values are not used but are reserved for
13-
* the future
14-
* @param isService A boolean flag that is part of the log along with `key`, `value`, and `sender` address.
15-
* This field is required formally but does not have any special meaning.
16-
* @param txNumberInBatch The L2 transaction number in the batch, in which the log was sent
17-
* @param sender The L2 address which sent the log
18-
* @param key The 32 bytes of information that was sent in the log
19-
* @param value The 32 bytes of information that was sent in the log
20-
* Both `key` and `value` are arbitrary 32-bytes selected by the log sender
21-
*/
22-
struct L2Log {
23-
uint8 l2ShardId;
24-
bool isService;
25-
uint16 txNumberInBatch;
26-
address sender;
27-
bytes32 key;
28-
bytes32 value;
29-
}
30-
3112
uint160 constant ALIAS_OFFSET = uint160(0x1111000000000000000000000000000000001111);
3213

3314
/**

zksync/L2Log.sol

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
/**
6+
* @dev The log passed from L2
7+
* @param l2ShardId The shard identifier, 0 - rollup, 1 - porter. All other values are not used but are reserved for
8+
* the future
9+
* @param isService A boolean flag that is part of the log along with `key`, `value`, and `sender` address.
10+
* This field is required formally but does not have any special meaning.
11+
* @param txNumberInBatch The L2 transaction number in the batch, in which the log was sent
12+
* @param sender The L2 address which sent the log
13+
* @param key The 32 bytes of information that was sent in the log
14+
* @param value The 32 bytes of information that was sent in the log
15+
* Both `key` and `value` are arbitrary 32-bytes selected by the log sender
16+
*/
17+
struct L2Log {
18+
uint8 l2ShardId;
19+
bool isService;
20+
uint16 txNumberInBatch;
21+
address sender;
22+
bytes32 key;
23+
bytes32 value;
24+
}
25+
26+
type L2LogLocator is uint256;
27+
28+
function L2LogLocatorFrom(uint256 _batchNumber, uint256 _messageIndex, uint256 _txNumber) pure returns (L2LogLocator) {
29+
return L2LogLocator.wrap(_batchNumber << 128 | _messageIndex << 16 | _txNumber);
30+
}
31+
32+
function batchNumber(L2LogLocator self) pure returns (uint256) {
33+
return L2LogLocator.unwrap(self) >> 128;
34+
}
35+
36+
function messageIndex(L2LogLocator self) pure returns (uint256) {
37+
return uint112(L2LogLocator.unwrap(self) >> 16);
38+
}
39+
40+
function txNumber(L2LogLocator self) pure returns (uint256) {
41+
return uint16(L2LogLocator.unwrap(self));
42+
}
43+
44+
using {batchNumber, txNumber, messageIndex} for L2LogLocator global;

zksync/L2Log.t.sol

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
import {L2LogLocator, L2LogLocatorFrom} from "./L2Log.sol";
6+
import {Test} from "forge-std/Test.sol";
7+
8+
contract L2LogLocatorTest is Test {
9+
function testAccessors() public pure {
10+
L2LogLocator l = L2LogLocatorFrom(1, 2, 3);
11+
assertEq(l.batchNumber(), 1);
12+
assertEq(l.messageIndex(), 2);
13+
assertEq(l.txNumber(), 3);
14+
15+
L2LogLocator k = L2LogLocatorFrom(type(uint128).max, type(uint112).max, type(uint16).max);
16+
assertEq(k.batchNumber(), type(uint128).max);
17+
assertEq(k.messageIndex(), type(uint112).max);
18+
assertEq(k.txNumber(), type(uint16).max);
19+
20+
L2LogLocator m = L2LogLocatorFrom(type(uint128).max - 1, type(uint112).max - 1, type(uint16).max - 1);
21+
assertEq(m.batchNumber(), type(uint128).max - 1);
22+
assertEq(m.messageIndex(), type(uint112).max - 1);
23+
assertEq(m.txNumber(), type(uint16).max - 1);
24+
}
25+
}

0 commit comments

Comments
 (0)