Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoSW diff adjustment #939

Merged
merged 14 commits into from
Mar 12, 2022
9 changes: 9 additions & 0 deletions devnet/singularity/cluster_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"DIFFICULTY_ADJUSTMENT_FACTOR": 1024,
"EPOCH_INTERVAL": 525600,
"POSW_CONFIG": {
"ENABLED": true,
"ENABLE_TIMESTAMP": 1645200000,
"DIFF_DIVIDER": 100,
"WINDOW_SIZE": 512,
"TOTAL_STAKE_PER_BLOCK": 10000000000000000000000,
"BOOST_TIMESTAMP": 1645372800,
"BOOST_MULTIPLER_PER_STEP": 2,
ping-ke marked this conversation as resolved.
Show resolved Hide resolved
"BOOST_STEPS": 10,
"BOOST_STEP_INTERVAL": 43200
}
},
"CHAINS": [
Expand Down
6 changes: 5 additions & 1 deletion mainnet/singularity/cluster_config_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@
"ENABLE_TIMESTAMP": 1569567600,
"DIFF_DIVIDER": 10000,
"WINDOW_SIZE": 512,
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000,
"BOOST_TIMESTAMP": 1646064000,
"BOOST_MULTIPLER_PER_STEP": 2,
"BOOST_STEPS": 10,
"BOOST_STEP_INTERVAL": 172800
}
},
"CHAINS": [
Expand Down
4 changes: 2 additions & 2 deletions quarkchain/cluster/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ async def get_stats(self):
shard["poswEnabled"] = config.ENABLED
shard["poswMinStake"] = config.TOTAL_STAKE_PER_BLOCK
shard["poswWindowSize"] = config.WINDOW_SIZE
shard["difficultyDivider"] = config.DIFF_DIVIDER
shard["difficultyDivider"] = config.get_diff_divider(shard_stats.timestamp)
shards.append(shard)
shards.sort(key=lambda x: x["fullShardId"])

Expand Down Expand Up @@ -1685,7 +1685,7 @@ async def get_work(
check(isinstance(block, RootBlock))
posw_mineable = await self.posw_mineable(block)
config = self.env.quark_chain_config.ROOT.POSW_CONFIG
return work, config.DIFF_DIVIDER if posw_mineable else None
return work, config.get_diff_divider(block.header.create_time) if posw_mineable else None

if branch.value not in self.branch_to_slaves:
return None, None
Expand Down
1 change: 1 addition & 0 deletions quarkchain/cluster/tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,7 @@ def add_root_block(addr, sign=False):
qkc_config.ROOT.POSW_CONFIG.WINDOW_SIZE = 2
# should always pass pow check if posw is applied
qkc_config.ROOT.POSW_CONFIG.DIFF_DIVIDER = 1000000
qkc_config.ROOT.POSW_CONFIG.BOOST_TIMESTAMP = 0
shard = next(iter(clusters[0].slave_list[0].shards.values()))

# monkey patch staking results
Expand Down
19 changes: 19 additions & 0 deletions quarkchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ class POSWConfig(BaseConfig):
# TODO: needs better tuning / estimating
# = total stakes / alpha
TOTAL_STAKE_PER_BLOCK = (10 ** 9) * QUARKSH_TO_JIAOZI
BOOST_TIMESTAMP = 0 # 0 mean Disable
BOOST_MULTIPLER_PER_STEP = 2
BOOST_STEPS = 10
BOOST_STEP_INTERVAL = 43200

def get_diff_divider(self, block_timestamp):
diff_divider = self.DIFF_DIVIDER
if 0 < self.BOOST_TIMESTAMP < block_timestamp:
steps = (block_timestamp - self.BOOST_TIMESTAMP) // self.BOOST_STEP_INTERVAL + 1
if steps > self.BOOST_STEPS:
steps = self.BOOST_STEPS

diff_divider = self.DIFF_DIVIDER * pow(self.BOOST_MULTIPLER_PER_STEP, steps)

return diff_divider


class ChainConfig(BaseConfig):
Expand Down Expand Up @@ -251,6 +266,10 @@ def __init__(self):
self.POSW_CONFIG.WINDOW_SIZE = 4320 # 72 hours
self.POSW_CONFIG.DIFF_DIVIDER = 1000
self.POSW_CONFIG.TOTAL_STAKE_PER_BLOCK = 240000 * QUARKSH_TO_JIAOZI
self.POSW_CONFIG.BOOST_TIMESTAMP = 0
self.POSW_CONFIG.BOOST_MULTIPLER_PER_STEP = 2
self.POSW_CONFIG.BOOST_STEPS = 10
self.POSW_CONFIG.BOOST_STEP_INTERVAL = 172800

def to_dict(self):
ret = super().to_dict()
Expand Down
42 changes: 38 additions & 4 deletions quarkchain/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ def test_serialization(self):
"ENABLE_TIMESTAMP": 0,
"DIFF_DIVIDER": 1000,
"WINDOW_SIZE": 4320,
"TOTAL_STAKE_PER_BLOCK": 240000000000000000000000
"TOTAL_STAKE_PER_BLOCK": 240000000000000000000000,
"BOOST_TIMESTAMP": 0,
"BOOST_MULTIPLER_PER_STEP": 2,
"BOOST_STEPS": 10,
"BOOST_STEP_INTERVAL": 172800
}
},
"CHAINS": [
Expand Down Expand Up @@ -115,7 +119,11 @@ def test_serialization(self):
"ENABLE_TIMESTAMP": 0,
"DIFF_DIVIDER": 20,
"WINDOW_SIZE": 256,
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000000
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000000,
"BOOST_TIMESTAMP": 0,
"BOOST_MULTIPLER_PER_STEP": 2,
"BOOST_STEPS": 10,
"BOOST_STEP_INTERVAL": 43200
},
"MAX_MINOR_BLOCKS_IN_MEMORY": 1536
},
Expand Down Expand Up @@ -153,7 +161,11 @@ def test_serialization(self):
"ENABLE_TIMESTAMP": 0,
"DIFF_DIVIDER": 20,
"WINDOW_SIZE": 256,
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000000
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000000,
"BOOST_TIMESTAMP": 0,
"BOOST_MULTIPLER_PER_STEP": 2,
"BOOST_STEPS": 10,
"BOOST_STEP_INTERVAL": 43200
},
"MAX_MINOR_BLOCKS_IN_MEMORY": 1536
},
Expand Down Expand Up @@ -191,7 +203,11 @@ def test_serialization(self):
"ENABLE_TIMESTAMP": 0,
"DIFF_DIVIDER": 20,
"WINDOW_SIZE": 256,
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000000
"TOTAL_STAKE_PER_BLOCK": 1000000000000000000000000000,
"BOOST_TIMESTAMP": 0,
"BOOST_MULTIPLER_PER_STEP": 2,
"BOOST_STEPS": 10,
"BOOST_STEP_INTERVAL": 43200
},
"MAX_MINOR_BLOCKS_IN_MEMORY": 1536
}
Expand Down Expand Up @@ -323,3 +339,21 @@ def test_special_contract_enable_ts(self):
env.cluster_config = cluster_config
for addr in PRECOMPILED_CONTRACTS_AFTER_EVM_ENABLED:
self.assertEqual(specials[addr][1], 123)

