Skip to content

Commit e97431b

Browse files
author
Ho
committed
contract with DA
1 parent 43f1f1b commit e97431b

File tree

5 files changed

+116
-7
lines changed

5 files changed

+116
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
node_modules
2-
2+
package-lock.json
33
#Hardhat files
44
cache
55
artifacts

contracts/FluiDex.sol

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

contracts/FluiDexDelegate.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ contract FluiDexDelegate is
104104
function submitBlock(
105105
uint256 _block_id,
106106
uint256[] memory _public_inputs,
107-
uint256[] memory _serialized_proof
107+
uint256[] memory _serialized_proof,
108+
bytes memory _public_data
108109
) external override returns (bool) {
109-
return target.submitBlock(_block_id, _public_inputs, _serialized_proof);
110+
return target.submitBlock(_block_id, _public_inputs, _serialized_proof, _public_data);
110111
}
111112

112113
/**

contracts/IFluiDex.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ interface IFluiDex {
4444
* @param _block_id the l2 block id
4545
* @param _public_inputs the public inputs of this block
4646
* @param _serialized_proof the serialized proof of this block
47+
* @param _public_data the serialized tx data inside this block (data availability)
4748
* @return true if the block was accepted
4849
*/
4950
function submitBlock(
5051
uint256 _block_id,
5152
uint256[] memory _public_inputs,
52-
uint256[] memory _serialized_proof
53+
uint256[] memory _serialized_proof,
54+
bytes memory _public_data
5355
) external returns (bool);
5456

5557
/**

contracts/IFluiDexDelegate.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ interface IFluiDexDelegate {
3232
* @param _block_id the l2 block id
3333
* @param _public_inputs the public inputs of this block
3434
* @param _serialized_proof the serialized proof of this block
35+
* @param _public_data the serialized tx data inside this block (data availability)
3536
* @return true if the block was accepted
3637
*/
3738
function submitBlock(
3839
uint256 _block_id,
3940
uint256[] memory _public_inputs,
40-
uint256[] memory _serialized_proof
41+
uint256[] memory _serialized_proof,
42+
bytes memory _public_data
4143
) external returns (bool);
4244
}

0 commit comments

Comments
 (0)