From 333847f7c95ead3024d58b8350eb350e4481dddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 18 Nov 2019 21:11:43 +0100 Subject: [PATCH] chainwatch: store mpool message additions --- .gitignore | 4 +++ cmd/lotus-chainwatch/dot.go | 49 +++++++++++++++++++++++++++++++++ cmd/lotus-chainwatch/main.go | 1 + cmd/lotus-chainwatch/storage.go | 26 +++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 cmd/lotus-chainwatch/dot.go diff --git a/.gitignore b/.gitignore index 1e4ce2d8e..6fef7d758 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,10 @@ build/.* build/paramfetch.sh /vendor +/blocks.dot +/blocks.svg +/chainwatch +/chainwatch.db *-fuzz.zip /chain/types/work_msg/ diff --git a/cmd/lotus-chainwatch/dot.go b/cmd/lotus-chainwatch/dot.go new file mode 100644 index 000000000..354e4bc67 --- /dev/null +++ b/cmd/lotus-chainwatch/dot.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "hash/crc32" + "strconv" + + "gopkg.in/urfave/cli.v2" +) + +var dotCmd = &cli.Command{ + Name: "dot", + Usage: "generate dot graphs", + Action: func(cctx *cli.Context) error { + st, err := openStorage() + if err != nil { + return err + } + + minH, err := strconv.ParseInt(cctx.Args().Get(0), 10, 32) + tosee, err := strconv.ParseInt(cctx.Args().Get(1), 10, 32) + maxH := minH + tosee + + res, err := st.db.Query("select block, parent, b.miner from block_parents inner join blocks b on block_parents.block = b.cid where b.height > ? and b.height < ?", minH, maxH) + if err != nil { + return err + } + + fmt.Println("digraph D {") + + for res.Next() { + var block,parent,miner string + if err := res.Scan(&block, &parent, &miner); err != nil { + return err + } + + col := crc32.Checksum([]byte(miner), crc32.MakeTable(crc32.Castagnoli)) & 0x80808080 + 0x70707070 + + fmt.Printf("%s [label = \"%s\", fillcolor = \"#%06x\", style=filled]\n%s -> %s\n", block, miner, col, block, parent) + } + if res.Err() != nil { + return res.Err() + } + + fmt.Println("}") + + return nil + }, +} \ No newline at end of file diff --git a/cmd/lotus-chainwatch/main.go b/cmd/lotus-chainwatch/main.go index 2888eb076..2d17761c1 100644 --- a/cmd/lotus-chainwatch/main.go +++ b/cmd/lotus-chainwatch/main.go @@ -20,6 +20,7 @@ func main() { local := []*cli.Command{ runCmd, + dotCmd, } app := &cli.App{ diff --git a/cmd/lotus-chainwatch/storage.go b/cmd/lotus-chainwatch/storage.go index 5405d4bdc..207152c17 100644 --- a/cmd/lotus-chainwatch/storage.go +++ b/cmd/lotus-chainwatch/storage.go @@ -101,6 +101,19 @@ create table if not exists blocks timestamp int not null ); +create table if not exists block_parents +( + block text not null + constraint block_parents_blocks_cid_fk + references blocks, + parent text not null + constraint block_parents_blocks_cid_fk_2 + references blocks +); + +create unique index if not exists block_parents_block_parent_uindex + on block_parents (block, parent); + create unique index if not exists blocks_cid_uindex on blocks (cid); @@ -248,6 +261,19 @@ func (st *storage) storeHeaders(bhs map[cid.Cid]*types.BlockHeader) error { } } + stmt2, err := tx.Prepare(`insert into block_parents (block, parent) values (?, ?) on conflict do nothing`) + if err != nil { + return err + } + defer stmt2.Close() + for _, bh := range bhs { + for _, parent := range bh.Parents { + if _, err := stmt2.Exec(bh.Cid().String(), parent.String()); err != nil { + return err + } + } + } + return tx.Commit() }