Skip to content

Commit 6214513

Browse files
authored
add native staking cache (#1877)
1 parent e9ae969 commit 6214513

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

action/protocol/poll/nativestaking.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type (
3939
getTipBlockTime GetTipBlockTime
4040
contract string
4141
abi abi.ABI
42+
bufferHeight uint64
43+
bufferResult *VoteTally
4244
}
4345

4446
pygg struct {
@@ -75,19 +77,24 @@ func NewNativeStaking(cm protocol.ChainManager, getTipBlockTime GetTipBlockTime)
7577
cm: cm,
7678
getTipBlockTime: getTipBlockTime,
7779
abi: abi,
80+
bufferHeight: 0,
81+
bufferResult: nil,
7882
}, nil
7983
}
8084

8185
// Votes returns the votes on height
82-
func (ns *NativeStaking) Votes(correctGas bool) (*VoteTally, time.Time, error) {
86+
func (ns *NativeStaking) Votes(height uint64, correctGas bool) (*VoteTally, time.Time, error) {
8387
if ns.contract == "" {
8488
return nil, time.Time{}, ErrNoData
8589
}
86-
8790
now, err := ns.getTipBlockTime()
8891
if err != nil {
8992
return nil, time.Time{}, errors.Wrap(err, "failed to get current block time")
9093
}
94+
if ns.bufferHeight == height && ns.bufferResult != nil {
95+
log.L().Info("Using cache native staking data", zap.Uint64("height", height))
96+
return ns.bufferResult, now, nil
97+
}
9198
// read voter list from staking contract
9299
votes := VoteTally{
93100
Candidates: make(map[[12]byte]*state.Candidate),
@@ -114,6 +121,8 @@ func (ns *NativeStaking) Votes(correctGas bool) (*VoteTally, time.Time, error) {
114121
}
115122
prevIndex = index
116123
}
124+
ns.bufferHeight = height
125+
ns.bufferResult = &votes
117126
return &votes, now, nil
118127
}
119128

action/protocol/poll/staking_committee.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (sc *stakingCommittee) DelegatesByHeight(height uint64) (state.CandidateLis
133133
}
134134

135135
timer = sc.timerFactory.NewTimer("Native")
136-
nativeVotes, ts, err := sc.nativeStaking.Votes(sc.hu.IsPost(config.Daytona, height))
136+
nativeVotes, ts, err := sc.nativeStaking.Votes(height, sc.hu.IsPost(config.Daytona, height))
137137
timer.End()
138138
if err == ErrNoData {
139139
// no native staking data

e2etest/staking_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func TestStakingContract(t *testing.T) {
112112
})
113113
require.NoError(err)
114114
ns.SetContract(r.ContractAddress)
115-
tally, now, err := ns.Votes(false)
115+
tally, now, err := ns.Votes(bc.TipHeight()+1, false)
116116
require.Equal(poll.ErrNoData, err)
117-
tally, now, err = ns.Votes(true)
117+
tally, now, err = ns.Votes(bc.TipHeight()+1, true)
118118
require.NoError(err)
119119
require.Equal(fixedTime, now)
120120
require.Equal(numVoter*int(numBucket), len(tally.Candidates))

0 commit comments

Comments
 (0)