Skip to content

Commit 322ed88

Browse files
committed
Change word "award" to "reward". Delete unused code. Rolls remain reward to next cycle. Add CycleStart event
1 parent c79e589 commit 322ed88

File tree

1 file changed

+36
-53
lines changed

1 file changed

+36
-53
lines changed

contracts/public_data_storage.sol

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
8484
mapping(bytes32 => CycleDataInfo) dataInfos;
8585

8686
SortedScoreList.List scoreList;
87-
uint256 totalAward; // 记录这个cycle的总奖励
87+
uint256 totalReward; // 记录这个cycle的总奖励
8888
}
8989

9090
struct CycleOutputInfo {
@@ -96,7 +96,6 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
9696
uint256 _currectCycle;
9797
mapping(uint256 => CycleInfo) _cycleInfos;
9898
uint256 _startBlock;
99-
uint64 _minRankingScore;
10099

101100
struct SysConfig {
102101
uint32 minDepositRatio;
@@ -126,7 +125,8 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
126125
event SupplierReward(address supplier, bytes32 mixedHash, uint256 amount);
127126
event SupplierPunished(address supplier, bytes32 mixedHash, uint256 amount);
128127
event ShowDataProof(address supplier, bytes32 dataMixedHash, uint256 nonce_block);
129-
event WithdrawAward(bytes32 mixedHash, uint256 cycle);
128+
event WithdrawReward(bytes32 mixedHash, uint256 cycle);
129+
event CycleStart(uint256 cycleNumber, uint256 startReward);
130130

131131
function initialize(address _gwtToken, address _Foundation) public initializer {
132132
__PublicDataStorageUpgradable_init(_gwtToken, _Foundation);
@@ -191,6 +191,19 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
191191
}
192192
}
193193

194+
function _getRemainScore(uint256 length) internal pure returns(uint16) {
195+
uint16[33] memory remainScores = [
196+
1600, 1360, 1180, 1030, 910, 810,
197+
730, 670, 617, 575, 539, 504,
198+
470, 437, 405, 374, 344, 315,
199+
287, 260, 234, 209, 185, 162,
200+
140, 119, 99, 80, 62, 45,
201+
29, 14, 0
202+
];
203+
204+
return remainScores[length];
205+
}
206+
194207
function _cycleNumber(uint256 blockNumber, uint256 startBlock) internal view returns(uint256) {
195208
uint cycleNumber = (blockNumber - startBlock) / sysConfig.blocksPerCycle;
196209
if (cycleNumber * sysConfig.blocksPerCycle + startBlock < blockNumber) {
@@ -209,13 +222,19 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
209222
CycleInfo storage cycleInfo = _cycleInfos[cycleNumber];
210223
// 如果cycle的reward为0,说明这个周期还没有开始
211224
// 开始一个周期:从上个周期的奖励中拿20%
212-
if (cycleInfo.totalAward == 0) {
213-
uint256 lastCycleReward = _cycleInfos[_currectCycle].totalAward;
214-
// 0.05作为基金会收入
225+
if (cycleInfo.totalReward == 0) {
226+
uint256 lastCycleReward = _cycleInfos[_currectCycle].totalReward;
227+
// 5%作为基金会收入
215228
uint256 fundationIncome = lastCycleReward * 5 / 100;
216229
gwtToken.transfer(foundationAddress, fundationIncome);
217-
cycleInfo.totalAward = (lastCycleReward - (lastCycleReward * 4 / 5) - fundationIncome);
218-
_currectCycle = cycleNumber;
230+
// 如果上一轮的获奖数据不足32个,剩余的奖金也滚动进此轮奖池
231+
uint16 remainScore = _getRemainScore(_cycleInfos[_currectCycle].scoreList.length());
232+
uint256 remainReward = lastCycleReward * 4 * remainScore / totalRewardScore / 5;
233+
234+
cycleInfo.totalReward = lastCycleReward - (lastCycleReward * 4 / 5) - fundationIncome + remainReward;
235+
_currectCycle = cycleNumber;
236+
237+
emit CycleStart(cycleNumber, cycleInfo.totalReward);
219238
}
220239

221240
return cycleInfo;
@@ -224,7 +243,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
224243

225244
function _addCycleReward(uint256 amount) private {
226245
CycleInfo storage cycleInfo = _ensureCurrentCycleStart();
227-
cycleInfo.totalAward += amount;
246+
cycleInfo.totalReward += amount;
228247
}
229248

230249
// 计算这些空间对应多少GWT,单位是wei
@@ -288,7 +307,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
288307
}
289308

290309
function getCycleInfo(uint256 cycleNumber) public view returns(CycleOutputInfo memory) {
291-
return CycleOutputInfo(_cycleInfos[cycleNumber].totalAward, _cycleInfos[cycleNumber].scoreList.getSortedList());
310+
return CycleOutputInfo(_cycleInfos[cycleNumber].totalReward, _cycleInfos[cycleNumber].scoreList.getSortedList());
292311
}
293312

294313
function getPledgeInfo(address supplier) public view returns(SupplierInfo memory) {
@@ -456,7 +475,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
456475
_updateLastSupplier(dataInfo, address(0), msg.sender);
457476

458477
//只有超过阈值才会更新排名,这个设定会导致用户不多的时候排名不满(强制性累积奖金)
459-
if (dataInfo.score > _minRankingScore) {
478+
if (dataInfo.score > sysConfig.minRankingScore) {
460479
if (cycleInfo.scoreList.maxlen() < sysConfig.topRewards) {
461480
cycleInfo.scoreList.setMaxLen(sysConfig.topRewards);
462481
}
@@ -523,9 +542,9 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
523542
if(root_hash < proof.proofResult) {
524543
_supplierInfos[proof.prover].lockedBalance -= proof.lockedAmount;
525544

526-
uint256 awardFromPunish = proof.lockedAmount * 8 / 10;
527-
gwtToken.transfer(msg.sender, awardFromPunish);
528-
gwtToken.transfer(foundationAddress, proof.lockedAmount - awardFromPunish);
545+
uint256 rewardFromPunish = proof.lockedAmount * 8 / 10;
546+
gwtToken.transfer(msg.sender, rewardFromPunish);
547+
gwtToken.transfer(foundationAddress, proof.lockedAmount - rewardFromPunish);
529548

530549
emit SupplierPunished(proof.prover, dataMixedHash, proof.lockedAmount);
531550
emit SupplierBalanceChanged(proof.prover, _supplierInfos[proof.prover].avalibleBalance, _supplierInfos[proof.prover].lockedBalance);
@@ -552,43 +571,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
552571
return IERCPublicDataContract(publicDataInfo.dataContract).getDataOwner(dataMixedHash);
553572
}
554573

555-
// return: 1: sponsor, 2- 6: last shower, 7: owner, 0: no one
556-
function _getWithdrawRole(address sender, bytes32 dataMixedHash, PublicData memory publicDataInfo, CycleDataInfo memory dataInfo) internal view returns(uint8) {
557-
uint8 user = 0;
558-
if (sender == publicDataInfo.sponsor) {
559-
user |= 1 << 1;
560-
}
561-
562-
if (sender == _getDataOwner(dataMixedHash, publicDataInfo)) {
563-
user |= 1 << 7;
564-
}
565-
566-
for (uint8 i = 0; i < dataInfo.lastShowers.length; i++) {
567-
if (dataInfo.lastShowers[i] == sender) {
568-
user |= uint8(1 << (i+2));
569-
}
570-
}
571-
572-
return user;
573-
}
574-
575-
// sponsor拿50%, owner拿20%, 5个last shower平分30%
576-
function _calcuteReward(uint8 user, uint256 totalReward, uint256 last_shower_length) internal pure returns(uint256) {
577-
uint reward = 0;
578-
if ((user >> 1) & 1 == 1) {
579-
reward += totalReward / 2;
580-
}
581-
if ((user >> 7) & 1 == 1) {
582-
reward += totalReward / 5;
583-
}
584-
if (user & 124 > 0) {
585-
reward += (totalReward - totalReward / 2 - totalReward / 5) / last_shower_length;
586-
}
587-
588-
return reward;
589-
}
590-
591-
function withdrawAward(uint256 cycleNumber, bytes32 dataMixedHash) public {
574+
function withdrawReward(uint256 cycleNumber, bytes32 dataMixedHash) public {
592575
// 判断这次的cycle已经结束
593576
//require(_currectCycle > cycleNumber, "cycle not finish");
594577
require(block.number > cycleNumber * sysConfig.blocksPerCycle + _startBlock, "cycle not finish");
@@ -602,7 +585,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
602585
require(dataInfo.score > 0, "already withdraw");
603586

604587
// 计算该得到多少奖励
605-
uint256 totalReward = cycleInfo.totalAward * 8 / 10;
588+
uint256 totalReward = cycleInfo.totalReward * 8 / 10;
606589

607590
uint8 score = _getRewardScore(scoreListRanking);
608591
// 如果数据总量不足32,那么多余的奖励沉淀在合约账户中
@@ -627,6 +610,6 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
627610

628611
// 更新积分
629612
emit DataPointAdded(dataMixedHash, score);
630-
emit WithdrawAward(dataMixedHash, cycleNumber);
613+
emit WithdrawReward(dataMixedHash, cycleNumber);
631614
}
632615
}

0 commit comments

Comments
 (0)