def test_get_diff_divider(self):
block_timestamp = 1646064000
config = QuarkChainConfig().ROOT.POSW_CONFIG
config.BOOST_TIMESTAMP = 0
self.assertEqual(config.DIFF_DIVIDER, config.get_diff_divider(block_timestamp))
config.BOOST_TIMESTAMP = block_timestamp + 1
self.assertEqual(config.DIFF_DIVIDER, config.get_diff_divider(block_timestamp))
config.BOOST_TIMESTAMP = block_timestamp - 1
self.assertEqual(config.DIFF_DIVIDER * config.BOOST_MULTIPLER_PER_STEP,
config.get_diff_divider(block_timestamp))
config.BOOST_TIMESTAMP = block_timestamp - config.BOOST_STEP_INTERVAL * config.BOOST_STEPS + 1
self.assertEqual(config.DIFF_DIVIDER * pow(config.BOOST_MULTIPLER_PER_STEP, config.BOOST_STEPS),
config.get_diff_divider(block_timestamp))
config.BOOST_TIMESTAMP = block_timestamp - config.BOOST_STEP_INTERVAL * config.BOOST_STEPS - 1
self.assertEqual(config.DIFF_DIVIDER * pow(config.BOOST_MULTIPLER_PER_STEP, config.BOOST_STEPS),
config.get_diff_divider(block_timestamp))