tendermint/rpc/test/helpers.go

92 lines
2.3 KiB
Go
Raw Normal View History

2016-01-10 13:33:52 -08:00
package rpctest
import (
2017-02-21 10:36:18 -08:00
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
2016-01-10 13:33:52 -08:00
2017-05-02 00:53:32 -07:00
"github.com/tendermint/tmlibs/log"
2016-01-10 13:33:52 -08:00
2017-02-21 10:36:18 -08:00
abci "github.com/tendermint/abci/types"
2017-05-04 19:33:08 -07:00
cfg "github.com/tendermint/tendermint/config"
2016-01-10 13:33:52 -08:00
nm "github.com/tendermint/tendermint/node"
2017-02-21 10:36:18 -08:00
"github.com/tendermint/tendermint/proxy"
core_grpc "github.com/tendermint/tendermint/rpc/grpc"
2017-02-21 10:36:18 -08:00
"github.com/tendermint/tendermint/types"
2016-01-10 13:33:52 -08:00
)
2017-05-04 19:33:08 -07:00
var config *cfg.Config
2016-01-10 13:33:52 -08:00
// f**ing long, but unique for each test
func makePathname() string {
// get path
p, err := os.Getwd()
if err != nil {
panic(err)
}
fmt.Println(p)
sep := string(filepath.Separator)
return strings.Replace(p, sep, "_", -1)
}
func randPort() int {
// returns between base and base + spread
base, spread := 20000, 20000
return base + rand.Intn(spread)
}
func makeAddrs() (string, string, string) {
start := randPort()
return fmt.Sprintf("tcp://0.0.0.0:%d", start),
fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
}
2017-02-21 10:36:18 -08:00
// GetConfig returns a config for the test cases as a singleton
2017-05-04 19:33:08 -07:00
func GetConfig() *cfg.Config {
2017-02-21 10:36:18 -08:00
if config == nil {
pathname := makePathname()
2017-05-04 19:33:08 -07:00
config = cfg.ResetTestRoot(pathname)
// and we use random ports to run in parallel
tm, rpc, grpc := makeAddrs()
2017-05-04 19:33:08 -07:00
config.P2P.ListenAddress = tm
2017-05-26 08:57:41 -07:00
config.RPC.ListenAddress = rpc
config.RPC.GRPCListenAddress = grpc
2017-02-21 10:36:18 -08:00
}
return config
2016-01-12 13:54:27 -08:00
}
2017-02-21 10:36:18 -08:00
func GetGRPCClient() core_grpc.BroadcastAPIClient {
2017-05-26 08:57:41 -07:00
grpcAddr := config.RPC.GRPCListenAddress
2017-02-21 10:36:18 -08:00
return core_grpc.StartGRPCClient(grpcAddr)
}
2016-01-10 13:33:52 -08:00
2017-02-21 10:36:18 -08:00
// StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
func StartTendermint(app abci.Application) *nm.Node {
2017-02-22 13:52:16 -08:00
node := NewTendermint(app)
node.Start()
2017-02-21 10:36:18 -08:00
fmt.Println("Tendermint running!")
return node
2016-01-10 13:33:52 -08:00
}
2017-02-21 10:36:18 -08:00
// NewTendermint creates a new tendermint server and sleeps forever
func NewTendermint(app abci.Application) *nm.Node {
// Create & start node
config := GetConfig()
2017-05-02 00:53:32 -07:00
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger = log.NewFilter(logger, log.AllowError())
2017-05-04 19:33:08 -07:00
privValidatorFile := config.PrivValidatorFile()
2017-09-18 20:16:14 -07:00
privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
2017-02-21 10:36:18 -08:00
papp := proxy.NewLocalClientCreator(app)
node, err := nm.NewNode(config, privValidator, papp,
nm.DefaultGenesisDocProviderFunc(config),
nm.DefaultDBProvider, logger)
if err != nil {
panic(err)
}
2017-02-21 10:36:18 -08:00
return node
2016-01-10 13:33:52 -08:00
}