You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.2 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package grpc
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "io/ioutil"
  7. "os"
  8. "time"
  9. grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
  10. grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
  11. "github.com/tendermint/tendermint/config"
  12. "github.com/tendermint/tendermint/libs/log"
  13. tmnet "github.com/tendermint/tendermint/libs/net"
  14. grpc "google.golang.org/grpc"
  15. "google.golang.org/grpc/credentials"
  16. "google.golang.org/grpc/credentials/insecure"
  17. "google.golang.org/grpc/keepalive"
  18. )
  19. // DefaultDialOptions constructs a list of grpc dial options
  20. func DefaultDialOptions(
  21. extraOpts ...grpc.DialOption,
  22. ) []grpc.DialOption {
  23. const (
  24. retries = 50 // 50 * 100ms = 5s total
  25. timeout = 1 * time.Second
  26. maxCallRecvMsgSize = 1 << 20 // Default 5Mb
  27. )
  28. var kacp = keepalive.ClientParameters{
  29. Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
  30. Timeout: 2 * time.Second, // wait 2 seconds for ping ack before considering the connection dead
  31. }
  32. opts := []grpc_retry.CallOption{
  33. grpc_retry.WithBackoff(grpc_retry.BackoffExponential(timeout)),
  34. }
  35. dialOpts := []grpc.DialOption{
  36. grpc.WithKeepaliveParams(kacp),
  37. grpc.WithDefaultCallOptions(
  38. grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize),
  39. grpc_retry.WithMax(retries),
  40. ),
  41. grpc.WithUnaryInterceptor(
  42. grpc_retry.UnaryClientInterceptor(opts...),
  43. ),
  44. }
  45. dialOpts = append(dialOpts, extraOpts...)
  46. return dialOpts
  47. }
  48. func GenerateTLS(certPath, keyPath, ca string, log log.Logger) grpc.DialOption {
  49. certificate, err := tls.LoadX509KeyPair(
  50. certPath,
  51. keyPath,
  52. )
  53. if err != nil {
  54. log.Error("error", err)
  55. os.Exit(1)
  56. }
  57. certPool := x509.NewCertPool()
  58. bs, err := ioutil.ReadFile(ca)
  59. if err != nil {
  60. log.Error("failed to read ca cert:", "error", err)
  61. os.Exit(1)
  62. }
  63. ok := certPool.AppendCertsFromPEM(bs)
  64. if !ok {
  65. log.Error("failed to append certs")
  66. os.Exit(1)
  67. }
  68. transportCreds := credentials.NewTLS(&tls.Config{
  69. Certificates: []tls.Certificate{certificate},
  70. RootCAs: certPool,
  71. MinVersion: tls.VersionTLS13,
  72. })
  73. return grpc.WithTransportCredentials(transportCreds)
  74. }
  75. // DialRemoteSigner is a generalized function to dial the gRPC server.
  76. func DialRemoteSigner(
  77. cfg *config.PrivValidatorConfig,
  78. chainID string,
  79. logger log.Logger,
  80. usePrometheus bool,
  81. ) (*SignerClient, error) {
  82. var transportSecurity grpc.DialOption
  83. if cfg.AreSecurityOptionsPresent() {
  84. transportSecurity = GenerateTLS(cfg.ClientCertificateFile(),
  85. cfg.ClientKeyFile(), cfg.RootCAFile(), logger)
  86. } else {
  87. transportSecurity = grpc.WithTransportCredentials(insecure.NewCredentials())
  88. logger.Info("Using an insecure gRPC connection!")
  89. }
  90. dialOptions := DefaultDialOptions()
  91. if usePrometheus {
  92. grpcMetrics := grpc_prometheus.DefaultClientMetrics
  93. dialOptions = append(dialOptions, grpc.WithUnaryInterceptor(grpcMetrics.UnaryClientInterceptor()))
  94. }
  95. dialOptions = append(dialOptions, transportSecurity)
  96. ctx := context.Background()
  97. _, address := tmnet.ProtocolAndAddress(cfg.ListenAddr)
  98. conn, err := grpc.DialContext(ctx, address, dialOptions...)
  99. if err != nil {
  100. logger.Error("unable to connect to server", "target", address, "err", err)
  101. }
  102. return NewSignerClient(conn, chainID, logger)
  103. }