From a2e6ad8455cbbe60013f487bfa8faaa647d521d9 Mon Sep 17 00:00:00 2001 From: Edgar Aroutiounian Date: Thu, 19 Mar 2020 22:24:58 -0700 Subject: [PATCH] [rpc] Move lifetime outside of validator so always show APR (#2542) --- hmy/api_backend.go | 12 ++++++++++++ staking/types/validator.go | 21 +++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/hmy/api_backend.go b/hmy/api_backend.go index 941124de40..5083a7a4c7 100644 --- a/hmy/api_backend.go +++ b/hmy/api_backend.go @@ -22,6 +22,7 @@ import ( "github.com/harmony-one/harmony/core/vm" internal_common "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/params" + "github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard/committee" "github.com/harmony-one/harmony/staking/availability" @@ -325,6 +326,10 @@ func (b *APIBackend) GetAllValidatorAddresses() []common.Address { return b.hmy.BlockChain().ValidatorCandidates() } +var ( + zero = numeric.ZeroDec() +) + // GetValidatorInformation returns the information of validator func (b *APIBackend) GetValidatorInformation( addr common.Address, @@ -346,6 +351,11 @@ func (b *APIBackend) GetValidatorInformation( EPoSStatus: effective.ValidatorStatus( inCommittee, wrapper.Status == effective.Active, ).String(), + Lifetime: &staking.AccumulatedOverLifetime{ + wrapper.BlockReward, + wrapper.Counters, + zero, + }, } snapshot, err := b.hmy.BlockChain().ReadValidatorSnapshotAtEpoch( @@ -366,6 +376,8 @@ func (b *APIBackend) GetValidatorInformation( return defaultReply, nil } + defaultReply.Lifetime.APR = stats.APR + if defaultReply.CurrentlyInCommittee { defaultReply.Performance = &staking.CurrentEpochPerformance{ CurrentSigningPercentage: *computed, diff --git a/staking/types/validator.go b/staking/types/validator.go index dc6b6f11ce..6188b01eb9 100644 --- a/staking/types/validator.go +++ b/staking/types/validator.go @@ -84,9 +84,9 @@ type ValidatorWrapper struct { Validator Delegations Delegations // - Counters counters + Counters counters `json:"-"` // All the rewarded accumulated so far - BlockReward *big.Int + BlockReward *big.Int `json:"-"` } // Computed represents current epoch @@ -136,11 +136,14 @@ type ValidatorRPCEnchanced struct { TotalDelegated *big.Int `json:"total-delegation"` CurrentlyInCommittee bool `json:"currently-in-committee"` EPoSStatus string `json:"epos-status"` + Lifetime *AccumulatedOverLifetime `json:"lifetime"` } -type accumulatedOverLifetime struct { - BlockReward *big.Int `json:"reward-accumulated"` - Signing counters `json:"blocks"` +// AccumulatedOverLifetime .. +type AccumulatedOverLifetime struct { + BlockReward *big.Int `json:"reward-accumulated"` + Signing counters `json:"blocks"` + APR numeric.Dec `json:"apr"` } func (w ValidatorWrapper) String() string { @@ -152,21 +155,19 @@ func (w ValidatorWrapper) String() string { func (w ValidatorWrapper) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Validator - Address string `json:"address"` - Delegations Delegations `json:"delegations"` - Lifetime accumulatedOverLifetime `json:"lifetime"` + Address string `json:"address"` + Delegations Delegations `json:"delegations"` }{ w.Validator, common2.MustAddressToBech32(w.Address), w.Delegations, - accumulatedOverLifetime{w.BlockReward, w.Counters}, }) } // ValidatorStats to record validator's performance and history records type ValidatorStats struct { // APR .. - APR numeric.Dec `json:"current-apr"` + APR numeric.Dec `json:"-"` // TotalEffectiveStake is the total effective stake this validator has TotalEffectiveStake numeric.Dec `json:"total-effective-stake"` // MetricsPerShard ..