-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtesting.go
More file actions
215 lines (183 loc) · 5.77 KB
/
testing.go
File metadata and controls
215 lines (183 loc) · 5.77 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package core
import (
"context"
"errors"
"net"
"path/filepath"
"sync"
"testing"
"time"
sdklog "cosmossdk.io/log"
tmrand "github.com/cometbft/cometbft/libs/rand"
"github.com/cometbft/cometbft/node"
cmthttp "github.com/cometbft/cometbft/rpc/client/http"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"github.com/celestiaorg/celestia-app/v9/test/util/genesis"
"github.com/celestiaorg/celestia-app/v9/test/util/testnode"
)
const chainID = "private"
// DefaultTestConfig returns the default testing configuration for Tendermint + Celestia App tandem.
//
// It fetches free ports from OS and sets them into configs, s.t.
// user can make use of them(unlike 0 port) and allowing to run
// multiple tests nodes in parallel.
//
// Additionally, it instructs Tendermint + Celestia App tandem to setup 10 funded accounts.
func DefaultTestConfig() *testnode.Config {
return testnode.DefaultConfig().
WithDelayedPrecommitTimeout(200 * time.Millisecond).
WithFundedAccounts(generateRandomAccounts(10)...). // 10 usually is enough for testing
WithChainID(chainID)
}
// StartTestNode simply starts Tendermint and Celestia App tandem with default testing
// configuration.
func StartTestNode(t *testing.T) testnode.Context {
return StartTestNodeWithConfig(t, DefaultTestConfig())
}
// StartTestNodeWithConfig starts Tendermint and Celestia App tandem with custom configuration.
func StartTestNodeWithConfig(t *testing.T, cfg *testnode.Config) testnode.Context {
cctx, _, _ := testnode.NewNetwork(t, cfg)
// we want to test over remote http client,
// so we are as close to the real environment as possible,
// however, it might be useful to use a local tendermint client
// if you need to debug something inside it
return cctx
}
// StartTestNodeWithConfigAndClient initializes a test node with default configuration and a WebSocket HTTP client.
func StartTestNodeWithConfigAndClient(t *testing.T) (testnode.Context, *cmthttp.HTTP) {
cctx, rpcAddr, _ := testnode.NewNetwork(t, DefaultTestConfig())
wsClient, err := cmthttp.New(rpcAddr, "/websocket")
if err != nil {
panic(err)
}
return cctx, wsClient
}
// generateRandomAccounts generates n random account names.
func generateRandomAccounts(n int) []string {
accounts := make([]string, n)
for i := range accounts {
accounts[i] = tmrand.Str(9)
}
return accounts
}
func newTestClient(t *testing.T, ip, port string) *grpc.ClientConn {
t.Helper()
retryInterceptor := grpc_retry.UnaryClientInterceptor(
grpc_retry.WithMax(5),
grpc_retry.WithCodes(codes.Unavailable),
grpc_retry.WithBackoff(
grpc_retry.BackoffExponentialWithJitter(time.Second, 2.0)),
)
retryStreamInterceptor := grpc_retry.StreamClientInterceptor(
grpc_retry.WithMax(5),
grpc_retry.WithCodes(codes.Unavailable),
grpc_retry.WithBackoff(
grpc_retry.BackoffExponentialWithJitter(time.Second, 2.0)),
)
opts := []grpc.DialOption{
grpc.WithUnaryInterceptor(retryInterceptor),
grpc.WithStreamInterceptor(retryStreamInterceptor),
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
endpoint := net.JoinHostPort(ip, port)
client, err := grpc.NewClient(endpoint, opts...)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
t.Cleanup(cancel)
ready := client.WaitForStateChange(ctx, connectivity.Ready)
require.True(t, ready)
return client
}
// Network wraps `testnode.Context` allowing to manually stop all underlying connections.
// TODO @vgonkivs: remove after https://github.com/celestiaorg/celestia-app/issues/4304 is done.
type Network struct {
testnode.Context
config *testnode.Config
app srvtypes.Application
tmNode *node.Node
logger sdklog.Logger
stopNode func() error
stopGRPC func() error
stopAPI func() error
stopOnce sync.Once
stopErr error
}
func NewNetwork(t testing.TB, config *testnode.Config) *Network {
t.Helper()
// initialize the genesis file and validator files for the first validator.
baseDir := filepath.Join(t.TempDir(), "testnode")
err := genesis.InitFiles(baseDir, config.TmConfig, config.AppConfig, config.Genesis, 0)
require.NoError(t, err)
tmNode, app, err := testnode.NewCometNode(baseDir, &config.UniversalTestingConfig)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
cctx := testnode.NewContext(
ctx,
config.Genesis.Keyring(),
config.TmConfig,
config.Genesis.ChainID,
config.AppConfig.API.Address,
)
return &Network{
Context: cctx,
config: config,
app: app,
tmNode: tmNode,
logger: sdklog.NewTestLogger(t),
}
}
func (n *Network) Start() error {
cctx, stopNode, err := testnode.StartNode(n.tmNode, n.Context)
if err != nil {
return err
}
coreEnv, err := n.tmNode.ConfigureRPC()
if err != nil {
return err
}
grpcSrv, cctx, cleanupGRPC, err := testnode.StartGRPCServer(
sdklog.NewNopLogger(), n.app, n.config.AppConfig, cctx, coreEnv)
if err != nil {
return err
}
apiServer, err := testnode.StartAPIServer(n.app, *n.config.AppConfig, cctx, grpcSrv)
if err != nil {
return err
}
n.Context = cctx
n.stopNode = stopNode
n.stopGRPC = cleanupGRPC
n.stopAPI = apiServer.Close
return nil
}
func (n *Network) Stop() error {
n.stopOnce.Do(func() {
var errs []error
if n.stopNode != nil {
if err := n.stopNode(); err != nil {
errs = append(errs, err)
}
}
if n.stopGRPC != nil {
if err := n.stopGRPC(); err != nil {
errs = append(errs, err)
}
}
if n.stopAPI != nil {
if err := n.stopAPI(); err != nil {
errs = append(errs, err)
}
}
n.stopErr = errors.Join(errs...)
})
return n.stopErr
}