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.

137 lines
3.3 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.GRPCListenAddress = grpc
  76. globalConfig.TxIndex.IndexTags = "app.creator,tx.height" // see kvstore application
  77. }
  78. return globalConfig
  79. }
  80. func GetGRPCClient() core_grpc.BroadcastAPIClient {
  81. grpcAddr := globalConfig.RPC.GRPCListenAddress
  82. return core_grpc.StartGRPCClient(grpcAddr)
  83. }
  84. // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
  85. func StartTendermint(app abci.Application) *nm.Node {
  86. node := NewTendermint(app)
  87. err := node.Start()
  88. if err != nil {
  89. panic(err)
  90. }
  91. // wait for rpc
  92. waitForRPC()
  93. waitForGRPC()
  94. fmt.Println("Tendermint running!")
  95. return node
  96. }
  97. // NewTendermint creates a new tendermint server and sleeps forever
  98. func NewTendermint(app abci.Application) *nm.Node {
  99. // Create & start node
  100. config := GetConfig()
  101. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  102. logger = log.NewFilter(logger, log.AllowError())
  103. pvFile := config.PrivValidatorFile()
  104. pv := privval.LoadOrGenFilePV(pvFile)
  105. papp := proxy.NewLocalClientCreator(app)
  106. nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
  107. if err != nil {
  108. panic(err)
  109. }
  110. node, err := nm.NewNode(config, pv, nodeKey, papp,
  111. nm.DefaultGenesisDocProviderFunc(config),
  112. nm.DefaultDBProvider,
  113. nm.DefaultMetricsProvider(config.Instrumentation),
  114. logger)
  115. if err != nil {
  116. panic(err)
  117. }
  118. return node
  119. }