package blockchain import ( "fmt" "net/http" "strconv" "github.com/emicklei/dot" ) const template = `
` // TreeHandler is a handler to serve /tree page in metrics. func (s *Service) TreeHandler(w http.ResponseWriter, _ *http.Request) { if s.headState() == nil { if _, err := w.Write([]byte("Unavailable during initial syncing")); err != nil { log.WithError(err).Error("Failed to render p2p info page") } } nodes := s.forkChoiceStore.Nodes() graph := dot.NewGraph(dot.Directed) graph.Attr("rankdir", "RL") graph.Attr("labeljust", "l") dotNodes := make([]*dot.Node, len(nodes)) avgBalance := uint64(averageBalance(s.headState().Balances())) for i := len(nodes) - 1; i >= 0; i-- { // Construct label for each node. slot := strconv.Itoa(int(nodes[i].Slot)) weight := strconv.Itoa(int(nodes[i].Weight / 1e9)) // Convert unit Gwei to unit ETH. votes := strconv.Itoa(int(nodes[i].Weight / 1e9 / avgBalance)) bestDescendent := strconv.Itoa(int(nodes[i].BestDescendent)) index := strconv.Itoa(int(i)) label := "slot: " + slot + "\n index: " + index + "\n bestDescendent: " + bestDescendent + "\n votes: " + votes + "\n weight: " + weight var dotN dot.Node if nodes[i].Parent != ^uint64(0) { dotN = graph.Node(index).Box().Attr("label", label) } if nodes[i].Slot == s.headSlot() && nodes[i].BestDescendent == ^uint64(0) { dotN = dotN.Attr("color", "green") } dotNodes[i] = &dotN } for i := len(nodes) - 1; i >= 0; i-- { if nodes[i].Parent != ^uint64(0) && nodes[i].Parent < uint64(len(dotNodes)) { graph.Edge(*dotNodes[i], *dotNodes[nodes[i].Parent]) } } w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "text/html") if _, err := fmt.Fprintf(w, template, graph.String()); err != nil { log.WithError(err).Error("Failed to render p2p info page") } }