Skip to content

Commit f2a1c4c

Browse files
author
Yutong Pei
authored
fix staking read state height issue (iotexproject#2463)
1 parent ee8a2e5 commit f2a1c4c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

action/protocol/staking/read_state.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ func readStateBucketCount(ctx context.Context, csr CandidateStateReader,
110110
if err != nil {
111111
return nil, 0, err
112112
}
113-
active, err := getActiveBucketsCount(ctx, csr)
113+
active, h, err := getActiveBucketsCount(ctx, csr)
114114
if err != nil {
115-
return nil, 0, err
115+
return nil, h, err
116116
}
117117
return &iotextypes.BucketsCount{
118118
Total: total,
119119
Active: active,
120-
}, csr.Height(), nil
120+
}, h, nil
121121
}
122122

123123
func readStateCandidates(ctx context.Context, csr CandidateStateReader,
@@ -155,12 +155,12 @@ func readStateTotalStakingAmount(ctx context.Context, csr CandidateStateReader,
155155
_ *iotexapi.ReadStakingDataRequest_TotalStakingAmount) (*iotextypes.AccountMeta, uint64, error) {
156156
meta := iotextypes.AccountMeta{}
157157
meta.Address = address.StakingBucketPoolAddr
158-
total, err := getTotalStakedAmount(ctx, csr)
158+
total, h, err := getTotalStakedAmount(ctx, csr)
159159
if err != nil {
160-
return nil, csr.Height(), err
160+
return nil, h, err
161161
}
162162
meta.Balance = total.String()
163-
return &meta, csr.Height(), nil
163+
return &meta, h, nil
164164
}
165165

166166
func toIoTeXTypesVoteBucketList(buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
@@ -219,35 +219,35 @@ func getPageOfCandidates(candidates CandidateList, offset, limit int) CandidateL
219219
return res
220220
}
221221

222-
func getTotalStakedAmount(ctx context.Context, csr CandidateStateReader) (*big.Int, error) {
222+
func getTotalStakedAmount(ctx context.Context, csr CandidateStateReader) (*big.Int, uint64, error) {
223223
chainCtx := protocol.MustGetBlockchainCtx(ctx)
224224
hu := config.NewHeightUpgrade(&chainCtx.Genesis)
225225
if hu.IsPost(config.Greenland, csr.Height()) {
226226
// after Greenland, read state from db
227227
var total totalAmount
228-
_, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
228+
h, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
229229
if err != nil {
230-
return nil, err
230+
return nil, h, err
231231
}
232-
return total.amount, nil
232+
return total.amount, h, nil
233233
}
234234

235235
// otherwise read from bucket pool
236-
return csr.TotalStakedAmount(), nil
236+
return csr.TotalStakedAmount(), csr.Height(), nil
237237
}
238238

239-
func getActiveBucketsCount(ctx context.Context, csr CandidateStateReader) (uint64, error) {
239+
func getActiveBucketsCount(ctx context.Context, csr CandidateStateReader) (uint64, uint64, error) {
240240
chainCtx := protocol.MustGetBlockchainCtx(ctx)
241241
hu := config.NewHeightUpgrade(&chainCtx.Genesis)
242242
if hu.IsPost(config.Greenland, csr.Height()) {
243243
// after Greenland, read state from db
244244
var total totalAmount
245-
_, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
245+
h, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
246246
if err != nil {
247-
return 0, err
247+
return 0, h, err
248248
}
249-
return total.count, nil
249+
return total.count, h, nil
250250
}
251251
// otherwise read from bucket pool
252-
return csr.ActiveBucketsCount(), nil
252+
return csr.ActiveBucketsCount(), csr.Height(), nil
253253
}

0 commit comments

Comments
 (0)