2016-01-10 16:33:52 -05:00
|
|
|
package rpctest
|
|
|
|
|
|
|
|
import (
|
2017-11-14 21:51:49 +00:00
|
|
|
"context"
|
2017-02-21 19:36:18 +01:00
|
|
|
"fmt"
|
2017-02-22 20:01:32 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-04-05 21:19:14 -07:00
|
|
|
"time"
|
2016-01-10 16:33:52 -05:00
|
|
|
|
2018-07-01 22:36:49 -04:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2016-01-10 16:33:52 -05:00
|
|
|
|
2018-06-22 06:59:02 +02:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2017-12-21 01:45:01 -05:00
|
|
|
|
2017-05-04 22:33:08 -04:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2019-02-06 10:14:03 -05:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2016-01-10 16:33:52 -05:00
|
|
|
nm "github.com/tendermint/tendermint/node"
|
2018-08-15 16:29:45 +08:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2018-06-01 19:17:37 +02:00
|
|
|
"github.com/tendermint/tendermint/privval"
|
2017-02-21 19:36:18 +01:00
|
|
|
"github.com/tendermint/tendermint/proxy"
|
2017-11-14 21:51:49 +00:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2017-04-26 19:57:33 -04:00
|
|
|
core_grpc "github.com/tendermint/tendermint/rpc/grpc"
|
2017-11-14 21:51:49 +00:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
|
2016-01-10 16:33:52 -05:00
|
|
|
)
|
|
|
|
|
2017-11-14 21:51:49 +00:00
|
|
|
var globalConfig *cfg.Config
|
|
|
|
|
|
|
|
func waitForRPC() {
|
|
|
|
laddr := GetConfig().RPC.ListenAddress
|
|
|
|
client := rpcclient.NewJSONRPCClient(laddr)
|
2018-04-05 21:19:14 -07:00
|
|
|
ctypes.RegisterAmino(client.Codec())
|
2017-11-14 21:51:49 +00:00
|
|
|
result := new(ctypes.ResultStatus)
|
|
|
|
for {
|
|
|
|
_, err := client.Call("status", map[string]interface{}{}, result)
|
|
|
|
if err == nil {
|
|
|
|
return
|
2018-04-05 21:19:14 -07:00
|
|
|
} else {
|
|
|
|
fmt.Println("error", err)
|
|
|
|
time.Sleep(time.Millisecond)
|
2017-11-14 21:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForGRPC() {
|
|
|
|
client := GetGRPCClient()
|
|
|
|
for {
|
|
|
|
_, err := client.Ping(context.Background(), &core_grpc.RequestPing{})
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 16:33:52 -05:00
|
|
|
|
2017-02-22 20:01:32 +01:00
|
|
|
// f**ing long, but unique for each test
|
|
|
|
func makePathname() string {
|
|
|
|
// get path
|
|
|
|
p, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-12-07 11:33:00 -06:00
|
|
|
// fmt.Println(p)
|
2017-02-22 20:01:32 +01:00
|
|
|
sep := string(filepath.Separator)
|
|
|
|
return strings.Replace(p, sep, "_", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randPort() int {
|
2019-02-06 10:14:03 -05:00
|
|
|
port, err := cmn.GetFreePort()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return port
|
2017-02-22 20:01:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeAddrs() (string, string, string) {
|
2019-02-06 10:14:03 -05:00
|
|
|
return fmt.Sprintf("tcp://0.0.0.0:%d", randPort()),
|
|
|
|
fmt.Sprintf("tcp://0.0.0.0:%d", randPort()),
|
|
|
|
fmt.Sprintf("tcp://0.0.0.0:%d", randPort())
|
2017-02-22 20:01:32 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 19:36:18 +01:00
|
|
|
// GetConfig returns a config for the test cases as a singleton
|
2017-05-04 22:33:08 -04:00
|
|
|
func GetConfig() *cfg.Config {
|
2017-11-14 21:51:49 +00:00
|
|
|
if globalConfig == nil {
|
2017-02-22 20:01:32 +01:00
|
|
|
pathname := makePathname()
|
2017-11-14 21:51:49 +00:00
|
|
|
globalConfig = cfg.ResetTestRoot(pathname)
|
2017-05-04 22:33:08 -04:00
|
|
|
|
2017-02-22 20:01:32 +01:00
|
|
|
// and we use random ports to run in parallel
|
|
|
|
tm, rpc, grpc := makeAddrs()
|
2017-11-14 21:51:49 +00:00
|
|
|
globalConfig.P2P.ListenAddress = tm
|
|
|
|
globalConfig.RPC.ListenAddress = rpc
|
2018-11-14 15:47:41 +03:00
|
|
|
globalConfig.RPC.CORSAllowedOrigins = []string{"https://tendermint.com/"}
|
2017-11-14 21:51:49 +00:00
|
|
|
globalConfig.RPC.GRPCListenAddress = grpc
|
2018-09-05 12:01:38 +04:00
|
|
|
globalConfig.TxIndex.IndexTags = "app.creator,tx.height" // see kvstore application
|
2017-02-21 19:36:18 +01:00
|
|
|
}
|
2017-11-14 21:51:49 +00:00
|
|
|
return globalConfig
|
2016-01-12 16:54:27 -05:00
|
|
|
}
|
|
|
|
|
2017-02-21 19:36:18 +01:00
|
|
|
func GetGRPCClient() core_grpc.BroadcastAPIClient {
|
2017-11-14 21:51:49 +00:00
|
|
|
grpcAddr := globalConfig.RPC.GRPCListenAddress
|
2017-02-21 19:36:18 +01:00
|
|
|
return core_grpc.StartGRPCClient(grpcAddr)
|
|
|
|
}
|
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
|
2019-02-18 13:23:40 +04:00
|
|
|
func StartTendermint(app abci.Application) *nm.Node {
|
|
|
|
node := NewTendermint(app)
|
2017-11-06 13:20:39 -05:00
|
|
|
err := node.Start()
|
2017-09-06 11:50:43 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-11-14 21:51:49 +00:00
|
|
|
|
|
|
|
// wait for rpc
|
|
|
|
waitForRPC()
|
|
|
|
waitForGRPC()
|
|
|
|
|
2017-02-21 19:36:18 +01:00
|
|
|
fmt.Println("Tendermint running!")
|
2017-11-14 21:51:49 +00:00
|
|
|
|
2019-02-18 13:23:40 +04:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopTendermint stops a test tendermint server, waits until it's stopped and
|
|
|
|
// cleans up test/config files.
|
|
|
|
func StopTendermint(node *nm.Node) {
|
|
|
|
node.Stop()
|
|
|
|
node.Wait()
|
|
|
|
os.RemoveAll(node.Config().RootDir)
|
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
|
2019-02-18 13:23:40 +04:00
|
|
|
func NewTendermint(app abci.Application) *nm.Node {
|
2017-02-21 19:36:18 +01:00
|
|
|
// Create & start node
|
|
|
|
config := GetConfig()
|
2017-05-02 11:53:32 +04:00
|
|
|
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
|
|
|
|
logger = log.NewFilter(logger, log.AllowError())
|
2018-12-22 05:58:27 +08:00
|
|
|
pvKeyFile := config.PrivValidatorKeyFile()
|
|
|
|
pvKeyStateFile := config.PrivValidatorStateFile()
|
|
|
|
pv := privval.LoadOrGenFilePV(pvKeyFile, pvKeyStateFile)
|
2017-02-21 19:36:18 +01:00
|
|
|
papp := proxy.NewLocalClientCreator(app)
|
2018-08-15 16:29:45 +08:00
|
|
|
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
node, err := nm.NewNode(config, pv, nodeKey, papp,
|
2017-09-20 18:29:36 -04:00
|
|
|
nm.DefaultGenesisDocProviderFunc(config),
|
2018-06-15 14:23:34 +04:00
|
|
|
nm.DefaultDBProvider,
|
2018-07-24 15:44:01 +04:00
|
|
|
nm.DefaultMetricsProvider(config.Instrumentation),
|
2018-06-15 14:23:34 +04:00
|
|
|
logger)
|
2017-09-20 18:29:36 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-02-18 13:23:40 +04:00
|
|
|
return node
|
2016-01-10 16:33:52 -05:00
|
|
|
}
|