chainwatch: store mpool message additions

This commit is contained in:
Łukasz Magiera 2019-11-18 21:11:43 +01:00
parent f40a1afac8
commit 333847f7c9
4 changed files with 80 additions and 0 deletions

4
.gitignore vendored
View File

@ -13,6 +13,10 @@
build/.*
build/paramfetch.sh
/vendor
/blocks.dot
/blocks.svg
/chainwatch
/chainwatch.db
*-fuzz.zip
/chain/types/work_msg/

View File

@ -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
},
}

View File

@ -20,6 +20,7 @@ func main() {
local := []*cli.Command{
runCmd,
dotCmd,
}
app := &cli.App{

View File

@ -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()
}