From c80e7f42806781469412bc4885d74754c08892d1 Mon Sep 17 00:00:00 2001 From: shazam8253 <54690736+shazam8253@users.noreply.github.com> Date: Tue, 24 Jun 2025 09:12:11 +0200 Subject: [PATCH] cmd/geth: era-download logic fix (#32081) Downloading from a range was failing because it would return and error early with an error misinterpreting "start-end". --------- Co-authored-by: shantichanal <158101918+shantichanal@users.noreply.github.com> Co-authored-by: Gary Rong --- cmd/geth/chaincmd.go | 24 ++++++---- cmd/geth/chaincmd_test.go | 98 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 cmd/geth/chaincmd_test.go diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index e0606f0d0b..fafdfe2f07 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -765,15 +765,8 @@ func downloadEra(ctx *cli.Context) error { } func parseRange(s string) (start uint64, end uint64, ok bool) { - if m, _ := regexp.MatchString("[0-9]+", s); m { - start, err := strconv.ParseUint(s, 10, 64) - if err != nil { - return 0, 0, false - } - end = start - return start, end, true - } - if m, _ := regexp.MatchString("[0-9]+-[0-9]+", s); m { + log.Info("Parsing block range", "input", s) + if m, _ := regexp.MatchString("^[0-9]+-[0-9]+$", s); m { s1, s2, _ := strings.Cut(s, "-") start, err := strconv.ParseUint(s1, 10, 64) if err != nil { @@ -783,6 +776,19 @@ func parseRange(s string) (start uint64, end uint64, ok bool) { if err != nil { return 0, 0, false } + if start > end { + return 0, 0, false + } + log.Info("Parsing block range", "start", start, "end", end) + return start, end, true + } + if m, _ := regexp.MatchString("^[0-9]+$", s); m { + start, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return 0, 0, false + } + end = start + log.Info("Parsing single block range", "block", start) return start, end, true } return 0, 0, false diff --git a/cmd/geth/chaincmd_test.go b/cmd/geth/chaincmd_test.go new file mode 100644 index 0000000000..131f5c4501 --- /dev/null +++ b/cmd/geth/chaincmd_test.go @@ -0,0 +1,98 @@ +// Copyright 2025 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import "testing" + +func TestParseRange(t *testing.T) { + var cases = []struct { + input string + valid bool + expStart uint64 + expEnd uint64 + }{ + { + input: "0", + valid: true, + expStart: 0, + expEnd: 0, + }, + { + input: "500", + valid: true, + expStart: 500, + expEnd: 500, + }, + { + input: "-1", + valid: false, + expStart: 0, + expEnd: 0, + }, + { + input: "1-1", + valid: true, + expStart: 1, + expEnd: 1, + }, + { + input: "0-1", + valid: true, + expStart: 0, + expEnd: 1, + }, + { + input: "1-0", + valid: false, + expStart: 0, + expEnd: 0, + }, + { + input: "1-1000", + valid: true, + expStart: 1, + expEnd: 1000, + }, + { + input: "1-1-", + valid: false, + expStart: 0, + expEnd: 0, + }, + { + input: "-1-1", + valid: false, + expStart: 0, + expEnd: 0, + }, + } + for _, c := range cases { + start, end, valid := parseRange(c.input) + if valid != c.valid { + t.Errorf("Unexpected result, want: %t, got: %t", c.valid, valid) + continue + } + if valid { + if c.expStart != start { + t.Errorf("Unexpected start, want: %d, got: %d", c.expStart, start) + } + if c.expEnd != end { + t.Errorf("Unexpected end, want: %d, got: %d", c.expEnd, end) + } + } + } +}