From 0a4a3c15835717a2bd2ff59c5e7bd4787c6e695e Mon Sep 17 00:00:00 2001 From: HuangYi Date: Tue, 31 May 2022 15:52:44 +0800 Subject: [PATCH] add more flags --- cmd/cronosd/cmd/reindex-duplicated-tx.go | 78 ++++++++++++++++++------ 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/cmd/cronosd/cmd/reindex-duplicated-tx.go b/cmd/cronosd/cmd/reindex-duplicated-tx.go index 6ce6eb5050..dd51771071 100644 --- a/cmd/cronosd/cmd/reindex-duplicated-tx.go +++ b/cmd/cronosd/cmd/reindex-duplicated-tx.go @@ -1,6 +1,7 @@ package cmd import ( + "bufio" "context" "errors" "fmt" @@ -24,14 +25,20 @@ import ( tmstore "github.com/tendermint/tendermint/store" ) -const FlagConcurrency = "concurrency" +const ( + FlagPrintTxs = "print-txs" + FlagBlocksFile = "blocks-file" + FlagStartBlock = "start-block" + FlagEndBlock = "end-block" + FlagConcurrency = "concurrency" +) // ReindexDuplicatedTx update the tx execution result of false-failed tx in tendermint db func ReindexDuplicatedTx() *cobra.Command { cmd := &cobra.Command{ Use: "reindex-duplicated-tx", Short: "Reindex tx that suffer from tendermint duplicated tx issue", - Args: cobra.ExactArgs(2), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { ctx := server.GetServerContextFromCmd(cmd) @@ -39,20 +46,10 @@ func ReindexDuplicatedTx() *cobra.Command { if err != nil { return err } - startHeight, err := strconv.ParseInt(args[0], 10, 64) - if err != nil { - return err - } - endHeight, err := strconv.ParseInt(args[1], 10, 64) + printTxs, err := cmd.Flags().GetBool(FlagPrintTxs) if err != nil { return err } - if startHeight < 1 { - return fmt.Errorf("invalid start-block: %d", startHeight) - } - if endHeight < startHeight { - return fmt.Errorf("invalid end-block %d, smaller than start-block", endHeight) - } tmDB, err := openTMDB(ctx.Config, chainID) if err != nil { @@ -81,6 +78,10 @@ func ReindexDuplicatedTx() *cobra.Command { return err } if txResult.Code == 0 && txResult.Code != indexed.Result.Code { + if printTxs { + fmt.Println(height, txIndex) + continue + } // a success tx but indexed wrong, reindex the tx result := &abci.TxResult{ Height: height, @@ -131,14 +132,53 @@ func ReindexDuplicatedTx() *cobra.Command { }(&wg) } - findBlock := func() { - for height := startHeight; height <= endHeight; height++ { - blockChan <- height + blocksFile, err := cmd.Flags().GetString(FlagBlocksFile) + if err != nil { + return err + } + findBlock := func() error { + if len(blocksFile) > 0 { + // read block numbers from file, one number per line + file, err := os.Open(blocksFile) + if err != nil { + return err + } + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + blockNumber, err := strconv.ParseInt(scanner.Text(), 10, 64) + if err != nil { + return err + } + blockChan <- blockNumber + } + } else { + startHeight, err := cmd.Flags().GetInt(FlagStartBlock) + if err != nil { + return err + } + endHeight, err := cmd.Flags().GetInt(FlagEndBlock) + if err != nil { + return err + } + if startHeight < 1 { + return fmt.Errorf("invalid start-block: %d", startHeight) + } + if endHeight < startHeight { + return fmt.Errorf("invalid end-block %d, smaller than start-block", endHeight) + } + + for height := startHeight; height <= endHeight; height++ { + blockChan <- int64(height) + } } + return nil } go func() { - findBlock() + if err := findBlock(); err != nil { + fmt.Fprintln(os.Stderr, err) + } close(blockChan) }() @@ -148,6 +188,10 @@ func ReindexDuplicatedTx() *cobra.Command { }, } cmd.Flags().String(flags.FlagChainID, "cronosmainnet_25-1", "network chain ID, only useful for psql tx indexer backend") + cmd.Flags().Bool(FlagPrintTxs, false, "Print the block number and tx indexes of the duplicated txs without patch") + cmd.Flags().String(FlagBlocksFile, "", "Read block numbers from a file instead of iterating all the blocks") + cmd.Flags().Int(FlagStartBlock, 1, "The start of the block range to iterate, inclusive") + cmd.Flags().Int(FlagEndBlock, -1, "The end of the block range to iterate, inclusive") cmd.Flags().Int(FlagConcurrency, runtime.NumCPU(), "Define how many workers run in concurrency") return cmd