Skip to content

Commit c6d6cf0

Browse files
committed
choose best grpc and rpc provider on first load and do the liveness test for configured provider
1 parent 1605730 commit c6d6cf0

File tree

10 files changed

+138
-72
lines changed

10 files changed

+138
-72
lines changed

cmd/git-gitopia/lfs/init.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
1414
"github.com/gitopia/git-remote-gitopia/config"
1515
"github.com/gitopia/git-remote-gitopia/core"
16+
"github.com/gitopia/git-remote-gitopia/core/api"
1617
gitopiatypes "github.com/gitopia/gitopia/v4/x/gitopia/types"
1718
"github.com/pkg/errors"
1819
"github.com/spf13/cobra"
@@ -51,7 +52,15 @@ func InitCommand() *cobra.Command {
5152

5253
interfaceRegistry := codectypes.NewInterfaceRegistry()
5354

54-
grpcConn, err := grpc.Dial(config.GRPCHost,
55+
grpcHost, _ := config.GitConfigGet(config.GitopiaConfigGRPCHostOption)
56+
if grpcHost == "" || !api.CheckGRPCHostLiveness(grpcHost) {
57+
provider := api.GetBestApiProvider()
58+
if err := api.SetConfiguredGRPCHost(provider.GRPCHost); err != nil {
59+
return err
60+
}
61+
}
62+
63+
grpcConn, err := grpc.Dial(grpcHost,
5564
grpc.WithTransportCredentials(insecure.NewCredentials()),
5665
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(interfaceRegistry).GRPCCodec())),
5766
)

