Skip to content

Commit a2e6ad8

Browse files
authored
[rpc] Move lifetime outside of validator so always show APR (#2542)
1 parent 2b578c0 commit a2e6ad8

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Diff for: hmy/api_backend.go

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/harmony-one/harmony/core/vm"
2323
internal_common "github.com/harmony-one/harmony/internal/common"
2424
"github.com/harmony-one/harmony/internal/params"
25+
"github.com/harmony-one/harmony/numeric"
2526
"github.com/harmony-one/harmony/shard"
2627
"github.com/harmony-one/harmony/shard/committee"
2728
"github.com/harmony-one/harmony/staking/availability"
@@ -325,6 +326,10 @@ func (b *APIBackend) GetAllValidatorAddresses() []common.Address {
325326
return b.hmy.BlockChain().ValidatorCandidates()
326327
}
327328

329+
var (
330+
zero = numeric.ZeroDec()
331+
)
332+
328333
// GetValidatorInformation returns the information of validator
329334
func (b *APIBackend) GetValidatorInformation(
330335
addr common.Address,
@@ -346,6 +351,11 @@ func (b *APIBackend) GetValidatorInformation(
346351
EPoSStatus: effective.ValidatorStatus(
347352
inCommittee, wrapper.Status == effective.Active,
348353
).String(),
354+
Lifetime: &staking.AccumulatedOverLifetime{
355+
wrapper.BlockReward,
356+
wrapper.Counters,
357+
zero,
358+
},
349359
}
350360

351361
snapshot, err := b.hmy.BlockChain().ReadValidatorSnapshotAtEpoch(
@@ -366,6 +376,8 @@ func (b *APIBackend) GetValidatorInformation(
366376
return defaultReply, nil
367377
}
368378

379+
defaultReply.Lifetime.APR = stats.APR
380+
369381
if defaultReply.CurrentlyInCommittee {
370382
defaultReply.Performance = &staking.CurrentEpochPerformance{
371383
CurrentSigningPercentage: *computed,

Diff for: staking/types/validator.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ type ValidatorWrapper struct {
8484
Validator
8585
Delegations Delegations
8686
//
87-
Counters counters
87+
Counters counters `json:"-"`
8888
// All the rewarded accumulated so far
89-
BlockReward *big.Int
89+
BlockReward *big.Int `json:"-"`
9090
}
9191

9292
// Computed represents current epoch
@@ -136,11 +136,14 @@ type ValidatorRPCEnchanced struct {
136136
TotalDelegated *big.Int `json:"total-delegation"`
137137
CurrentlyInCommittee bool `json:"currently-in-committee"`
138138
EPoSStatus string `json:"epos-status"`
139+
Lifetime *AccumulatedOverLifetime `json:"lifetime"`
139140
}
140141

141-
type accumulatedOverLifetime struct {
142-
BlockReward *big.Int `json:"reward-accumulated"`
143-
Signing counters `json:"blocks"`
142+
// AccumulatedOverLifetime ..
143+
type AccumulatedOverLifetime struct {
144+
BlockReward *big.Int `json:"reward-accumulated"`
145+
Signing counters `json:"blocks"`
146+
APR numeric.Dec `json:"apr"`
144147
}
145148

146149
func (w ValidatorWrapper) String() string {
@@ -152,21 +155,19 @@ func (w ValidatorWrapper) String() string {
152155
func (w ValidatorWrapper) MarshalJSON() ([]byte, error) {
153156
return json.Marshal(struct {
154157
Validator
155-
Address string `json:"address"`
156-
Delegations Delegations `json:"delegations"`
157-
Lifetime accumulatedOverLifetime `json:"lifetime"`
158+
Address string `json:"address"`
159+
Delegations Delegations `json:"delegations"`
158160
}{
159161
w.Validator,
160162
common2.MustAddressToBech32(w.Address),
161163
w.Delegations,
162-
accumulatedOverLifetime{w.BlockReward, w.Counters},
163164
})
164165
}
165166

166167
// ValidatorStats to record validator's performance and history records
167168
type ValidatorStats struct {
168169
// APR ..
169-
APR numeric.Dec `json:"current-apr"`
170+
APR numeric.Dec `json:"-"`
170171
// TotalEffectiveStake is the total effective stake this validator has
171172
TotalEffectiveStake numeric.Dec `json:"total-effective-stake"`
172173
// MetricsPerShard ..

0 commit comments

Comments
 (0)