diff --git a/chain.go b/chain.go index 05ae9ab..8268aa2 100644 --- a/chain.go +++ b/chain.go @@ -21,7 +21,7 @@ import ( // SanitizeBlockForCompareFunc takes a chain agnostic [block] and transforms it in-place, removing fields // that should not be compared. -type SanitizeBlockForCompareFunc[B Block] func(block B) B +type SanitizeBlockForCompareFunc func(block *pbbstream.Block) *pbbstream.Block // Chain is the omni config object for configuring your chain specific information. It contains various // fields that are used everywhere to properly configure the `firehose-` binary. @@ -154,6 +154,12 @@ type Chain[B Block] struct { } type ToolsConfig[B Block] struct { + // SanitizeBlockForCompare is a function that takes a chain agnostic [block] and transforms it in-place, removing fields + // that should not be compared. + // + // The [SanitizeBlockForCompare] is optional, if nil, no-op sanitizer be used. + SanitizeBlockForCompare SanitizeBlockForCompareFunc + // RegisterExtraCmd enables you to register extra commands to the `fire tools` group. // The callback function is called with the `toolsCmd` command that is the root command of the `fire tools` // as well as the chain, the root logger and root tracer for tools. @@ -183,6 +189,15 @@ type ToolsConfig[B Block] struct { MergedBlockUpgrader func(block *pbbstream.Block) (*pbbstream.Block, error) } +// GetSanitizeBlockForCompare returns the [SanitizeBlockForCompare] value if defined, otherwise a no-op sanitizer. +func (t *ToolsConfig[B]) GetSanitizeBlockForCompare() SanitizeBlockForCompareFunc { + if t == nil || t.SanitizeBlockForCompare == nil { + return func(block *pbbstream.Block) *pbbstream.Block { return block } + } + + return t.SanitizeBlockForCompare +} + type TransformFlags struct { // Register is a function that will be called when we need to register the flags for the transforms. // You received the command's flag set and you are responsible of registering the flags. diff --git a/cmd/tools/compare/tools_compare_blocks.go b/cmd/tools/compare/tools_compare_blocks.go index 2211189..063a116 100644 --- a/cmd/tools/compare/tools_compare_blocks.go +++ b/cmd/tools/compare/tools_compare_blocks.go @@ -81,6 +81,7 @@ func NewToolsCompareBlocksCmd[B firecore.Block](chain *firecore.Chain[B]) *cobra } func runCompareBlocksE[B firecore.Block](chain *firecore.Chain[B]) firecore.CommandExecutor { + return func(cmd *cobra.Command, args []string) error { displayDiff := sflags.MustGetBool(cmd, "diff") includeUnknownFields := sflags.MustGetBool(cmd, "include-unknown-fields") @@ -123,6 +124,8 @@ func runCompareBlocksE[B firecore.Block](chain *firecore.Chain[B]) firecore.Comm return fmt.Errorf("creating registry: %w", err) } + sanitizer := chain.Tools.GetSanitizeBlockForCompare() + err = storeReference.Walk(ctx, check.WalkBlockPrefix(blockRange, 100), func(filename string) (err error) { fileStartBlock, err := strconv.Atoi(filename) if err != nil { @@ -153,6 +156,7 @@ func runCompareBlocksE[B firecore.Block](chain *firecore.Chain[B]) firecore.Comm uint64(fileStartBlock), stopBlock, &warnAboutExtraBlocks, + sanitizer, registry, ) if err != nil { @@ -171,6 +175,7 @@ func runCompareBlocksE[B firecore.Block](chain *firecore.Chain[B]) firecore.Comm uint64(fileStartBlock), stopBlock, &warnAboutExtraBlocks, + sanitizer, registry, ) if err != nil { @@ -234,6 +239,7 @@ func readBundle( fileStartBlock, stopBlock uint64, warnAboutExtraBlocks *sync.Once, + sanitizer firecore.SanitizeBlockForCompareFunc, registry *fcproto.Registry, ) ([]string, map[string]*dynamicpb.Message, map[string]uint64, error) { fileReader, err := store.OpenObject(ctx, filename) @@ -268,6 +274,10 @@ func readBundle( continue } + if sanitizer != nil { + curBlock = sanitizer(curBlock) + } + curBlockPB, err := registry.Unmarshal(curBlock.Payload) if err != nil {