Skip to content

Commit

Permalink
[rpc] Move lifetime outside of validator so always show APR (#2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial authored Mar 20, 2020
1 parent 2b578c0 commit a2e6ad8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
12 changes: 12 additions & 0 deletions hmy/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand Down
21 changes: 11 additions & 10 deletions staking/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 ..
Expand Down

0 comments on commit a2e6ad8

Please sign in to comment.