tendermint/rpc/test/helpers.go

92 lines
2.5 KiB
Go
Raw Normal View History

2016-01-10 16:33:52 -05:00
package rpctest
import (
2017-02-21 19:36:18 +01:00
"fmt"
2016-01-10 16:33:52 -05:00
2017-02-21 19:36:18 +01:00
logger "github.com/tendermint/go-logger"
2016-01-10 16:33:52 -05:00
2017-02-21 19:36:18 +01:00
abci "github.com/tendermint/abci/types"
cfg "github.com/tendermint/go-config"
2016-01-12 16:54:27 -05:00
client "github.com/tendermint/go-rpc/client"
2016-05-08 15:00:58 -07:00
"github.com/tendermint/tendermint/config/tendermint_test"
2016-01-10 16:33:52 -05:00
nm "github.com/tendermint/tendermint/node"
2017-02-21 19:36:18 +01:00
"github.com/tendermint/tendermint/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"
core_grpc "github.com/tendermint/tendermint/rpc/grpc"
"github.com/tendermint/tendermint/types"
2016-01-10 16:33:52 -05:00
)
var (
2017-02-21 19:36:18 +01:00
config cfg.Config
node *nm.Node
2016-01-10 16:33:52 -05:00
)
2017-02-21 19:36:18 +01:00
const tmLogLevel = "error"
2016-01-12 16:54:27 -05:00
2017-02-21 19:36:18 +01:00
// GetConfig returns a config for the test cases as a singleton
func GetConfig() cfg.Config {
if config == nil {
config = tendermint_test.ResetConfig("rpc_test_client_test")
// Shut up the logging
logger.SetLogLevel(tmLogLevel)
}
return config
2016-01-12 16:54:27 -05:00
}
2017-02-21 19:36:18 +01:00
// GetClient gets a rpc client pointing to the test tendermint rpc
func GetClient() *rpcclient.HTTPClient {
rpcAddr := GetConfig().GetString("rpc_laddr")
return rpcclient.New(rpcAddr, "/websocket")
}
2016-06-21 13:19:49 -04:00
2017-02-21 19:36:18 +01:00
// GetURIClient gets a uri client pointing to the test tendermint rpc
func GetURIClient() *client.ClientURI {
rpcAddr := GetConfig().GetString("rpc_laddr")
return client.NewClientURI(rpcAddr)
}
2016-01-10 16:33:52 -05:00
2017-02-21 19:36:18 +01:00
// GetJSONClient gets a http/json client pointing to the test tendermint rpc
func GetJSONClient() *client.ClientJSONRPC {
rpcAddr := GetConfig().GetString("rpc_laddr")
return client.NewClientJSONRPC(rpcAddr)
2016-01-10 16:33:52 -05:00
}
2017-02-21 19:36:18 +01:00
func GetGRPCClient() core_grpc.BroadcastAPIClient {
grpcAddr := config.GetString("grpc_laddr")
return core_grpc.StartGRPCClient(grpcAddr)
}
2016-01-10 16:33:52 -05:00
2017-02-21 19:36:18 +01:00
func GetWSClient() *client.WSClient {
rpcAddr := GetConfig().GetString("rpc_laddr")
wsc := client.NewWSClient(rpcAddr, "/websocket")
2016-01-13 21:20:25 -05:00
if _, err := wsc.Start(); err != nil {
panic(err)
2016-01-10 16:33:52 -05:00
}
2016-01-13 21:20:25 -05:00
return wsc
2016-01-10 16:33:52 -05:00
}
2017-02-21 19:36:18 +01:00
// StartTendermint starts a test tendermint server in a go routine and returns when it is initialized
// TODO: can one pass an Application in????
func StartTendermint(app abci.Application) *nm.Node {
// start a node
fmt.Println("Starting Tendermint...")
2016-01-10 16:33:52 -05:00
2017-02-21 19:36:18 +01:00
node = NewTendermint(app)
fmt.Println("Tendermint running!")
return node
2016-01-10 16:33:52 -05:00
}
2017-02-21 19:36:18 +01:00
// NewTendermint creates a new tendermint server and sleeps forever
func NewTendermint(app abci.Application) *nm.Node {
// Create & start node
config := GetConfig()
privValidatorFile := config.GetString("priv_validator_file")
privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
papp := proxy.NewLocalClientCreator(app)
node := nm.NewNode(config, privValidator, papp)
2016-01-10 16:33:52 -05:00
2017-02-21 19:36:18 +01:00
// node.Start now does everything including the RPC server
node.Start()
return node
2016-01-10 16:33:52 -05:00
}