Skip to content

Commit 335a5ee

Browse files
authored
replace buffer key from height to epoch number (#1885)
1 parent 6214513 commit 335a5ee

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

action/protocol/poll/nativestaking.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type (
3939
getTipBlockTime GetTipBlockTime
4040
contract string
4141
abi abi.ABI
42-
bufferHeight uint64
42+
bufferEpochNum uint64
4343
bufferResult *VoteTally
4444
}
4545

@@ -77,22 +77,22 @@ func NewNativeStaking(cm protocol.ChainManager, getTipBlockTime GetTipBlockTime)
7777
cm: cm,
7878
getTipBlockTime: getTipBlockTime,
7979
abi: abi,
80-
bufferHeight: 0,
80+
bufferEpochNum: 0,
8181
bufferResult: nil,
8282
}, nil
8383
}
8484

8585
// Votes returns the votes on height
86-
func (ns *NativeStaking) Votes(height uint64, correctGas bool) (*VoteTally, time.Time, error) {
86+
func (ns *NativeStaking) Votes(epochNum uint64, height uint64, correctGas bool) (*VoteTally, time.Time, error) {
8787
if ns.contract == "" {
8888
return nil, time.Time{}, ErrNoData
8989
}
9090
now, err := ns.getTipBlockTime()
9191
if err != nil {
9292
return nil, time.Time{}, errors.Wrap(err, "failed to get current block time")
9393
}
94-
if ns.bufferHeight == height && ns.bufferResult != nil {
95-
log.L().Info("Using cache native staking data", zap.Uint64("height", height))
94+
if ns.bufferEpochNum == epochNum && ns.bufferResult != nil {
95+
log.L().Info("Using cache native staking data", zap.Uint64("epochNum", epochNum))
9696
return ns.bufferResult, now, nil
9797
}
9898
// read voter list from staking contract
@@ -121,8 +121,10 @@ func (ns *NativeStaking) Votes(height uint64, correctGas bool) (*VoteTally, time
121121
}
122122
prevIndex = index
123123
}
124-
ns.bufferHeight = height
125-
ns.bufferResult = &votes
124+
if epochNum > ns.bufferEpochNum {
125+
ns.bufferEpochNum = epochNum
126+
ns.bufferResult = &votes
127+
}
126128
return &votes, now, nil
127129
}
128130

action/protocol/poll/staking_committee.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ func (sc *stakingCommittee) DelegatesByHeight(height uint64) (state.CandidateLis
122122
if err != nil {
123123
return nil, err
124124
}
125+
epochNum := sc.getEpochNum(height)
125126
// convert to epoch start height
126-
epochHeight := sc.getEpochHeight(sc.getEpochNum(height))
127+
epochHeight := sc.getEpochHeight(epochNum)
127128
if sc.hu.IsPre(config.Cook, epochHeight) {
128129
return sc.filterDelegates(cand), nil
129130
}
@@ -133,7 +134,7 @@ func (sc *stakingCommittee) DelegatesByHeight(height uint64) (state.CandidateLis
133134
}
134135

135136
timer = sc.timerFactory.NewTimer("Native")
136-
nativeVotes, ts, err := sc.nativeStaking.Votes(height, sc.hu.IsPost(config.Daytona, height))
137+
nativeVotes, ts, err := sc.nativeStaking.Votes(epochNum, height, sc.hu.IsPost(config.Daytona, height))
137138
timer.End()
138139
if err == ErrNoData {
139140
// no native staking data

e2etest/staking_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ func TestStakingContract(t *testing.T) {
112112
})
113113
require.NoError(err)
114114
ns.SetContract(r.ContractAddress)
115-
tally, now, err := ns.Votes(bc.TipHeight()+1, false)
115+
// Using a random value for epoch number
116+
tally, now, err := ns.Votes(bc.TipHeight(), bc.TipHeight()+1, false)
116117
require.Equal(poll.ErrNoData, err)
117-
tally, now, err = ns.Votes(bc.TipHeight()+1, true)
118+
tally, now, err = ns.Votes(bc.TipHeight(), bc.TipHeight()+1, true)
118119
require.NoError(err)
119120
require.Equal(fixedTime, now)
120121
require.Equal(numVoter*int(numBucket), len(tally.Candidates))

0 commit comments

Comments
 (0)