Skip to content

Commit

Permalink
Ensure fundationAddress is a dividend contract. Add a challenge fail …
Browse files Browse the repository at this point in the history
…event
  • Loading branch information
weiqiushi committed Aug 12, 2024
1 parent e118e03 commit f175781
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions contracts/public_data_storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.0;
import "./gwt.sol";
import "./sortedlist.sol";
import "./PublicDataProof.sol";
import "./dividend.sol";

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
Expand Down Expand Up @@ -84,7 +85,7 @@ contract PublicDataStorage is
}

GWT public gwtToken; // Gb per Week Token
address public foundationAddress;
DividendContract public dividendContract;

mapping(address => SupplierInfo) _supplierInfos;
mapping(bytes32 => PublicData) _publicDatas;
Expand Down Expand Up @@ -154,34 +155,35 @@ contract PublicDataStorage is
address newSponsor
);

event DataPointAdded(bytes32 mixedHash, uint64 point);
event SupplierReward(address supplier, bytes32 mixedHash, uint256 amount);
event SupplierPunished(address supplier, bytes32 mixedHash, uint256 amount);
event DataPointAdded(bytes32 indexed mixedHash, uint64 point);
event SupplierReward(address indexed supplier, bytes32 indexed mixedHash, uint256 amount);
event SupplierPunished(address indexed supplier, bytes32 indexed mixedHash, uint256 amount);
event ShowDataProof(
address supplier,
bytes32 dataMixedHash,
address indexed supplier,
bytes32 indexed dataMixedHash,
uint256 nonce_block
);
event WithdrawReward(bytes32 mixedHash, uint256 cycle);
event WithdrawReward(bytes32 indexed mixedHash, uint256 indexed cycle);
event CycleStart(uint256 cycleNumber, uint256 startReward);
event ChallengeFail(address indexed prover, bytes32 indexed dataMixedHash, uint256 nonce_block);

function initialize(
address _gwtToken,
address _Foundation
address _dividendContract
) public initializer {
__PublicDataStorageUpgradable_init(_gwtToken, _Foundation);
__PublicDataStorageUpgradable_init(_gwtToken, _dividendContract);
}

function __PublicDataStorageUpgradable_init(
address _gwtToken,
address _Foundation
address _dividendContract
) internal onlyInitializing {
__UUPSUpgradeable_init();
__Ownable_init(msg.sender);

gwtToken = GWT(_gwtToken);
currectCycle = 2;
foundationAddress = _Foundation;
dividendContract = DividendContract(payable(_dividendContract));
totalRewardScore = 1600;

sysConfig.minPledgeRate = 16; // Create data is the minimum of 16 times
Expand Down Expand Up @@ -258,7 +260,11 @@ contract PublicDataStorage is
uint256 lastCycleReward = curCycleInfo.totalAward;
// 5% as a foundation income
uint256 fundationIncome = (lastCycleReward * 5) / 100;
gwtToken.transfer(foundationAddress, fundationIncome);

// deposit fundation income to dividend contract
gwtToken.approve(address(dividendContract), fundationIncome);
dividendContract.deposit(fundationIncome, address(gwtToken));

// If the last round of the award -winning data is less than 32, the remaining bonuses are also rolled into this round prize pool
uint16 remainScore = _getRemainScore(curCycleInfo.scoreList.length());
uint256 remainReward = (lastCycleReward * 4 * remainScore) / totalRewardScore / 5;
Expand Down Expand Up @@ -288,6 +294,10 @@ contract PublicDataStorage is
return (uint256(fixedDataSize) * 10 ** 18) >> 30;
}

function minCreateDepositAmount(uint64 dataSize, uint64 pledgeRate) public view returns (uint256) {
return pledgeRate * _dataSizeToGWT(dataSize) * sysConfig.minPublicDataStorageWeeks * sysConfig.createDepositRatio;
}

