2021-11-01 11:30:35 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-29 10:18:02 +00:00
|
|
|
pragma solidity ^0.8.8;
|
2021-11-01 11:30:35 +00:00
|
|
|
|
2023-01-17 12:55:58 +00:00
|
|
|
import "./Configuration.sol";
|
2023-01-09 13:20:59 +00:00
|
|
|
import "./Requests.sol";
|
2023-01-10 13:16:32 +00:00
|
|
|
import "./Periods.sol";
|
2022-09-29 10:18:02 +00:00
|
|
|
|
2023-01-10 13:16:32 +00:00
|
|
|
abstract contract Proofs is Periods {
|
2023-01-23 10:57:10 +00:00
|
|
|
ProofConfig private _config;
|
2023-01-17 12:55:58 +00:00
|
|
|
|
2023-01-23 10:57:10 +00:00
|
|
|
constructor(ProofConfig memory config) Periods(config.period) {
|
2022-03-09 10:21:19 +00:00
|
|
|
require(block.number > 256, "Insufficient block height");
|
2023-01-23 10:57:10 +00:00
|
|
|
_config = config;
|
2022-03-02 14:44:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 10:57:10 +00:00
|
|
|
mapping(SlotId => uint256) private _slotStarts;
|
|
|
|
mapping(SlotId => uint256) private _probabilities;
|
|
|
|
mapping(SlotId => uint256) private _missed;
|
|
|
|
mapping(SlotId => mapping(Period => bool)) private _received;
|
|
|
|
mapping(SlotId => mapping(Period => bool)) private _missing;
|
2021-11-01 11:30:35 +00:00
|
|
|
|
2023-01-18 14:26:21 +00:00
|
|
|
function slotState(SlotId id) public view virtual returns (SlotState);
|
2021-11-03 12:24:50 +00:00
|
|
|
|
2023-01-09 13:41:28 +00:00
|
|
|
function missingProofs(SlotId slotId) public view returns (uint256) {
|
2023-01-23 10:57:10 +00:00
|
|
|
return _missed[slotId];
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 11:51:26 +00:00
|
|
|
function _startRequiringProofs(SlotId id, uint256 probability) internal {
|
2023-01-23 10:57:10 +00:00
|
|
|
_slotStarts[id] = block.timestamp;
|
|
|
|
_probabilities[id] = probability;
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _getPointer(SlotId id, Period period) internal view returns (uint8) {
|
2022-03-10 09:12:03 +00:00
|
|
|
uint256 blockNumber = block.number % 256;
|
2023-01-17 08:12:15 +00:00
|
|
|
uint256 periodNumber = Period.unwrap(period) % 256;
|
2023-01-09 13:20:59 +00:00
|
|
|
uint256 idOffset = uint256(SlotId.unwrap(id)) % 256;
|
2022-03-10 09:12:03 +00:00
|
|
|
uint256 pointer = (blockNumber + periodNumber + idOffset) % 256;
|
|
|
|
return uint8(pointer);
|
|
|
|
}
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2023-01-17 07:37:59 +00:00
|
|
|
function getPointer(SlotId id) public view returns (uint8) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _getPointer(id, _blockPeriod());
|
2022-03-10 12:35:41 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _getChallenge(uint8 pointer) internal view returns (bytes32) {
|
2022-03-10 09:12:03 +00:00
|
|
|
bytes32 hash = blockhash(block.number - 1 - pointer);
|
|
|
|
assert(uint256(hash) != 0);
|
|
|
|
return keccak256(abi.encode(hash));
|
|
|
|
}
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _getChallenge(
|
2023-01-10 10:04:04 +00:00
|
|
|
SlotId id,
|
2023-01-17 08:12:15 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) internal view returns (bytes32) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _getChallenge(_getPointer(id, period));
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 07:37:59 +00:00
|
|
|
function getChallenge(SlotId id) public view returns (bytes32) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _getChallenge(id, _blockPeriod());
|
2022-03-10 12:04:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 10:04:04 +00:00
|
|
|
function _getProofRequirement(
|
|
|
|
SlotId id,
|
2023-01-17 08:12:15 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) internal view returns (bool isRequired, uint8 pointer) {
|
2023-01-16 15:31:04 +00:00
|
|
|
SlotState state = slotState(id);
|
2023-01-23 10:57:10 +00:00
|
|
|
Period start = _periodOf(_slotStarts[id]);
|
2023-01-19 15:47:29 +00:00
|
|
|
if (state != SlotState.Filled || !_isAfter(period, start)) {
|
2022-04-05 09:27:02 +00:00
|
|
|
return (false, 0);
|
2022-03-10 09:12:03 +00:00
|
|
|
}
|
2023-01-19 15:47:29 +00:00
|
|
|
pointer = _getPointer(id, period);
|
|
|
|
bytes32 challenge = _getChallenge(pointer);
|
2023-01-23 10:57:10 +00:00
|
|
|
uint256 probability = (_probabilities[id] * (256 - _config.downtime)) / 256;
|
2023-01-16 15:31:04 +00:00
|
|
|
isRequired = uint256(challenge) % probability == 0;
|
2022-04-05 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 15:47:29 +00:00
|
|
|
function _isProofRequired(
|
2023-01-10 10:04:04 +00:00
|
|
|
SlotId id,
|
2023-01-17 08:12:15 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) internal view returns (bool) {
|
2022-04-05 09:27:02 +00:00
|
|
|
bool isRequired;
|
|
|
|
uint8 pointer;
|
2023-01-17 08:12:15 +00:00
|
|
|
(isRequired, pointer) = _getProofRequirement(id, period);
|
2023-01-23 10:57:10 +00:00
|
|
|
return isRequired && pointer >= _config.downtime;
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 07:37:59 +00:00
|
|
|
function isProofRequired(SlotId id) public view returns (bool) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _isProofRequired(id, _blockPeriod());
|
2022-03-08 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 07:37:59 +00:00
|
|
|
function willProofBeRequired(SlotId id) public view returns (bool) {
|
2022-04-05 09:27:02 +00:00
|
|
|
bool isRequired;
|
|
|
|
uint8 pointer;
|
2023-01-19 15:47:29 +00:00
|
|
|
(isRequired, pointer) = _getProofRequirement(id, _blockPeriod());
|
2023-01-23 10:57:10 +00:00
|
|
|
return isRequired && pointer < _config.downtime;
|
2022-04-05 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 14:19:00 +00:00
|
|
|
function submitProof(SlotId id, bytes calldata proof) public {
|
2022-04-12 06:43:47 +00:00
|
|
|
require(proof.length > 0, "Invalid proof"); // TODO: replace by actual check
|
2023-01-23 10:57:10 +00:00
|
|
|
require(!_received[id][_blockPeriod()], "Proof already submitted");
|
|
|
|
_received[id][_blockPeriod()] = true;
|
2022-04-12 06:43:47 +00:00
|
|
|
emit ProofSubmitted(id, proof);
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 13:16:32 +00:00
|
|
|
function _markProofAsMissing(SlotId id, Period missedPeriod) internal {
|
2023-01-19 15:47:29 +00:00
|
|
|
uint256 end = _periodEnd(missedPeriod);
|
2023-01-17 12:55:58 +00:00
|
|
|
require(end < block.timestamp, "Period has not ended yet");
|
2023-01-23 10:57:10 +00:00
|
|
|
require(block.timestamp < end + _config.timeout, "Validation timed out");
|
|
|
|
require(!_received[id][missedPeriod], "Proof was submitted, not missing");
|
2023-01-19 15:47:29 +00:00
|
|
|
require(_isProofRequired(id, missedPeriod), "Proof was not required");
|
2023-01-23 10:57:10 +00:00
|
|
|
require(!_missing[id][missedPeriod], "Proof already marked as missing");
|
|
|
|
_missing[id][missedPeriod] = true;
|
|
|
|
_missed[id] += 1;
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
2022-04-12 06:43:47 +00:00
|
|
|
|
2023-01-09 13:20:59 +00:00
|
|
|
event ProofSubmitted(SlotId id, bytes proof);
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|