Skip to content

Commit

Permalink
put back the SanitizeBlockForCompare method, now with pbbstream block
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Feb 16, 2024
1 parent 8ff6297 commit 764b793
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-<chain>` binary.
Expand Down Expand Up @@ -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<chain> tools` group.
// The callback function is called with the `toolsCmd` command that is the root command of the `fire<chain> tools`
// as well as the chain, the root logger and root tracer for tools.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions cmd/tools/compare/tools_compare_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -153,6 +156,7 @@ func runCompareBlocksE[B firecore.Block](chain *firecore.Chain[B]) firecore.Comm
uint64(fileStartBlock),
stopBlock,
&warnAboutExtraBlocks,
sanitizer,
registry,
)
if err != nil {
Expand All @@ -171,6 +175,7 @@ func runCompareBlocksE[B firecore.Block](chain *firecore.Chain[B]) firecore.Comm
uint64(fileStartBlock),
stopBlock,
&warnAboutExtraBlocks,
sanitizer,
registry,
)
if err != nil {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -268,6 +274,10 @@ func readBundle(
continue
}

if sanitizer != nil {
curBlock = sanitizer(curBlock)
}

curBlockPB, err := registry.Unmarshal(curBlock.Payload)

if err != nil {
Expand Down

0 comments on commit 764b793

Please sign in to comment.