@@ -5,6 +5,7 @@ pragma solidity ^0.8.0;
55import {RLPReader} from "../lib/RLPReader.sol " ;
66import {MerklePatriciaProof} from "../lib/MerklePatriciaProof.sol " ;
77import {Merkle} from "../lib/Merkle.sol " ;
8+ import "../lib/ExitPayloadReader.sol " ;
89
910
1011interface IFxStateSender {
@@ -28,9 +29,13 @@ contract ICheckpointManager {
2829}
2930
3031abstract contract FxBaseRootTunnel {
31- using RLPReader for bytes ;
3232 using RLPReader for RLPReader.RLPItem;
3333 using Merkle for bytes32 ;
34+ using ExitPayloadReader for bytes ;
35+ using ExitPayloadReader for ExitPayloadReader.ExitPayload;
36+ using ExitPayloadReader for ExitPayloadReader.Log;
37+ using ExitPayloadReader for ExitPayloadReader.LogTopics;
38+ using ExitPayloadReader for ExitPayloadReader.Receipt;
3439
3540 // keccak256(MessageSent(bytes))
3641 bytes32 public constant SEND_MESSAGE_EVENT_SIG = 0x8c5261668696ce22758910d05bab8f186d6eb247ceac2af2e82c7dc17669b036 ;
@@ -69,20 +74,20 @@ abstract contract FxBaseRootTunnel {
6974 }
7075
7176 function _validateAndExtractMessage (bytes memory inputData ) internal returns (bytes memory ) {
72- RLPReader.RLPItem[] memory inputDataRLPList = inputData
73- .toRlpItem ()
74- .toList ();
77+ ExitPayloadReader.ExitPayload memory payload = inputData.toExitPayload ();
7578
79+ bytes memory branchMaskBytes = payload.getBranchMaskAsBytes ();
80+ uint256 blockNumber = payload.getBlockNumber ();
7681 // checking if exit has already been processed
7782 // unique exit is identified using hash of (blockNumber, branchMask, receiptLogIndex)
7883 bytes32 exitHash = keccak256 (
7984 abi.encodePacked (
80- inputDataRLPList[ 2 ]. toUint (), // blockNumber
85+ blockNumber,
8186 // first 2 nibbles are dropped while generating nibble array
8287 // this allows branch masks that are valid but bypass exitHash check (changing first 2 nibbles only)
8388 // so converting to nibble array and then hashing it
84- MerklePatriciaProof._getNibbleArray (inputDataRLPList[ 8 ]. toBytes ()), // branchMask
85- inputDataRLPList[ 9 ]. toUint () // receiptLogIndex
89+ MerklePatriciaProof._getNibbleArray (branchMaskBytes),
90+ payload. getReceiptLogIndex ()
8691 )
8792 );
8893 require (
@@ -91,51 +96,43 @@ abstract contract FxBaseRootTunnel {
9196 );
9297 processedExits[exitHash] = true ;
9398
94- RLPReader.RLPItem[] memory receiptRLPList = inputDataRLPList[6 ]
95- .toBytes ()
96- .toRlpItem ()
97- .toList ();
98- RLPReader.RLPItem memory logRLP = receiptRLPList[3 ]
99- .toList ()[
100- inputDataRLPList[9 ].toUint () // receiptLogIndex
101- ];
102-
103- RLPReader.RLPItem[] memory logRLPList = logRLP.toList ();
104-
99+ ExitPayloadReader.Receipt memory receipt = payload.getReceipt ();
100+ ExitPayloadReader.Log memory log = receipt.getLog ();
101+
105102 // check child tunnel
106- require (fxChildTunnel == RLPReader. toAddress (logRLPList[ 0 ] ), "FxRootTunnel: INVALID_FX_CHILD_TUNNEL " );
103+ require (fxChildTunnel == log. getEmitter ( ), "FxRootTunnel: INVALID_FX_CHILD_TUNNEL " );
107104
105+ bytes32 receiptRoot = payload.getReceiptRoot ();
108106 // verify receipt inclusion
109107 require (
110108 MerklePatriciaProof.verify (
111- inputDataRLPList[ 6 ] .toBytes (), // receipt
112- inputDataRLPList[ 8 ]. toBytes (), // branchMask
113- inputDataRLPList[ 7 ]. toBytes (), // receiptProof
114- bytes32 (inputDataRLPList[ 5 ]. toUint ()) // receiptRoot
109+ receipt .toBytes (),
110+ branchMaskBytes,
111+ payload. getReceiptProof (),
112+ receiptRoot
115113 ),
116114 "FxRootTunnel: INVALID_RECEIPT_PROOF "
117115 );
118116
119117 // verify checkpoint inclusion
120118 _checkBlockMembershipInCheckpoint (
121- inputDataRLPList[ 2 ]. toUint (), // blockNumber
122- inputDataRLPList[ 3 ]. toUint (), // blockTime
123- bytes32 (inputDataRLPList[ 4 ]. toUint ()), // txRoot
124- bytes32 (inputDataRLPList[ 5 ]. toUint ()), // receiptRoot
125- inputDataRLPList[ 0 ]. toUint (), // headerNumber
126- inputDataRLPList[ 1 ]. toBytes () // blockProof
119+ blockNumber,
120+ payload. getBlockTime (),
121+ payload. getTxRoot (),
122+ receiptRoot,
123+ payload. getHeaderNumber (),
124+ payload. getBlockProof ()
127125 );
128126
129- RLPReader.RLPItem[] memory logTopicRLPList = logRLPList[ 1 ]. toList (); // topics
127+ ExitPayloadReader.LogTopics memory topics = log. getTopics ();
130128
131129 require (
132- bytes32 (logTopicRLPList[ 0 ] .toUint ()) == SEND_MESSAGE_EVENT_SIG, // topic0 is event sig
130+ bytes32 (topics. getField ( 0 ) .toUint ()) == SEND_MESSAGE_EVENT_SIG, // topic0 is event sig
133131 "FxRootTunnel: INVALID_SIGNATURE "
134132 );
135133
136134 // received message data
137- bytes memory receivedData = logRLPList[2 ].toBytes ();
138- (bytes memory message ) = abi.decode (receivedData, (bytes )); // event decodes params again, so decoding bytes to get message
135+ (bytes memory message ) = abi.decode (log.getData (), (bytes )); // event decodes params again, so decoding bytes to get message
139136 return message;
140137 }
141138
@@ -198,4 +195,4 @@ abstract contract FxBaseRootTunnel {
198195 * @param message bytes message that was sent from Child Tunnel
199196 */
200197 function _processMessageFromChild (bytes memory message ) virtual internal ;
201- }
198+ }
0 commit comments