aboutsummaryrefslogtreecommitdiffstats
path: root/consensus
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2019-03-26 11:16:22 +0800
committerGitHub <noreply@github.com>2019-03-26 11:16:22 +0800
commit54ab3efcd95426d2f126a7b0fe99dd0a4751b944 (patch)
tree3490a2f367a6a51dff2d89fdd8a8610d20ca98a0 /consensus
parent6031f16bddde8913b774c9583e750012a8d83743 (diff)
downloaddexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.tar
dexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.tar.gz
dexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.tar.bz2
dexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.tar.lz
dexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.tar.xz
dexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.tar.zst
dexon-54ab3efcd95426d2f126a7b0fe99dd0a4751b944.zip
core: set extended round block reward to zero (#298)
To discourage DKG set from prolonging the round indefinitely, we set the block reward of the extended round to 0. The gas fee is send to the DEXON governance owner for safe keeping and later used by the foundation.
Diffstat (limited to 'consensus')
-rw-r--r--consensus/dexcon/dexcon.go52
-rw-r--r--consensus/dexcon/dexcon_test.go2
2 files changed, 35 insertions, 19 deletions
diff --git a/consensus/dexcon/dexcon.go b/consensus/dexcon/dexcon.go
index 3e520438b..0b6fa192f 100644
--- a/consensus/dexcon/dexcon.go
+++ b/consensus/dexcon/dexcon.go
@@ -111,8 +111,21 @@ func (d *Dexcon) Prepare(chain consensus.ChainReader, header *types.Header) erro
return nil
}
-func (d *Dexcon) calculateBlockReward(round int64, state *state.StateDB) *big.Int {
- gs := d.govStateFetcer.GetStateForConfigAtRound(uint64(round))
+func (d *Dexcon) inExtendedRound(header *types.Header, state *state.StateDB) bool {
+ gs := vm.GovernanceState{state}
+ rgs := d.govStateFetcer.GetStateForConfigAtRound(header.Round)
+
+ roundEnd := gs.RoundHeight(new(big.Int).SetUint64(header.Round)).Uint64() + rgs.RoundLength().Uint64()
+
+ // Round 0 starts and height 0 instead of height 1.
+ if header.Round == 0 {
+ roundEnd += 1
+ }
+ return header.Number.Uint64() >= roundEnd
+}
+
+func (d *Dexcon) calculateBlockReward(round uint64) *big.Int {
+ gs := d.govStateFetcer.GetStateForConfigAtRound(round)
config := gs.Configuration()
blocksPerRound := config.RoundLength
@@ -177,24 +190,27 @@ func (d *Dexcon) Finalize(chain consensus.ChainReader, header *types.Header, sta
}
// Distribute block reward and halving condition.
- if header.Coinbase == (common.Address{}) {
- header.Reward = new(big.Int)
- } else {
- reward := d.calculateBlockReward(int64(header.Round), state)
- state.AddBalance(header.Coinbase, reward)
- header.Reward = reward
- gs.IncTotalSupply(reward)
-
- // Record last proposed height.
- gs.PutLastProposedHeight(header.Coinbase, header.Number)
-
- // Check if halving checkpoint reached.
- config := gs.Configuration()
- if gs.TotalSupply().Cmp(config.NextHalvingSupply) >= 0 {
- gs.MiningHalved()
- }
+ reward := new(big.Int)
+
+ // If this is not an empty block and we are not in extended round, calculate
+ // the block reward.
+ if header.Coinbase != (common.Address{}) && !d.inExtendedRound(header, state) {
+ reward = d.calculateBlockReward(header.Round)
+ }
+
+ header.Reward = reward
+ state.AddBalance(header.Coinbase, reward)
+ gs.IncTotalSupply(reward)
+
+ // Check if halving checkpoint reached.
+ config := gs.Configuration()
+ if gs.TotalSupply().Cmp(config.NextHalvingSupply) >= 0 {
+ gs.MiningHalved()
}
+ // Record last proposed height.
+ gs.PutLastProposedHeight(header.Coinbase, header.Number)
+
header.Root = state.IntermediateRoot(true)
return types.NewBlock(header, txs, uncles, receipts), nil
}
diff --git a/consensus/dexcon/dexcon_test.go b/consensus/dexcon/dexcon_test.go
index 0181a80f3..bfded8db8 100644
--- a/consensus/dexcon/dexcon_test.go
+++ b/consensus/dexcon/dexcon_test.go
@@ -96,7 +96,7 @@ func (d *DexconTestSuite) TestBlockRewardCalculation() {
// blockReard = miningVelocity * totalStaked * roundInterval / aYear / numBlocksInCurRound
// 0.1875 * 1e18 * 3600 * 1000 / (86400 * 1000 * 365 * 3600) = 5945585996.96
- d.Require().Equal(big.NewInt(5945585996), consensus.calculateBlockReward(0, d.stateDB))
+ d.Require().Equal(big.NewInt(5945585996), consensus.calculateBlockReward(0))
}
func TestDexcon(t *testing.T) {