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.

132 lines
3.1 KiB

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