Skip to content

Commit 5a6fff9

Browse files
committed
store candidate votes in erigondb
1 parent 0cdc9de commit 5a6fff9

File tree

8 files changed

+101
-8
lines changed

8 files changed

+101
-8
lines changed

blockindex/contractstaking/indexer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ func (s *Indexer) LoadStakeView(ctx context.Context, sr protocol.StateReader) (s
132132
})
133133
processorBuilder := newEventProcessorBuilder(s.contractAddr)
134134
cfg := &stakingindex.VoteViewConfig{ContractAddr: s.contractAddr}
135-
return stakingindex.NewVoteView(s, cfg, s.height, cur, processorBuilder, calculateUnmutedVoteWeightAt), nil
135+
mgr := stakingindex.NewCandidateVotesManager(s.ContractAddress())
136+
return stakingindex.NewVoteView(s, cfg, s.height, cur, processorBuilder, mgr, calculateUnmutedVoteWeightAt), nil
136137
}
137138

138139
// Stop stops the indexer

state/factory/erigonstore/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func init() {
3737
assertions.MustNoError(storageRegistry.RegisterNamespace(state.CandidateNamespace, CandidatesContractIndex))
3838
assertions.MustNoError(storageRegistry.RegisterNamespace(state.CandsMapNamespace, CandidateMapContractIndex))
3939
assertions.MustNoError(storageRegistry.RegisterNamespace(state.StakingNamespace, BucketPoolContractIndex))
40+
assertions.MustNoError(storageRegistry.RegisterNamespace(state.StakingViewNamespace, StakingViewContractIndex))
4041

4142
assertions.MustNoError(storageRegistry.RegisterObjectStorage(state.AccountKVNamespace, &state.Account{}, AccountIndex))
4243
assertions.MustNoError(storageRegistry.RegisterObjectStorage(state.AccountKVNamespace, &state.CandidateList{}, PollLegacyCandidateListContractIndex))

state/factory/statedb.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package factory
88
import (
99
"context"
1010
"fmt"
11+
"slices"
1112
"strconv"
1213
"sync"
1314
"time"
@@ -567,15 +568,22 @@ func (sdb *stateDB) flusherOptions(preEaster bool) []db.KVStoreFlusherOption {
567568
return wi.Serialize()
568569
}),
569570
}
570-
if !preEaster {
571-
return opts
571+
serializeFilterNs := []string{state.StakingViewNamespace}
572+
if preEaster {
573+
serializeFilterNs = append(serializeFilterNs, evm.CodeKVNameSpace, staking.CandsMapNS)
572574
}
573-
return append(
574-
opts,
575+
opts = append(opts,
576+
db.FlushTranslateOption(func(wi *batch.WriteInfo) *batch.WriteInfo {
577+
if wi.Namespace() == state.StakingViewNamespace {
578+
return nil
579+
}
580+
return wi
581+
}),
575582
db.SerializeFilterOption(func(wi *batch.WriteInfo) bool {
576-
return wi.Namespace() == evm.CodeKVNameSpace || wi.Namespace() == staking.CandsMapNS
583+
return slices.Contains(serializeFilterNs, wi.Namespace())
577584
}),
578585
)
586+
return opts
579587
}
580588

581589
func (sdb *stateDB) state(h uint64, ns string, addr []byte, s interface{}) error {

state/tables.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const (
3838
// - "4" + <bucketID> --> Endorsement
3939
StakingNamespace = "Staking"
4040

41+
// StakingViewNamespace is the namespace to store staking view information
42+
// - "voteview" + <contractAddress> --> CandidateVotes
43+
StakingViewNamespace = "StakingView"
44+
4145
// CandidateNamespace is the namespace to store candidate information
4246
// - <ID> --> Candidate
4347
CandidateNamespace = "Candidate"

systemcontractindex/stakingindex/candidate_votes.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/iotexproject/iotex-core/v2/action/protocol"
1010
"github.com/iotexproject/iotex-core/v2/systemcontractindex/stakingindex/stakingpb"
11+
"github.com/iotexproject/iotex-core/v2/systemcontracts"
1112
)
1213

1314
// CandidateVotes is the interface to manage candidate votes
@@ -125,6 +126,18 @@ func (cv *candidateVotes) Deserialize(data []byte) error {
125126
return nil
126127
}
127128

129+
func (cv *candidateVotes) Encode() (systemcontracts.GenericValue, error) {
130+
data, err := cv.Serialize()
131+
if err != nil {
132+
return systemcontracts.GenericValue{}, err
133+
}
134+
return systemcontracts.GenericValue{PrimaryData: data}, nil
135+
}
136+
137+
func (cv *candidateVotes) Decode(data systemcontracts.GenericValue) error {
138+
return cv.Deserialize(data.PrimaryData)
139+
}
140+
128141
func (cv *candidateVotes) Commit() CandidateVotes {
129142
return cv
130143
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package stakingindex
2+
3+
import (
4+
"context"
5+
6+
"github.com/pkg/errors"
7+
8+
"github.com/iotexproject/iotex-address/address"
9+
10+
"github.com/iotexproject/iotex-core/v2/action/protocol"
11+
"github.com/iotexproject/iotex-core/v2/state"
12+
)
13+
14+
var (
15+
voteViewKeyPrefix = []byte("voteview")
16+
voteViewNS = state.StakingViewNamespace
17+
)
18+
19+
// CandidateVotesManager defines the interface to manage candidate votes
20+
type CandidateVotesManager interface {
21+
Load(ctx context.Context, sr protocol.StateReader) (CandidateVotes, error)
22+
Store(ctx context.Context, sm protocol.StateManager, candVotes CandidateVotes) error
23+
}
24+
25+
type candidateVotesManager struct {
26+
contractAddr address.Address
27+
}
28+
29+
// NewCandidateVotesManager creates a new instance of CandidateVotesManager
30+
func NewCandidateVotesManager(contractAddr address.Address) CandidateVotesManager {
31+
return &candidateVotesManager{
32+
contractAddr: contractAddr,
33+
}
34+
}
35+
36+
func (s *candidateVotesManager) Store(ctx context.Context, sm protocol.StateManager, candVotes CandidateVotes) error {
37+
if _, err := sm.PutState(candVotes,
38+
protocol.KeyOption(s.key()),
39+
protocol.NamespaceOption(voteViewNS),
40+
); err != nil {
41+
return errors.Wrap(err, "failed to put candidate votes state")
42+
}
43+
return nil
44+
}
45+
46+
func (s *candidateVotesManager) Load(ctx context.Context, sr protocol.StateReader) (CandidateVotes, error) {
47+
cur := newCandidateVotes()
48+
_, err := sr.State(cur,
49+
protocol.KeyOption(s.key()),
50+
protocol.NamespaceOption(voteViewNS),
51+
)
52+
if err != nil {
53+
return nil, errors.Wrap(err, "failed to get candidate votes state")
54+
}
55+
return cur, nil
56+
}
57+
58+
func (s *candidateVotesManager) key() []byte {
59+
return append(voteViewKeyPrefix, s.contractAddr.Bytes()...)
60+
}

systemcontractindex/stakingindex/index.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ func (s *Indexer) LoadStakeView(ctx context.Context, sr protocol.StateReader) (s
176176
cfg := &VoteViewConfig{
177177
ContractAddr: s.common.ContractAddress(),
178178
}
179+
mgr := NewCandidateVotesManager(s.ContractAddress())
179180
processorBuilder := newEventProcessorBuilder(s.common.ContractAddress(), s.timestamped, s.muteHeight)
180-
return NewVoteView(s, cfg, s.common.Height(), s.createCandidateVotes(s.cache.buckets), processorBuilder, s.calculateContractVoteWeight), nil
181+
return NewVoteView(s, cfg, s.common.Height(), s.createCandidateVotes(s.cache.buckets), processorBuilder, mgr, s.calculateContractVoteWeight), nil
181182
}
182183

183184
// ContractStakingBuckets returns all the contract staking buckets

systemcontractindex/stakingindex/voteview.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type (
3030
height uint64
3131
cur CandidateVotes
3232
store BucketStore
33+
cvm CandidateVotesManager
3334
processorBuilder EventProcessorBuilder
3435
calculateVoteWeightFn CalculateUnmutedVoteWeightAtFn
3536
}
@@ -42,6 +43,7 @@ func NewVoteView(
4243
height uint64,
4344
cur CandidateVotes,
4445
processorBuilder EventProcessorBuilder,
46+
cvm CandidateVotesManager,
4547
fn CalculateUnmutedVoteWeightAtFn,
4648
) staking.ContractStakeView {
4749
return &voteView{
@@ -50,6 +52,7 @@ func NewVoteView(
5052
height: height,
5153
cur: cur,
5254
processorBuilder: processorBuilder,
55+
cvm: cvm,
5356
calculateVoteWeightFn: fn,
5457
}
5558
}
@@ -71,6 +74,7 @@ func (s *voteView) Wrap() staking.ContractStakeView {
7174
cur: cur,
7275
store: store,
7376
processorBuilder: s.processorBuilder,
77+
cvm: s.cvm,
7478
calculateVoteWeightFn: s.calculateVoteWeightFn,
7579
}
7680
}
@@ -88,6 +92,7 @@ func (s *voteView) Fork() staking.ContractStakeView {
8892
cur: cur,
8993
store: store,
9094
processorBuilder: s.processorBuilder,
95+
cvm: s.cvm,
9196
calculateVoteWeightFn: s.calculateVoteWeightFn,
9297
}
9398
}
@@ -167,5 +172,5 @@ func (s *voteView) AddBlockReceipts(ctx context.Context, receipts []*action.Rece
167172

168173
func (s *voteView) Commit(ctx context.Context, sm protocol.StateManager) error {
169174
s.cur = s.cur.Commit()
170-
return nil
175+
return s.cvm.Store(ctx, sm, s.cur)
171176
}

0 commit comments

Comments
 (0)