Commit df627af6 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

rename: witness -> validator

parent 17f2e022
...@@ -7,14 +7,14 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; ...@@ -7,14 +7,14 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Pausable} from "@openzeppelin/contracts/utils/Pausable.sol"; import {Pausable} from "@openzeppelin/contracts/utils/Pausable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {IWitness} from "./interface/IWitness.sol"; import {IValidator} from "./interface/IValidator.sol";
import {IDistribution} from "./interface/IDistribution.sol"; import {IDistribution} from "./interface/IDistribution.sol";
contract Distribution is IDistribution, Ownable, Pausable, ReentrancyGuard { contract Distribution is IDistribution, Ownable, Pausable, ReentrancyGuard {
constructor() Ownable(msg.sender) { constructor() Ownable(msg.sender) {
} }
IWitness public witness; IValidator public validator;
IERC20 public token = IERC20(address(0xfFb096e2B90324FFcCbaf987BdD724462c0aE18c)); IERC20 public token = IERC20(address(0xfFb096e2B90324FFcCbaf987BdD724462c0aE18c));
...@@ -45,6 +45,6 @@ contract Distribution is IDistribution, Ownable, Pausable, ReentrancyGuard { ...@@ -45,6 +45,6 @@ contract Distribution is IDistribution, Ownable, Pausable, ReentrancyGuard {
} }
function _merkleRoot() internal returns (bytes32){ function _merkleRoot() internal returns (bytes32){
return witness.getMerkleRoot(); return validator.getMerkleRoot();
} }
} }
\ No newline at end of file
...@@ -4,12 +4,12 @@ pragma solidity ^0.8.20; ...@@ -4,12 +4,12 @@ pragma solidity ^0.8.20;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {IWitness} from "./interface/IWitness.sol"; import {IValidator} from "./interface/IValidator.sol";
contract Witness is IWitness, Ownable { contract Validator is IValidator, Ownable {
using EnumerableSet for EnumerableSet.AddressSet; using EnumerableSet for EnumerableSet.AddressSet;
EnumerableSet.AddressSet private witnessSet; EnumerableSet.AddressSet private validatorSet;
bytes32 public merkleRoot; // 当前最新的 merkle root bytes32 public merkleRoot; // 当前最新的 merkle root
bytes32 public merkleSumRoot; // 当前最新的 merkle sum root bytes32 public merkleSumRoot; // 当前最新的 merkle sum root
...@@ -19,7 +19,7 @@ contract Witness is IWitness, Ownable { ...@@ -19,7 +19,7 @@ contract Witness is IWitness, Ownable {
// 记录每天每个 merkle root 提交次数 2023-01-01 -> hash(merkleRoot, merkleSumRoot) -> 1 // 记录每天每个 merkle root 提交次数 2023-01-01 -> hash(merkleRoot, merkleSumRoot) -> 1
mapping(uint256 => mapping(bytes32 => uint256)) public dailyMerkleRootSubmitCountMap; mapping(uint256 => mapping(bytes32 => uint256)) public dailyMerkleRootSubmitCountMap;
// 记录每天每个 witness 是否已提交过 // 记录每天每个 validator 是否已提交过
mapping(uint256 => mapping(address => bool)) public dailyMerkleRootSubmittedMap; mapping(uint256 => mapping(address => bool)) public dailyMerkleRootSubmittedMap;
// 记录每天已验证的 merkle root (已超过生效阈值) // 记录每天已验证的 merkle root (已超过生效阈值)
mapping(uint256 => bool) public dailyMerkleRootVerifiedMap; mapping(uint256 => bool) public dailyMerkleRootVerifiedMap;
...@@ -31,11 +31,11 @@ contract Witness is IWitness, Ownable { ...@@ -31,11 +31,11 @@ contract Witness is IWitness, Ownable {
uint256 public dailyDistribution; uint256 public dailyDistribution;
constructor() Ownable(msg.sender) { constructor() Ownable(msg.sender) {
witnessSet.add(msg.sender); // for test validatorSet.add(msg.sender); // for test
} }
modifier onlyWitness() { modifier onlyValidator() {
require(witnessSet.contains(msg.sender), "Only witness"); require(validatorSet.contains(msg.sender), "Only validator");
_; _;
} }
...@@ -43,7 +43,7 @@ contract Witness is IWitness, Ownable { ...@@ -43,7 +43,7 @@ contract Witness is IWitness, Ownable {
event MerkleRootThresholdChanged(uint256 merkleRootThreshold); event MerkleRootThresholdChanged(uint256 merkleRootThreshold);
event MerkleRootSubmitted(address indexed addr, bytes32 merkleRoot, bytes32 merkleSumRoot, uint256 indexed count, uint256 indexed dailySubmitCount); event MerkleRootSubmitted(address indexed addr, bytes32 merkleRoot, bytes32 merkleSumRoot, uint256 indexed count, uint256 indexed dailySubmitCount);
event DailyDistributionChanged(uint256 dailyDistribution); event DailyDistributionChanged(uint256 dailyDistribution);
event WitnessChanged(address indexed addr, bool indexed ok, uint256 indexed count); event ValidatorChanged(address indexed addr, bool indexed ok, uint256 indexed count);
function getMerkleRoot() public view returns (bytes32){ function getMerkleRoot() public view returns (bytes32){
return merkleRoot; return merkleRoot;
...@@ -53,8 +53,8 @@ contract Witness is IWitness, Ownable { ...@@ -53,8 +53,8 @@ contract Witness is IWitness, Ownable {
return dailyDistribution / dailyWorkload; return dailyDistribution / dailyWorkload;
} }
function submitMerkleRoot(uint256 _date, bytes32 _merkleRoot, bytes32 _merkleSumRoot) public onlyWitness { function submitMerkleRoot(uint256 _date, bytes32 _merkleRoot, bytes32 _merkleSumRoot) public onlyValidator {
require(dailyMerkleRootSubmitCount <= witnessSet.length(), "Too many submissions"); require(dailyMerkleRootSubmitCount <= validatorSet.length(), "Too many submissions");
require(!dailyMerkleRootSubmittedMap[_date][msg.sender], "Already submitted"); require(!dailyMerkleRootSubmittedMap[_date][msg.sender], "Already submitted");
// check already verified // check already verified
if (dailyMerkleRootVerifiedMap[_date]) { if (dailyMerkleRootVerifiedMap[_date]) {
...@@ -98,32 +98,32 @@ contract Witness is IWitness, Ownable { ...@@ -98,32 +98,32 @@ contract Witness is IWitness, Ownable {
emit MerkleRootThresholdChanged(_merkleRootThreshold); emit MerkleRootThresholdChanged(_merkleRootThreshold);
} }
function setWitness(address addr, bool ok) public onlyOwner { function setValidator(address addr, bool ok) public onlyOwner {
if (ok) { if (ok) {
_addWitness(addr); _addValidator(addr);
} else { } else {
_delWitness(addr); _delValidator(addr);
} }
emit WitnessChanged(addr, ok, witnessSet.length()); emit ValidatorChanged(addr, ok, validatorSet.length());
} }
function _addWitness(address addr) internal { function _addValidator(address addr) internal {
bool exist = witnessSet.contains(addr); bool exist = validatorSet.contains(addr);
require(!exist, "Already exist"); require(!exist, "Already exist");
witnessSet.add(addr); validatorSet.add(addr);
} }
function _delWitness(address addr) internal { function _delValidator(address addr) internal {
bool exist = witnessSet.contains(addr); bool exist = validatorSet.contains(addr);
require(exist, "Not exist"); require(exist, "Not exist");
witnessSet.remove(addr); validatorSet.remove(addr);
} }
function getWitnesses() public view returns (address[] memory) { function getValidatores() public view returns (address[] memory) {
return witnessSet.values(); return validatorSet.values();
} }
function isWitness(address addr) public view returns (bool) { function isValidator(address addr) public view returns (bool) {
return witnessSet.contains(addr); return validatorSet.contains(addr);
} }
} }
\ No newline at end of file
// SPDX-License-Identifier: GPL-3.0 // SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20; pragma solidity ^0.8.20;
interface IWitness { interface IValidator {
function getMerkleRoot() external returns (bytes32); function getMerkleRoot() external returns (bytes32);
function submitMerkleRoot(uint256 _date, bytes32 _merkleRoot, bytes32 _merkleSumRoot) external; function submitMerkleRoot(uint256 _date, bytes32 _merkleRoot, bytes32 _merkleSumRoot) external;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment