forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
104 lines (88 loc) · 2 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 {
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)
}
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
res.Body.Close()
var resultBlock ctypes.ResultBlock
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)
}
}
// wait for tendermint to start
func WaitForStart(port string) {
var err error
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
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
}
// waiting for server to start ...
if res.StatusCode != http.StatusOK {
res.Body.Close()
return
}
}
if err != nil {
panic(err)
}
}
// TODO: these functions just print to Stdout.
// consider using the logger.
// 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)
}