cmd/git-remote-gitopia/gitopia.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ type GitopiaHandler struct {
5555
func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
5656
var err error
5757

58-
grpcHost := api.GetConfiguredGRPCHost()
59-
if grpcHost == "" || !api.CheckLiveness(grpcHost) {
60-
grpcHost = api.GetBestGRPCHost()
61-
if err := api.SetConfiguredGRPCHost(grpcHost); err != nil {
58+
grpcHost, _ := config.GitConfigGet(config.GitopiaConfigGRPCHostOption)
59+
tmAddr, _ := config.GitConfigGet(config.GitopiaConfigTmAddrOption)
60+
if grpcHost == "" || tmAddr == "" || !api.CheckGRPCHostLiveness(grpcHost) || !api.CheckRPCHostLiveness(tmAddr) {
61+
provider := api.GetBestApiProvider()
62+
grpcHost = provider.GRPCHost
63+
if err := api.SetConfiguredGRPCHost(provider.GRPCHost); err != nil {
64+
return err
65+
}
66+
if err := api.SetConfiguredTmAddr(provider.TMAddr); err != nil {
6267
return err
6368
}
6469
}

config/config.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ import (
77
)
88

99
const (
10-
AppName = "git-remote-gitopia"
11-
gitopiaConfigSection = "gitopia"
10+
AppName = "git-remote-gitopia"
11+
GitopiaConfigSection = "gitopia"
12+
GitopiaConfigChainIdOption = "chainId"
13+
GitopiaConfigGRPCHostOption = "grpcHost"
14+
GitopiaConfigTmAddrOption = "tmAddr"
15+
GitopiaConfigGitServerHostOption = "gitServerHost"
16+
GitopiaConfigKeyOption = "key"
17+
GitopiaConfigBackendOption = "backend"
18+
GitopiaConfigGasPricesOption = "gasPrices"
19+
GitopiaConfigFeeGranterOption = "feeGranter"
20+
GitopiaConfigDenomOption = "denom"
1221
)
1322

14-
func gitConfigGet(key string) (string, error) {
23+
func GitConfigGet(key string) (string, error) {
1524
cmd := exec.Command("git", "config", "--get", fmt.Sprintf("gitopia.%s", key))
1625
stdout, err := cmd.Output()
1726

@@ -24,25 +33,19 @@ func gitConfigGet(key string) (string, error) {
2433
}
2534

2635
func LoadGitConfig() error {
27-
if res, err := gitConfigGet("chainId"); err == nil {
36+
if res, err := GitConfigGet(GitopiaConfigChainIdOption); err == nil {
2837
ChainId = res
2938
}
30-
if res, err := gitConfigGet("grpcHost"); err == nil {
31-
GRPCHost = res
32-
}
33-
if res, err := gitConfigGet("gitServerHost"); err == nil {
39+
if res, err := GitConfigGet(GitopiaConfigGitServerHostOption); err == nil {
3440
GitServerHost = res
3541
}
36-
if res, err := gitConfigGet("tmAddr"); err == nil {
37-
TmAddr = res
38-
}
39-
if res, err := gitConfigGet("gasPrices"); err == nil {
42+
if res, err := GitConfigGet(GitopiaConfigGasPricesOption); err == nil {
4043
GasPrices = res
4144
}
42-
if res, err := gitConfigGet("feeGranter"); err == nil {
45+
if res, err := GitConfigGet(GitopiaConfigFeeGranterOption); err == nil {
4346
FeeGranterAddr = res
4447
}
45-
if res, err := gitConfigGet("denom"); err == nil {
48+
if res, err := GitConfigGet(GitopiaConfigDenomOption); err == nil {
4649
Denom = res
4750
}
4851
return nil

config/config_dev.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ package config
44

55
var (
66
ChainId = "gitopia"
7-
GRPCHost = "grpc.devnet.gitopia.com:9090"
87
GitServerHost = "https://server.devnet.gitopia.com"
9-
TmAddr = "https://rpc.devnet.gitopia.com:443"
108
GasPrices = "0.001ulore"
119
FeeGranterAddr = ""
1210
Denom = "ulore"

config/config_prod.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ package config
44

55
var (
66
ChainId = "gitopia"
7-
GRPCHost = "grpc.gitopia.com:9090"
87
GitServerHost = "https://server.gitopia.com"
9-
TmAddr = "https://rpc.gitopia.com:443"
108
GasPrices = "0.001ulore"
119
FeeGranterAddr = "gitopia13ashgc6j5xle4m47kqyn5psavq0u3klmscfxql"
1210
Denom = "ulore"

config/config_testing.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ package config
44

55
var (
66
ChainId = "localgitopia"
7-
GRPCHost = "localhost:9090"
87
GitServerHost = "http://localhost:5001"
9-
TmAddr = "http://localhost:26657"
108
GasPrices = "0.001ulore"
119
FeeGranterAddr = "gitopia12sjhqc3rqgvu3zpg8ekmwl005rp4ys58ekqg89"
1210
Denom = "ulore"

core/api/grpc.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package api
22

33
import (
44
"context"
5-
"os/exec"
6-
"strings"
75
"time"
86

97
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
@@ -12,13 +10,7 @@ import (
1210
"google.golang.org/grpc/credentials/insecure"
1311
)
1412

15-
func getGRPCHosts() []string {
16-
return []string{
17-
"gitopia-grpc.polkachu.com:11390",
18-
}
19-
}
20-
21-
func CheckLiveness(host string) bool {
13+
func CheckGRPCHostLiveness(host string) bool {
2214
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
2315
if err != nil {
2416
return false
@@ -30,7 +22,7 @@ func CheckLiveness(host string) bool {
3022
return err == nil
3123
}
3224

33-
func checkLatency(host string) time.Duration {
25+
func checkGRPCHostLatency(host string) time.Duration {
3426
start := time.Now()
3527
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
3628
if err != nil {
@@ -46,30 +38,6 @@ func checkLatency(host string) time.Duration {
4638
return time.Since(start)
4739
}
4840

49-
func GetBestGRPCHost() string {
50-
hosts := getGRPCHosts()
51-
bestHost := hosts[0]
52-
bestLatency := time.Hour
53-
54-
for _, host := range hosts {
55-
latency := checkLatency(host)
56-
if latency < bestLatency {
57-
bestHost = host
58-
bestLatency = latency
59-
}
60-
}
61-
return bestHost
62-
}
63-
64-
func GetConfiguredGRPCHost() string {
65-
cmd := exec.Command("git", "config", "--get", "gitopia.grpcHost")
66-
out, err := cmd.Output()
67-
if err != nil {
68-
return ""
69-
}
70-
return strings.TrimSpace(string(out))
71-
}
72-
7341
func SetConfiguredGRPCHost(host string) error {
7442
cmd := core.GitCommand("git", "config", "--global", "gitopia.grpcHost", host)
7543
return cmd.Run()

core/api/providers.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package api
2+
3+
import (
4+
"time"
5+
)
6+
7+
type ProviderConfig struct {
8+
GRPCHost string
9+
TMAddr string
10+
}
11+
12+
var providers = []ProviderConfig{
13+
{
14+
GRPCHost: "gitopia.declab.pro:9017",
15+
TMAddr: "https://gitopia.declab.pro:26625",
16+
},
17+
{
18+
GRPCHost: "gitopia-grpc.polkachu.com:11390",
19+
TMAddr: "https://gitopia-rpc.polkachu.com:443",
20+
},
21+
{
22+
GRPCHost: "gitopia.grpc.m.stavr.tech:5123",
23+
TMAddr: "http://gitopia.rpc.m.stavr.tech:51057",
24+
},
25+
{
26+
GRPCHost: "gitopia-rpc.stakeangle.com:41390",
27+
TMAddr: "https://gitopia-rpc.stakeangle.com",
28+
},
29+
}
30+
31+
func GetBestApiProvider() ProviderConfig {
32+
bestHost := providers[0]
33+
bestLatency := time.Hour
34+
35+
for _, p := range providers {
36+
latency := checkGRPCHostLatency(p.GRPCHost)
37+
if latency < bestLatency && CheckRPCHostLiveness(p.TMAddr) {
38+
bestHost = p
39+
bestLatency = latency
40+
}
41+
}
42+
43+
return bestHost
44+
}

core/api/rpc.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
clienthttp "github.com/cometbft/cometbft/rpc/client/http"
8+
jsonrpcclient "github.com/cometbft/cometbft/rpc/jsonrpc/client"
9+
"github.com/gitopia/git-remote-gitopia/core"
10+
)
11+
12+
func CheckRPCHostLiveness(host string) bool {
13+
httpClient, err := jsonrpcclient.DefaultHTTPClient(host)
14+
if err != nil {
15+
return false
16+
}
17+
httpClient.Timeout = time.Duration(5) * time.Second
18+
c, err := clienthttp.NewWithClient(host, "/websocket", httpClient)
19+
if err != nil {
20+
return false
21+
}
22+
23+
res, err := c.Status(context.Background())
24+
if err != nil {
25+
return false
26+
}
27+
if res.SyncInfo.CatchingUp {
28+
return false
29+
}
30+
31+
return true
32+
}
33+
34+
func SetConfiguredTmAddr(addr string) error {
35+
cmd := core.GitCommand("git", "config", "--global", "gitopia.tmAddr", addr)
36+
return cmd.Run()
37+
}

core/wallet/os_keyring.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ import (
2828
)
2929

3030
const (
31-
AppName = "git-remote-gitopia"
32-
AccountAddressPrefix = "gitopia"
33-
gitopiaConfigSection = "gitopia"
34-
gitopiaConfigKeyOption = "key"
35-
gitopiaConfigBackendOption = "backend"
31+
AppName = "git-remote-gitopia"
32+
AccountAddressPrefix = "gitopia"
3633
)
3734

3835
var (
@@ -68,28 +65,37 @@ func InitOSKeyringWallet(bankClient banktypes.QueryClient, feegrantClient feegra
6865
if err != nil {
6966
return nil, errors.Wrap(err, "error loading git config")
7067
}
71-
if conf.Raw.HasSection(gitopiaConfigSection) &&
72-
conf.Raw.Section(gitopiaConfigSection).HasOption(gitopiaConfigKeyOption) {
73-
key = conf.Raw.Section(gitopiaConfigSection).Option(gitopiaConfigKeyOption)
68+
if conf.Raw.HasSection(config.GitopiaConfigSection) &&
69+
conf.Raw.Section(config.GitopiaConfigSection).HasOption(config.GitopiaConfigKeyOption) {
70+
key = conf.Raw.Section(config.GitopiaConfigSection).Option(config.GitopiaConfigKeyOption)
7471
} else {
7572
return nil, ErrGitopiaKeyNotConfigured
7673
}
7774

78-
if conf.Raw.HasSection(gitopiaConfigSection) &&
79-
conf.Raw.Section(gitopiaConfigSection).HasOption(gitopiaConfigBackendOption) {
80-
backend = conf.Raw.Section(gitopiaConfigSection).Option(gitopiaConfigBackendOption)
75+
if conf.Raw.HasSection(config.GitopiaConfigSection) &&
76+
conf.Raw.Section(config.GitopiaConfigSection).HasOption(config.GitopiaConfigBackendOption) {
77+
backend = conf.Raw.Section(config.GitopiaConfigSection).Option(config.GitopiaConfigBackendOption)
8178
} else {
8279
backend = keyring.BackendOS // default to OS. same as cosmos keys subcommand
8380
}
8481

82+
grpcHost, err := config.GitConfigGet(config.GitopiaConfigGRPCHostOption)
83+
if err != nil {
84+
return nil, err
85+
}
86+
tmAddr, err := config.GitConfigGet(config.GitopiaConfigTmAddrOption)
87+
if err != nil {
88+
return nil, err
89+
}
90+
8591
l := logrus.New()
8692
l.SetOutput(os.Stderr)
8793
ctx := logger.ContextWithValue(context.Background(), l)
8894
glib.WithAppName(AppName)
8995
glib.WithGasPrices(config.GasPrices)
90-
glib.WithGitopiaAddr(config.GRPCHost)
96+
glib.WithGitopiaAddr(grpcHost)
9197
glib.WithChainId(config.ChainId)
92-
glib.WithTmAddr(config.TmAddr)
98+
glib.WithTmAddr(tmAddr)
9399

94100
cc, err := glib.GetClientContextWithOptions(AppName, backend, key)
95101
if err != nil {

0 commit comments

Comments
 (0)