-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
iuwqyir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.