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.

330 lines
8.2 KiB

  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/spf13/viper"
  13. "google.golang.org/grpc"
  14. abciclient "github.com/tendermint/tendermint/abci/client"
  15. "github.com/tendermint/tendermint/abci/server"
  16. "github.com/tendermint/tendermint/config"
  17. "github.com/tendermint/tendermint/crypto/ed25519"
  18. "github.com/tendermint/tendermint/internal/p2p"
  19. "github.com/tendermint/tendermint/libs/log"
  20. tmnet "github.com/tendermint/tendermint/libs/net"
  21. "github.com/tendermint/tendermint/light"
  22. lproxy "github.com/tendermint/tendermint/light/proxy"
  23. lrpc "github.com/tendermint/tendermint/light/rpc"
  24. dbs "github.com/tendermint/tendermint/light/store/db"
  25. "github.com/tendermint/tendermint/node"
  26. "github.com/tendermint/tendermint/privval"
  27. grpcprivval "github.com/tendermint/tendermint/privval/grpc"
  28. privvalproto "github.com/tendermint/tendermint/proto/tendermint/privval"
  29. rpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
  30. "github.com/tendermint/tendermint/test/e2e/app"
  31. e2e "github.com/tendermint/tendermint/test/e2e/pkg"
  32. )
  33. var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
  34. // main is the binary entrypoint.
  35. func main() {
  36. ctx, cancel := context.WithCancel(context.Background())
  37. defer cancel()
  38. if len(os.Args) != 2 {
  39. fmt.Printf("Usage: %v <configfile>", os.Args[0])
  40. return
  41. }
  42. configFile := ""
  43. if len(os.Args) == 2 {
  44. configFile = os.Args[1]
  45. }
  46. if err := run(ctx, configFile); err != nil {
  47. logger.Error(err.Error())
  48. os.Exit(1)
  49. }
  50. }
  51. // run runs the application - basically like main() with error handling.
  52. func run(ctx context.Context, configFile string) error {
  53. cfg, err := LoadConfig(configFile)
  54. if err != nil {
  55. return err
  56. }
  57. // Start remote signer (must start before node if running builtin).
  58. if cfg.PrivValServer != "" {
  59. if err = startSigner(ctx, cfg); err != nil {
  60. return err
  61. }
  62. if cfg.Protocol == "builtin" {
  63. time.Sleep(1 * time.Second)
  64. }
  65. }
  66. // Start app server.
  67. switch cfg.Protocol {
  68. case "socket", "grpc":
  69. err = startApp(ctx, cfg)
  70. case "builtin":
  71. switch cfg.Mode {
  72. case string(e2e.ModeLight):
  73. err = startLightNode(ctx, cfg)
  74. case string(e2e.ModeSeed):
  75. err = startSeedNode(ctx)
  76. default:
  77. err = startNode(ctx, cfg)
  78. }
  79. default:
  80. err = fmt.Errorf("invalid protocol %q", cfg.Protocol)
  81. }
  82. if err != nil {
  83. return err
  84. }
  85. // Apparently there's no way to wait for the server, so we just sleep
  86. for {
  87. time.Sleep(1 * time.Hour)
  88. }
  89. }
  90. // startApp starts the application server, listening for connections from Tendermint.
  91. func startApp(ctx context.Context, cfg *Config) error {
  92. app, err := app.NewApplication(cfg.App())
  93. if err != nil {
  94. return err
  95. }
  96. server, err := server.NewServer(logger, cfg.Listen, cfg.Protocol, app)
  97. if err != nil {
  98. return err
  99. }
  100. err = server.Start(ctx)
  101. if err != nil {
  102. return err
  103. }
  104. logger.Info(fmt.Sprintf("Server listening on %v (%v protocol)", cfg.Listen, cfg.Protocol))
  105. return nil
  106. }
  107. // startNode starts a Tendermint node running the application directly. It assumes the Tendermint
  108. // configuration is in $TMHOME/config/tendermint.toml.
  109. //
  110. // FIXME There is no way to simply load the configuration from a file, so we need to pull in Viper.
  111. func startNode(ctx context.Context, cfg *Config) error {
  112. app, err := app.NewApplication(cfg.App())
  113. if err != nil {
  114. return err
  115. }
  116. tmcfg, nodeLogger, err := setupNode()
  117. if err != nil {
  118. return fmt.Errorf("failed to setup config: %w", err)
  119. }
  120. n, err := node.New(
  121. ctx,
  122. tmcfg,
  123. nodeLogger,
  124. abciclient.NewLocalCreator(app),
  125. nil,
  126. )
  127. if err != nil {
  128. return err
  129. }
  130. return n.Start(ctx)
  131. }
  132. func startSeedNode(ctx context.Context) error {
  133. tmcfg, nodeLogger, err := setupNode()
  134. if err != nil {
  135. return fmt.Errorf("failed to setup config: %w", err)
  136. }
  137. tmcfg.Mode = config.ModeSeed
  138. n, err := node.New(ctx, tmcfg, nodeLogger, nil, nil)
  139. if err != nil {
  140. return err
  141. }
  142. return n.Start(ctx)
  143. }
  144. func startLightNode(ctx context.Context, cfg *Config) error {
  145. tmcfg, nodeLogger, err := setupNode()
  146. if err != nil {
  147. return err
  148. }
  149. dbContext := &config.DBContext{ID: "light", Config: tmcfg}
  150. lightDB, err := config.DefaultDBProvider(dbContext)
  151. if err != nil {
  152. return err
  153. }
  154. providers := rpcEndpoints(tmcfg.P2P.PersistentPeers)
  155. c, err := light.NewHTTPClient(
  156. context.Background(),
  157. cfg.ChainID,
  158. light.TrustOptions{
  159. Period: tmcfg.StateSync.TrustPeriod,
  160. Height: tmcfg.StateSync.TrustHeight,
  161. Hash: tmcfg.StateSync.TrustHashBytes(),
  162. },
  163. providers[0],
  164. providers[1:],
  165. dbs.New(lightDB),
  166. light.Logger(nodeLogger),
  167. )
  168. if err != nil {
  169. return err
  170. }
  171. rpccfg := rpcserver.DefaultConfig()
  172. rpccfg.MaxBodyBytes = tmcfg.RPC.MaxBodyBytes
  173. rpccfg.MaxHeaderBytes = tmcfg.RPC.MaxHeaderBytes
  174. rpccfg.MaxOpenConnections = tmcfg.RPC.MaxOpenConnections
  175. // If necessary adjust global WriteTimeout to ensure it's greater than
  176. // TimeoutBroadcastTxCommit.
  177. // See https://github.com/tendermint/tendermint/issues/3435
  178. if rpccfg.WriteTimeout <= tmcfg.RPC.TimeoutBroadcastTxCommit {
  179. rpccfg.WriteTimeout = tmcfg.RPC.TimeoutBroadcastTxCommit + 1*time.Second
  180. }
  181. p, err := lproxy.NewProxy(c, tmcfg.RPC.ListenAddress, providers[0], rpccfg, nodeLogger,
  182. lrpc.KeyPathFn(lrpc.DefaultMerkleKeyPathFn()))
  183. if err != nil {
  184. return err
  185. }
  186. logger.Info("Starting proxy...", "laddr", tmcfg.RPC.ListenAddress)
  187. if err := p.ListenAndServe(ctx); err != http.ErrServerClosed {
  188. // Error starting or closing listener:
  189. logger.Error("proxy ListenAndServe", "err", err)
  190. }
  191. return nil
  192. }
  193. // startSigner starts a signer server connecting to the given endpoint.
  194. func startSigner(ctx context.Context, cfg *Config) error {
  195. filePV, err := privval.LoadFilePV(cfg.PrivValKey, cfg.PrivValState)
  196. if err != nil {
  197. return err
  198. }
  199. protocol, address := tmnet.ProtocolAndAddress(cfg.PrivValServer)
  200. var dialFn privval.SocketDialer
  201. switch protocol {
  202. case "tcp":
  203. dialFn = privval.DialTCPFn(address, 3*time.Second, ed25519.GenPrivKey())
  204. case "unix":
  205. dialFn = privval.DialUnixFn(address)
  206. case "grpc":
  207. lis, err := net.Listen("tcp", address)
  208. if err != nil {
  209. return err
  210. }
  211. ss := grpcprivval.NewSignerServer(cfg.ChainID, filePV, logger)
  212. s := grpc.NewServer()
  213. privvalproto.RegisterPrivValidatorAPIServer(s, ss)
  214. go func() { // no need to clean up since we remove docker containers
  215. if err := s.Serve(lis); err != nil {
  216. panic(err)
  217. }
  218. go func() {
  219. <-ctx.Done()
  220. s.GracefulStop()
  221. }()
  222. }()
  223. return nil
  224. default:
  225. return fmt.Errorf("invalid privval protocol %q", protocol)
  226. }
  227. endpoint := privval.NewSignerDialerEndpoint(logger, dialFn,
  228. privval.SignerDialerEndpointRetryWaitInterval(1*time.Second),
  229. privval.SignerDialerEndpointConnRetries(100))
  230. err = privval.NewSignerServer(endpoint, cfg.ChainID, filePV).Start(ctx)
  231. if err != nil {
  232. return err
  233. }
  234. logger.Info(fmt.Sprintf("Remote signer connecting to %v", cfg.PrivValServer))
  235. return nil
  236. }
  237. func setupNode() (*config.Config, log.Logger, error) {
  238. var tmcfg *config.Config
  239. home := os.Getenv("TMHOME")
  240. if home == "" {
  241. return nil, nil, errors.New("TMHOME not set")
  242. }
  243. viper.AddConfigPath(filepath.Join(home, "config"))
  244. viper.SetConfigName("config")
  245. if err := viper.ReadInConfig(); err != nil {
  246. return nil, nil, err
  247. }
  248. tmcfg = config.DefaultConfig()
  249. if err := viper.Unmarshal(tmcfg); err != nil {
  250. return nil, nil, err
  251. }
  252. tmcfg.SetRoot(home)
  253. if err := tmcfg.ValidateBasic(); err != nil {
  254. return nil, nil, fmt.Errorf("error in config file: %w", err)
  255. }
  256. nodeLogger, err := log.NewDefaultLogger(tmcfg.LogFormat, tmcfg.LogLevel)
  257. if err != nil {
  258. return nil, nil, err
  259. }
  260. return tmcfg, nodeLogger.With("module", "main"), nil
  261. }
  262. // rpcEndpoints takes a list of persistent peers and splits them into a list of rpc endpoints
  263. // using 26657 as the port number
  264. func rpcEndpoints(peers string) []string {
  265. arr := strings.Split(peers, ",")
  266. endpoints := make([]string, len(arr))
  267. for i, v := range arr {
  268. addr, err := p2p.ParseNodeAddress(v)
  269. if err != nil {
  270. panic(err)
  271. }
  272. // use RPC port instead
  273. addr.Port = 26657
  274. var rpcEndpoint string
  275. // for ipv6 addresses
  276. if strings.Contains(addr.Hostname, ":") {
  277. rpcEndpoint = "http://[" + addr.Hostname + "]:" + fmt.Sprint(addr.Port)
  278. } else { // for ipv4 addresses
  279. rpcEndpoint = "http://" + addr.Hostname + ":" + fmt.Sprint(addr.Port)
  280. }
  281. endpoints[i] = rpcEndpoint
  282. }
  283. return endpoints
  284. }