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.

120 lines
3.1 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. "os"
  7. "time"
  8. grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
  9. grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
  10. "github.com/tendermint/tendermint/config"
  11. "github.com/tendermint/tendermint/libs/log"
  12. tmnet "github.com/tendermint/tendermint/libs/net"
  13. "google.golang.org/grpc"
  14. "google.golang.org/grpc/credentials"
  15. "google.golang.org/grpc/keepalive"
  16. )
  17. // DefaultDialOptions constructs a list of grpc dial options
  18. func DefaultDialOptions(
  19. extraOpts ...grpc.DialOption,
  20. ) []grpc.DialOption {
  21. const (
  22. retries = 50 // 50 * 100ms = 5s total
  23. timeout = 1 * time.Second
  24. maxCallRecvMsgSize = 1 << 20 // Default 5Mb
  25. )
  26. var kacp = keepalive.ClientParameters{
  27. Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
  28. Timeout: 2 * time.Second, // wait 2 seconds for ping ack before considering the connection dead
  29. }
  30. opts := []grpc_retry.CallOption{
  31. grpc_retry.WithBackoff(grpc_retry.BackoffExponential(timeout)),
  32. }
  33. dialOpts := []grpc.DialOption{
  34. grpc.WithKeepaliveParams(kacp),
  35. grpc.WithDefaultCallOptions(
  36. grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize),
  37. grpc_retry.WithMax(retries),
  38. ),
  39. grpc.WithUnaryInterceptor(
  40. grpc_retry.UnaryClientInterceptor(opts...),
  41. ),
  42. }
  43. dialOpts = append(dialOpts, extraOpts...)
  44. return dialOpts
  45. }
  46. func GenerateTLS(certPath, keyPath, ca string, log log.Logger) grpc.DialOption {
  47. certificate, err := tls.LoadX509KeyPair(
  48. certPath,
  49. keyPath,
  50. )
  51. if err != nil {
  52. log.Error("error", err)
  53. os.Exit(1)
  54. }
  55. certPool := x509.NewCertPool()
  56. bs, err := os.ReadFile(ca)
  57. if err != nil {
  58. log.Error("failed to read ca cert:", "error", err)
  59. os.Exit(1)
  60. }
  61. ok := certPool.AppendCertsFromPEM(bs)
  62. if !ok {
  63. log.Error("failed to append certs")
  64. os.Exit(1)
  65. }
  66. transportCreds := credentials.NewTLS(&tls.Config{
  67. Certificates: []tls.Certificate{certificate},
  68. RootCAs: certPool,
  69. MinVersion: tls.VersionTLS13,
  70. })
  71. return grpc.WithTransportCredentials(transportCreds)
  72. }
  73. // DialRemoteSigner is a generalized function to dial the gRPC server.
  74. func DialRemoteSigner(
  75. cfg *config.PrivValidatorConfig,
  76. chainID string,
  77. logger log.Logger,
  78. usePrometheus bool,
  79. ) (*SignerClient, error) {
  80. var transportSecurity grpc.DialOption
  81. if cfg.AreSecurityOptionsPresent() {
  82. transportSecurity = GenerateTLS(cfg.ClientCertificateFile(),
  83. cfg.ClientKeyFile(), cfg.RootCAFile(), logger)
  84. } else {
  85. transportSecurity = grpc.WithInsecure()
  86. logger.Info("Using an insecure gRPC connection!")
  87. }
  88. dialOptions := DefaultDialOptions()
  89. if usePrometheus {
  90. grpcMetrics := grpc_prometheus.DefaultClientMetrics
  91. dialOptions = append(dialOptions, grpc.WithUnaryInterceptor(grpcMetrics.UnaryClientInterceptor()))
  92. }
  93. dialOptions = append(dialOptions, transportSecurity)
  94. ctx := context.TODO()
  95. _, address := tmnet.ProtocolAndAddress(cfg.ListenAddr)
  96. conn, err := grpc.DialContext(ctx, address, dialOptions...)
  97. if err != nil {
  98. logger.Error("unable to connect to server", "target", address, "err", err)
  99. }
  100. return NewSignerClient(conn, chainID, logger)
  101. }