Skip to content

Commit d66891d

Browse files
author
gitopia1gyrcgtfa8lqmytl5svv3y8s439l7xhccgh8q4p
committed
Merge pull request #21 from git-remote-gitopia/release-v1.9.0
2 parents e93b476 + ae13c9d commit d66891d

File tree

16 files changed

+1315
-434
lines changed

16 files changed

+1315
-434
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes will be documented here.
44

5+
## [v1.9.0] - 2024-08-20
6+
7+
- upgrade cosmos-sdk to v0.47.13, gitopia to v4.0.0 and gitopia-go to v0.6.0
8+
- automatic selection of grpc host with the least latency
9+
- set gas adjustment to 1.8
10+
511
## [v1.8.0] - 2024-05-31
612

713
- Support custom configuration for api and other parameters

cmd/git-gitopia/lfs/init.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ 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-
gitopiatypes "github.com/gitopia/gitopia/v2/x/gitopia/types"
16+
"github.com/gitopia/git-remote-gitopia/core/api"
17+
gitopiatypes "github.com/gitopia/gitopia/v4/x/gitopia/types"
1718
"github.com/pkg/errors"
1819
"github.com/spf13/cobra"
1920
"google.golang.org/grpc"
@@ -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: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
12-
"github.com/cosmos/cosmos-sdk/codec"
1312
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
1413
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
1514
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -19,9 +18,10 @@ import (
1918
"github.com/cosmos/cosmos-sdk/x/feegrant"
2019
"github.com/gitopia/git-remote-gitopia/config"
2120
core "github.com/gitopia/git-remote-gitopia/core"
21+
"github.com/gitopia/git-remote-gitopia/core/api"
2222
"github.com/gitopia/git-remote-gitopia/core/wallet"
23-
gitopiatypes "github.com/gitopia/gitopia/v2/x/gitopia/types"
24-
"github.com/gitopia/gitopia/v2/x/gitopia/utils"
23+
gitopiatypes "github.com/gitopia/gitopia/v4/x/gitopia/types"
24+
"github.com/gitopia/gitopia/v4/x/gitopia/utils"
2525
"github.com/go-git/go-git/v5/plumbing"
2626
"github.com/pkg/errors"
2727
"google.golang.org/grpc"
@@ -55,32 +55,39 @@ type GitopiaHandler struct {
5555
func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
5656
var err error
5757

58-
interfaceRegistry := codectypes.NewInterfaceRegistry()
59-
authtypes.RegisterInterfaces(interfaceRegistry)
60-
cryptocodec.RegisterInterfaces(interfaceRegistry)
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 {
67+
return err
68+
}
69+
}
6170

62-
h.grpcConn, err = grpc.Dial(config.GRPCHost,
63-
grpc.WithTransportCredentials(insecure.NewCredentials()),
64-
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(interfaceRegistry).GRPCCodec())),
65-
)
71+
h.grpcConn, err = grpc.Dial(grpcHost, grpc.WithTransportCredentials(insecure.NewCredentials()))
6672
if err != nil {
6773
return err
6874
}
69-
// defer grpcConn.Close()
75+
76+
interfaceRegistry := codectypes.NewInterfaceRegistry()
77+
authtypes.RegisterInterfaces(interfaceRegistry)
78+
cryptocodec.RegisterInterfaces(interfaceRegistry)
7079

7180
h.queryClient = gitopiatypes.NewQueryClient(h.grpcConn)
7281
serviceClient := tmservice.NewServiceClient(h.grpcConn)
7382
h.feegrantClient = feegrant.NewQueryClient(h.grpcConn)
7483
h.bankClient = banktypes.NewQueryClient(h.grpcConn)
7584

76-
// Get chain id for signing transaction
7785
nodeInfoRes, err := serviceClient.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
7886
if err != nil {
7987
return err
8088
}
8189
h.chainId = nodeInfoRes.DefaultNodeInfo.Network
8290

83-
// Get RepositoryId
8491
res, err := h.queryClient.AnyRepository(context.Background(), &gitopiatypes.QueryGetAnyRepositoryRequest{
8592
Id: h.remoteUserId,
8693
RepositoryName: h.remoteRepositoryName,
@@ -336,6 +343,10 @@ func (h *GitopiaHandler) Push(remote *core.Remote, refsToPush []core.RefToPush)
336343
}, deleteTags))
337344
}
338345

346+
if h.wallet.Type() == wallet.LEDGER {
347+
remote.Logger.Println("Please sign the gitopia transaction on your ledger device.")
348+
}
349+
339350
if err := h.wallet.SignAndBroadcast(h.grpcConn, msg); err != nil {
340351
return nil, err
341352
}

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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
package config
44

55
var (
6-
ChainId = "gitopia"
7-
GRPCHost = "localhost:9090"
6+
ChainId = "localgitopia"
87
GitServerHost = "http://localhost:5001"
9-
TmAddr = "http://localhost:26657"
108
GasPrices = "0.001ulore"
11-
FeeGranterAddr = ""
9+
FeeGranterAddr = "gitopia12sjhqc3rqgvu3zpg8ekmwl005rp4ys58ekqg89"
1210
Denom = "ulore"
1311
)

core/api/grpc.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+
"context"
5+
"time"
6+
7+
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
8+
"github.com/gitopia/git-remote-gitopia/core"
9+
"google.golang.org/grpc"
10+
"google.golang.org/grpc/credentials/insecure"
11+
)
12+
13+
func CheckGRPCHostLiveness(host string) bool {
14+
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
15+
if err != nil {
16+
return false
17+
}
18+
defer conn.Close()
19+
20+
client := tmservice.NewServiceClient(conn)
21+
_, err = client.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
22+
return err == nil
23+
}
24+
25+
func checkGRPCHostLatency(host string) time.Duration {
26+
start := time.Now()
27+
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
28+
if err != nil {
29+
return time.Hour
30+
}
31+
defer conn.Close()
32+
33+
client := tmservice.NewServiceClient(conn)
34+
_, err = client.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
35+
if err != nil {
36+
return time.Hour
37+
}
38+
return time.Since(start)
39+
}
40+
41+
func SetConfiguredGRPCHost(host string) error {
42+
cmd := core.GitCommand("git", "config", "--global", "gitopia.grpcHost", host)
43+
return cmd.Run()
44+
}

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:443",
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+
}

0 commit comments

Comments
 (0)