Skip to content

Commit c80e7f4

Browse files
shazam8253shantichanalrjl493456442
authored
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 <[email protected]> Co-authored-by: Gary Rong <[email protected]>
1 parent 0b21c4a commit c80e7f4

File tree

2 files changed

+113
-9
lines changed

2 files changed

+113
-9
lines changed

cmd/geth/chaincmd.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -765,15 +765,8 @@ func downloadEra(ctx *cli.Context) error {
765765
}
766766

767767
func parseRange(s string) (start uint64, end uint64, ok bool) {
768-
if m, _ := regexp.MatchString("[0-9]+", s); m {
769-
start, err := strconv.ParseUint(s, 10, 64)
770-
if err != nil {
771-
return 0, 0, false
772-
}
773-
end = start
774-
return start, end, true
775-
}
776-
if m, _ := regexp.MatchString("[0-9]+-[0-9]+", s); m {
768+
log.Info("Parsing block range", "input", s)
769+
if m, _ := regexp.MatchString("^[0-9]+-[0-9]+$", s); m {
777770
s1, s2, _ := strings.Cut(s, "-")
778771
start, err := strconv.ParseUint(s1, 10, 64)
779772
if err != nil {
@@ -783,6 +776,19 @@ func parseRange(s string) (start uint64, end uint64, ok bool) {
783776
if err != nil {
784777
return 0, 0, false
785778
}
779+
if start > end {
780+
return 0, 0, false
781+
}
782+
log.Info("Parsing block range", "start", start, "end", end)
783+
return start, end, true
784+
}
785+
if m, _ := regexp.MatchString("^[0-9]+$", s); m {
786+
start, err := strconv.ParseUint(s, 10, 64)
787+
if err != nil {
788+
return 0, 0, false
789+
}
790+
end = start
791+
log.Info("Parsing single block range", "block", start)
786792
return start, end, true
787793
}
788794
return 0, 0, false

cmd/geth/chaincmd_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import "testing"
20+
21+
func TestParseRange(t *testing.T) {
22+
var cases = []struct {
23+
input string
24+
valid bool
25+
expStart uint64
26+
expEnd uint64
27+
}{
28+
{
29+
input: "0",
30+
valid: true,
31+
expStart: 0,
32+
expEnd: 0,
33+
},
34+
{
35+
input: "500",
36+
valid: true,
37+
expStart: 500,
38+
expEnd: 500,
39+
},
40+
{
41+
input: "-1",
42+
valid: false,
43+
expStart: 0,
44+
expEnd: 0,
45+
},
46+
{
47+
input: "1-1",
48+
valid: true,
49+
expStart: 1,
50+
expEnd: 1,
51+
},
52+
{
53+
input: "0-1",
54+
valid: true,
55+
expStart: 0,
56+
expEnd: 1,
57+
},
58+
{
59+
input: "1-0",
60+
valid: false,
61+
expStart: 0,
62+
expEnd: 0,
63+
},
64+
{
65+
input: "1-1000",
66+
valid: true,
67+
expStart: 1,
68+
expEnd: 1000,
69+
},
70+
{
71+
input: "1-1-",
72+
valid: false,
73+
expStart: 0,
74+
expEnd: 0,
75+
},
76+
{
77+
input: "-1-1",
78+
valid: false,
79+
expStart: 0,
80+
expEnd: 0,
81+
},
82+
}
83+
for _, c := range cases {
84+
start, end, valid := parseRange(c.input)
85+
if valid != c.valid {
86+
t.Errorf("Unexpected result, want: %t, got: %t", c.valid, valid)
87+
continue
88+
}
89+
if valid {
90+
if c.expStart != start {
91+
t.Errorf("Unexpected start, want: %d, got: %d", c.expStart, start)
92+
}
93+
if c.expEnd != end {
94+
t.Errorf("Unexpected end, want: %d, got: %d", c.expEnd, end)
95+
}
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)