Skip to content

Commit

Permalink
refactor: nuking l1 to l2 messages from block body
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Mar 18, 2024
1 parent 5ab07fb commit 7bde758
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 186 deletions.
49 changes: 24 additions & 25 deletions l1-contracts/slither_output.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ src/core/libraries/HeaderLib.sol#L148


- [ ] ID-2
[TxsDecoder.decode(bytes).vars](src/core/libraries/decoders/TxsDecoder.sol#L79) is a local variable never initialized
[TxsDecoder.decode(bytes).vars](src/core/libraries/decoders/TxsDecoder.sol#L77) is a local variable never initialized

src/core/libraries/decoders/TxsDecoder.sol#L79
src/core/libraries/decoders/TxsDecoder.sol#L77


## unused-return
Expand All @@ -50,10 +50,10 @@ src/core/Rollup.sol#L57-L96
Impact: Medium
Confidence: High
- [ ] ID-4
Dubious typecast in [TxsDecoder.read1(bytes,uint256)](src/core/libraries/decoders/TxsDecoder.sol#L314-L316):
bytes => bytes1 casting occurs in [uint256(uint8(bytes1(slice(_data,_offset,1))))](src/core/libraries/decoders/TxsDecoder.sol#L315)
Dubious typecast in [MessagesDecoder.read4(bytes,uint256)](src/core/libraries/decoders/MessagesDecoder.sol#L133-L135):
bytes => bytes4 casting occurs in [uint256(uint32(bytes4(_data)))](src/core/libraries/decoders/MessagesDecoder.sol#L134)

src/core/libraries/decoders/TxsDecoder.sol#L314-L316
src/core/libraries/decoders/MessagesDecoder.sol#L133-L135


- [ ] ID-5
Expand All @@ -64,20 +64,27 @@ src/core/messagebridge/Outbox.sol#L38-L46


- [ ] ID-6
Dubious typecast in [TxsDecoder.read4(bytes,uint256)](src/core/libraries/decoders/TxsDecoder.sol#L324-L326):
bytes => bytes4 casting occurs in [uint256(uint32(bytes4(slice(_data,_offset,4))))](src/core/libraries/decoders/TxsDecoder.sol#L325)
Dubious typecast in [MessagesDecoder.read1(bytes,uint256)](src/core/libraries/decoders/MessagesDecoder.sol#L123-L125):
bytes => bytes1 casting occurs in [uint256(uint8(bytes1(_data)))](src/core/libraries/decoders/MessagesDecoder.sol#L124)

src/core/libraries/decoders/TxsDecoder.sol#L324-L326
src/core/libraries/decoders/MessagesDecoder.sol#L123-L125


- [ ] ID-7
Dubious typecast in [MessagesDecoder.read4(bytes,uint256)](src/core/libraries/decoders/MessagesDecoder.sol#L160-L162):
bytes => bytes4 casting occurs in [uint256(uint32(bytes4(_data)))](src/core/libraries/decoders/MessagesDecoder.sol#L161)
Dubious typecast in [TxsDecoder.read4(bytes,uint256)](src/core/libraries/decoders/TxsDecoder.sol#L317-L319):
bytes => bytes4 casting occurs in [uint256(uint32(bytes4(slice(_data,_offset,4))))](src/core/libraries/decoders/TxsDecoder.sol#L318)

src/core/libraries/decoders/MessagesDecoder.sol#L160-L162
src/core/libraries/decoders/TxsDecoder.sol#L317-L319


- [ ] ID-8
Dubious typecast in [TxsDecoder.read1(bytes,uint256)](src/core/libraries/decoders/TxsDecoder.sol#L307-L309):
bytes => bytes1 casting occurs in [uint256(uint8(bytes1(slice(_data,_offset,1))))](src/core/libraries/decoders/TxsDecoder.sol#L308)

src/core/libraries/decoders/TxsDecoder.sol#L307-L309


- [ ] ID-9
Dubious typecast in [HeaderLib.decode(bytes)](src/core/libraries/HeaderLib.sol#L143-L184):
bytes => bytes32 casting occurs in [header.lastArchive = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L151-L153)
bytes => bytes4 casting occurs in [header.lastArchive = AppendOnlyTreeSnapshot(bytes32(_header),uint32(bytes4(_header)))](src/core/libraries/HeaderLib.sol#L151-L153)
Expand All @@ -103,13 +110,6 @@ Dubious typecast in [HeaderLib.decode(bytes)](src/core/libraries/HeaderLib.sol#L
src/core/libraries/HeaderLib.sol#L143-L184


- [ ] ID-9
Dubious typecast in [MessagesDecoder.read1(bytes,uint256)](src/core/libraries/decoders/MessagesDecoder.sol#L150-L152):
bytes => bytes1 casting occurs in [uint256(uint8(bytes1(_data)))](src/core/libraries/decoders/MessagesDecoder.sol#L151)

src/core/libraries/decoders/MessagesDecoder.sol#L150-L152


## missing-zero-check
Impact: Low
Confidence: Medium
Expand Down Expand Up @@ -213,18 +213,17 @@ src/core/messagebridge/NewOutbox.sol#L18-L132
Impact: Informational
Confidence: High
- [ ] ID-21
[MessagesDecoder.decode(bytes)](src/core/libraries/decoders/MessagesDecoder.sol#L60-L142) uses assembly
- [INLINE ASM](src/core/libraries/decoders/MessagesDecoder.sol#L79-L81)
- [INLINE ASM](src/core/libraries/decoders/MessagesDecoder.sol#L112-L118)
[TxsDecoder.computeRoot(bytes32[])](src/core/libraries/decoders/TxsDecoder.sol#L249-L268) uses assembly
- [INLINE ASM](src/core/libraries/decoders/TxsDecoder.sol#L256-L258)

src/core/libraries/decoders/MessagesDecoder.sol#L60-L142
src/core/libraries/decoders/TxsDecoder.sol#L249-L268


- [ ] ID-22
[TxsDecoder.computeRoot(bytes32[])](src/core/libraries/decoders/TxsDecoder.sol#L256-L275) uses assembly
- [INLINE ASM](src/core/libraries/decoders/TxsDecoder.sol#L263-L265)
[MessagesDecoder.decode(bytes)](src/core/libraries/decoders/MessagesDecoder.sol#L53-L115) uses assembly
- [INLINE ASM](src/core/libraries/decoders/MessagesDecoder.sol#L86-L92)

src/core/libraries/decoders/TxsDecoder.sol#L256-L275
src/core/libraries/decoders/MessagesDecoder.sol#L53-L115


## dead-code
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ contract Rollup is IRollup {
}

// Decode the cross-chain messages (Will be removed as part of message model change)
(,,, bytes32[] memory l2ToL1Msgs) = MessagesDecoder.decode(_body);
(, bytes32[] memory l2ToL1Msgs) = MessagesDecoder.decode(_body);

bytes32[] memory publicInputs = new bytes32[](1);
publicInputs[0] = _computePublicInputHash(_header, _archive);
Expand Down
81 changes: 27 additions & 54 deletions l1-contracts/src/core/libraries/decoders/MessagesDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,44 @@ import {Hash} from "../Hash.sol";
* -------------------
* L2 Body Data Specification
* -------------------
* -------------------
* L2 Body Data Specification
* -------------------
* | byte start | num bytes | name
* | --- | --- | ---
* | 0x0 | 0x4 | len(newL1ToL2Msgs) (denoted a)
* | 0x4 | a * 0x20 | newL1ToL2Msgs
* | 0x4 + a * 0x20 = tx0Start | 0x4 | len(numTxs) (denoted t)
* | | | TxEffect 0 {
* | tx0Start | 0x1 | len(newNoteHashes) (denoted b)
* | tx0Start + 0x1 | b * 0x20 | newNoteHashes
* | tx0Start + 0x1 + b * 0x20 | 0x1 | len(newNullifiers) (denoted c)
* | tx0Start + 0x1 + b * 0x20 + 0x1 | c * 0x20 | newNullifiers
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(newL2ToL1Msgs) (denoted d)
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | newL2ToL1Msgs
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(newPublicDataWrites) (denoted e)
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | newPublicDataWrites
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(newEncryptedLogs) (denoted f)
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | newEncryptedLogs
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(newUnencryptedLogs) (denoted g)
* | tx0Start + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | newUnencryptedLogs
* | | | },
* | | | TxEffect 1 {
* | | | ...
* | | | },
* | | | ...
* | | | TxEffect (t - 1) {
* | | | ...
* | | | },
* | byte start | num bytes | name
* | --- | --- | ---
* | 0x0 | 0x4 | len(numTxs) (denoted t)
* | | | TxEffect 0 {
* | 0x4 | 0x1 | len(newNoteHashes) (denoted b)
* | 0x4 + 0x1 | b * 0x20 | newNoteHashes
* | 0x4 + 0x1 + b * 0x20 | 0x1 | len(newNullifiers) (denoted c)
* | 0x4 + 0x1 + b * 0x20 + 0x1 | c * 0x20 | newNullifiers
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 | 0x1 | len(newL2ToL1Msgs) (denoted d)
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 | d * 0x20 | newL2ToL1Msgs
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 | 0x1 | len(newPublicDataWrites) (denoted e)
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 | e * 0x40 | newPublicDataWrites
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 | 0x04 | byteLen(newEncryptedLogs) (denoted f)
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 | f | newEncryptedLogs
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f | 0x04 | byteLen(newUnencryptedLogs) (denoted g)
* | 0x4 + 0x1 + b * 0x20 + 0x1 + c * 0x20 + 0x1 + d * 0x20 + 0x01 + e * 0x40 + 0x4 + f + 0x4 | g | newUnencryptedLogs
* | | | },
* | | | TxEffect 1 {
* | | | ...
* | | | },
* | | | ...
* | | | TxEffect (t - 1) {
* | | | ...
* | | | },
*/
library MessagesDecoder {
/**
* @notice Computes consumables for the block
* @param _body - The L2 block calldata.
* @return inHash - The hash of the L1 to L2 messages
* @return outHash - The hash of the L1 to L2 messages
* @return l1ToL2Msgs - The L1 to L2 messages of the block
* @return l2ToL1Msgs - The L2 to L1 messages of the block
*/
function decode(bytes calldata _body)
internal
pure
returns (
bytes32 inHash,
bytes32 outHash,
bytes32[] memory l1ToL2Msgs,
bytes32[] memory l2ToL1Msgs
)
returns (bytes32 outHash, bytes32[] memory l2ToL1Msgs)
{
l1ToL2Msgs = new bytes32[](Constants.NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP);

uint256 offset = 0;
// L1 to L2 messages
uint256 count = read4(_body, offset);
offset += 0x4;

// `l1ToL2Msgs` is fixed size so if `lengths.l1Tol2MsgsCount` < `Constants.NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP` the array
// will contain some zero values.
assembly {
calldatacopy(add(l1ToL2Msgs, 0x20), add(_body.offset, offset), mul(count, 0x20))
}

offset += count * 0x20;

uint256 numTxs = read4(_body, offset);
offset += 0x4;

Expand All @@ -90,7 +64,7 @@ library MessagesDecoder {
// Now we iterate over the tx effects
for (uint256 i = 0; i < numTxs; i++) {
// Note hashes
count = read1(_body, offset);
uint256 count = read1(_body, offset);
offset += 0x1;
offset += count * 0x20; // each note hash is 0x20 bytes long

Expand Down Expand Up @@ -135,10 +109,9 @@ library MessagesDecoder {
offset += 0x4 + length;
}

inHash = sha256(abi.encodePacked(l1ToL2Msgs));
outHash = sha256(abi.encodePacked(l2ToL1Msgs));

return (inHash, outHash, l1ToL2Msgs, l2ToL1Msgs);
return (outHash, l2ToL1Msgs);
}

/**
Expand Down
Loading

0 comments on commit 7bde758

Please sign in to comment.