2019-11-18 20:11:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-14 20:04:27 +00:00
|
|
|
"database/sql"
|
2019-11-18 20:11:43 +00:00
|
|
|
"fmt"
|
|
|
|
"hash/crc32"
|
|
|
|
"strconv"
|
|
|
|
|
2019-11-19 12:57:16 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-07-16 14:45:31 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-07-16 14:45:31 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-18 20:11:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var dotCmd = &cli.Command{
|
2019-12-06 00:27:32 +00:00
|
|
|
Name: "dot",
|
|
|
|
Usage: "generate dot graphs",
|
2019-12-05 11:12:08 +00:00
|
|
|
ArgsUsage: "<minHeight> <toseeHeight>",
|
2019-11-18 20:11:43 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-07-16 14:45:31 +00:00
|
|
|
ll := cctx.String("log-level")
|
|
|
|
if err := logging.SetLogLevel("*", ll); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
db, err := sql.Open("postgres", cctx.String("db"))
|
2019-11-18 20:11:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-14 20:04:27 +00:00
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Errorw("Failed to close database", "error", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
return xerrors.Errorf("Database failed to respond to ping (is it online?): %w", err)
|
|
|
|
}
|
2019-11-18 20:11:43 +00:00
|
|
|
|
|
|
|
minH, err := strconv.ParseInt(cctx.Args().Get(0), 10, 32)
|
2020-06-02 14:29:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 20:11:43 +00:00
|
|
|
tosee, err := strconv.ParseInt(cctx.Args().Get(1), 10, 32)
|
2020-06-02 14:29:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 20:11:43 +00:00
|
|
|
maxH := minH + tosee
|
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
res, err := db.Query(`select block, parent, b.miner, b.height, p.height from block_parents
|
2019-12-18 17:40:05 +00:00
|
|
|
inner join blocks b on block_parents.block = b.cid
|
|
|
|
inner join blocks p on block_parents.parent = p.cid
|
2020-01-08 14:08:34 +00:00
|
|
|
where b.height > $1 and b.height < $2`, minH, maxH)
|
2019-12-18 17:40:05 +00:00
|
|
|
|
2019-11-18 20:11:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("digraph D {")
|
|
|
|
|
2020-07-14 20:04:27 +00:00
|
|
|
hl, err := syncedBlocks(db)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-12-11 22:17:44 +00:00
|
|
|
|
2019-11-18 20:11:43 +00:00
|
|
|
for res.Next() {
|
2019-11-18 21:39:07 +00:00
|
|
|
var block, parent, miner string
|
2019-12-18 17:40:05 +00:00
|
|
|
var height, ph uint64
|
|
|
|
if err := res.Scan(&block, &parent, &miner, &height, &ph); err != nil {
|
2019-11-19 12:57:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bc, err := cid.Parse(block)
|
|
|
|
if err != nil {
|
2019-11-18 20:11:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-11 22:17:44 +00:00
|
|
|
_, has := hl[bc]
|
2019-11-19 12:57:16 +00:00
|
|
|
|
|
|
|
col := crc32.Checksum([]byte(miner), crc32.MakeTable(crc32.Castagnoli))&0xc0c0c0c0 + 0x30303030
|
|
|
|
|
|
|
|
hasstr := ""
|
|
|
|
if !has {
|
2019-12-18 17:40:05 +00:00
|
|
|
//col = 0xffffffff
|
2019-11-19 12:57:16 +00:00
|
|
|
hasstr = " UNSYNCED"
|
|
|
|
}
|
2019-11-18 20:11:43 +00:00
|
|
|
|
2019-12-18 17:40:05 +00:00
|
|
|
nulls := height - ph - 1
|
|
|
|
for i := uint64(0); i < nulls; i++ {
|
2019-12-19 15:50:18 +00:00
|
|
|
name := block + "NP" + fmt.Sprint(i)
|
2019-12-18 17:40:05 +00:00
|
|
|
|
|
|
|
fmt.Printf("%s [label = \"NULL:%d\", fillcolor = \"#ffddff\", style=filled, forcelabels=true]\n%s -> %s\n",
|
2019-12-19 15:50:18 +00:00
|
|
|
name, height-nulls+i, name, parent)
|
2019-12-18 17:40:05 +00:00
|
|
|
|
|
|
|
parent = name
|
|
|
|
}
|
|
|
|
|
2019-11-19 12:57:16 +00:00
|
|
|
fmt.Printf("%s [label = \"%s:%d%s\", fillcolor = \"#%06x\", style=filled, forcelabels=true]\n%s -> %s\n", block, miner, height, hasstr, col, block, parent)
|
2019-11-18 20:11:43 +00:00
|
|
|
}
|
|
|
|
if res.Err() != nil {
|
|
|
|
return res.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("}")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2019-11-18 21:39:07 +00:00
|
|
|
}
|
2020-07-14 20:04:27 +00:00
|
|
|
|
|
|
|
func syncedBlocks(db *sql.DB) (map[cid.Cid]struct{}, error) {
|
|
|
|
// timestamp is used to return a configurable amount of rows based on when they were last added.
|
|
|
|
rws, err := db.Query(`select cid FROM blocks_synced`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("Failed to query blocks_synced: %w", err)
|
|
|
|
}
|
|
|
|
out := map[cid.Cid]struct{}{}
|
|
|
|
|
|
|
|
for rws.Next() {
|
|
|
|
var c string
|
|
|
|
if err := rws.Scan(&c); err != nil {
|
|
|
|
return nil, xerrors.Errorf("Failed to scan blocks_synced: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ci, err := cid.Parse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("Failed to parse blocks_synced: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out[ci] = struct{}{}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|