Skip to content

Commit

Permalink
add more flags
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 31, 2022
1 parent fd18406 commit 0a4a3c1
Showing 1 changed file with 61 additions and 17 deletions.
78 changes: 61 additions & 17 deletions cmd/cronosd/cmd/reindex-duplicated-tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"context"
"errors"
"fmt"
Expand All @@ -24,35 +25,31 @@ 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)

chainID, err := cmd.Flags().GetString(flags.FlagChainID)
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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}()

Expand All @@ -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
Expand Down

0 comments on commit 0a4a3c1

Please sign in to comment.