From c0d84f7a30193120f2e6c76bde23570e754ee7ad Mon Sep 17 00:00:00 2001 From: Ganesha Upadhyaya Date: Mon, 18 May 2020 16:05:13 -0700 Subject: [PATCH] use snapshot total delegation instead of current total delegation --- staking/apr/compute.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/staking/apr/compute.go b/staking/apr/compute.go index 5b016eedd..8afd293e2 100644 --- a/staking/apr/compute.go +++ b/staking/apr/compute.go @@ -20,6 +20,8 @@ var ( ErrInsufficientEpoch = errors.New("insufficient past epochs to compute apr") // ErrCouldNotRetreiveHeaderByNumber is returned when fail to retrieve header by number ErrCouldNotRetreiveHeaderByNumber = errors.New("could not retrieve header by number") + // ErrZeroStakeOneEpochAgo is returned when total delegation is zero for one epoch ago + ErrZeroStakeOneEpochAgo = errors.New("zero total delegation one epoch ago") ) // Reader .. @@ -132,7 +134,16 @@ func ComputeForValidator( return &zero, nil } - total := numeric.NewDecFromBigInt(validatorNow.TotalDelegation()) + total := numeric.NewDecFromBigInt(oneSnapshotAgo.Validator.TotalDelegation()) + if total.IsZero() { + return nil, errors.Wrapf( + ErrZeroStakeOneEpochAgo, + "current epoch %d, one-epoch-ago %d", + block.Epoch().Uint64(), + oneEpochAgo.Uint64(), + ) + } + result := numeric.NewDecFromBigInt(estimatedRewardPerYear).Quo( total, )