mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
4235980511
* Delete old code * RPC mock testing * Fixed BUILD * Conflict * Lint * More lint
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const latestSlotCount = 10
|
|
|
|
// HeadsHandler is a handler to serve /heads page in metrics.
|
|
func (s *Service) HeadsHandler(w http.ResponseWriter, _ *http.Request) {
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := fmt.Fprintf(w, "\n %s\t%s\t", "Head slot", "Head root"); err != nil {
|
|
logrus.WithError(err).Error("Failed to render chain heads page")
|
|
return
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(w, "\n %s\t%s\t", "---------", "---------"); err != nil {
|
|
logrus.WithError(err).Error("Failed to render chain heads page")
|
|
return
|
|
}
|
|
|
|
slots := s.latestHeadSlots()
|
|
for _, slot := range slots {
|
|
r := hex.EncodeToString(bytesutil.Trunc(s.canonicalRoots[uint64(slot)]))
|
|
if _, err := fmt.Fprintf(w, "\n %d\t\t%s\t", slot, r); err != nil {
|
|
logrus.WithError(err).Error("Failed to render chain heads page")
|
|
return
|
|
}
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
if _, err := w.Write(buf.Bytes()); err != nil {
|
|
log.WithError(err).Error("Failed to render chain heads page")
|
|
}
|
|
|
|
}
|
|
|
|
// This returns the latest head slots in a slice and up to latestSlotCount
|
|
func (s *Service) latestHeadSlots() []int {
|
|
slots := make([]int, 0, len(s.canonicalRoots))
|
|
for k := range s.canonicalRoots {
|
|
slots = append(slots, int(k))
|
|
}
|
|
sort.Ints(slots)
|
|
if (len(slots)) > latestSlotCount {
|
|
return slots[len(slots)-latestSlotCount:]
|
|
}
|
|
return slots
|
|
}
|