-
Notifications
You must be signed in to change notification settings - Fork 5
/
candidates.go
178 lines (155 loc) · 4.28 KB
/
candidates.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
167
168
169
170
171
172
173
174
175
176
177
178
package selection
import (
"crypto/sha256"
"fmt"
"github.com/BerithFoundation/berith-chain/common"
"github.com/BerithFoundation/berith-chain/params"
"math/big"
"math/rand"
)
const (
MaxMiner = 10000
)
var (
maxElectScore = int64(5000000)
minElectScore = int64(10000)
)
type Candidates struct {
selections []Candidate
total uint64 // Total Selection Point: Staking + Advantage
ts uint64
}
type JSONCandidates struct {
User []JSONCandidate `json:"user"`
Total uint64 `json:"total"`
}
func NewCandidates() *Candidates {
return &Candidates{
selections: make([]Candidate, 0),
total: 0,
ts: 0,
}
}
/*
[BERITH]
Function to register Staker to elect Block Creator
The function to be called later is the BlockCreator function.
*/
func (cs *Candidates) Add(c Candidate) {
cs.total += c.point
c.val = cs.total
cs.selections = append(cs.selections, c)
}
/*
[Berith]
The block constructor is selected and the result is returned in VoteResults.
*/
func (cs *Candidates) selectBlockCreator(config *params.ChainConfig, number uint64) VoteResults {
candidateCount := len(cs.selections)
queue := new(Queue).setQueueAsCandidates(candidateCount)
result := make(VoteResults)
currentElectScore := maxElectScore
electScoreGap := (maxElectScore - minElectScore) / int64(candidateCount)
// Block number is used as a seed so that all nodes have the same random value
rand.Seed(cs.GetSeed(config, number))
err := queue.enqueue(Range{
min: 0,
max: cs.total,
start: 0,
end: candidateCount,
})
if err != nil {
fmt.Println(err)
return result
}
for count := 1; count <= MaxMiner && queue.front != queue.rear; count++ {
r, err := queue.dequeue()
if err != nil {
fmt.Println(err)
return result
}
account := r.binarySearch(queue, cs)
result[account] = VoteResult{
Score: big.NewInt(currentElectScore + int64(cs.ts)),
Rank: count,
}
currentElectScore -= electScoreGap
}
return result
}
/*
[Berith]
The block constructor is selected and the result is returned in VoteResults.
*/
func (cs *Candidates) selectBIP3BlockCreator(config *params.ChainConfig, number uint64) VoteResults {
result := make(VoteResults)
currentElectScore := maxElectScore
electScoreGap := (maxElectScore - minElectScore) / int64(len(cs.selections))
rank := 1
// Block number is used as a seed so that all nodes have the same random value
rand.Seed(cs.GetSeed(config, number))
for len(cs.selections) > 0 {
// The random number below the total elected point is taken and used as the number to select the elected person.
electedNumber := uint64(rand.Int63n(int64(cs.total)))
// Search for candidates corresponding to electedNumber by binary search.
var chosen int
start := 0
end := len(cs.selections) - 1
for {
mid := (start + end) / 2
startElectRange := uint64(0)
if mid > 0 {
startElectRange = cs.selections[mid-1].val
}
endElectRange := cs.selections[mid].val
if electedNumber >= startElectRange && electedNumber <= endElectRange {
chosen = mid
cddt := cs.selections[mid]
result[cddt.address] = VoteResult{
Rank: rank,
Score: big.NewInt(currentElectScore),
}
currentElectScore -= electScoreGap
rank++
break
}
if electedNumber < startElectRange {
end = mid - 1
}
if electedNumber > endElectRange {
start = mid + 1
}
}
// Prepare for the selection of next-ranked candidates,
// except for the data of candidates already elected.
out := cs.selections[chosen]
for i := chosen; i+1 < len(cs.selections); i++ {
newCddt := cs.selections[i+1]
newCddt.val -= out.point
cs.selections[i] = newCddt
}
cs.selections = cs.selections[:len(cs.selections)-1]
cs.total -= out.point
}
return result
}
/*
[BERITH]
Function to convert block number to hash and force it to int64
Write the result value as Seed.
*/
func (cs Candidates) GetSeed(config *params.ChainConfig, number uint64) int64 {
// [Berith]
// Prior to IsBIP2, only 1 byte of the block number is used as a seed
// After IsBIP2, the entire block number is used as a seed
bt := []byte{byte(number)}
if config.IsBIP2(new(big.Int).SetUint64(number)) {
bt = big.NewInt(0).SetUint64(number).Bytes()
}
hash := sha256.New()
hash.Write(bt)
md := hash.Sum(nil)
h := common.BytesToHash(md)
seed := h.Big().Int64()
return seed
}