internal/era: simplify clauses

This commit is contained in:
Martin Holst Swende 2023-05-29 16:59:38 +02:00 committed by lightclient
parent 235d413c2f
commit 1000755740
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E

View file

@ -53,6 +53,7 @@ func Filename(network string, epoch int, root common.Hash) string {
} }
// ReadDir reads all the era1 files in a directory for a given network. // ReadDir reads all the era1 files in a directory for a given network.
// Format: <network>-<epoch>-<hexroot>.era1
func ReadDir(dir, network string) ([]string, error) { func ReadDir(dir, network string) ([]string, error) {
entries, err := os.ReadDir(dir) entries, err := os.ReadDir(dir)
if err != nil { if err != nil {
@ -63,14 +64,15 @@ func ReadDir(dir, network string) ([]string, error) {
eras []string eras []string
) )
for _, entry := range entries { for _, entry := range entries {
if path.Ext(entry.Name()) == ".era1" { if path.Ext(entry.Name()) != ".era1" {
n := strings.Split(entry.Name(), "-") continue
if len(n) != 3 { }
parts := strings.Split(entry.Name(), "-")
if len(parts) != 3 || parts[0] != network {
// invalid era1 filename, skip // invalid era1 filename, skip
continue continue
} }
if n[0] == network { if epoch, err := strconv.ParseUint(parts[1], 10, 64); err != nil {
if epoch, err := strconv.ParseUint(n[1], 10, 64); err != nil {
return nil, fmt.Errorf("malformed era1 filename: %s", entry.Name()) return nil, fmt.Errorf("malformed era1 filename: %s", entry.Name())
} else if epoch != next { } else if epoch != next {
return nil, fmt.Errorf("missing epoch %d", next) return nil, fmt.Errorf("missing epoch %d", next)
@ -78,8 +80,6 @@ func ReadDir(dir, network string) ([]string, error) {
next += 1 next += 1
eras = append(eras, entry.Name()) eras = append(eras, entry.Name())
} }
}
}
return eras, nil return eras, nil
} }