From e76099f75682968ab3217d1e128b21ea0158ff36 Mon Sep 17 00:00:00 2001 From: envestcc Date: Mon, 6 Jan 2025 11:04:44 +0800 Subject: [PATCH] dbwatcher --- Makefile | 4 ++ blockchain/blockdao/blockdao.go | 6 +- blockchain/blockdao/blockindexer.go | 6 +- tools/dbwatcher/erigon.go | 87 +++++++++++++++++++++++++++++ tools/dbwatcher/main.go | 31 ++++++++++ 5 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 tools/dbwatcher/erigon.go create mode 100644 tools/dbwatcher/main.go diff --git a/Makefile b/Makefile index b68de03970..90f9f0aea3 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/blockchain/blockdao/blockdao.go b/blockchain/blockdao/blockdao.go index 31a0f21444..52000e346a 100644 --- a/blockchain/blockdao/blockdao.go +++ b/blockchain/blockdao/blockdao.go @@ -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() diff --git a/blockchain/blockdao/blockindexer.go b/blockchain/blockdao/blockindexer.go index baa5e52046..3c4c1f6766 100644 --- a/blockchain/blockdao/blockindexer.go +++ b/blockchain/blockdao/blockindexer.go @@ -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{ diff --git a/tools/dbwatcher/erigon.go b/tools/dbwatcher/erigon.go new file mode 100644 index 0000000000..7c10aa0aeb --- /dev/null +++ b/tools/dbwatcher/erigon.go @@ -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 +} diff --git a/tools/dbwatcher/main.go b/tools/dbwatcher/main.go new file mode 100644 index 0000000000..d33e310fff --- /dev/null +++ b/tools/dbwatcher/main.go @@ -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() +}