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.

139 lines
3.5 KiB

7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package rpctest
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "github.com/tendermint/tendermint/libs/log"
  10. abci "github.com/tendermint/tendermint/abci/types"
  11. cmn "github.com/tendermint/tendermint/libs/common"
  12. cfg "github.com/tendermint/tendermint/config"
  13. nm "github.com/tendermint/tendermint/node"
  14. "github.com/tendermint/tendermint/p2p"
  15. "github.com/tendermint/tendermint/privval"
  16. "github.com/tendermint/tendermint/proxy"
  17. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  18. core_grpc "github.com/tendermint/tendermint/rpc/grpc"
  19. rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
  20. )
  21. var globalConfig *cfg.Config
  22. func waitForRPC() {
  23. laddr := GetConfig().RPC.ListenAddress
  24. client := rpcclient.NewJSONRPCClient(laddr)
  25. ctypes.RegisterAmino(client.Codec())
  26. result := new(ctypes.ResultStatus)
  27. for {
  28. _, err := client.Call("status", map[string]interface{}{}, result)
  29. if err == nil {
  30. return
  31. } else {
  32. fmt.Println("error", err)
  33. time.Sleep(time.Millisecond)
  34. }
  35. }
  36. }
  37. func waitForGRPC() {
  38. client := GetGRPCClient()
  39. for {
  40. _, err := client.Ping(context.Background(), &core_grpc.RequestPing{})
  41. if err == nil {
  42. return
  43. }
  44. }
  45. }
  46. // f**ing long, but unique for each test
  47. func makePathname() string {
  48. // get path
  49. p, err := os.Getwd()
  50. if err != nil {
  51. panic(err)
  52. }
  53. // fmt.Println(p)
  54. sep := string(filepath.Separator)
  55. return strings.Replace(p, sep, "_", -1)
  56. }
  57. func randPort() int {
  58. return int(cmn.RandUint16()/2 + 10000)
  59. }
  60. func makeAddrs() (string, string, string) {
  61. start := randPort()
  62. return fmt.Sprintf("tcp://0.0.0.0:%d", start),
  63. fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
  64. fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
  65. }
  66. // GetConfig returns a config for the test cases as a singleton
  67. func GetConfig() *cfg.Config {
  68. if globalConfig == nil {
  69. pathname := makePathname()
  70. globalConfig = cfg.ResetTestRoot(pathname)
  71. // and we use random ports to run in parallel
  72. tm, rpc, grpc := makeAddrs()
  73. globalConfig.P2P.ListenAddress = tm
  74. globalConfig.RPC.ListenAddress = rpc
  75. globalConfig.RPC.CORSAllowedOrigins = []string{"https://tendermint.com/"}
  76. globalConfig.RPC.GRPCListenAddress = grpc
  77. globalConfig.TxIndex.IndexTags = "app.creator,tx.height" // see kvstore application
  78. }
  79. return globalConfig
  80. }
  81. func GetGRPCClient() core_grpc.BroadcastAPIClient {
  82. grpcAddr := globalConfig.RPC.GRPCListenAddress
  83. return core_grpc.StartGRPCClient(grpcAddr)
  84. }
  85. // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
  86. func StartTendermint(app abci.Application) *nm.Node {
  87. node := NewTendermint(app)
  88. err := node.Start()
  89. if err != nil {
  90. panic(err)
  91. }
  92. // wait for rpc
  93. waitForRPC()
  94. waitForGRPC()
  95. fmt.Println("Tendermint running!")
  96. return node
  97. }
  98. // NewTendermint creates a new tendermint server and sleeps forever
  99. func NewTendermint(app abci.Application) *nm.Node {
  100. // Create & start node
  101. config := GetConfig()
  102. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  103. logger = log.NewFilter(logger, log.AllowError())
  104. pvKeyFile := config.PrivValidatorKeyFile()
  105. pvKeyStateFile := config.PrivValidatorStateFile()
  106. pv := privval.LoadOrGenFilePV(pvKeyFile, pvKeyStateFile)
  107. papp := proxy.NewLocalClientCreator(app)
  108. nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
  109. if err != nil {
  110. panic(err)
  111. }
  112. node, err := nm.NewNode(config, pv, nodeKey, papp,
  113. nm.DefaultGenesisDocProviderFunc(config),
  114. nm.DefaultDBProvider,
  115. nm.DefaultMetricsProvider(config.Instrumentation),
  116. logger)
  117. if err != nil {
  118. panic(err)
  119. }
  120. return node
  121. }