Skip to content

Commit

Permalink
Merge pull request #25 from terraswap/fix/grpc-cert
Browse files Browse the repository at this point in the history
Change Dial to DialContext
  • Loading branch information
jbamlee authored Jul 9, 2024
2 parents 1084cf6 + 5b0aa44 commit 840c456
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions internal/pkg/terraswap/databases/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"net"
"strings"
"time"

Expand Down Expand Up @@ -54,34 +55,42 @@ func New(host, chainId string, insecureCon bool, log configs.LogConfig) Terraswa
}

func connectGRPC(host string, isInsecure bool) *grpc.ClientConn {
var opts []grpc.DialOption

if isInsecure {
cred := insecure.NewCredentials()
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(cred))
opts = append(opts, grpc.WithTransportCredentials(cred))
} else {
dialer := &net.Dialer{
Timeout: 5 * time.Second,
}
// Establish a TLS connection to fetch server certificates with a timeout
conn, err := tls.DialWithDialer(dialer, "tcp", host, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
panic(err.Error())
}
return conn
}

var opts []grpc.DialOption
certs := conn.ConnectionState().PeerCertificates
conn.Close()

conn, err := tls.Dial("tcp", host, &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
panic(err.Error())
}
pool := x509.NewCertPool()
pool.AddCert(certs[0])

certs := conn.ConnectionState().PeerCertificates
conn.Close()
clientCert := credentials.NewClientTLSFromCert(pool, "")
opts = append(opts, grpc.WithTransportCredentials(clientCert))
}

pool := x509.NewCertPool()
pool.AddCert(certs[0])
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

clientCert := credentials.NewClientTLSFromCert(pool, "")
opts = append(opts, grpc.WithTransportCredentials(clientCert))
opts = append(opts, grpc.WithBlock())

rpcConn, _ := grpc.Dial(host, opts...)
rpcConn, err := grpc.DialContext(ctx, host, opts...)
if err != nil {
panic(err.Error())
}

return rpcConn
}
Expand Down Expand Up @@ -162,7 +171,6 @@ func (t *terraswapGrpcCon) GetZeroPoolPairs(pairs []terraswap.Pair) (map[string]
}

func (t *terraswapGrpcCon) getPoolInfo(addr string) (*terraswap.PoolInfo, error) {
time.Sleep(1 * time.Second)
client := wasmtype.NewQueryClient(t.con)
res, err := client.SmartContractState(context.Background(), &wasmtype.QuerySmartContractStateRequest{
Address: addr,
Expand All @@ -186,7 +194,6 @@ func (t *terraswapGrpcCon) getPoolInfo(addr string) (*terraswap.PoolInfo, error)
}

func (t *terraswapGrpcCon) GetTokenInfo(tokenAddress string) (*terraswap.Token, error) {
time.Sleep(1 * time.Second)
client := wasmtype.NewQueryClient(t.con)
res, err := client.SmartContractState(context.Background(), &wasmtype.QuerySmartContractStateRequest{
Address: tokenAddress,
Expand Down

0 comments on commit 840c456

Please sign in to comment.