|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/ClickHouse/clickhouse-go/v2" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + config "github.com/thirdweb-dev/indexer/configs" |
| 13 | + "github.com/thirdweb-dev/indexer/internal/orchestrator" |
| 14 | + "github.com/thirdweb-dev/indexer/internal/rpc" |
| 15 | + "github.com/thirdweb-dev/indexer/internal/storage" |
| 16 | + "github.com/thirdweb-dev/indexer/internal/validation" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + validateAndFixCmd = &cobra.Command{ |
| 21 | + Use: "validateAndFix", |
| 22 | + Short: "Validate and fix blockchain data", |
| 23 | + Long: "Validate blockchain data in batches and automatically fix any issues found including duplicates, gaps, and invalid blocks", |
| 24 | + Run: func(cmd *cobra.Command, args []string) { |
| 25 | + RunValidateAndFix(cmd, args) |
| 26 | + }, |
| 27 | + } |
| 28 | +) |
| 29 | + |
| 30 | +func RunValidateAndFix(cmd *cobra.Command, args []string) { |
| 31 | + batchSize := big.NewInt(1000) |
| 32 | + fixBatchSize := 0 // default is no batch size |
| 33 | + if len(args) > 0 { |
| 34 | + batchSizeFromArgs, err := strconv.Atoi(args[0]) |
| 35 | + if err != nil { |
| 36 | + log.Fatal().Err(err).Msg("Failed to parse batch size") |
| 37 | + } |
| 38 | + if batchSizeFromArgs < 1 { |
| 39 | + batchSizeFromArgs = 1 |
| 40 | + } |
| 41 | + batchSize = big.NewInt(int64(batchSizeFromArgs)) |
| 42 | + log.Info().Msgf("Using batch size %d from args", batchSize) |
| 43 | + } |
| 44 | + if len(args) > 1 { |
| 45 | + fixBatchSizeFromArgs, err := strconv.Atoi(args[1]) |
| 46 | + if err != nil { |
| 47 | + log.Fatal().Err(err).Msg("Failed to parse fix batch size") |
| 48 | + } |
| 49 | + fixBatchSize = fixBatchSizeFromArgs |
| 50 | + } |
| 51 | + log.Debug().Msgf("Batch size: %d, fix batch size: %d", batchSize, fixBatchSize) |
| 52 | + batchSize = new(big.Int).Sub(batchSize, big.NewInt(1)) // -1 because range ends are inclusive |
| 53 | + |
| 54 | + rpcClient, err := rpc.Initialize() |
| 55 | + if err != nil { |
| 56 | + log.Fatal().Err(err).Msg("Failed to initialize RPC") |
| 57 | + } |
| 58 | + log.Info().Msgf("Running validationAndFix for chain %d", rpcClient.GetChainID()) |
| 59 | + |
| 60 | + s, err := storage.NewStorageConnector(&config.Cfg.Storage) |
| 61 | + if err != nil { |
| 62 | + log.Fatal().Err(err).Msg("Failed to initialize storage") |
| 63 | + } |
| 64 | + cursor, err := validation.InitCursor(rpcClient.GetChainID(), s) |
| 65 | + if err != nil { |
| 66 | + log.Fatal().Err(err).Msg("Failed to initialize cursor") |
| 67 | + } |
| 68 | + log.Debug().Msgf("Cursor initialized for chain %d, starting from block %d", rpcClient.GetChainID(), cursor.LastScannedBlockNumber) |
| 69 | + |
| 70 | + conn, err := clickhouse.Open(&clickhouse.Options{ |
| 71 | + Addr: []string{fmt.Sprintf("%s:%d", config.Cfg.Storage.Main.Clickhouse.Host, config.Cfg.Storage.Main.Clickhouse.Port)}, |
| 72 | + Protocol: clickhouse.Native, |
| 73 | + TLS: &tls.Config{ |
| 74 | + MinVersion: tls.VersionTLS12, |
| 75 | + }, |
| 76 | + Auth: clickhouse.Auth{ |
| 77 | + Username: config.Cfg.Storage.Main.Clickhouse.Username, |
| 78 | + Password: config.Cfg.Storage.Main.Clickhouse.Password, |
| 79 | + }, |
| 80 | + Settings: func() clickhouse.Settings { |
| 81 | + settings := clickhouse.Settings{ |
| 82 | + "do_not_merge_across_partitions_select_final": "1", |
| 83 | + "use_skip_indexes_if_final": "1", |
| 84 | + "optimize_move_to_prewhere_if_final": "1", |
| 85 | + "async_insert": "1", |
| 86 | + "wait_for_async_insert": "1", |
| 87 | + } |
| 88 | + return settings |
| 89 | + }(), |
| 90 | + }) |
| 91 | + if err != nil { |
| 92 | + log.Fatal().Err(err).Msg("Failed to connect to ClickHouse") |
| 93 | + } |
| 94 | + defer conn.Close() |
| 95 | + |
| 96 | + startBlock := new(big.Int).Add(cursor.LastScannedBlockNumber, big.NewInt(1)) |
| 97 | + |
| 98 | + for startBlock.Cmp(cursor.MaxBlockNumber) <= 0 { |
| 99 | + batchEndBlock := new(big.Int).Add(startBlock, batchSize) |
| 100 | + if batchEndBlock.Cmp(cursor.MaxBlockNumber) > 0 { |
| 101 | + batchEndBlock = new(big.Int).Set(cursor.MaxBlockNumber) |
| 102 | + } |
| 103 | + |
| 104 | + log.Info().Msgf("Validating batch of blocks from %s to %s", startBlock.String(), batchEndBlock.String()) |
| 105 | + err := validateAndFixRange(rpcClient, s, conn, startBlock, batchEndBlock, fixBatchSize) |
| 106 | + if err != nil { |
| 107 | + log.Fatal().Err(err).Msgf("failed to validate and fix range %v-%v", startBlock, batchEndBlock) |
| 108 | + } |
| 109 | + |
| 110 | + startBlock = new(big.Int).Add(batchEndBlock, big.NewInt(1)) |
| 111 | + cursor.Update(batchEndBlock) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Validates a range of blocks (end and start are inclusive) for a given chain and fixes any problems it finds |
| 117 | + */ |
| 118 | +func validateAndFixRange(rpcClient rpc.IRPCClient, s storage.IStorage, conn clickhouse.Conn, startBlock *big.Int, endBlock *big.Int, fixBatchSize int) error { |
| 119 | + validator := orchestrator.NewValidator(rpcClient, s) |
| 120 | + |
| 121 | + chainId := rpcClient.GetChainID() |
| 122 | + err := validation.FindAndRemoveDuplicates(conn, chainId, startBlock, endBlock) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("failed to find and fix duplicates: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + err = validator.FindAndFixGaps(startBlock, endBlock) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("failed to find and fix gaps: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + _, invalidBlocks, err := validator.ValidateBlockRange(startBlock, endBlock) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("failed to validate and fix blocks: %w", err) |
| 135 | + } |
| 136 | + |
| 137 | + invalidBlockNumbers := make([]*big.Int, 0) |
| 138 | + for _, blockData := range invalidBlocks { |
| 139 | + invalidBlockNumbers = append(invalidBlockNumbers, blockData.Block.Number) |
| 140 | + } |
| 141 | + |
| 142 | + if len(invalidBlocks) > 0 { |
| 143 | + err = validator.FixBlocks(invalidBlockNumbers, fixBatchSize) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("failed to fix blocks: %w", err) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + log.Debug().Msgf("ValidationAndFix complete for range %v-%v", startBlock, endBlock) |
| 150 | + return nil |
| 151 | +} |
0 commit comments