/**
* @dev Create(Register) public data, To become public data, any data needs to be registered in public data contracts first
* @param dataMixedHash The hash of the data
Expand Down Expand Up @@ -321,10 +331,7 @@ contract PublicDataStorage is
// minamount = data size*gwt exchange ratio*minimum hour length*pledge rate
// get data size from data hash
uint64 dataSize = PublicDataProof.lengthFromMixedHash(dataMixedHash);
uint256 minAmount = pledgeRate *
_dataSizeToGWT(dataSize) *
sysConfig.minPublicDataStorageWeeks *
sysConfig.createDepositRatio;
uint256 minAmount = minCreateDepositAmount(dataSize, pledgeRate);
require(depositAmount >= minAmount, "deposit amount is too small");

PublicData storage publicDataInfo = _publicDatas[dataMixedHash];
Expand Down Expand Up @@ -477,36 +484,35 @@ contract PublicDataStorage is
);
}

function _getLockAmountByHash(
bytes32 dataMixedHash,
ShowType showType
) internal view returns (uint256, bool) {
uint64 dataSize = PublicDataProof.lengthFromMixedHash(dataMixedHash);
uint256 normalLockAmount = _dataSizeToGWT(dataSize) *
_publicDatas[dataMixedHash].pledgeRate *
sysConfig.minLockWeeks;
function getLockAmount(uint64 dataSize, ShowType showType, uint64 pledgeRate, uint256 dataBalance) public view returns (uint256) {
uint256 normalLockAmount = _dataSizeToGWT(dataSize) * pledgeRate * sysConfig.minLockWeeks;
if(showType == ShowType.Immediately) {
uint256 immediatelyLockAmount = (_publicDatas[dataMixedHash].dataBalance * 2) / 10;
uint256 immediatelyLockAmount = (dataBalance * 2) / 10;
immediatelyLockAmount < sysConfig.minImmediatelyLockAmount ? sysConfig.minImmediatelyLockAmount : immediatelyLockAmount;
return (normalLockAmount + immediatelyLockAmount, true);
return normalLockAmount + immediatelyLockAmount;
} else {
return (normalLockAmount, false);
return normalLockAmount;
}
}

function _getLockAmountByHash(
bytes32 dataMixedHash,
ShowType showType
) internal view returns (uint256) {
uint64 dataSize = PublicDataProof.lengthFromMixedHash(dataMixedHash);
return getLockAmount(dataSize, showType, _publicDatas[dataMixedHash].pledgeRate, _publicDatas[dataMixedHash].dataBalance);
}

function _LockSupplierPledge(
address supplierAddress,
bytes32 dataMixedHash,
ShowType showType
) internal returns (uint256, bool) {
) internal returns (uint256) {
_adjustSupplierBalance(supplierAddress);

SupplierInfo storage supplierInfo = _supplierInfos[supplierAddress];

(uint256 lockAmount, bool isImmediately) = _getLockAmountByHash(
dataMixedHash,
showType
);
uint256 lockAmount = _getLockAmountByHash(dataMixedHash, showType);

require(
supplierInfo.avalibleBalance >= lockAmount,
Expand All @@ -520,7 +526,7 @@ contract PublicDataStorage is
supplierInfo.avalibleBalance,
supplierInfo.lockedBalance
);
return (lockAmount, isImmediately);
return lockAmount;
}

function _verifyDataProof(
Expand Down Expand Up @@ -794,9 +800,13 @@ contract PublicDataStorage is
// challenge success!
_supplierInfos[proof.prover].lockedBalance -= proof.lockedAmount;

// send 80% punish income to challenger
uint256 rewardFromPunish = (proof.lockedAmount * 8) / 10;
gwtToken.transfer(msg.sender, rewardFromPunish);
gwtToken.transfer(foundationAddress, proof.lockedAmount - rewardFromPunish);

// deposit 20% punish income to dividend contract
gwtToken.approve(address(dividendContract), proof.lockedAmount - rewardFromPunish);
dividendContract.deposit(proof.lockedAmount - rewardFromPunish, address(gwtToken));

emit SupplierPunished(proof.prover, dataMixedHash, proof.lockedAmount);
emit SupplierBalanceChanged(proof.prover,
Expand All @@ -805,21 +815,17 @@ contract PublicDataStorage is
);
} else {
// challenge failed!
// TODO: emit event?
emit ChallengeFail(msg.sender, dataMixedHash, nonce_block);
return;
}
}

// lock supplier pledge
// Decide The Amount According To ShowType
PublicData storage publicDataInfo = _publicDatas[dataMixedHash];
(uint256 lockAmount, bool isImmediately) = _LockSupplierPledge(
msg.sender,
dataMixedHash,
showType
);
uint256 lockAmount = _LockSupplierPledge(msg.sender,dataMixedHash,showType);

if (isImmediately) {
if (showType == ShowType.Immediately) {
_onProofSuccess(proof, publicDataInfo, dataMixedHash, proof.prover);
}

Expand Down

0 comments on commit f175781

Please sign in to comment.