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
8 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. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/tendermint/tmlibs/log"
  10. abci "github.com/tendermint/abci/types"
  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. // returns between base and base + spread
  53. base, spread := 20000, 20000
  54. return base + rand.Intn(spread)
  55. }
  56. func makeAddrs() (string, string, string) {
  57. start := randPort()
  58. return fmt.Sprintf("tcp://0.0.0.0:%d", start),
  59. fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
  60. fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
  61. }
  62. // GetConfig returns a config for the test cases as a singleton
  63. func GetConfig() *cfg.Config {
  64. if globalConfig == nil {
  65. pathname := makePathname()
  66. globalConfig = cfg.ResetTestRoot(pathname)
  67. // and we use random ports to run in parallel
  68. tm, rpc, grpc := makeAddrs()
  69. globalConfig.P2P.ListenAddress = tm
  70. globalConfig.RPC.ListenAddress = rpc
  71. globalConfig.RPC.GRPCListenAddress = grpc
  72. }
  73. return globalConfig
  74. }
  75. func GetGRPCClient() core_grpc.BroadcastAPIClient {
  76. grpcAddr := globalConfig.RPC.GRPCListenAddress
  77. return core_grpc.StartGRPCClient(grpcAddr)
  78. }
  79. // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
  80. func StartTendermint(app abci.Application) *nm.Node {
  81. node := NewTendermint(app)
  82. err := node.Start()
  83. if err != nil {
  84. panic(err)
  85. }
  86. // wait for rpc
  87. waitForRPC()
  88. waitForGRPC()
  89. fmt.Println("Tendermint running!")
  90. return node
  91. }
  92. // NewTendermint creates a new tendermint server and sleeps forever
  93. func NewTendermint(app abci.Application) *nm.Node {
  94. // Create & start node
  95. config := GetConfig()
  96. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  97. logger = log.NewFilter(logger, log.AllowError())
  98. privValidatorFile := config.PrivValidatorFile()
  99. privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
  100. papp := proxy.NewLocalClientCreator(app)
  101. node, err := nm.NewNode(config, privValidator, papp,
  102. nm.DefaultGenesisDocProviderFunc(config),
  103. nm.DefaultDBProvider, logger)
  104. if err != nil {
  105. panic(err)
  106. }
  107. return node
  108. }