2018-04-24 17:55:15 -07:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
amino "github.com/tendermint/go-amino"
|
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Uses localhost
|
|
|
|
func WaitForHeight(height int64, port string) {
|
|
|
|
for {
|
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)
|
|
|
|
|
|
|
|
// get url, try a few times
|
|
|
|
var res *http.Response
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
res, err = http.Get(url)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
2018-04-24 17:55:15 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
res.Body.Close()
|
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
var resultBlock ctypes.ResultBlock
|
2018-04-24 17:55:15 -07:00
|
|
|
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("RES", res)
|
|
|
|
fmt.Println("BODY", string(body))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resultBlock.Block.Height >= height {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
// wait for tendermint to start
|
2018-04-24 17:55:15 -07:00
|
|
|
func WaitForStart(port string) {
|
2018-05-31 18:46:25 -07:00
|
|
|
var err error
|
|
|
|
for i := 0; i < 5; i++ {
|
2018-04-24 17:55:15 -07:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)
|
|
|
|
|
|
|
|
// get url, try a few times
|
|
|
|
var res *http.Response
|
|
|
|
res, err = http.Get(url)
|
|
|
|
if err == nil || res == nil {
|
|
|
|
continue
|
2018-04-24 17:55:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// waiting for server to start ...
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
res.Body.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 18:46:25 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-24 17:55:15 -07:00
|
|
|
}
|
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
// TODO: these functions just print to Stdout.
|
|
|
|
// consider using the logger.
|
|
|
|
|
2018-04-24 17:55:15 -07:00
|
|
|
// Wait for the RPC server to respond to /status
|
|
|
|
func WaitForRPC(laddr string) {
|
|
|
|
fmt.Println("LADDR", laddr)
|
|
|
|
client := rpcclient.NewJSONRPCClient(laddr)
|
|
|
|
ctypes.RegisterAmino(client.Codec())
|
|
|
|
result := new(ctypes.ResultStatus)
|
|
|
|
for {
|
|
|
|
_, err := client.Call("status", map[string]interface{}{}, result)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Waiting for RPC server to start on %s:%v\n", laddr, err)
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cdc = amino.NewCodec()
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ctypes.RegisterAmino(cdc)
|
|
|
|
}
|