-
Notifications
You must be signed in to change notification settings - Fork 556
/
node.go
192 lines (164 loc) · 5.05 KB
/
node.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package chain
import (
"context"
"fmt"
"regexp"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/osmosis-labs/osmosis/v20/tests/e2e/containers"
"github.com/osmosis-labs/osmosis/v20/tests/e2e/initialization"
)
type NodeConfig struct {
initialization.Node
OperatorAddress string
SnapshotInterval uint64
chainId string
rpcClient *rpchttp.HTTP
t *testing.T
containerManager *containers.Manager
// Add this to help with logging / tracking time since start.
setupTime time.Time
}
// NewNodeConfig returens new initialized NodeConfig.
func NewNodeConfig(t *testing.T, initNode *initialization.Node, initConfig *initialization.NodeConfig, chainId string, containerManager *containers.Manager) *NodeConfig {
t.Helper()
return &NodeConfig{
Node: *initNode,
SnapshotInterval: initConfig.SnapshotInterval,
chainId: chainId,
containerManager: containerManager,
t: t,
setupTime: time.Now(),
}
}
// Run runs a node container for the given nodeIndex.
// The node configuration must be already added to the chain config prior to calling this
// method.
func (n *NodeConfig) Run() error {
maxRetries := 3
currentRetry := 0
for currentRetry < maxRetries {
n.t.Logf("starting node container: %s", n.Name)
resource, err := n.containerManager.RunNodeResource(n.chainId, n.Name, n.ConfigDir)
if err != nil {
return err
}
hostPort := resource.GetHostPort("26657/tcp")
rpcClient, err := rpchttp.New("tcp://"+hostPort, "/websocket")
if err != nil {
return err
}
n.rpcClient = rpcClient
success := false
timeout := time.After(time.Second * 20)
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-timeout:
n.t.Logf("Osmosis node failed to produce blocks")
break
case <-ticker.C:
_, err := n.QueryCurrentHeight()
if err == nil {
n.t.Logf("started node container: %s", n.Name)
success = true
break
}
}
if success {
break
}
}
if success {
break
} else {
n.t.Logf("failed to start node container, retrying... (%d/%d)", currentRetry+1, maxRetries)
err := n.containerManager.RemoveNodeResource(n.Name)
if err != nil {
return err
}
currentRetry++
}
}
if currentRetry >= maxRetries {
return fmt.Errorf("failed to start node container after %d retries", maxRetries)
}
if err := n.extractOperatorAddressIfValidator(); err != nil {
return err
}
return nil
}
// Stop stops the node from running and removes its container.
func (n *NodeConfig) Stop() error {
n.t.Logf("stopping node container: %s", n.Name)
if err := n.containerManager.RemoveNodeResource(n.Name); err != nil {
return err
}
n.t.Logf("stopped node container: %s", n.Name)
return nil
}
func (n *NodeConfig) WaitForNumHeights(numBlocks int) {
targetHeight, err := n.QueryCurrentHeight()
require.NoError(n.t, err)
targetHeight += int64(numBlocks)
// Ensure the nodes are making progress.
doneCondition := func(syncInfo coretypes.SyncInfo) bool {
curHeight := syncInfo.LatestBlockHeight
if curHeight < targetHeight {
n.t.Logf("current block height is %d, waiting to reach: %d", curHeight, targetHeight)
return false
}
return !syncInfo.CatchingUp
}
n.WaitUntil(doneCondition)
}
// WaitUntil waits until node reaches doneCondition. Return nil
// if reached, error otherwise.
func (n *NodeConfig) WaitUntil(doneCondition func(syncInfo coretypes.SyncInfo) bool) {
var latestBlockHeight int64
for i := 0; i < waitUntilrepeatMax; i++ {
status, err := n.rpcClient.Status(context.Background())
require.NoError(n.t, err)
latestBlockHeight = status.SyncInfo.LatestBlockHeight
// let the node produce a few blocks
if !doneCondition(status.SyncInfo) {
time.Sleep(waitUntilRepeatPauseTime)
continue
}
return
}
n.t.Errorf("node %s timed out waiting for condition, latest block height was %d", n.Name, latestBlockHeight)
}
func (n *NodeConfig) extractOperatorAddressIfValidator() error {
if !n.IsValidator {
n.t.Logf("node (%s) is not a validator, skipping", n.Name)
return nil
}
cmd := []string{"osmosisd", "debug", "addr", n.PublicKey}
n.t.Logf("extracting validator operator addresses for validator: %s", n.Name)
_, errBuf, err := n.containerManager.ExecCmd(n.t, n.Name, cmd, "")
if err != nil {
return err
}
re := regexp.MustCompile("osmovaloper(.{39})")
operAddr := fmt.Sprintf("%s\n", re.FindString(errBuf.String()))
n.OperatorAddress = strings.TrimSuffix(operAddr, "\n")
return nil
}
func (n *NodeConfig) GetHostPort(portId string) (string, error) {
return n.containerManager.GetHostPort(n.Name, portId)
}
func (n *NodeConfig) WithSetupTime(t time.Time) *NodeConfig {
n.setupTime = t
return n
}
func (n *NodeConfig) LogActionF(msg string, args ...interface{}) {
timeSinceStart := time.Since(n.setupTime).Round(time.Millisecond)
s := fmt.Sprintf(msg, args...)
n.t.Logf("[%s] %s. From container %s", timeSinceStart, s, n.Name)
}