Skip to content

Commit

Permalink
modified estimated activation api (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
gts2030 authored Sep 6, 2024
1 parent fd80fb9 commit 181c35f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
25 changes: 16 additions & 9 deletions beacon-chain/rpc/over/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,37 @@ func (s *Server) EstimatedActivation(w http.ResponseWriter, r *http.Request) {
// If validator is not found, it will return an estimation based on current state
// when new deposit is included. (Status = 0)
status := uint64(0)
eth1DataVotesLength := params.BeaconConfig().Eth1DataVotesLength()
remainingSlotsInPeriod := eth1DataVotesLength - uint64(headSlot.ModSlot(primitives.Slot(eth1DataVotesLength)))
baseEligibleSlots := params.BeaconConfig().Eth1FollowDistance +
eth1DataVotesLength/2 +
uint64(params.BeaconConfig().SlotsPerEpoch.Mul(3)) +
remainingSlotsInPeriod
eligibleEpoch := slots.ToEpoch(headSlot.Add(baseEligibleSlots))
eligibleEpoch := calculateEligibleEpoch(headSlot)

if pendingQueuedCount == 0 {
httputil.WriteJson(w, &structs.EstimatedActivationResponse{
WaitingEpoch: uint64(0),
EligibleEpoch: uint64(eligibleEpoch),
EligibleEpoch: eligibleEpoch,
Status: status,
})
return
}

httputil.WriteJson(w, &structs.EstimatedActivationResponse{
WaitingEpoch: calculateWaitingEpoch(activeCount, pendingQueuedCount),
EligibleEpoch: uint64(eligibleEpoch),
EligibleEpoch: eligibleEpoch,
Status: status,
})
}

func calculateEligibleEpoch(headSlot primitives.Slot) uint64 {
epochsPerEth1VotingPeriod := params.BeaconConfig().EpochsPerEth1VotingPeriod

currentEpoch := slots.ToEpoch(headSlot)
currentPeriodStartEpoch := currentEpoch - currentEpoch.Mod(uint64(epochsPerEth1VotingPeriod))
midEpochInThisPeriod := currentPeriodStartEpoch + epochsPerEth1VotingPeriod/2
if currentEpoch < midEpochInThisPeriod {
return uint64(currentPeriodStartEpoch.Add(uint64(epochsPerEth1VotingPeriod))+epochsPerEth1VotingPeriod/2) + 1
} else {
return uint64(currentPeriodStartEpoch.Add(uint64(epochsPerEth1VotingPeriod.Mul(2)))+epochsPerEth1VotingPeriod/2) + 1
}
}

// GetEpochReward returns total reward at given epoch.
func (s *Server) GetEpochReward(w http.ResponseWriter, r *http.Request) {
ctx, span := trace.StartSpan(r.Context(), "over.GetEpochReward")
Expand Down
20 changes: 20 additions & 0 deletions beacon-chain/rpc/over/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ func TestEstimatedActivation_NoPendingValidators(t *testing.T) {
})
}

func TestCalculateEligibleEpoch(t *testing.T) {
tests := []struct {
headSlot primitives.Slot
want uint64
}{
{headSlot: 93122, want: 2977},
{headSlot: 93287, want: 3041},
{headSlot: 107802, want: 3489},
{headSlot: 120101, want: 3873},
{headSlot: 141829, want: 4513},
{headSlot: 156671, want: 4961},
{headSlot: 156680, want: 5025},
{headSlot: 156693, want: 5025},
}
for _, test := range tests {
got := calculateEligibleEpoch(test.headSlot)
assert.Equal(t, test.want, got, "Incorrect Eligible Epoch")
}
}

func TestGetEpochReward(t *testing.T) {
st, err := util.NewBeaconState()
require.NoError(t, err)
Expand Down

0 comments on commit 181c35f

Please sign in to comment.