-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockReward.go
166 lines (138 loc) · 5.39 KB
/
blockReward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ethapi
import (
"math/big"
"github.com/dece-cash/go-dece/core/types"
)
var (
big1 = big.NewInt(1)
big2 = big.NewInt(2)
big6 = big.NewInt(6)
big9 = big.NewInt(9)
bigMinus99 = big.NewInt(-99)
bigOne *big.Int = big.NewInt(1)
big200W *big.Int = big.NewInt(2000000)
base = big.NewInt(1e+17)
big100 = big.NewInt(100)
oneSero = new(big.Int).Mul(big.NewInt(10), base)
lReward = new(big.Int).Mul(big.NewInt(176), base)
hReward = new(big.Int).Mul(big.NewInt(445), base)
hRewardV4 = new(big.Int).Mul(big.NewInt(356), base)
argA, _ = new(big.Int).SetString("985347985347985", 10)
argB, _ = new(big.Int).SetString("16910256410256400000", 10)
oriReward = new(big.Int).Mul(big.NewInt(66773505743), big.NewInt(1000000000))
interval = big.NewInt(8294400)
halveNimber = big.NewInt(3057600)
difficultyL1 = big.NewInt(340000000)
difficultyL2 = big.NewInt(1700000000)
difficultyL3 = big.NewInt(4000000000)
difficultyL4 = big.NewInt(17000000000)
)
func accumulateRewardsV1(diff *big.Int, gasUsed uint64, gasLimit uint64) *big.Int {
reward := new(big.Int).Mul(big.NewInt(350), base)
difficulty := big.NewInt(1717986918)
if diff.Cmp(difficulty) < 0 {
ratio := new(big.Int).Div(new(big.Int).Mul(diff, big100), difficulty).Uint64()
if ratio >= 80 {
reward = reward.Mul(reward, big.NewInt(4)).Div(reward, big.NewInt(5))
} else if ratio >= 60 {
reward = reward.Mul(reward, big.NewInt(3)).Div(reward, big.NewInt(5))
} else if ratio >= 40 {
reward = reward.Mul(reward, big.NewInt(2)).Div(reward, big.NewInt(5))
} else if ratio >= 20 {
reward = reward.Mul(reward, big.NewInt(1)).Div(reward, big.NewInt(5))
} else {
reward = big.NewInt(0).Set(oneSero)
}
}
ratio := new(big.Int).Div(new(big.Int).Mul(new(big.Int).SetUint64(gasUsed), big100), new(big.Int).SetUint64(gasLimit)).Uint64()
if ratio >= 80 {
reward = new(big.Int).Div(new(big.Int).Mul(reward, big6), big.NewInt(5))
} else {
reward = reward.Mul(reward, big.NewInt(4)).Div(reward, big.NewInt(5))
}
if reward.Cmp(oneSero) < 0 {
reward = big.NewInt(0).Set(oneSero)
}
return reward
}
func accumulateRewardsV2(number, diff *big.Int) [2]*big.Int {
var res [2]*big.Int
rewardStd := new(big.Int).Set(oriReward)
if number.Uint64() >= halveNimber.Uint64() {
i := new(big.Int).Add(new(big.Int).Div(new(big.Int).Sub(number, halveNimber), interval), big1)
rewardStd.Div(rewardStd, new(big.Int).Exp(big2, i, nil))
}
var reward *big.Int
if diff.Cmp(difficultyL1) < 0 { // <3.4
reward = new(big.Int).Mul(big.NewInt(10), base)
} else if diff.Cmp(difficultyL2) < 0 { // <17
ratio := new(big.Int).Add(new(big.Int).Mul(big.NewInt(56), base), new(big.Int).Mul(big.NewInt(16470000000), new(big.Int).Sub(diff, difficultyL1)))
reward = new(big.Int).Div(new(big.Int).Mul(rewardStd, ratio), oriReward)
} else if diff.Cmp(difficultyL3) < 0 { // <40
ratio := new(big.Int).Add(new(big.Int).Mul(big.NewInt(280), base), new(big.Int).Mul(big.NewInt(2170000000), new(big.Int).Sub(diff, difficultyL2)))
reward = new(big.Int).Div(new(big.Int).Mul(rewardStd, ratio), oriReward)
} else if diff.Cmp(difficultyL4) < 0 { // <170
ratio := new(big.Int).Add(new(big.Int).Mul(big.NewInt(330), base), new(big.Int).Mul(big.NewInt(2590000000), new(big.Int).Sub(diff, difficultyL3)))
reward = new(big.Int).Div(new(big.Int).Mul(rewardStd, ratio), oriReward)
} else {
reward = rewardStd
}
res[0] = reward
res[1] = new(big.Int).Div(rewardStd, big.NewInt(15))
return res
}
func accumulateRewardsV3(number, bdiff *big.Int) [2]*big.Int {
var res [2]*big.Int
diff := new(big.Int).Div(bdiff, big.NewInt(1000000000))
reward := new(big.Int).Add(new(big.Int).Mul(argA, diff), argB)
if reward.Cmp(lReward) < 0 {
reward = new(big.Int).Set(lReward)
} else if reward.Cmp(hReward) > 0 {
reward = new(big.Int).Set(hReward)
}
i := new(big.Int).Add(new(big.Int).Div(new(big.Int).Sub(number, halveNimber), interval), big1)
reward.Div(reward, new(big.Int).Exp(big2, i, nil))
res[0] = reward
res[1] = big.NewInt(0)
return res
}
func accumulateRewardsV4(number, bdiff *big.Int) [2]*big.Int {
var res [2]*big.Int
diff := new(big.Int).Div(bdiff, big.NewInt(1000000000))
reward := new(big.Int).Add(new(big.Int).Mul(argA, diff), argB)
if reward.Cmp(lReward) < 0 {
reward = new(big.Int).Set(lReward)
} else if reward.Cmp(hRewardV4) > 0 {
reward = new(big.Int).Set(hRewardV4)
}
i := new(big.Int).Add(new(big.Int).Div(new(big.Int).Sub(number, halveNimber), interval), big1)
reward.Div(reward, new(big.Int).Exp(big2, i, nil))
res[0] = reward
res[1] = big.NewInt(0)
return res
}
func accumulateRewardsV5(number, bdiff *big.Int) [2]*big.Int {
var res [2]*big.Int
diff := new(big.Int).Div(bdiff, big.NewInt(1000000000))
reward := new(big.Int).Add(new(big.Int).Mul(argA, diff), argB)
if reward.Cmp(lReward) < 0 {
reward = new(big.Int).Set(lReward)
} else if reward.Cmp(hRewardV4) > 0 {
reward = new(big.Int).Set(hRewardV4)
}
i := new(big.Int).Add(new(big.Int).Div(new(big.Int).Sub(number, halveNimber), interval), big1)
reward.Div(reward, new(big.Int).Exp(big2, new(big.Int).Add(i, big1), nil))
res[0] = reward
res[1] = big.NewInt(0)
return res
}
/**
[0] block reward
[1] community reward
[2] team reward
*/
func GetBlockReward(block *types.Block) [2]*big.Int {
number := block.Number()
diff := block.Difficulty()
return accumulateRewardsV5(number, diff)
}