cmd/geth: fix era downloader

This commit is contained in:
Gary Rong 2025-06-24 14:21:36 +08:00
parent b62c0c67fa
commit dc18c0be54
2 changed files with 109 additions and 12 deletions

View file

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"slices" "slices"
"strconv" "strconv"
@ -765,25 +764,25 @@ func downloadEra(ctx *cli.Context) error {
} }
func parseRange(s string) (start uint64, end uint64, ok bool) { func parseRange(s string) (start uint64, end uint64, ok bool) {
if m, _ := regexp.MatchString("[0-9]+", s); m { if strings.Contains(s, "-") {
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 {
s1, s2, _ := strings.Cut(s, "-") s1, s2, _ := strings.Cut(s, "-")
start, err := strconv.ParseUint(s1, 10, 64) start, err := strconv.ParseUint(s1, 10, 64)
if err != nil { if err != nil {
return 0, 0, false return 0, 0, false
} }
end, err = strconv.ParseUint(s2, 10, 64) end, err := strconv.ParseUint(s2, 10, 64)
if err != nil { if err != nil {
return 0, 0, false return 0, 0, false
} }
if start > end {
return 0, 0, false
}
return start, end, true return start, end, true
} }
return 0, 0, false start, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, 0, false
}
end = start
return start, end, true
} }

98
cmd/geth/chaincmd_test.go Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
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)
}
}
}
}