Skip to content

Commit

Permalink
dbwatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jan 6, 2025
1 parent 05448e3 commit e76099f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,7 @@ newxctl:
.PHONY: iomigrater
iomigrater:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_IOMIGRATER) -v ./tools/iomigrater

.PHONY: dbwatcher
dbwatcher:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/dbwatcher -v ./tools/dbwatcher
6 changes: 3 additions & 3 deletions blockchain/blockdao/blockdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ func (dao *blockDAO) TransactionLogs(height uint64) (*iotextypes.TransactionLogs
}

func (dao *blockDAO) PutBlock(ctx context.Context, blk *block.Block) error {
if blk.Height() >= 12000000 {
panic("stop at 12000000")
}
// if blk.Height() >= 12000000 {
// panic("stop at 12000000")
// }
timer := dao.timerFactory.NewTimer("put_block")
if err := dao.blockStore.PutBlock(ctx, blk); err != nil {
timer.End()
Expand Down
6 changes: 3 additions & 3 deletions blockchain/blockdao/blockindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func (bic *BlockIndexerChecker) CheckIndexer(ctx context.Context, indexer BlockI
bcCtx.Tip.Timestamp = time.Unix(g.Timestamp, 0)
}
for {
if blk.Height() >= 12000000 {
panic("stop at 12000000")
}
// if blk.Height() >= 12000000 {
// panic("stop at 12000000")
// }
if err = indexer.PutBlock(protocol.WithFeatureCtx(protocol.WithBlockCtx(
protocol.WithBlockchainCtx(ctx, bcCtx),
protocol.BlockCtx{
Expand Down
87 changes: 87 additions & 0 deletions tools/dbwatcher/erigon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"context"
"fmt"

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
erigonlog "github.com/ledgerwatch/log/v3"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var WatchErigon = &cobra.Command{
Use: "erigon",
Short: "watch erigon",
Long: "watch erigon",
RunE: func(cmd *cobra.Command, args []string) error {
return watchErigon(args[0])
},
}
var (
keyLimit = uint64(10)
)

func init() {
WatchErigon.PersistentFlags().Uint64VarP(&keyLimit, "limit", "l", 10, "key limit")
}

func watchErigon(path string) error {
fmt.Println("walking erigon", path)
lg := erigonlog.New()
lg.SetHandler(erigonlog.StdoutHandler)
rw, err := mdbx.NewMDBX(lg).Path(path).WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg {
defaultBuckets["erigonsystem"] = kv.TableCfgItem{}
return defaultBuckets
}).Readonly().Open(context.Background())
if err != nil {
return errors.Wrapf(err, "open db: %s", path)
}
defer rw.Close()
return walkdbkv(rw)
}

func walkdbkv(rw kv.RoDB) error {
tx, err := rw.BeginRo(context.Background())
if err != nil {
return errors.Wrap(err, "begin ro")
}
defer tx.Rollback()

dbsize, err := tx.DBSize()
if err != nil {
return errors.Wrap(err, "db size")
}
fmt.Printf("db size: %d\n\n", dbsize)
tables, err := tx.ListBuckets()
if err != nil {
return errors.Wrap(err, "list tables")
}
// fmt.Printf("tables: %v\n", tables)
total := uint64(0)
for _, table := range tables {
tsize, err := tx.BucketSize(table)
if err != nil {
return errors.Wrapf(err, "table size: %s", table)
}
if tsize == 0 {
continue
}
total += tsize
keynum := uint64(0)
err = tx.ForEach(table, nil, func(k, v []byte) error {
if keyLimit == 0 || keynum < keyLimit {
fmt.Printf("table: %s, key: %x, value: %x\n", table, k, v)
}
keynum++
return nil
})
if err != nil {
return errors.Wrapf(err, "for each table: %s", table)
}
fmt.Printf("table: %s, size: %d, keynum: %d\n\n", table, tsize, keynum)
}
fmt.Printf("total size used: %d\n", total)
return nil
}
31 changes: 31 additions & 0 deletions tools/dbwatcher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"os"

"github.com/spf13/cobra"
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "dbwatcher",
Short: "Command-line interface for watch of IoTeX blockchain db file",
Long: "dbwatcher is a command-line interface for watch of IoTeX blockchain db file.",
}

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func init() {
RootCmd.AddCommand(WatchErigon)

RootCmd.HelpFunc()
}

func main() {
Execute()
}

0 comments on commit e76099f

Please sign in to comment.