Skip to content

Commit

Permalink
Try use timestamp instead of blocknumber in public_data_store,GWT no …
Browse files Browse the repository at this point in the history
…need approve before send to contract...
  • Loading branch information
weiqiushi committed Feb 5, 2024
1 parent 0a220ed commit 92ca0d8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
8 changes: 8 additions & 0 deletions contracts/exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ contract Exchange is Initializable, UUPSUpgradeable, OwnableUpgradeable {
mapping (bytes32 => uint256) _mintDMC;

address mintAdmin;
uint256 version;
function initialize(address _dmcToken, address _gwtToken) public initializer {
__ExchangeUpgradable_init(_dmcToken, _gwtToken);
}
Expand All @@ -25,12 +26,19 @@ contract Exchange is Initializable, UUPSUpgradeable, OwnableUpgradeable {
dmcToken = DMCToken(_dmcToken);
gwtToken = GWTToken(_gwtToken);
mintAdmin = msg.sender;
version = 1;
}

function _authorizeUpgrade(address newImplementation) internal virtual override onlyOwner {

}

function updateV1(address _gwtToken) public onlyOwner {
require(version == 0, "already updated");
gwtToken = GWTToken(_gwtToken);
version = 1;
}

function setMintAdmin(address _mintAdmin) public onlyOwner {
mintAdmin = _mintAdmin;
}
Expand Down
13 changes: 13 additions & 0 deletions contracts/gwt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,17 @@ contract GWTToken is ERC20Burnable, Ownable {
function _update(address sender, address to, uint256 amount) internal override canTransfer(sender, to) {
super._update(sender, to, amount);
}

// only called from contract
function deductFrom(address spender, uint256 amount) public {
address sender = msg.sender;
require(allow_transfer[sender], "not allowed");
super._transfer(spender, sender, amount);
}

function burnFrom(address account, uint256 amount) public override {
address sender = msg.sender;
require(allow_minter[sender], "not allowed");
super._burn(account, amount);
}
}
73 changes: 43 additions & 30 deletions contracts/public_data_storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface IERC721VerifyDataHash{
* 可能有精度损失?
*/

contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable {
contract PublicDataStorageV1 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
enum ShowType { Normal, Immediately }

struct PublicData {
Expand Down Expand Up @@ -115,6 +115,8 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
SysConfig public sysConfig;
uint256 public totalRewardScore;

uint256 public version;

event SupplierBalanceChanged(address supplier, uint256 avalibleBalance, uint256 lockedBalance);
event GWTStacked(address supplier, uint256 amount);
event GWTUnstacked(address supplier, uint256 amount);
Expand All @@ -138,7 +140,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
__Ownable_init(msg.sender);

gwtToken = GWTToken(_gwtToken);
_startBlock = block.number;
_startBlock = block.timestamp;
_currectCycle = 0;
foundationAddress = _Foundation;
totalRewardScore = 1600;
Expand All @@ -147,14 +149,16 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
sysConfig.minDepositRatio = 64; // create data时最小为64倍
sysConfig.minPublicDataStorageWeeks = 96; // create data时最小为96周
sysConfig.minLockWeeks = 24; // show的时候最小为24周,目前固定为最小值
sysConfig.blocksPerCycle = 17280; // 每个cycle为72小时
sysConfig.blocksPerCycle = 3 days; // 每个cycle为72小时
sysConfig.topRewards = 32; // top 32名进榜
sysConfig.lockAfterShow = 11520; // show成功后48小时内才能解锁
sysConfig.showTimeout = 5760; // show之后24小时允许挑战
sysConfig.lockAfterShow = 2 days; // show成功后48小时内才能解锁
sysConfig.showTimeout = 24 hours; // show之后24小时允许挑战
sysConfig.maxNonceBlockDistance = 2; // 允许的nonce block距离, 要小于256
sysConfig.minRankingScore = 64; // 最小的排名分数
sysConfig.minDataSize = 1 << 27; // dataSize换算GWT时,最小值为128M
sysConfig.createDepositRatio = 5; // 因为初期推荐使用Immediate Show,这里会设置成5倍,可以让前十几个show都可以立即成立

version = 1;
}

function _authorizeUpgrade(address newImplementation) internal virtual override onlyOwner {
Expand All @@ -165,7 +169,6 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
for (uint i = 0; i < contractAddrs.length; i++) {
_allowedPublicDataContract[contractAddrs[i]] = true;
}

}

function denyPublicDataContract(address[] calldata contractAddrs) public onlyOwner {
Expand Down Expand Up @@ -205,16 +208,28 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
return remainScores[length];
}

function _cycleNumber(uint256 blockNumber, uint256 startBlock) internal view returns(uint256) {
uint cycleNumber = (blockNumber - startBlock) / sysConfig.blocksPerCycle;
if (cycleNumber * sysConfig.blocksPerCycle + startBlock < blockNumber) {
cycleNumber += 1;
function updateV1(uint256 startBlockTimestamp, address _gwtToken) public onlyOwner {
require(version == 0, "already updated");
_startBlock = startBlockTimestamp;
sysConfig.blocksPerCycle *= 15;
sysConfig.lockAfterShow *= 15;
sysConfig.showTimeout *= 15;

// maxNonceBlockDistance还保留块号逻辑

if (_gwtToken != address(0)) {
gwtToken = GWTToken(_gwtToken);
}
return cycleNumber;

version = 1;
}

function _curCycleNumber() internal view returns(uint256) {
return _cycleNumber(block.number, _startBlock);
uint cycleNumber = (block.timestamp - _startBlock) / sysConfig.blocksPerCycle;
if (cycleNumber * sysConfig.blocksPerCycle + _startBlock < block.timestamp) {
cycleNumber += 1;
}
return cycleNumber;
}

// 通过记录一个最后的周期来解决周期之间可能有空洞的问题
Expand Down Expand Up @@ -282,7 +297,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
publicDataInfo.maxDeposit = depositAmount;
publicDataInfo.sponsor = msg.sender;
publicDataInfo.dataContract = publicDataContract;
gwtToken.transferFrom(msg.sender, address(this), depositAmount);
gwtToken.deductFrom(msg.sender, depositAmount);

uint256 balance_add = (depositAmount * 8) / 10;
publicDataInfo.dataBalance += balance_add;
Expand Down Expand Up @@ -329,7 +344,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
require(publicDataInfo.maxDeposit > 0, "public data not exist");

// transfer deposit
gwtToken.transferFrom(msg.sender, address(this), depositAmount);
gwtToken.deductFrom(msg.sender, depositAmount);

uint256 balance_add = (depositAmount * 8) / 10;
publicDataInfo.dataBalance += balance_add;
Expand All @@ -355,14 +370,14 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable

function _adjustSupplierBalance(address supplier) internal {
SupplierInfo storage supplierInfo = _supplierInfos[supplier];
if (supplierInfo.unlockBlock < block.number) {
if (supplierInfo.unlockBlock < block.timestamp) {
supplierInfo.avalibleBalance += supplierInfo.lockedBalance;
supplierInfo.lockedBalance = 0;
}
}

function pledgeGwt(uint256 amount) public {
gwtToken.transferFrom(msg.sender, address(this), amount);
gwtToken.deductFrom(msg.sender, amount);
_supplierInfos[msg.sender].avalibleBalance += amount;

emit SupplierBalanceChanged(msg.sender, _supplierInfos[msg.sender].avalibleBalance, _supplierInfos[msg.sender].lockedBalance);
Expand Down Expand Up @@ -398,7 +413,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
require(supplierInfo.avalibleBalance >= lockAmount, "insufficient balance");
supplierInfo.avalibleBalance -= lockAmount;
supplierInfo.lockedBalance += lockAmount;
supplierInfo.unlockBlock = block.number + sysConfig.lockAfterShow;
supplierInfo.unlockBlock = block.timestamp + sysConfig.lockAfterShow;
emit SupplierBalanceChanged(supplierAddress, supplierInfo.avalibleBalance, supplierInfo.lockedBalance);
return (lockAmount, isImmediately);
}
Expand Down Expand Up @@ -494,16 +509,14 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
DataProof storage proof = _publicDataProofs[proofKey];

require(proof.proofBlockHeight > 0, "proof not exist");
require(block.number - proof.proofBlockHeight > sysConfig.showTimeout, "proof not unlock");
require(block.timestamp - proof.proofBlockHeight > sysConfig.showTimeout, "proof not unlock");

if (block.number - proof.proofBlockHeight > sysConfig.showTimeout) {
//Last Show Proof successed! 获得奖励+增加积分
PublicData storage publicDataInfo = _publicDatas[dataMixedHash];
_onProofSuccess(proof, publicDataInfo,dataMixedHash);

//防止重入:反复领取奖励
proof.proofBlockHeight = 0;
}
//Last Show Proof successed! 获得奖励+增加积分
PublicData storage publicDataInfo = _publicDatas[dataMixedHash];
_onProofSuccess(proof, publicDataInfo,dataMixedHash);

//防止重入:反复领取奖励
proof.proofBlockHeight = 0;
}

function showData(bytes32 dataMixedHash, uint256 nonce_block, uint32 index, bytes16[] calldata m_path, bytes calldata leafdata, ShowType showType) public {
Expand All @@ -517,7 +530,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
require(block.number - nonce_block <= sysConfig.maxNonceBlockDistance, "invalid nonce block");
isNewShow = true;
} else {
require(block.number - proof.proofBlockHeight <= sysConfig.showTimeout, "challenge timeout");
require(block.timestamp - proof.proofBlockHeight <= sysConfig.showTimeout, "challenge timeout");
}

(bytes32 root_hash,) = _verifyDataProof(dataMixedHash,nonce_block,index,m_path,leafdata);
Expand All @@ -530,7 +543,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
proof.lockedAmount = lockAmount;
proof.nonceBlockHeight = nonce_block;
proof.proofResult = root_hash;
proof.proofBlockHeight = block.number;
proof.proofBlockHeight = block.timestamp;
proof.prover = msg.sender;
//proof.showType = showType;

Expand Down Expand Up @@ -559,7 +572,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable

proof.lockedAmount = lockAmount;
proof.proofResult = root_hash;
proof.proofBlockHeight = block.number;
proof.proofBlockHeight = block.timestamp;
proof.prover = msg.sender;
//proof.showType = showType;
}
Expand All @@ -575,7 +588,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
function withdrawReward(uint256 cycleNumber, bytes32 dataMixedHash) public {
// 判断这次的cycle已经结束
//require(_currectCycle > cycleNumber, "cycle not finish");
require(block.number > cycleNumber * sysConfig.blocksPerCycle + _startBlock, "cycle not finish");
require(block.timestamp > cycleNumber * sysConfig.blocksPerCycle + _startBlock, "cycle not finish");
CycleInfo storage cycleInfo = _cycleInfos[cycleNumber];
CycleDataInfo storage dataInfo = cycleInfo.dataInfos[dataMixedHash];
//REVIEW:一次排序并保存的GAS和32次内存排序的成本问题?
Expand Down
2 changes: 1 addition & 1 deletion scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export async function main() {
await (await publicDataStorage.setSysConfig(setConfig)).wait();

let bridge = await (await ethers.getContractFactory("OwnedNFTBridge")).deploy();
await (await publicDataStorage.allowPublicDataContract(await bridge.getAddress())).wait()
await (await publicDataStorage.allowPublicDataContract([await bridge.getAddress()])).wait()
depolyedInfo.Bridge = await bridge.getAddress();
saveInfo();
}
Expand Down

0 comments on commit 92ca0d8

Please sign in to comment.