Skip to content

Commit

Permalink
use region instead of country
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jun 23, 2024
1 parent 7cba9c9 commit 1b7f4b5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (

require (
github.com/Masterminds/semver/v3 v3.2.0
github.com/biter777/countries v1.7.5
github.com/btcsuite/btcd/btcec/v2 v2.2.1
github.com/btcsuite/btcd/btcutil v1.1.2
github.com/cometbft/cometbft-db v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/biter777/countries v1.7.5 h1:MJ+n3+rSxWQdqVJU8eBy9RqcdH6ePPn4PJHocVWUa+Q=
github.com/biter777/countries v1.7.5/go.mod h1:1HSpZ526mYqKJcpT5Ti1kcGQ0L0SrXWIaptUWjFfv2E=
github.com/bkielbasa/cyclop v1.2.0 h1:7Jmnh0yL2DjKfw28p86YTd/B4lRGcNuu12sKE35sM7A=
github.com/bkielbasa/cyclop v1.2.0/go.mod h1:qOI0yy6A7dYC4Zgsa72Ppm9kONl0RoIlPbzot9mhmeI=
github.com/blizzy78/varnamelen v0.8.0 h1:oqSblyuQvFsW1hbBHh1zfwrKe3kcSj0rnXkKzsQ089M=
Expand Down
17 changes: 14 additions & 3 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"time"

"github.com/biter777/countries"

Check failure on line 11 in p2p/switch.go

View workflow job for this annotation

GitHub Actions / golangci-lint

import 'github.com/biter777/countries' is not allowed from list 'main' (depguard)
"github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/cmap"
"github.com/cometbft/cometbft/libs/rand"
Expand Down Expand Up @@ -178,7 +179,12 @@ func getOwnRegion() (string, error) {
return "", fmt.Errorf("failed to get own region")
}

return ipInfo.Country, nil
country := countries.ByName(ipInfo.Country)
if country == countries.Unknown {
return "", fmt.Errorf("could not find country: %s", ipInfo.Country)
}

return country.Info().Region.String(), nil
}

// SwitchFilterTimeout sets the timeout used for peer filters.
Expand Down Expand Up @@ -890,10 +896,15 @@ func getRegionFromIP(ip string) (string, error) {
fmt.Println("ipInfoPeer", ipInfo)

if ipInfo.Status != "success" {
return "", fmt.Errorf("failed to get region from IP %s", ip)
return "", fmt.Errorf("failed to get country from IP %s", ip)
}

country := countries.ByName(ipInfo.Country)
if country == countries.Unknown {
return "", fmt.Errorf("could not find country: %s", ipInfo.Country)
}

return ipInfo.Country, nil
return country.Info().Region.String(), nil
}

func (sw *Switch) filterPeer(p Peer) error {
Expand Down

0 comments on commit 1b7f4b5

Please sign in to comment.