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.

126 lines
3.1 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. globalConfig.TxIndex.IndexTags = "app.creator" // see dummy application
  73. }
  74. return globalConfig
  75. }
  76. func GetGRPCClient() core_grpc.BroadcastAPIClient {
  77. grpcAddr := globalConfig.RPC.GRPCListenAddress
  78. return core_grpc.StartGRPCClient(grpcAddr)
  79. }
  80. // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
  81. func StartTendermint(app abci.Application) *nm.Node {
  82. node := NewTendermint(app)
  83. err := node.Start()
  84. if err != nil {
  85. panic(err)
  86. }
  87. // wait for rpc
  88. waitForRPC()
  89. waitForGRPC()
  90. fmt.Println("Tendermint running!")
  91. return node
  92. }
  93. // NewTendermint creates a new tendermint server and sleeps forever
  94. func NewTendermint(app abci.Application) *nm.Node {
  95. // Create & start node
  96. config := GetConfig()
  97. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  98. logger = log.NewFilter(logger, log.AllowError())
  99. privValidatorFile := config.PrivValidatorFile()
  100. privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
  101. papp := proxy.NewLocalClientCreator(app)
  102. node, err := nm.NewNode(config, privValidator, papp,
  103. nm.DefaultGenesisDocProviderFunc(config),
  104. nm.DefaultDBProvider, logger)
  105. if err != nil {
  106. panic(err)
  107. }
  108. return node
  109. }