Skip to content

Commit

Permalink
fix error when calling unsynchronized epoch details, increased cachin…
Browse files Browse the repository at this point in the history
…g times for finalized information
  • Loading branch information
pk910 committed Aug 4, 2023
1 parent 47762f4 commit 7c53fdd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
64 changes: 34 additions & 30 deletions handlers/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Epoch(w http.ResponseWriter, r *http.Request) {
if handleTemplateError(w, r, "slot.go", "Slot", "blockSlot", templates.GetTemplate(notfoundTemplateFiles...).ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
return
}

data := InitPageData(w, r, "blockchain", "/epoch", fmt.Sprintf("Epoch %v", epoch), epochTemplateFiles)
Expand Down Expand Up @@ -77,43 +78,44 @@ func buildEpochPageData(epoch uint64) (*models.EpochPageData, time.Duration) {
finalizedHead, _ := services.GlobalBeaconService.GetFinalizedBlockHead()
slotAssignments, syncedEpochs := services.GlobalBeaconService.GetProposerAssignments(epoch, epoch)

dbEpochs := services.GlobalBeaconService.GetDbEpochs(epoch, 1)
if dbEpochs[0] == nil {
return nil, -1
}
dbEpoch := dbEpochs[0]
nextEpoch := epoch + 1
if nextEpoch > currentEpoch {
nextEpoch = 0
}
firstSlot := epoch * utils.Config.Chain.Config.SlotsPerEpoch
lastSlot := firstSlot + utils.Config.Chain.Config.SlotsPerEpoch - 1
pageData := &models.EpochPageData{
Epoch: epoch,
PreviousEpoch: epoch - 1,
NextEpoch: nextEpoch,
Ts: utils.EpochToTime(epoch),
Synchronized: syncedEpochs[epoch],
Finalized: uint64(finalizedHead.Data.Header.Message.Slot) >= lastSlot,
AttestationCount: dbEpoch.AttestationCount,
DepositCount: dbEpoch.DepositCount,
ExitCount: dbEpoch.ExitCount,
WithdrawalCount: dbEpoch.WithdrawCount,
WithdrawalAmount: dbEpoch.WithdrawAmount,
ProposerSlashingCount: dbEpoch.ProposerSlashingCount,
AttesterSlashingCount: dbEpoch.AttesterSlashingCount,
EligibleEther: dbEpoch.Eligible,
TargetVoted: dbEpoch.VotedTarget,
HeadVoted: dbEpoch.VotedHead,
TotalVoted: dbEpoch.VotedTotal,
SyncParticipation: float64(dbEpoch.SyncParticipation) * 100,
ValidatorCount: dbEpoch.ValidatorCount,
AverageValidatorBalance: dbEpoch.ValidatorBalance / dbEpoch.ValidatorCount,
Epoch: epoch,
PreviousEpoch: epoch - 1,
NextEpoch: nextEpoch,
Ts: utils.EpochToTime(epoch),
Synchronized: syncedEpochs[epoch],
Finalized: uint64(finalizedHead.Data.Header.Message.Slot) >= lastSlot,
}
if dbEpoch.Eligible > 0 {
pageData.TargetVoteParticipation = float64(dbEpoch.VotedTarget) * 100.0 / float64(dbEpoch.Eligible)
pageData.HeadVoteParticipation = float64(dbEpoch.VotedHead) * 100.0 / float64(dbEpoch.Eligible)
pageData.TotalVoteParticipation = float64(dbEpoch.VotedTotal) * 100.0 / float64(dbEpoch.Eligible)

dbEpochs := services.GlobalBeaconService.GetDbEpochs(epoch, 1)
dbEpoch := dbEpochs[0]
if dbEpoch != nil {
pageData.AttestationCount = dbEpoch.AttestationCount
pageData.DepositCount = dbEpoch.DepositCount
pageData.ExitCount = dbEpoch.ExitCount
pageData.WithdrawalCount = dbEpoch.WithdrawCount
pageData.WithdrawalAmount = dbEpoch.WithdrawAmount
pageData.ProposerSlashingCount = dbEpoch.ProposerSlashingCount
pageData.AttesterSlashingCount = dbEpoch.AttesterSlashingCount
pageData.EligibleEther = dbEpoch.Eligible
pageData.TargetVoted = dbEpoch.VotedTarget
pageData.HeadVoted = dbEpoch.VotedHead
pageData.TotalVoted = dbEpoch.VotedTotal
pageData.SyncParticipation = float64(dbEpoch.SyncParticipation) * 100
pageData.ValidatorCount = dbEpoch.ValidatorCount
pageData.AverageValidatorBalance = dbEpoch.ValidatorBalance / dbEpoch.ValidatorCount

if dbEpoch.Eligible > 0 {
pageData.TargetVoteParticipation = float64(dbEpoch.VotedTarget) * 100.0 / float64(dbEpoch.Eligible)
pageData.HeadVoteParticipation = float64(dbEpoch.VotedHead) * 100.0 / float64(dbEpoch.Eligible)
pageData.TotalVoteParticipation = float64(dbEpoch.VotedTotal) * 100.0 / float64(dbEpoch.Eligible)
}
}

// load slots
Expand Down Expand Up @@ -181,7 +183,9 @@ func buildEpochPageData(epoch uint64) (*models.EpochPageData, time.Duration) {
pageData.BlockCount = uint64(blockCount)

var cacheTimeout time.Duration
if pageData.Finalized {
if !pageData.Synchronized {
cacheTimeout = 5 * time.Minute
} else if pageData.Finalized {
cacheTimeout = 30 * time.Minute
} else {
cacheTimeout = 12 * time.Second
Expand Down
7 changes: 6 additions & 1 deletion handlers/epochs.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ func buildEpochsPageData(firstEpoch uint64, pageSize uint64) (*models.EpochsPage
dbIdx := 0
dbCnt := len(dbEpochs)
epochCount := uint64(0)
allFinalized := true
for epochIdx := int64(firstEpoch); epochIdx >= 0 && epochCount < epochLimit; epochIdx-- {
epoch := uint64(epochIdx)
finalized := false
if finalizedHead != nil && uint64(finalizedHead.Data.Header.Message.Slot) >= epoch*utils.Config.Chain.Config.SlotsPerEpoch {
finalized = true
} else {
allFinalized = false
}
epochData := &models.EpochsPageDataEpoch{
Epoch: epoch,
Expand Down Expand Up @@ -140,7 +143,9 @@ func buildEpochsPageData(firstEpoch uint64, pageSize uint64) (*models.EpochsPage
pageData.LastEpoch = firstEpoch - pageData.EpochCount + 1

var cacheTimeout time.Duration
if firstEpoch+2 < uint64(currentEpoch) {
if allFinalized {
cacheTimeout = 30 * time.Minute
} else if firstEpoch+2 < uint64(currentEpoch) {
cacheTimeout = 10 * time.Minute
} else {
cacheTimeout = 12 * time.Second
Expand Down
2 changes: 1 addition & 1 deletion handlers/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func buildSlotPageData(blockSlot int64, blockRoot []byte) (*models.SlotPageData,
cacheTimeout = timeDiff
}
} else if pageData.EpochFinalized {
cacheTimeout = 10 * time.Minute
cacheTimeout = 30 * time.Minute
} else if blockData != nil {
cacheTimeout = 5 * time.Minute
} else {
Expand Down
7 changes: 6 additions & 1 deletion handlers/slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,14 @@ func buildSlotsPageData(firstSlot uint64, pageSize uint64) (*models.SlotsPageDat
dbIdx := 0
dbCnt := len(dbSlots)
blockCount := uint64(0)
allFinalized := true
for slotIdx := int64(firstSlot); slotIdx >= int64(lastSlot); slotIdx-- {
slot := uint64(slotIdx)
finalized := false
if finalizedHead != nil && uint64(finalizedHead.Data.Header.Message.Slot) >= slot {
finalized = true
} else {
allFinalized = false
}
haveBlock := false
for dbIdx < dbCnt && dbSlots[dbIdx] != nil && dbSlots[dbIdx].Slot == slot {
Expand Down Expand Up @@ -188,7 +191,9 @@ func buildSlotsPageData(firstSlot uint64, pageSize uint64) (*models.SlotsPageDat
pageData.LastSlot = lastSlot

var cacheTimeout time.Duration
if firstEpoch < uint64(currentEpoch) {
if allFinalized {
cacheTimeout = 30 * time.Minute
} else if firstEpoch < uint64(currentEpoch) {
cacheTimeout = 10 * time.Minute
} else {
cacheTimeout = 12 * time.Second
Expand Down

0 comments on commit 7c53fdd

Please sign in to comment.