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.

86 lines
2.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package rpctest
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/tendermint/tmlibs/log"
  9. abci "github.com/tendermint/abci/types"
  10. cfg "github.com/tendermint/tendermint/config"
  11. nm "github.com/tendermint/tendermint/node"
  12. "github.com/tendermint/tendermint/proxy"
  13. core_grpc "github.com/tendermint/tendermint/rpc/grpc"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. var config *cfg.Config
  17. // f**ing long, but unique for each test
  18. func makePathname() string {
  19. // get path
  20. p, err := os.Getwd()
  21. if err != nil {
  22. panic(err)
  23. }
  24. fmt.Println(p)
  25. sep := string(filepath.Separator)
  26. return strings.Replace(p, sep, "_", -1)
  27. }
  28. func randPort() int {
  29. // returns between base and base + spread
  30. base, spread := 20000, 20000
  31. return base + rand.Intn(spread)
  32. }
  33. func makeAddrs() (string, string, string) {
  34. start := randPort()
  35. return fmt.Sprintf("tcp://0.0.0.0:%d", start),
  36. fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
  37. fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
  38. }
  39. // GetConfig returns a config for the test cases as a singleton
  40. func GetConfig() *cfg.Config {
  41. if config == nil {
  42. pathname := makePathname()
  43. config = cfg.ResetTestRoot(pathname)
  44. // and we use random ports to run in parallel
  45. tm, rpc, grpc := makeAddrs()
  46. config.P2P.ListenAddress = tm
  47. config.RPC.ListenAddress = rpc
  48. config.RPC.GRPCListenAddress = grpc
  49. }
  50. return config
  51. }
  52. func GetGRPCClient() core_grpc.BroadcastAPIClient {
  53. grpcAddr := config.RPC.GRPCListenAddress
  54. return core_grpc.StartGRPCClient(grpcAddr)
  55. }
  56. // StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
  57. func StartTendermint(app abci.Application) *nm.Node {
  58. node := NewTendermint(app)
  59. node.Start()
  60. fmt.Println("Tendermint running!")
  61. return node
  62. }
  63. // NewTendermint creates a new tendermint server and sleeps forever
  64. func NewTendermint(app abci.Application) *nm.Node {
  65. // Create & start node
  66. config := GetConfig()
  67. logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  68. logger = log.NewFilter(logger, log.AllowError())
  69. privValidatorFile := config.PrivValidatorFile()
  70. privValidator := types.LoadOrGenPrivValidator(privValidatorFile, logger)
  71. papp := proxy.NewLocalClientCreator(app)
  72. node := nm.NewNode(config, privValidator, papp, logger)
  73. return node
  74. }