Skip to content

chain validation and fix command #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func init() {
viper.BindPFlag("workMode.liveModeThreshold", rootCmd.PersistentFlags().Lookup("workMode-liveModeThreshold"))
rootCmd.AddCommand(orchestratorCmd)
rootCmd.AddCommand(apiCmd)
rootCmd.AddCommand(validateAndFixCmd)
rootCmd.AddCommand(validateCmd)
}

func initConfig() {
Expand Down
76 changes: 76 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"math/big"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
config "github.com/thirdweb-dev/indexer/configs"
"github.com/thirdweb-dev/indexer/internal/orchestrator"
"github.com/thirdweb-dev/indexer/internal/rpc"
"github.com/thirdweb-dev/indexer/internal/storage"
)

var (
validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate blockchain data integrity",
Long: "Validate a range of blocks for data integrity issues including transaction roots and logs bloom verification",
Run: func(cmd *cobra.Command, args []string) {
RunValidate(cmd, args)
},
}
)

/**
* Validates a range of blocks (end and start are inclusive) for a given chain
* First argument is the start block number
* Second argument (optional) is the end block number
*/
func RunValidate(cmd *cobra.Command, args []string) {
if len(args) < 1 {
log.Fatal().Msg("Start block number is required")
}
startBlock, success := new(big.Int).SetString(args[0], 10)
if !success {
log.Fatal().Msg("Failed to parse start block number")
}

var endBlock *big.Int
if len(args) > 1 {
endBlock, success = new(big.Int).SetString(args[1], 10)
if !success {
log.Fatal().Msg("Failed to parse end block number")
}
}
if endBlock == nil {
endBlock = startBlock
}

rpcClient, err := rpc.Initialize()
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize RPC")
}
log.Info().Msgf("Running validation for chain %d", rpcClient.GetChainID())

s, err := storage.NewStorageConnector(&config.Cfg.Storage)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize storage")
}

validator := orchestrator.NewValidator(rpcClient, s)

_, invalidBlocks, err := validator.ValidateBlockRange(startBlock, endBlock)
if err != nil {
log.Fatal().Err(err).Msg("Failed to validate blocks")
}

if len(invalidBlocks) > 0 {
log.Info().Msgf("Found %d invalid blocks", len(invalidBlocks))
for _, block := range invalidBlocks {
log.Info().Msgf("Invalid block: %s", block.Block.Number)
}
} else {
log.Info().Msg("No invalid blocks found")
}
}
151 changes: 151 additions & 0 deletions cmd/validate_and_fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package cmd

import (
"crypto/tls"
"fmt"
"math/big"
"strconv"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
config "github.com/thirdweb-dev/indexer/configs"
"github.com/thirdweb-dev/indexer/internal/orchestrator"
"github.com/thirdweb-dev/indexer/internal/rpc"
"github.com/thirdweb-dev/indexer/internal/storage"
"github.com/thirdweb-dev/indexer/internal/validation"
)

var (
validateAndFixCmd = &cobra.Command{
Use: "validateAndFix",
Short: "Validate and fix blockchain data",
Long: "Validate blockchain data in batches and automatically fix any issues found including duplicates, gaps, and invalid blocks",
Run: func(cmd *cobra.Command, args []string) {
RunValidateAndFix(cmd, args)
},
}
)

func RunValidateAndFix(cmd *cobra.Command, args []string) {
batchSize := big.NewInt(1000)
fixBatchSize := 0 // default is no batch size
if len(args) > 0 {
batchSizeFromArgs, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse batch size")
}
if batchSizeFromArgs < 1 {
batchSizeFromArgs = 1
}
batchSize = big.NewInt(int64(batchSizeFromArgs))
log.Info().Msgf("Using batch size %d from args", batchSize)
}
if len(args) > 1 {
fixBatchSizeFromArgs, err := strconv.Atoi(args[1])
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse fix batch size")
}
fixBatchSize = fixBatchSizeFromArgs
}
log.Debug().Msgf("Batch size: %d, fix batch size: %d", batchSize, fixBatchSize)
batchSize = new(big.Int).Sub(batchSize, big.NewInt(1)) // -1 because range ends are inclusive

rpcClient, err := rpc.Initialize()
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize RPC")
}
log.Info().Msgf("Running validationAndFix for chain %d", rpcClient.GetChainID())

s, err := storage.NewStorageConnector(&config.Cfg.Storage)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize storage")
}
cursor, err := validation.InitCursor(rpcClient.GetChainID(), s)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize cursor")
}
log.Debug().Msgf("Cursor initialized for chain %d, starting from block %d", rpcClient.GetChainID(), cursor.LastScannedBlockNumber)

conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", config.Cfg.Storage.Main.Clickhouse.Host, config.Cfg.Storage.Main.Clickhouse.Port)},
Protocol: clickhouse.Native,
TLS: &tls.Config{
MinVersion: tls.VersionTLS12,
},
Auth: clickhouse.Auth{
Username: config.Cfg.Storage.Main.Clickhouse.Username,
Password: config.Cfg.Storage.Main.Clickhouse.Password,
},
Settings: func() clickhouse.Settings {
settings := clickhouse.Settings{
"do_not_merge_across_partitions_select_final": "1",
"use_skip_indexes_if_final": "1",
"optimize_move_to_prewhere_if_final": "1",
"async_insert": "1",
"wait_for_async_insert": "1",
}
return settings
}(),
})
if err != nil {
log.Fatal().Err(err).Msg("Failed to connect to ClickHouse")
}
defer conn.Close()

startBlock := new(big.Int).Add(cursor.LastScannedBlockNumber, big.NewInt(1))

for startBlock.Cmp(cursor.MaxBlockNumber) <= 0 {
batchEndBlock := new(big.Int).Add(startBlock, batchSize)
if batchEndBlock.Cmp(cursor.MaxBlockNumber) > 0 {
batchEndBlock = new(big.Int).Set(cursor.MaxBlockNumber)
}

log.Info().Msgf("Validating batch of blocks from %s to %s", startBlock.String(), batchEndBlock.String())
err := validateAndFixRange(rpcClient, s, conn, startBlock, batchEndBlock, fixBatchSize)
if err != nil {
log.Fatal().Err(err).Msgf("failed to validate and fix range %v-%v", startBlock, batchEndBlock)
}

startBlock = new(big.Int).Add(batchEndBlock, big.NewInt(1))
cursor.Update(batchEndBlock)
}
}

/**
* Validates a range of blocks (end and start are inclusive) for a given chain and fixes any problems it finds
*/
func validateAndFixRange(rpcClient rpc.IRPCClient, s storage.IStorage, conn clickhouse.Conn, startBlock *big.Int, endBlock *big.Int, fixBatchSize int) error {
validator := orchestrator.NewValidator(rpcClient, s)

chainId := rpcClient.GetChainID()
err := validation.FindAndRemoveDuplicates(conn, chainId, startBlock, endBlock)
if err != nil {
return fmt.Errorf("failed to find and fix duplicates: %w", err)
}

err = validator.FindAndFixGaps(startBlock, endBlock)
if err != nil {
return fmt.Errorf("failed to find and fix gaps: %w", err)
}

_, invalidBlocks, err := validator.ValidateBlockRange(startBlock, endBlock)
if err != nil {
return fmt.Errorf("failed to validate and fix blocks: %w", err)
}

invalidBlockNumbers := make([]*big.Int, 0)
for _, blockData := range invalidBlocks {
invalidBlockNumbers = append(invalidBlockNumbers, blockData.Block.Number)
}

if len(invalidBlocks) > 0 {
err = validator.FixBlocks(invalidBlockNumbers, fixBatchSize)
if err != nil {
return fmt.Errorf("failed to fix blocks: %w", err)
}
}

log.Debug().Msgf("ValidationAndFix complete for range %v-%v", startBlock, endBlock)
return nil
}
27 changes: 15 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ go 1.23.0

require (
github.com/ClickHouse/clickhouse-go/v2 v2.36.0
github.com/ethereum/go-ethereum v1.14.8
github.com/ethereum/go-ethereum v1.15.11
github.com/gin-gonic/gin v1.10.0
github.com/gorilla/schema v1.4.1
github.com/holiman/uint256 v1.3.2
github.com/prometheus/client_golang v1.20.4
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
Expand All @@ -24,21 +25,21 @@ require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/bits-and-blooms/bitset v1.20.0 // indirect
github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/consensys/bavard v0.1.27 // indirect
github.com/consensys/gnark-crypto v0.16.0 // indirect
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
github.com/ethereum/go-verkle v0.2.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -53,11 +54,11 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -68,11 +69,13 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
Expand All @@ -81,6 +84,7 @@ require (
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
Expand All @@ -92,13 +96,12 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/supranational/blst v0.3.14 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/twmb/franz-go/pkg/kmsg v1.9.0 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/urfave/cli/v2 v2.27.4 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/otel v1.36.0 // indirect
go.opentelemetry.io/otel/trace v1.36.0 // indirect
Expand Down
Loading