From 7cc5841248fb996955403c4f6ce04e54ca21b77a Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Wed, 20 Mar 2024 10:58:51 -0400 Subject: [PATCH] Fixed `tools check merged-blocks` default range and behavior when unbounded range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `-r ` is not provided to now be `[0, +∞]` (was previously `[HEAD, +∞]`). It's now able to run without a block range provided. Fixes #28 --- CHANGELOG.md | 4 ++++ cmd/tools/check/blocks.go | 8 -------- cmd/tools/check/check.go | 2 +- types/flags.go | 22 +++++++++++++++++++--- types/utils.go | 23 ++++++++++++++++++++++- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a85cee0..2255763 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ If you were at `firehose-core` version `1.0.0` and are bumping to `1.1.0`, you s ## Unreleased +* Fixed `tools check merged-blocks` default range when `-r ` is not provided to now be `[0, +∞]` (was previously `[HEAD, +∞]`). + +* Fixed `tools check merged-blocks` to be able to run without a block range provided. + * Added API Key authentication to `client.NewFirehoseFetchClient` and `client.NewFirehoseClient`. > [!NOTE] diff --git a/cmd/tools/check/blocks.go b/cmd/tools/check/blocks.go index 0aa4f62..33904eb 100644 --- a/cmd/tools/check/blocks.go +++ b/cmd/tools/check/blocks.go @@ -41,14 +41,6 @@ func CheckMergedBlocks[B firecore.Block](ctx context.Context, chain *firecore.Ch var highestBlockSeen uint64 lowestBlockSeen := firecore.MaxUint64 - if !blockRange.IsResolved() { - return fmt.Errorf("check merged blocks can only work with fully resolved range, got %s", blockRange) - } - - // if blockRange.Start < bstream.GetProtocolFirstStreamableBlock { - // blockRange.Start = bstream.GetProtocolFirstStreamableBlock - // } - holeFound := false expected = types.RoundToBundleStartBlock(uint64(blockRange.Start), fileBlockSize) currentStartBlk := uint64(blockRange.Start) diff --git a/cmd/tools/check/check.go b/cmd/tools/check/check.go index aebbf1f..13d893d 100644 --- a/cmd/tools/check/check.go +++ b/cmd/tools/check/check.go @@ -83,7 +83,7 @@ func createToolsCheckMergedBlocksE[B firecore.Block](chain *firecore.Chain[B], r storeURL := args[0] fileBlockSize := uint64(100) - blockRange, err := types.GetBlockRangeFromFlag(cmd, "range") + blockRange, err := types.GetBlockRangeFromFlagDefault(cmd, "range", types.NewOpenRange(0)) if err != nil { return err } diff --git a/types/flags.go b/types/flags.go index 06b51b6..53fd793 100644 --- a/types/flags.go +++ b/types/flags.go @@ -9,23 +9,39 @@ import ( "github.com/streamingfast/cli/sflags" ) +// GetBlockRangeFromArg returns the block range from the given argument or the range +// [HEAD, +∞] if the argument is empty. func GetBlockRangeFromArg(in string) (out BlockRange, err error) { - return ParseBlockRange(in, bstream.GetProtocolFirstStreamableBlock) + return ParseBlockRangeDefault(in, bstream.GetProtocolFirstStreamableBlock, NewOpenRange(-1)) } +// GetBlockRangeFromArgDefault returns a block range from a string argument, using the default block range +// `defaultRange` if the input is empty. +func GetBlockRangeFromArgDefault(in string, defaultRange BlockRange) (out BlockRange, err error) { + return ParseBlockRangeDefault(in, bstream.GetProtocolFirstStreamableBlock, defaultRange) +} + +// GetBlockRangeFromFlag returns the block range from the given flag name or the range +// [HEAD, +∞] if the flag is not set. func GetBlockRangeFromFlag(cmd *cobra.Command, flagName string) (out BlockRange, err error) { + return GetBlockRangeFromFlagDefault(cmd, flagName, NewOpenRange(-1)) +} + +// GetBlockRangeFromFlagDefault returns a block range from a flag, using the default block range +// `defaultRange` if the flag is not set at all. +func GetBlockRangeFromFlagDefault(cmd *cobra.Command, flagName string, defaultRange BlockRange) (out BlockRange, err error) { stringRange := sflags.MustGetString(cmd, flagName) rawRanges := strings.Split(stringRange, ",") if len(rawRanges) == 0 { - return + return defaultRange, nil } if len(rawRanges) > 1 { return out, fmt.Errorf("accepting a single range for now, got %d", len(rawRanges)) } - out, err = ParseBlockRange(rawRanges[0], bstream.GetProtocolFirstStreamableBlock) + out, err = ParseBlockRangeDefault(rawRanges[0], bstream.GetProtocolFirstStreamableBlock, defaultRange) if err != nil { return out, fmt.Errorf("decode range: %w", err) } diff --git a/types/utils.go b/types/utils.go index c241b08..48ce0cf 100644 --- a/types/utils.go +++ b/types/utils.go @@ -26,8 +26,29 @@ func PrettyBlockNum(b uint64) string { return "#" + strings.ReplaceAll(humanize.Comma(int64(b)), ",", " ") } +// Deprecated: use ParseBlockRangeDefault instead and provide the default range when the input is +// empty. func ParseBlockRange(input string, firstStreamableBlock uint64) (out BlockRange, err error) { - if input == "" || input == "-1" { + return ParseBlockRangeDefault(input, firstStreamableBlock, NewOpenRange(-1)) +} + +// ParseBlockRangeDefault parses a block range from a string, using the default block range +// `defaultRange` if the input is empty. The input "-1" is interpreted as an open range from [HEAD, +∞]. +// +// The accepted inputs are: +// - ":" (an open range from 0 to +∞) +// - "-1" (an open range from HEAD to +∞, equivalent to "-1:") +// - "123" (a single block leading to a closed range from 123 to 123) +// - "123:456" (a range of blocks) +// - "123:" (a range of blocks from 123 to +∞) +// - ":456" (a range of blocks from to 456) +// - "-1:456" (a range of blocks from HEAD to +∞ (assuming HEAD is before 456)) +func ParseBlockRangeDefault(input string, firstStreamableBlock uint64, defaultRange BlockRange) (out BlockRange, err error) { + if input == "" { + return defaultRange, nil + } + + if input == "-1" { return NewOpenRange(-1), nil }