Skip to content

Commit 7394593

Browse files
authored
remove CandidateCenter interface (iotexproject#2337)
1 parent 07d3851 commit 7394593

6 files changed

+48
-63
lines changed

action/protocol/staking/bucket_pool.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ func (bp *BucketPool) Copy(enableSMStorage bool) *BucketPool {
149149
return &pool
150150
}
151151

152-
// SyncPool sync the data from state manager
153-
func (bp *BucketPool) SyncPool(sm protocol.StateManager) error {
152+
// Sync syncs the data from state manager
153+
func (bp *BucketPool) Sync(sm protocol.StateManager) error {
154154
if bp.enableSMStorage {
155155
_, err := sm.State(bp.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
156156
return err

action/protocol/staking/candidate_center.go

+34-39
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,11 @@ import (
1010
"sync"
1111

1212
"github.com/iotexproject/iotex-address/address"
13+
14+
"github.com/iotexproject/iotex-core/action/protocol"
1315
)
1416

1517
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-
3418
// candChange captures the change to candidates
3519
candChange struct {
3620
dirty map[string]*Candidate
@@ -45,8 +29,8 @@ type (
4529
selfStkBucketMap map[uint64]*Candidate
4630
}
4731

48-
// candCenter is a struct to manage the candidates
49-
candCenter struct {
32+
// CandidateCenter is a struct to manage the candidates
33+
CandidateCenter struct {
5034
base *candBase
5135
size int
5236
change *candChange
@@ -66,14 +50,14 @@ func listToCandChange(l CandidateList) (*candChange, error) {
6650
return cv, nil
6751
}
6852

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) {
7155
delta, err := listToCandChange(all)
7256
if err != nil {
7357
return nil, err
7458
}
7559

76-
c := candCenter{
60+
c := CandidateCenter{
7761
base: &candBase{
7862
nameMap: make(map[string]*Candidate),
7963
ownerMap: make(map[string]*Candidate),
@@ -94,12 +78,12 @@ func NewCandidateCenter(all CandidateList) (CandidateCenter, error) {
9478
}
9579

9680
// Size returns number of candidates
97-
func (m *candCenter) Size() int {
81+
func (m *CandidateCenter) Size() int {
9882
return m.size
9983
}
10084

10185
// All returns all candidates in candidate center
102-
func (m *candCenter) All() CandidateList {
86+
func (m *CandidateCenter) All() CandidateList {
10387
list := m.change.all()
10488
if list == nil {
10589
return m.base.all()
@@ -114,21 +98,21 @@ func (m *candCenter) All() CandidateList {
11498
}
11599

116100
// Base returns the confirmed base state
117-
func (m candCenter) Base() CandidateCenter {
118-
return &candCenter{
101+
func (m CandidateCenter) Base() *CandidateCenter {
102+
return &CandidateCenter{
119103
base: m.base,
120104
size: len(m.base.ownerMap),
121105
change: newCandChange(),
122106
}
123107
}
124108

125109
// Delta exports the pending changes
126-
func (m *candCenter) Delta() CandidateList {
110+
func (m *CandidateCenter) Delta() CandidateList {
127111
return m.change.all()
128112
}
129113

130114
// SetDelta sets the delta
131-
func (m *candCenter) SetDelta(l CandidateList) error {
115+
func (m *CandidateCenter) SetDelta(l CandidateList) error {
132116
if len(l) == 0 {
133117
m.change = nil
134118
m.change = newCandChange()
@@ -157,7 +141,7 @@ func (m *candCenter) SetDelta(l CandidateList) error {
157141
}
158142

159143
// Commit writes the change into base
160-
func (m *candCenter) Commit() error {
144+
func (m *CandidateCenter) Commit() error {
161145
size, err := m.base.commit(m.change)
162146
if err != nil {
163147
return err
@@ -168,8 +152,19 @@ func (m *candCenter) Commit() error {
168152
return nil
169153
}
170154

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+
171166
// 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 {
173168
if hit := m.change.containsName(name); hit {
174169
return true
175170
}
@@ -181,7 +176,7 @@ func (m *candCenter) ContainsName(name string) bool {
181176
}
182177

183178
// 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 {
185180
if owner == nil {
186181
return false
187182
}
@@ -195,7 +190,7 @@ func (m *candCenter) ContainsOwner(owner address.Address) bool {
195190
}
196191

197192
// 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 {
199194
if operator == nil {
200195
return false
201196
}
@@ -211,7 +206,7 @@ func (m *candCenter) ContainsOperator(operator address.Address) bool {
211206
}
212207

213208
// 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 {
215210
if hit := m.change.containsSelfStakingBucket(index); hit {
216211
return true
217212
}
@@ -223,7 +218,7 @@ func (m *candCenter) ContainsSelfStakingBucket(index uint64) bool {
223218
}
224219

225220
// GetByName returns the candidate by name
226-
func (m *candCenter) GetByName(name string) *Candidate {
221+
func (m *CandidateCenter) GetByName(name string) *Candidate {
227222
if d := m.change.getByName(name); d != nil {
228223
return d
229224
}
@@ -235,7 +230,7 @@ func (m *candCenter) GetByName(name string) *Candidate {
235230
}
236231

237232
// GetByOwner returns the candidate by owner
238-
func (m *candCenter) GetByOwner(owner address.Address) *Candidate {
233+
func (m *CandidateCenter) GetByOwner(owner address.Address) *Candidate {
239234
if owner == nil {
240235
return nil
241236
}
@@ -251,7 +246,7 @@ func (m *candCenter) GetByOwner(owner address.Address) *Candidate {
251246
}
252247

253248
// GetBySelfStakingIndex returns the candidate by self-staking index
254-
func (m *candCenter) GetBySelfStakingIndex(index uint64) *Candidate {
249+
func (m *CandidateCenter) GetBySelfStakingIndex(index uint64) *Candidate {
255250
if d := m.change.getBySelfStakingIndex(index); d != nil {
256251
return d
257252
}
@@ -263,7 +258,7 @@ func (m *candCenter) GetBySelfStakingIndex(index uint64) *Candidate {
263258
}
264259

265260
// 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 {
267262
if err := d.Validate(); err != nil {
268263
return err
269264
}
@@ -282,7 +277,7 @@ func (m *candCenter) Upsert(d *Candidate) error {
282277
return nil
283278
}
284279

285-
func (m *candCenter) collision(d *Candidate) error {
280+
func (m *CandidateCenter) collision(d *Candidate) error {
286281
if err := m.change.collision(d); err != nil {
287282
return err
288283
}

action/protocol/staking/candidate_center_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// testEqual verifies m contains exactly the list
15-
func testEqual(m CandidateCenter, l CandidateList) bool {
15+
func testEqual(m *CandidateCenter, l CandidateList) bool {
1616
for _, v := range l {
1717
d := m.GetByOwner(v.Owner)
1818
if d == nil {
@@ -43,7 +43,7 @@ func testEqual(m CandidateCenter, l CandidateList) bool {
4343

4444
// testEqualAllCommit validates candidate center
4545
// with original candidates = old, number of changed cand = change, and number of new cand = increase
46-
func testEqualAllCommit(r *require.Assertions, m CandidateCenter, old CandidateList, change, increase int,
46+
func testEqualAllCommit(r *require.Assertions, m *CandidateCenter, old CandidateList, change, increase int,
4747
) (CandidateList, error) {
4848
// capture all candidates
4949
size := len(old)

action/protocol/staking/candidate_statemanager.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type (
4242

4343
candSM struct {
4444
protocol.StateManager
45-
candCenter *candCenter
45+
candCenter *CandidateCenter
4646
bucketPool *BucketPool
4747
}
4848
)
@@ -61,25 +61,17 @@ func NewCandidateStateManager(sm protocol.StateManager, enableSMStorage bool) (C
6161
view := csr.BaseView()
6262
csm := &candSM{
6363
StateManager: sm,
64-
// TODO: remove CandidateCenter interface, no need for (*candCenter)
65-
candCenter: view.candCenter.Base().(*candCenter),
66-
bucketPool: view.bucketPool.Copy(enableSMStorage),
64+
candCenter: view.candCenter.Base(),
65+
bucketPool: view.bucketPool.Copy(enableSMStorage),
6766
}
6867

6968
// extract view change from SM
70-
if err := csm.bucketPool.SyncPool(sm); err != nil {
71-
return nil, err
72-
}
73-
74-
// TODO: remove CandidateCenter interface, convert the code below to candCenter.SyncCenter()
75-
delta := CandidateList{}
76-
if err = sm.Unload(protocolID, stakingCandCenter, &delta); err != nil && err != protocol.ErrNoName {
77-
return nil, errors.Wrap(err, "failed to create CandidateStateManager")
69+
if err := csm.bucketPool.Sync(sm); err != nil {
70+
return nil, errors.Wrap(err, "failed to sync bucket pool")
7871
}
7972

80-
// apply delta to the center
81-
if err := csm.candCenter.SetDelta(delta); err != nil {
82-
return nil, err
73+
if err := csm.candCenter.Sync(sm); err != nil {
74+
return nil, errors.Wrap(err, "failed to sync candidate center")
8375
}
8476
return csm, nil
8577
}

action/protocol/staking/candidate_statereader.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
type (
2121
// CandidateStateReader contains candidate center and bucket pool
2222
CandidateStateReader interface {
23-
// TODO: remove CandidateCenter interface, return *candCenter
2423
Height() uint64
2524
SR() protocol.StateReader
2625
BaseView() *ViewData
@@ -38,7 +37,7 @@ type (
3837

3938
// ViewData is the data that need to be stored in protocol's view
4039
ViewData struct {
41-
candCenter *candCenter
40+
candCenter *CandidateCenter
4241
bucketPool *BucketPool
4342
}
4443
)
@@ -140,9 +139,8 @@ func CreateBaseView(sr protocol.StateReader, enableSMStorage bool) (*ViewData, u
140139
return nil, height, err
141140
}
142141

143-
// TODO: remove CandidateCenter interface, no need for (*candCenter)
144142
return &ViewData{
145-
candCenter: center.(*candCenter),
143+
candCenter: center,
146144
bucketPool: pool,
147145
}, height, nil
148146
}

action/protocol/staking/handlers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
)
3838

3939
// Delete should only be used by test
40-
func (m *candCenter) deleteForTestOnly(owner address.Address) {
40+
func (m *CandidateCenter) deleteForTestOnly(owner address.Address) {
4141
if owner == nil {
4242
return
4343
}

0 commit comments

Comments
 (0)