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.

125 lines
3.0 KiB

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