2018-04-24 17:55:15 -07:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-04-22 12:21:48 -07:00
|
|
|
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
|
2018-04-24 17:55:15 -07:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-04-24 17:55:15 -07:00
|
|
|
)
|
|
|
|
|
2018-06-25 14:53:48 -07:00
|
|
|
// Wait for N tendermint blocks to pass using the Tendermint RPC
|
|
|
|
// on localhost
|
|
|
|
func WaitForNextNBlocksTM(n int64, port string) {
|
2018-07-02 21:33:53 -07:00
|
|
|
// get the latest block and wait for n more
|
2018-06-13 17:41:01 -07:00
|
|
|
url := fmt.Sprintf("http://localhost:%v", port)
|
2020-04-22 12:21:48 -07:00
|
|
|
cl, err := rpchttp.New(url, "/websocket")
|
2020-01-16 13:46:51 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to create Tendermint HTTP client: %s", err))
|
|
|
|
}
|
|
|
|
|
2018-07-02 21:33:53 -07:00
|
|
|
var height int64
|
2020-01-16 13:46:51 -08:00
|
|
|
|
|
|
|
resBlock, err := cl.Block(nil)
|
2018-07-02 21:33:53 -07:00
|
|
|
if err != nil || resBlock.Block == nil {
|
|
|
|
// wait for the first block to exist
|
|
|
|
WaitForHeightTM(1, port)
|
|
|
|
height = 1 + n
|
|
|
|
} else {
|
|
|
|
height = resBlock.Block.Height + n
|
2018-06-13 17:41:01 -07:00
|
|
|
}
|
2020-01-16 13:46:51 -08:00
|
|
|
|
2018-07-02 21:33:53 -07:00
|
|
|
waitForHeightTM(height, url)
|
2018-06-13 17:41:01 -07:00
|
|
|
}
|
|
|
|
|
2018-06-13 18:33:09 -07:00
|
|
|
// Wait for the given height from the Tendermint RPC
|
|
|
|
// on localhost
|
2018-06-13 17:41:01 -07:00
|
|
|
func WaitForHeightTM(height int64, port string) {
|
|
|
|
url := fmt.Sprintf("http://localhost:%v", port)
|
|
|
|
waitForHeightTM(height, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForHeightTM(height int64, url string) {
|
2020-04-22 12:21:48 -07:00
|
|
|
cl, err := rpchttp.New(url, "/websocket")
|
2020-01-16 13:46:51 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to create Tendermint HTTP client: %s", err))
|
|
|
|
}
|
|
|
|
|
2018-04-24 17:55:15 -07:00
|
|
|
for {
|
2018-06-13 17:41:01 -07:00
|
|
|
// get url, try a few times
|
|
|
|
var resBlock *ctypes.ResultBlock
|
|
|
|
var err error
|
|
|
|
INNER:
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
resBlock, err = cl.Block(nil)
|
|
|
|
if err == nil {
|
|
|
|
break INNER
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-24 17:55:15 -07:00
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
if resBlock.Block != nil && resBlock.Block.Height >= height {
|
2018-06-13 17:41:01 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2018-06-13 17:41:01 -07:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 21:33:53 -07:00
|
|
|
// wait for tendermint to start by querying tendermint
|
|
|
|
func WaitForTMStart(port string) {
|
|
|
|
url := fmt.Sprintf("http://localhost:%v/block", port)
|
|
|
|
WaitForStart(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForStart waits for the node to start by pinging the url
|
2018-12-04 01:57:44 -08:00
|
|
|
// every 100ms for 10s until it returns 200. If it takes longer than 5s,
|
2018-07-02 21:33:53 -07:00
|
|
|
// it panics.
|
|
|
|
func WaitForStart(url string) {
|
|
|
|
var err error
|
2018-04-24 17:55:15 -07:00
|
|
|
|
2018-06-30 16:32:52 -07:00
|
|
|
// ping the status endpoint a few times a second
|
|
|
|
// for a few seconds until we get a good response.
|
|
|
|
// otherwise something probably went wrong
|
2018-12-04 01:57:44 -08:00
|
|
|
for i := 0; i < 100; i++ {
|
2018-06-30 16:32:52 -07:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
2018-05-31 18:46:25 -07:00
|
|
|
|
|
|
|
var res *http.Response
|
2019-10-14 08:43:19 -07:00
|
|
|
res, err = http.Get(url) // nolint:gosec
|
2018-06-30 16:32:52 -07:00
|
|
|
if err != nil || res == nil {
|
2018-05-31 18:46:25 -07:00
|
|
|
continue
|
2018-04-24 17:55:15 -07:00
|
|
|
}
|
2018-07-02 21:33:53 -07:00
|
|
|
// body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
// fmt.Println("BODY", string(body))
|
2018-06-30 16:32:52 -07:00
|
|
|
err = res.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-24 17:55:15 -07:00
|
|
|
|
2018-06-30 16:32:52 -07:00
|
|
|
if res.StatusCode == http.StatusOK {
|
|
|
|
// good!
|
2018-04-24 17:55:15 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-06-30 16:32:52 -07:00
|
|
|
// still haven't started up?! panic!
|
|
|
|
panic(err)
|
2018-04-24 17:55:15 -07:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
var cdc = codec.New()
|
2018-04-24 17:55:15 -07:00
|
|
|
|
|
|
|
func init() {
|
2020-05-19 13:17:29 -07:00
|
|
|
ctypes.RegisterAmino(cdc.Amino)
|
2018-04-24 17:55:15 -07:00
|
|
|
}
|
2019-06-26 13:30:36 -07:00
|
|
|
|
|
|
|
//DONTCOVER
|