Files
prysm/tools/blocktree/main.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

103 lines
2.5 KiB
Go

/*
*
- Block tree graph viz
*
- Given a DB, start slot and end slot. This tool computes the graphviz data
- needed to construct the block tree in graphviz data format. Then one can paste
- the data in a Graph rendering engine (ie. http://www.webgraphviz.com/) to see the visual format.
*/
package main
import (
"context"
"encoding/hex"
"flag"
"fmt"
"strconv"
"github.com/OffchainLabs/prysm/v7/beacon-chain/db/filters"
"github.com/OffchainLabs/prysm/v7/beacon-chain/db/kv"
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
"github.com/emicklei/dot"
)
var (
// Required fields
datadir = flag.String("datadir", "", "Path to data directory.")
startSlot = flag.Uint("startSlot", 0, "Start slot of the block tree")
endSlot = flag.Uint("endSlot", 0, "Start slot of the block tree")
)
// Used for tree, each node is a representation of a node in the graph
type node struct {
parentRoot [32]byte
dothNode *dot.Node
score map[uint64]bool
}
func main() {
flag.Parse()
database, err := kv.NewKVStore(context.Background(), *datadir)
if err != nil {
panic(err)
}
graph := dot.NewGraph(dot.Directed)
graph.Attr("rankdir", "RL")
graph.Attr("labeljust", "l")
startSlot := primitives.Slot(*startSlot)
endSlot := primitives.Slot(*endSlot)
filter := filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(endSlot)
blks, roots, err := database.Blocks(context.Background(), filter)
if err != nil {
panic(err)
}
// Construct nodes
m := make(map[[32]byte]*node)
for i := range blks {
b := blks[i]
r := roots[i]
m[r] = &node{score: make(map[uint64]bool)}
state, err := database.State(context.Background(), r)
if err != nil {
panic(err)
}
slot := b.Block().Slot()
// If the state is not available, roll back
for state == nil {
slot--
_, rts, err := database.BlockRootsBySlot(context.Background(), slot)
if err != nil {
panic(err)
}
state, err = database.State(context.Background(), rts[0])
if err != nil {
panic(err)
}
}
// Construct label of each node.
rStr := hex.EncodeToString(r[:2])
label := "slot: " + strconv.Itoa(int(b.Block().Slot())) + "\n root: " + rStr // lint:ignore uintcast -- this is OK for logging.
dotN := graph.Node(rStr).Box().Attr("label", label)
n := &node{
parentRoot: b.Block().ParentRoot(),
dothNode: &dotN,
}
m[r] = n
}
// Construct an edge only if block's parent exist in the tree.
for _, n := range m {
if _, ok := m[n.parentRoot]; ok {
graph.Edge(*n.dothNode, *m[n.parentRoot].dothNode)
}
}
fmt.Println(graph.String())
}