@@ -149,18 +149,122 @@ contract FluiDexDemo is AccessControl, IFluiDex, Ownable, ReentrancyGuard {
149149 return block_states[_block_id];
150150 }
151151
152+ /**
153+ * @notice to verify the validity of a sole block
154+ * @param _public_inputs the public inputs of this block
155+ * @param _serialized_proof the serialized proof of this block
156+ * @param _public_data the serialized tx data inside this block (data availability)
157+ * @return true if the block was accepted
158+ */
159+ function verifyBlock (
160+ uint256 [] memory _public_inputs ,
161+ uint256 [] memory _serialized_proof ,
162+ bytes memory _public_data
163+ ) public view returns (bool ) {
164+ // _public_inputs[2]/[3] is the low/high 128bit of sha256 hash of _public_data respectively
165+ require (_public_inputs.length >= 4 );
166+
167+ bytes32 h = sha256 (_public_data);
168+
169+ console.logBytes (_public_data);
170+ console.logBytes32 (h);
171+
172+ uint256 h_lo = 0 ;
173+ for (uint i= 0 ;i< 16 ;i++ ){
174+ uint tmp = uint (uint8 (h[i+16 ]))<< (120-8 * i);
175+ h_lo = h_lo + tmp;
176+
177+ }
178+ uint256 h_hi = 0 ;
179+ for (uint i= 0 ;i< 16 ;i++ ){
180+ uint tmp = uint (uint8 (h[i]))<< (120-8 * i);
181+ h_hi = h_hi + tmp;
182+ }
183+
184+ assert (_public_inputs[2 ] == h_hi);
185+ assert (_public_inputs[3 ] == h_lo);
186+
187+ return verifier.verify_serialized_proof (
188+ _public_inputs,
189+ _serialized_proof
190+ );
191+ }
192+
193+ /**
194+ * @notice to test a block can be submitted or not, this require the block itself
195+ * is valid, and be consistent with state root list
196+ * @param _public_inputs the public inputs of this block
197+ * @param _serialized_proof the serialized proof of this block
198+ * @param _public_data the serialized tx data inside this block (data availability)
199+ * @return true if the block can be submitted
200+ */
201+ function verifySubmitting (
202+ uint256 _block_id ,
203+ uint256 [] memory _public_inputs ,
204+ uint256 [] memory _serialized_proof ,
205+ bytes memory _public_data
206+ ) public view returns (bool ) {
207+ // _public_inputs[0] is previous_state_root
208+ // _public_inputs[1] is new_state_root
209+ require (_public_inputs.length >= 2 );
210+ if (_block_id == 0 ) {
211+ assert (_public_inputs[0 ] == GENESIS_ROOT);
212+ } else {
213+ assert (_public_inputs[0 ] == state_roots[_block_id - 1 ]);
214+ }
215+
216+ return verifyBlock (
217+ _public_inputs,
218+ _serialized_proof,
219+ _public_data
220+ );
221+ }
222+
223+ /**
224+ * @notice request to submit a new l2 block, same parameters with verifySubmitting
225+ * @return true if the block was accepted
226+ */
227+ function submitBlock (
228+ uint256 _block_id ,
229+ uint256 [] memory _public_inputs ,
230+ uint256 [] memory _serialized_proof ,
231+ bytes memory _public_data
232+ ) external override returns (bool ) {
233+
234+ require (block_states[_block_id] != BlockState.Verified, "Block must not be submitted twice " );
235+ if (_block_id > 0 ) {
236+ require (block_states[_block_id - 1 ] == BlockState.Verified, "Previous block must be verified " );
237+ }
238+
239+ bool ret = verifySubmitting (
240+ _block_id,
241+ _public_inputs,
242+ _serialized_proof,
243+ _public_data
244+ );
245+
246+ if (! ret){
247+ return ret;
248+ }
249+
250+ state_roots[_block_id] = _public_inputs[1 ];
251+ block_states[_block_id] = BlockState.Verified;
252+
253+ return true ;
254+ }
255+
152256 /**
153257 * @notice request to submit a new l2 block
154258 * @param _block_id the l2 block id
155259 * @param _public_inputs the public inputs of this block
156260 * @param _serialized_proof the serialized proof of this block
157261 * @return true if the block was accepted
158262 */
159- function submitBlock (
263+ function submitBlockLegacy (
160264 uint256 _block_id ,
161265 uint256 [] memory _public_inputs ,
162266 uint256 [] memory _serialized_proof
163- ) external override returns (bool ) {
267+ ) external returns (bool ) {
164268 // _public_inputs[0] is previous_state_root
165269 // _public_inputs[1] is new_state_root
166270 require (_public_inputs.length >= 2 );
0 commit comments