Skip to content

Commit 71916bb

Browse files
CoderZhienvestcc
andauthored
Merge nft votes in candidate center (#4705)
Co-authored-by: envestcc <chen1233216@hotmail.com>
1 parent dbf6b00 commit 71916bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1585
-830
lines changed

action/protocol/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ type (
162162
NotSlashUnproductiveDelegates bool
163163
CandidateBLSPublicKey bool
164164
NotUseMinSelfStakeToBeActive bool
165-
LoadContractStakingFromIndexer bool
165+
StoreVoteOfNFTBucketIntoView bool
166166
}
167167

168168
// FeatureWithHeightCtx provides feature check functions.
@@ -328,7 +328,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
328328
NotSlashUnproductiveDelegates: !g.IsToBeEnabled(height),
329329
CandidateBLSPublicKey: g.IsToBeEnabled(height),
330330
NotUseMinSelfStakeToBeActive: !g.IsToBeEnabled(height),
331-
LoadContractStakingFromIndexer: !g.IsToBeEnabled(height),
331+
StoreVoteOfNFTBucketIntoView: !g.IsToBeEnabled(height),
332332
},
333333
)
334334
}

action/protocol/mock_protocol.go

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/staking/builder.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ type (
66

77
// BuilderConfig returns the configuration of the builder
88
BuilderConfig struct {
9-
Staking genesis.Staking
10-
PersistStakingPatchBlock uint64
11-
FixAliasForNonStopHeight uint64
12-
StakingPatchDir string
13-
Revise ReviseConfig
9+
Staking genesis.Staking
10+
PersistStakingPatchBlock uint64
11+
FixAliasForNonStopHeight uint64
12+
SkipContractStakingViewHeight uint64
13+
StakingPatchDir string
14+
Revise ReviseConfig
1415
}
1516
)

