mirror of
https://github.com/fluencelabs/lazy-snark
synced 2025-04-25 15:02:15 +00:00
102 lines
2.9 KiB
Solidity
102 lines
2.9 KiB
Solidity
pragma solidity ^0.5.4;
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
import "./IVerifier.sol";
|
|
import "./Structs.sol";
|
|
|
|
contract Lazy is Structs {
|
|
event Submitted(address indexed sender, uint256 index, Task task);
|
|
event Challenged(address indexed challenger, uint256 index);
|
|
|
|
|
|
enum Status {UNCHECKED, VALID, INVALID, FINALIZED}
|
|
struct Task {
|
|
Data data;
|
|
Proof proof;
|
|
address payable submitter;
|
|
uint timestamp;
|
|
Status status;
|
|
}
|
|
|
|
Task[] public tasks;
|
|
function tasksNum() external view returns(uint) {
|
|
return tasks.length;
|
|
}
|
|
|
|
uint256 public stake;
|
|
IVerifier public verifier;
|
|
|
|
/// @dev This function submits data.
|
|
/// @param data - public inptut for zkp
|
|
/// @param proof - proof that verifies input
|
|
function submit(Data calldata data, Proof calldata proof) external payable {
|
|
require(msg.value == stake);
|
|
|
|
Task memory task = Task(data, proof, msg.sender, uint96(now), Status.UNCHECKED);
|
|
uint index = tasks.push(task);
|
|
|
|
emit Submitted(msg.sender, index, task);
|
|
}
|
|
|
|
/// @dev This function challenges a submission by calling the validation function.
|
|
/// @param id The id of the submission to challenge.
|
|
function challenge(uint id) external {
|
|
Task storage task = tasks[id];
|
|
require(now < task.timestamp + 1 weeks);
|
|
require(task.status == Status.UNCHECKED);
|
|
|
|
if (verifier.isValid(task.data, task.proof)) {
|
|
task.status = Status.VALID;
|
|
task.submitter.transfer(stake);
|
|
} else {
|
|
task.status = Status.INVALID;
|
|
msg.sender.transfer(stake);
|
|
}
|
|
|
|
|
|
// пруф не подходит, на это надо реагировать
|
|
|
|
emit Challenged(msg.sender, id);
|
|
}
|
|
|
|
function finzalize(uint id) external {
|
|
Task storage task = tasks[id];
|
|
require(now > task.timestamp + 1 weeks);
|
|
require(task.status == Status.UNCHECKED);
|
|
|
|
task.status = Status.FINALIZED;
|
|
msg.sender.transfer(stake);
|
|
}
|
|
|
|
function taskDataById(uint id) external view returns(
|
|
uint[5] memory input,
|
|
uint[2] memory a,
|
|
uint[4] memory b,
|
|
uint[2] memory c
|
|
) {
|
|
Task memory task = tasks[id];
|
|
input = task.data.input;
|
|
a = task.proof.a;
|
|
b[0] = task.proof.b[0][0];
|
|
b[1] = task.proof.b[0][1];
|
|
b[2] = task.proof.b[1][0];
|
|
b[3] = task.proof.b[1][1];
|
|
c = task.proof.c;
|
|
}
|
|
|
|
|
|
function last5Timestamps() view external returns (uint256[5] memory result) {
|
|
uint256 length = tasks.length;
|
|
for (uint256 i = 1; i <= 5; i++) {
|
|
result[i - 1] = tasks[length - i].timestamp;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
function getDataById(uint256 id) view external returns (Task memory task) {
|
|
task = tasks[tasks.length - 1 - id];
|
|
}
|
|
}
|
|
|