Skip to content

Commit

Permalink
Fixed tools check merged-blocks default range and behavior when unb…
Browse files Browse the repository at this point in the history
…ounded range

When `-r <range>` is not provided to now be `[0, +∞]` (was previously `[HEAD, +∞]`).

It's now able to run without a block range provided.

Fixes #28
  • Loading branch information
maoueh committed Mar 20, 2024
1 parent 14c50b0 commit 7cc5841
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <range>` 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]
Expand Down
8 changes: 0 additions & 8 deletions cmd/tools/check/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 19 additions & 3 deletions types/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 22 additions & 1 deletion types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <firstStreamableBlock> 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
}

Expand Down

0 comments on commit 7cc5841

Please sign in to comment.