Skip to content

Commit

Permalink
Modify public_data_storage contract according to the dmcx economic do…
Browse files Browse the repository at this point in the history
…cument
  • Loading branch information
weiqiushi committed Aug 16, 2024
1 parent f175781 commit f063f75
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 65 deletions.
102 changes: 56 additions & 46 deletions contracts/public_data_storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ contract PublicDataStorage is
Immediately
}

struct ShowRecord {
uint256 showTime;
uint256 showCycle;
}

struct PublicData {
address sponsor;
address dataContract;
uint256 maxDeposit;
uint256 dataBalance;
uint64 pledgeRate;
mapping(address => uint256) show_records; //miner address - > last show time
mapping(address => ShowRecord) show_records; //miner address - > last show time
}

struct PublicDataForOutput {
Expand All @@ -82,6 +87,7 @@ contract PublicDataStorage is
uint256 lockedBalance;
uint256 unlockTime;
uint256 lastShowBlock;
string supplierExtra;
}

GWT public gwtToken; // Gb per Week Token
Expand All @@ -103,9 +109,10 @@ contract PublicDataStorage is
struct CycleInfo {
mapping(bytes32 => CycleDataInfo) dataInfos;
SortedScoreList.List scoreList;
uint256 totalAward; // Record the total reward of this Cycle
uint256 totalShowPower; //Record the support of this cycle
uint256 totalAward; // Record the total reward of this Cycle
uint256 totalShowPower; //Record the support of this cycle
uint256 cycleStartTime;
uint256 cycleGrowthRate; // from 5 to 10000, Thousandths
}

struct CycleOutputInfo {
Expand Down Expand Up @@ -182,7 +189,7 @@ contract PublicDataStorage is
__Ownable_init(msg.sender);

gwtToken = GWT(_gwtToken);
currectCycle = 2;
currectCycle = 2; // Start from cycle 2. Cycle 0 and 1 treats as initial cycle, its power should be 0;
dividendContract = DividendContract(payable(_dividendContract));
totalRewardScore = 1600;

Expand Down Expand Up @@ -269,6 +276,20 @@ contract PublicDataStorage is
uint16 remainScore = _getRemainScore(curCycleInfo.scoreList.length());
uint256 remainReward = (lastCycleReward * 4 * remainScore) / totalRewardScore / 5;

// Calculate the GrowthRate of this cycle when the cycle ends
uint256 last_power = _cycleInfos[currectCycle - 1].totalShowPower;
if (last_power > 0 && curCycleInfo.totalShowPower > last_power*1005/1000) {
uint256 growth_rate = (curCycleInfo.totalShowPower - last_power) * 1000 / last_power;
if (growth_rate > 10000) {
growth_rate = 10000;
}

curCycleInfo.cycleGrowthRate = growth_rate;
} else {
curCycleInfo.cycleGrowthRate = 5;
}

// move to next cycle
currectCycle += 1;
_cycleInfos[currectCycle].cycleStartTime = block.timestamp;
_cycleInfos[currectCycle].totalAward = lastCycleReward - ((lastCycleReward * 4) / 5) - fundationIncome + remainReward;
Expand Down Expand Up @@ -392,7 +413,7 @@ contract PublicDataStorage is
);
}

