@@ -10,27 +10,11 @@ import (
10
10
"sync"
11
11
12
12
"github.com/iotexproject/iotex-address/address"
13
+
14
+ "github.com/iotexproject/iotex-core/action/protocol"
13
15
)
14
16
15
17
type (
16
- // CandidateCenter is the candidate center
17
- CandidateCenter interface {
18
- Size () int
19
- All () CandidateList
20
- Base () CandidateCenter
21
- Delta () CandidateList
22
- SetDelta (CandidateList ) error
23
- Commit () error
24
- ContainsName (string ) bool
25
- ContainsOwner (address.Address ) bool
26
- ContainsOperator (address.Address ) bool
27
- ContainsSelfStakingBucket (uint64 ) bool
28
- GetByName (string ) * Candidate
29
- GetByOwner (address.Address ) * Candidate
30
- GetBySelfStakingIndex (uint64 ) * Candidate
31
- Upsert (* Candidate ) error
32
- }
33
-
34
18
// candChange captures the change to candidates
35
19
candChange struct {
36
20
dirty map [string ]* Candidate
45
29
selfStkBucketMap map [uint64 ]* Candidate
46
30
}
47
31
48
- // candCenter is a struct to manage the candidates
49
- candCenter struct {
32
+ // CandidateCenter is a struct to manage the candidates
33
+ CandidateCenter struct {
50
34
base * candBase
51
35
size int
52
36
change * candChange
@@ -66,14 +50,14 @@ func listToCandChange(l CandidateList) (*candChange, error) {
66
50
return cv , nil
67
51
}
68
52
69
- // NewCandidateCenter creates an instance of candCenter
70
- func NewCandidateCenter (all CandidateList ) (CandidateCenter , error ) {
53
+ // NewCandidateCenter creates an instance of CandidateCenter
54
+ func NewCandidateCenter (all CandidateList ) (* CandidateCenter , error ) {
71
55
delta , err := listToCandChange (all )
72
56
if err != nil {
73
57
return nil , err
74
58
}
75
59
76
- c := candCenter {
60
+ c := CandidateCenter {
77
61
base : & candBase {
78
62
nameMap : make (map [string ]* Candidate ),
79
63
ownerMap : make (map [string ]* Candidate ),
@@ -94,12 +78,12 @@ func NewCandidateCenter(all CandidateList) (CandidateCenter, error) {
94
78
}
95
79
96
80
// Size returns number of candidates
97
- func (m * candCenter ) Size () int {
81
+ func (m * CandidateCenter ) Size () int {
98
82
return m .size
99
83
}
100
84
101
85
// All returns all candidates in candidate center
102
- func (m * candCenter ) All () CandidateList {
86
+ func (m * CandidateCenter ) All () CandidateList {
103
87
list := m .change .all ()
104
88
if list == nil {
105
89
return m .base .all ()
@@ -114,21 +98,21 @@ func (m *candCenter) All() CandidateList {
114
98
}
115
99
116
100
// Base returns the confirmed base state
117
- func (m candCenter ) Base () CandidateCenter {
118
- return & candCenter {
101
+ func (m CandidateCenter ) Base () * CandidateCenter {
102
+ return & CandidateCenter {
119
103
base : m .base ,
120
104
size : len (m .base .ownerMap ),
121
105
change : newCandChange (),
122
106
}
123
107
}
124
108
125
109
// Delta exports the pending changes
126
- func (m * candCenter ) Delta () CandidateList {
110
+ func (m * CandidateCenter ) Delta () CandidateList {
127
111
return m .change .all ()
128
112
}
129
113
130
114
// SetDelta sets the delta
131
- func (m * candCenter ) SetDelta (l CandidateList ) error {
115
+ func (m * CandidateCenter ) SetDelta (l CandidateList ) error {
132
116
if len (l ) == 0 {
133
117
m .change = nil
134
118
m .change = newCandChange ()
@@ -157,7 +141,7 @@ func (m *candCenter) SetDelta(l CandidateList) error {
157
141
}
158
142
159
143
// Commit writes the change into base
160
- func (m * candCenter ) Commit () error {
144
+ func (m * CandidateCenter ) Commit () error {
161
145
size , err := m .base .commit (m .change )
162
146
if err != nil {
163
147
return err
@@ -168,8 +152,19 @@ func (m *candCenter) Commit() error {
168
152
return nil
169
153
}
170
154
155
+ // Sync syncs the data from state manager
156
+ func (m * CandidateCenter ) Sync (sm protocol.StateManager ) error {
157
+ delta := CandidateList {}
158
+ if err := sm .Unload (protocolID , stakingCandCenter , & delta ); err != nil && err != protocol .ErrNoName {
159
+ return err
160
+ }
161
+
162
+ // apply delta to the center
163
+ return m .SetDelta (delta )
164
+ }
165
+
171
166
// ContainsName returns true if the map contains the candidate by name
172
- func (m * candCenter ) ContainsName (name string ) bool {
167
+ func (m * CandidateCenter ) ContainsName (name string ) bool {
173
168
if hit := m .change .containsName (name ); hit {
174
169
return true
175
170
}
@@ -181,7 +176,7 @@ func (m *candCenter) ContainsName(name string) bool {
181
176
}
182
177
183
178
// ContainsOwner returns true if the map contains the candidate by owner
184
- func (m * candCenter ) ContainsOwner (owner address.Address ) bool {
179
+ func (m * CandidateCenter ) ContainsOwner (owner address.Address ) bool {
185
180
if owner == nil {
186
181
return false
187
182
}
@@ -195,7 +190,7 @@ func (m *candCenter) ContainsOwner(owner address.Address) bool {
195
190
}
196
191
197
192
// ContainsOperator returns true if the map contains the candidate by operator
198
- func (m * candCenter ) ContainsOperator (operator address.Address ) bool {
193
+ func (m * CandidateCenter ) ContainsOperator (operator address.Address ) bool {
199
194
if operator == nil {
200
195
return false
201
196
}
@@ -211,7 +206,7 @@ func (m *candCenter) ContainsOperator(operator address.Address) bool {
211
206
}
212
207
213
208
// ContainsSelfStakingBucket returns true if the map contains the self staking bucket index
214
- func (m * candCenter ) ContainsSelfStakingBucket (index uint64 ) bool {
209
+ func (m * CandidateCenter ) ContainsSelfStakingBucket (index uint64 ) bool {
215
210
if hit := m .change .containsSelfStakingBucket (index ); hit {
216
211
return true
217
212
}
@@ -223,7 +218,7 @@ func (m *candCenter) ContainsSelfStakingBucket(index uint64) bool {
223
218
}
224
219
225
220
// GetByName returns the candidate by name
226
- func (m * candCenter ) GetByName (name string ) * Candidate {
221
+ func (m * CandidateCenter ) GetByName (name string ) * Candidate {
227
222
if d := m .change .getByName (name ); d != nil {
228
223
return d
229
224
}
@@ -235,7 +230,7 @@ func (m *candCenter) GetByName(name string) *Candidate {
235
230
}
236
231
237
232
// GetByOwner returns the candidate by owner
238
- func (m * candCenter ) GetByOwner (owner address.Address ) * Candidate {
233
+ func (m * CandidateCenter ) GetByOwner (owner address.Address ) * Candidate {
239
234
if owner == nil {
240
235
return nil
241
236
}
@@ -251,7 +246,7 @@ func (m *candCenter) GetByOwner(owner address.Address) *Candidate {
251
246
}
252
247
253
248
// GetBySelfStakingIndex returns the candidate by self-staking index
254
- func (m * candCenter ) GetBySelfStakingIndex (index uint64 ) * Candidate {
249
+ func (m * CandidateCenter ) GetBySelfStakingIndex (index uint64 ) * Candidate {
255
250
if d := m .change .getBySelfStakingIndex (index ); d != nil {
256
251
return d
257
252
}
@@ -263,7 +258,7 @@ func (m *candCenter) GetBySelfStakingIndex(index uint64) *Candidate {
263
258
}
264
259
265
260
// Upsert adds a candidate into map, overwrites if already exist
266
- func (m * candCenter ) Upsert (d * Candidate ) error {
261
+ func (m * CandidateCenter ) Upsert (d * Candidate ) error {
267
262
if err := d .Validate (); err != nil {
268
263
return err
269
264
}
@@ -282,7 +277,7 @@ func (m *candCenter) Upsert(d *Candidate) error {
282
277
return nil
283
278
}
284
279
285
- func (m * candCenter ) collision (d * Candidate ) error {
280
+ func (m * CandidateCenter ) collision (d * Candidate ) error {
286
281
if err := m .change .collision (d ); err != nil {
287
282
return err
288
283
}
0 commit comments