action/protocol/staking/candidate_statereader.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type (
4646
NativeBucketIndicesByVoter(addr address.Address) (*BucketIndices, uint64, error)
4747
NativeBucketIndicesByCandidate(addr address.Address) (*BucketIndices, uint64, error)
4848
CandidateByAddress(name address.Address) (*Candidate, uint64, error)
49-
getAllCandidates() (CandidateList, uint64, error)
49+
CreateCandidateCenter() (*CandidateCenter, uint64, error)
5050
ReadState
5151
Height() uint64
5252
SR() protocol.StateReader
@@ -152,12 +152,7 @@ func CreateBaseView(sr protocol.StateReader, enableSMStorage bool) (*viewData, u
152152
}
153153

154154
csr := newCandidateStateReader(sr)
155-
all, height, err := csr.getAllCandidates()
156-
if err != nil && errors.Cause(err) != state.ErrStateNotExist {
157-
return nil, height, err
158-
}
159-
160-
center, err := NewCandidateCenter(all)
155+
center, height, err := csr.CreateCandidateCenter()
161156
if err != nil {
162157
return nil, height, err
163158
}
@@ -301,21 +296,29 @@ func (c *candSR) CandidateByAddress(name address.Address) (*Candidate, uint64, e
301296
return &d, height, err
302297
}
303298

304-
func (c *candSR) getAllCandidates() (CandidateList, uint64, error) {
299+
func (c *candSR) CreateCandidateCenter() (*CandidateCenter, uint64, error) {
305300
height, iter, err := c.States(protocol.NamespaceOption(_candidateNameSpace))
301+
var cands CandidateList
302+
switch errors.Cause(err) {
303+
case nil:
304+
cands = make(CandidateList, 0, iter.Size())
305+
for i := 0; i < iter.Size(); i++ {
306+
c := &Candidate{}
307+
if _, err := iter.Next(c); err != nil {
308+
return nil, height, errors.Wrapf(err, "failed to deserialize candidate")
309+
}
310+
cands = append(cands, c)
311+
}
312+
case state.ErrStateNotExist:
313+
default:
314+
return nil, height, err
315+
}
316+
center, err := NewCandidateCenter(cands)
306317
if err != nil {
307318
return nil, height, err
308319
}
309320

310-
cands := make(CandidateList, 0, iter.Size())
311-
for i := 0; i < iter.Size(); i++ {
312-
c := &Candidate{}
313-
if _, err := iter.Next(c); err != nil {
314-
return nil, height, errors.Wrapf(err, "failed to deserialize candidate")
315-
}
316-
cands = append(cands, c)
317-
}
318-
return cands, height, nil
321+
return center, height, nil
319322
}
320323

321324
func (c *candSR) NewBucketPool(enableSMStorage bool) (*BucketPool, error) {

action/protocol/staking/candidate_statereader_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func Test_CandidateStateReader(t *testing.T) {
2222
sm := testdb.NewMockStateManager(ctrl)
2323
h, err := sm.Height()
2424
require.NoError(err)
25-
csr, err := ConstructBaseView(sm)
25+
_, err = ConstructBaseView(sm)
2626
require.Equal(err, protocol.ErrNoName)
2727
view, _, err := CreateBaseView(sm, false)
2828
require.NoError(err)
29-
csr = &candSR{
29+
csr := &candSR{
3030
StateReader: sm,
3131
height: h,
3232
view: view,

action/protocol/staking/candidate_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ func TestGetPutCandidate(t *testing.T) {
214214
}
215215

216216
// get all candidates
217-
all, _, err := csr.getAllCandidates()
217+
cc, _, err := csr.CreateCandidateCenter()
218218
require.NoError(err)
219+
all := cc.All()
219220
require.Equal(len(testCandidates), len(all))
220221
for _, e := range testCandidates {
221222
for i := range all {

action/protocol/staking/contractstake_indexer.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313

1414
"github.com/ethereum/go-ethereum/accounts/abi"
1515
"github.com/iotexproject/iotex-address/address"
16+
"github.com/iotexproject/iotex-core/v2/action"
1617
"github.com/iotexproject/iotex-core/v2/action/protocol"
18+
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/contractstaking"
1719
)
1820

1921
var (
@@ -25,7 +27,18 @@ var (
2527
)
2628

2729
type (
28-
30+
// EventHandler is the interface for handling staking events
31+
EventHandler interface {
32+
PutBucketType(address.Address, *ContractStakingBucketType) error
33+
DeductBucket(address.Address, uint64) (*contractstaking.Bucket, error)
34+
PutBucket(address.Address, uint64, *contractstaking.Bucket) error
35+
DeleteBucket(address.Address, uint64) error
36+
}
37+
// EventProcessor is the interface for processing staking events
38+
EventProcessor interface {
39+
// ProcessReceipts processes receipts
40+
ProcessReceipts(context.Context, ...*action.Receipt) error
41+
}
2942
// ContractStakingIndexer defines the interface of contract staking reader
3043
ContractStakingIndexer interface {
3144
Height() (uint64, error)
@@ -41,6 +54,8 @@ type (
4154
ContractAddress() address.Address
4255
// LoadStakeView loads the contract stake view from state reader
4356
LoadStakeView(context.Context, protocol.StateReader) (ContractStakeView, error)
57+
// CreateEventProcessor creates a new event processor
58+
CreateEventProcessor(context.Context, EventHandler) EventProcessor
4459
}
4560
// ContractStakingIndexerWithBucketType defines the interface of contract staking reader with bucket type
4661
ContractStakingIndexerWithBucketType interface {

action/protocol/staking/contractstake_indexer_mock.go

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/staking/contractstaking/bucket.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type (
3535
}
3636
)
3737

38+
// ErrBucketNotExist is the error when bucket does not exist
39+
var ErrBucketNotExist = errors.New("bucket does not exist")
40+
3841
func (b *Bucket) toProto() *stakingpb.SystemStakingBucket {
3942
if b == nil {
4043
return nil

0 commit comments

Comments
 (0)