|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import { Initializable } from |
| 5 | + "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; |
| 6 | +import { AccessControlUpgradeable } from |
| 7 | + "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol"; |
| 8 | +import { UUPSUpgradeable } from |
| 9 | + "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol"; |
| 10 | +import { EnumerableSet } from |
| 11 | + "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; |
| 12 | +import { EnumerableMap } from |
| 13 | + "@openzeppelin/contracts/utils/structs/EnumerableMap.sol"; |
| 14 | +import { VMahout } from "./VMahout.sol"; |
| 15 | + |
| 16 | +/** |
| 17 | + * @title PropertyDataConsensus |
| 18 | + * @notice Permissionless consensus system for property data with UUPS upgradeability |
| 19 | + */ |
| 20 | +contract PropertyDataConsensus is |
| 21 | + Initializable, |
| 22 | + AccessControlUpgradeable, |
| 23 | + UUPSUpgradeable |
| 24 | +{ |
| 25 | + using EnumerableSet for EnumerableSet.AddressSet; |
| 26 | + using EnumerableMap for EnumerableMap.Bytes32ToBytes32Map; |
| 27 | + |
| 28 | + event DataSubmitted( |
| 29 | + bytes32 indexed propertyHash, |
| 30 | + bytes32 indexed dataGroupHash, |
| 31 | + address indexed submitter, |
| 32 | + bytes32 dataHash |
| 33 | + ); |
| 34 | + event MinimumConsensusUpdated(uint256 oldValue, uint256 newValue); |
| 35 | + event DataGroupConsensusUpdated( |
| 36 | + bytes32 indexed dataGroupHash, uint256 oldValue, uint256 newValue |
| 37 | + ); |
| 38 | + event DataGroupHeartBeat( |
| 39 | + bytes32 indexed propertyHash, |
| 40 | + bytes32 indexed dataGroupHash, |
| 41 | + bytes32 indexed dataHash, |
| 42 | + address submitter |
| 43 | + ); |
| 44 | + |
| 45 | + // --- Structs --- |
| 46 | + struct DataVersion { |
| 47 | + bytes32 dataHash; |
| 48 | + address[] oracles; |
| 49 | + uint256 timestamp; |
| 50 | + } |
| 51 | + |
| 52 | + struct DataItem { |
| 53 | + bytes32 propertyHash; |
| 54 | + bytes32 dataGroupHash; |
| 55 | + bytes32 dataHash; |
| 56 | + } |
| 57 | + |
| 58 | + struct DataSubmission { |
| 59 | + address oracle; |
| 60 | + uint256 timestamp; |
| 61 | + } |
| 62 | + |
| 63 | + // @deprecated there is no consensus rule for the oracle submission anymore |
| 64 | + uint256 public minimumConsensus; |
| 65 | + |
| 66 | + // @deprecated we don't need to store what used to be a submission data anymore |
| 67 | + mapping(bytes32 => mapping(bytes32 => EnumerableSet.AddressSet)) private |
| 68 | + _submissionData; |
| 69 | + mapping(bytes32 => bytes32) private _currentConsensusDataHash; |
| 70 | + |
| 71 | + // @deprecated we don't store consensus log anymore |
| 72 | + mapping(bytes32 => DataVersion[]) private _consensusLog; |
| 73 | + |
| 74 | + VMahout public vMahout; |
| 75 | + |
| 76 | + // @deprecated this is not used anymore |
| 77 | + mapping(bytes32 => uint256) public consensusRequired; |
| 78 | + |
| 79 | + bytes32 public constant LEXICON_ORACLE_MANAGER_ROLE = |
| 80 | + keccak256("LEXICON_ORACLE_MANAGER_ROLE"); |
| 81 | + |
| 82 | + mapping(bytes32 => EnumerableMap.Bytes32ToBytes32Map) private _dataStorage; |
| 83 | + // Key is composite hash of propertyHash and dataGroupHash to ensure uniqueness |
| 84 | + mapping(bytes32 => DataSubmission) private _dataSubmissions; |
| 85 | + |
| 86 | + error EmptyBatchSubmission(); |
| 87 | + |
| 88 | + /// @custom:oz-upgrades-unsafe-allow constructor |
| 89 | + constructor() { |
| 90 | + _disableInitializers(); |
| 91 | + } |
| 92 | + |
| 93 | + function _getPropertyHashFieldHash( |
| 94 | + bytes32 propertyHash, |
| 95 | + bytes32 dataGroupHash |
| 96 | + ) |
| 97 | + internal |
| 98 | + pure |
| 99 | + returns (bytes32) |
| 100 | + { |
| 101 | + return keccak256(abi.encodePacked(propertyHash, dataGroupHash)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @notice Initialize the contract |
| 106 | + * @param initialAdmin Address that will be granted DEFAULT_ADMIN_ROLE and ORACLE_MANAGER_ROLE. |
| 107 | + */ |
| 108 | + function initialize(address initialAdmin) external initializer { |
| 109 | + __AccessControl_init(); |
| 110 | + __UUPSUpgradeable_init(); |
| 111 | + |
| 112 | + _grantRole(DEFAULT_ADMIN_ROLE, initialAdmin); |
| 113 | + } |
| 114 | + |
| 115 | + function submitData( |
| 116 | + bytes32 propertyHash, |
| 117 | + bytes32 dataGroupHash, |
| 118 | + bytes32 dataHash |
| 119 | + ) |
| 120 | + public |
| 121 | + { |
| 122 | + _submitDataInternal(propertyHash, dataGroupHash, dataHash); |
| 123 | + if (address(vMahout) != address(0)) { |
| 124 | + vMahout.mint(msg.sender, 1 ether); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + function submitBatchData(DataItem[] calldata items) public { |
| 129 | + uint256 length = items.length; |
| 130 | + if (length == 0) { |
| 131 | + revert EmptyBatchSubmission(); |
| 132 | + } |
| 133 | + unchecked { |
| 134 | + for (uint256 i = 0; i < length; i++) { |
| 135 | + _submitDataInternal( |
| 136 | + items[i].propertyHash, |
| 137 | + items[i].dataGroupHash, |
| 138 | + items[i].dataHash |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | + if (address(vMahout) != address(0)) { |
| 143 | + vMahout.mint(msg.sender, length * 1 ether); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + function _submitDataInternal( |
| 148 | + bytes32 propertyHash, |
| 149 | + bytes32 dataGroupHash, |
| 150 | + bytes32 dataHash |
| 151 | + ) |
| 152 | + internal |
| 153 | + { |
| 154 | + address submitter = msg.sender; |
| 155 | + bytes32 propertyDataHash = |
| 156 | + _getPropertyHashFieldHash(propertyHash, dataHash); |
| 157 | + (bool exists, bytes32 currentDataHash) = |
| 158 | + _dataStorage[propertyHash].tryGet(dataGroupHash); |
| 159 | + if (exists && currentDataHash == dataHash) { |
| 160 | + if (_dataSubmissions[propertyDataHash].oracle == submitter) { |
| 161 | + emit DataGroupHeartBeat( |
| 162 | + propertyHash, dataGroupHash, dataHash, submitter |
| 163 | + ); |
| 164 | + _dataSubmissions[propertyDataHash].timestamp = block.timestamp; |
| 165 | + return; |
| 166 | + } |
| 167 | + } |
| 168 | + _dataStorage[propertyHash].set(dataGroupHash, dataHash); |
| 169 | + _dataSubmissions[propertyDataHash] = |
| 170 | + DataSubmission(submitter, block.timestamp); |
| 171 | + emit DataSubmitted(propertyHash, dataGroupHash, submitter, dataHash); |
| 172 | + } |
| 173 | + |
| 174 | + function getCurrentFieldDataHash( |
| 175 | + bytes32 propertyHash, |
| 176 | + bytes32 dataGroupHash |
| 177 | + ) |
| 178 | + public |
| 179 | + view |
| 180 | + returns (bytes32) |
| 181 | + { |
| 182 | + return _dataStorage[propertyHash].get(dataGroupHash); |
| 183 | + } |
| 184 | + |
| 185 | + function _authorizeUpgrade(address newImplementation) |
| 186 | + internal |
| 187 | + override |
| 188 | + onlyRole(DEFAULT_ADMIN_ROLE) |
| 189 | + { } |
| 190 | + |
| 191 | + /** |
| 192 | + * @notice Sets the vMahout token address used for minting rewards to oracles. |
| 193 | + * @dev Can only be called by an account with DEFAULT_ADMIN_ROLE. |
| 194 | + * @param _vMahout The address of the vMahout token contract. |
| 195 | + */ |
| 196 | + function setVMahout(address _vMahout) |
| 197 | + external |
| 198 | + onlyRole(DEFAULT_ADMIN_ROLE) |
| 199 | + { |
| 200 | + vMahout = VMahout(_vMahout); |
| 201 | + } |
| 202 | +} |
0 commit comments