function getPledgeInfo(
function getSupplierInfo(
address supplier
) public view returns (SupplierInfo memory) {
return _supplierInfos[supplier];
Expand All @@ -408,7 +429,16 @@ contract PublicDataStorage is
return _getDataOwner(dataMixedHash, _publicDatas[dataMixedHash]);
}


function setSupplierExtra(string calldata extra) public {
SupplierInfo storage supplierInfo = _supplierInfos[msg.sender];
require(supplierInfo.avalibleBalance + supplierInfo.lockedBalance > 0, "MUST pledge first");
supplierInfo.supplierExtra = extra;
}

function getSupplierExtra(address supplier) public view returns (string memory) {
return _supplierInfos[supplier].supplierExtra;
}

/**
* @dev Adds a deposit to the public data storage,If this recharge exceeds 10%of the maximum recharge amount, the sponser that updates the public data is msg.sender
* @param dataMixedHash The hash of the mixed data.
Expand Down Expand Up @@ -594,8 +624,9 @@ contract PublicDataStorage is
}
}

// Since the unit of CyclePower is GB, first expand 1024*1024*1000, and then calculate
function _getGWTDifficultRatio(uint256 lastCyclePower, uint256 curCyclePower) public pure returns (uint256) {
// growth_rate is thousandths, 5-10000
// also return thousandths.
function _getGWTDifficultRatio(uint256 total_size, uint256 growth_rate) public pure returns (uint256) {
// This function is essentially, the weekly interest rate is returned
// 1) Calculate the basic difficulty values ​​according to the total power. Each time the computing power doubles, the basic difficulty value will decrease from <= 1pb, 2pb, 4pb, 8pb, 16pb, 32pb, 64pb, 128pb, 256pb, 512pb ..Adjust the foundation difficulty
// The multiplier result is 8X -1X. When the total power is 1PB (GWT), the multiplier rate is 8X, and the computing power then doubles, and the magnification decreases by 10%.Multiple rate = 0.9^(log2 (support/1pb)), double the total power each time, the multiplier is 90%
Expand All @@ -605,34 +636,10 @@ contract PublicDataStorage is
// According to the above rules, the largest GWT mining ratio is the largest, 16%of the total mortgage (16%of the weekly return).That is, the miners pledged 100 GWTs in public data mining. After 1 week, they could dig out 16 GWT, which is close to 6.25 weeks.
// If the total computing power is low in the early days, but no one digs it, the weekly return is 1.6%(1.6%of the weekly return), that is, the miners pledged 100 GWTs in public data mining.1.6 GWT, close to 62.5 weeks back

//uint256 base_r = 0.002;
uint256 base_r = 2097152;
if (curCyclePower == 0) {
// base_r = 0.01
base_r = 10485760;
} else {
// A mathematical function, satisfying: y = f (x), the meaning of X is that the value domain of the growth rate is from [0, positive infinity] Y's value range is 0.2%, and the maximum value is 2%.I hope that before X is 200%(2 times), Y can quickly increase to 1%
if (curCyclePower > lastCyclePower) {
base_r += (8 * (curCyclePower - lastCyclePower) * 1024 * 1024 * 1000) / lastCyclePower;
//base_r = 0.002 + (0.008 * (curCyclePower - lastCyclePower)) / lastCyclePower;
if (base_r > 20971520) {
base_r = 20971520;
}
}
}
// 8 * 0.9^(log2((curCyclePower / 1PB)));
// CurcyclePower's units are GB
int128 exp1 = ABDKMath64x64.fromUInt(curCyclePower).log_2().toInt() - 20;
if (exp1 < 0) {
exp1 = 0;
}
uint256 ratio = ABDKMath64x64.divu(9, 10).pow(uint256(int256(exp1))).toUInt() * 8;
//uint256 ratio = 8 * ((9/10)^(log2(curCyclePower / 1024 / 1024)));
// = 8 * 0.9^(log2(curCyclePower) - 20)
if (ratio < 1) {
ratio = 1;
}
return ratio * base_r;
// m = 1024*1024 - 1024*8*growth_rate
// result = (0.243*m) / (total_size+0.867*m) + 0.02
uint256 m = 1024 * (1024*10000 - 8*growth_rate);
return (243*m)*1000 / (total_size*1000 + 867*m) + 20;
}

function _onProofSuccess(
Expand Down Expand Up @@ -672,17 +679,18 @@ contract PublicDataStorage is
// Update Cycle's Last Shower
_updateLastSupplier(dataInfo, challengeAddr, msg.sender);

uint256 lastRecordShowTime = publicDataInfo.show_records[msg.sender];
if (lastRecordShowTime == 0) {
publicDataInfo.show_records[msg.sender] = block.timestamp;
ShowRecord storage lastShowRecord = publicDataInfo.show_records[msg.sender];
if (lastShowRecord.showTime == 0) {
publicDataInfo.show_records[msg.sender] = ShowRecord(block.timestamp, currectCycle);
} else {
//The second SHOW! Get GWT mining award
if (block.timestamp - lastRecordShowTime > 1 weeks) {
uint256 showDeltaTime = block.timestamp - lastShowRecord.showTime;
if (showDeltaTime > 1 weeks) {
// calcute the valid storage power
// reward = size * T * pledgeRate * difficultRatio
// T = currect time - last show time, T must be greater than 1 week, up to 4 weeks
publicDataInfo.show_records[msg.sender] = block.timestamp;
uint256 storageWeeks = (block.timestamp - lastRecordShowTime) / 1 weeks;
publicDataInfo.show_records[msg.sender] = ShowRecord(block.timestamp, currectCycle);
uint256 storageWeeks = (showDeltaTime) / 1 weeks;
if (storageWeeks > 8) {
storageWeeks = 8;
}
Expand All @@ -696,10 +704,12 @@ contract PublicDataStorage is
cycleInfo.totalShowPower += storagePower ;

// the difficult ratio is calculated by the total power of the current cycle and the total power of the last cycle
uint256 lastCyclePower = _cycleInfos[currectCycle - 2].totalShowPower;
uint256 curCyclePower = _cycleInfos[currectCycle - 1].totalShowPower;
// because the return of _getGWTDifficultRatio is expanded by 1024*1024*1000 times, so here to divide
uint256 gwtReward = storagePower * _getGWTDifficultRatio(lastCyclePower, curCyclePower) / 1024 / 1024 / 1000;
CycleInfo storage lastShowCycle = _cycleInfos[lastShowRecord.showCycle];
CycleInfo storage curCycle = _cycleInfos[currectCycle - 1];
uint256 avgShowPower = (lastShowCycle.totalShowPower + curCycle.totalShowPower) / 2;
uint256 avgGrowthRate = (lastShowCycle.cycleGrowthRate + curCycle.cycleGrowthRate) / 2;
// _getGWTDifficultRatio return thousandths
uint256 gwtReward = storagePower * _getGWTDifficultRatio(avgShowPower, avgGrowthRate) * (10 ** 18) / 1000;

// 80% of the reward is given to the miner, and 20% is given to the current data balance
gwtToken.mint(msg.sender, gwtReward * 8 / 10);
Expand Down
83 changes: 64 additions & 19 deletions doc/economic_sim/dmcx_economic_sim.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 126,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 127,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -290,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 128,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -395,7 +395,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 129,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -500,7 +500,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 130,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -615,23 +615,26 @@
},
{
"cell_type": "code",
"execution_count": 239,
"execution_count": 131,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"public data hash0 min stake amount:3.84 GWT,immediate stake amount:213.84 GWT\n",
"public data hash3 min stake amount:1996.8000000000002 GWT,immediate stake amount:4821.72922698268 GWT\n",
"show data hash0 get blance reward: 38.087807115830266\n",
"show data hash3 get blance reward: 1412.46461349134\n",
"total_size:1225014.5142455557,growth_rate:5.036804229031415,difficult ratio:0.13665198936584874\n",
"show data hash0 get blance reward:34.27902640424724,power:402.2857142857143,power reward:43.97851452048344\n",
"total_size:1220534.497522133,growth_rate:5.0025,difficult ratio:0.13692058391200523\n",
"show data hash3 get blance reward:1271.2181521422062,power:5895.314285714287,power reward:645.751899475829\n",
"total_size:1551717.2603819617,growth_rate:0.042835620204552824,difficult ratio:0.12352194901444982\n",
"show data hash3 get blance reward:1156.2041850431574,power:5705.142857142857,power reward:563.7682920961221\n"
"public data hash3 min stake amount:1996.8000000000002 GWT,immediate stake amount:5830.656000000001 GWT\n",
"show data hash0 get blance reward: 46.080000000000005\n",
"show data hash3 get blance reward: 1916.9280000000003\n",
"get cycle index: 6, show power 994785.972943761\n",
"total_size:1088030.1932454647,growth_rate:5.0025,difficult ratio:0.1448184010709371\n",
"show data hash0 get blance reward:41.47200000000001,power:402.2857142857143,power reward:46.60669913322959\n",
"get cycle index: 13, show power 1409782.4952518851\n",
"total_size:1295528.4543995268,growth_rate:5.080889500612143,difficult ratio:0.13283524644157663\n",
"show data hash3 get blance reward:1725.2352000000003,power:5895.314285714287,power reward:626.4844207947236\n",
"get cycle index: 23, show power 2569215.6682717884\n",
"total_size:1992726.2422563,growth_rate:0.02281478249322075,difficult ratio:0.10779692707060394\n",
"show data hash3 get blance reward:1564.4582628899013,power:5705.142857142857,power reward:491.99749479904443\n"
]
}
],
Expand All @@ -642,7 +645,6 @@
"global compute_power_cycles\n",
"compute_power_cycles = []\n",
"\n",
"\n",
"def covert_timestr_to_timestamp(timestr):\n",
" dt = datetime.strptime(timestr, \"%Y-%m-%d %H:%M:%S\")\n",
" return dt.timestamp()\n",
Expand Down Expand Up @@ -704,6 +706,7 @@
"\n",
" last_cycle_info = compute_power_cycles[last_show_cycle]\n",
" this_cycle_info = compute_power_cycles[cycle_index-1]\n",
" print(f\"get cycle index: {cycle_index}, show power {this_cycle_info['total_power']}\")\n",
" \n",
" compute_power = cacl_compute_power(data_info[\"size\"],data_info[\"pelgement\"],duration)\n",
"\n",
Expand Down Expand Up @@ -820,9 +823,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 132,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"reward rank 0 ,reward percent:0.15\n",
"reward rank 1 ,reward percent:0.1125\n",
"reward rank 2 ,reward percent:0.09375\n",
"reward rank 3 ,reward percent:0.075\n",
"reward rank 4 ,reward percent:0.0625\n",
"reward rank 5 ,reward percent:0.05\n",
"reward rank 6 ,reward percent:0.0375\n",
"reward rank 7 ,reward percent:0.033125\n",
"reward rank 8 ,reward percent:0.02625\n",
"reward rank 9 ,reward percent:0.0225\n",
"reward rank 10 ,reward percent:0.021875\n",
"reward rank 11 ,reward percent:0.02125\n",
"reward rank 12 ,reward percent:0.020625\n",
"reward rank 13 ,reward percent:0.02\n",
"reward rank 14 ,reward percent:0.019375\n",
"reward rank 15 ,reward percent:0.01875\n",
"reward rank 16 ,reward percent:0.018125\n",
"reward rank 17 ,reward percent:0.0175\n",
"reward rank 18 ,reward percent:0.016875\n",
"reward rank 19 ,reward percent:0.01625\n",
"reward rank 20 ,reward percent:0.015625\n",
"reward rank 21 ,reward percent:0.015\n",
"reward rank 22 ,reward percent:0.014375\n",
"reward rank 23 ,reward percent:0.01375\n",
"reward rank 24 ,reward percent:0.013125\n",
"reward rank 25 ,reward percent:0.0125\n",
"reward rank 26 ,reward percent:0.011875\n",
"reward rank 27 ,reward percent:0.01125\n",
"reward rank 28 ,reward percent:0.010625\n",
"reward rank 29 ,reward percent:0.01\n",
"reward rank 30 ,reward percent:0.009375\n",
"reward rank 31 ,reward percent:0.00875\n",
"=============================================\n",
"rank 12 ,pool reword:140020\n",
"\t total reward:2310.33,sponsor reward:1155.165,owner reward:462.06600000000003,supplier reward:138.6198\n"
]
}
],
"source": [
"def get_winner_reward_percent(reword_rank):\n",
" rewardScores = [\n",
Expand Down Expand Up @@ -956,7 +1001,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.2"
}
},
"nbformat": 4,
Expand Down

0 comments on commit f063f75

Please sign in to comment.