-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
146 lines (126 loc) · 3.98 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
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/percosis-labs/percosis/v16/tests/e2e/containers"
"github.com/percosis-labs/percosis/v16/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 {
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
require.Eventually(
n.t,
func() bool {
// This fails if unsuccessful.
_, err := n.QueryCurrentHeight()
if err != nil {
return false
}
n.t.Logf("started node container: %s", n.Name)
return true
},
2*time.Minute,
time.Second,
"Percosis node failed to produce blocks",
)
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
}
// 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{"percosisd", "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("percovaloper(.{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)
}