forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
141 lines (121 loc) · 3.86 KB
/
testing.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
package core
import (
"fmt"
"net"
"net/url"
"testing"
appconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/stretchr/testify/require"
tmconfig "github.com/tendermint/tendermint/config"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/celestiaorg/celestia-app/test/util/testnode"
)
// TestConfig encompasses all the configs required to run test Tendermint + Celestia App tandem.
type TestConfig struct {
ConsensusParams *tmproto.ConsensusParams
Tendermint *tmconfig.Config
App *appconfig.Config
Accounts []string
SuppressLogs bool
}
// 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() *TestConfig {
conCfg := testnode.DefaultParams()
tnCfg := testnode.DefaultTendermintConfig()
tnCfg.RPC.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", getFreePort())
tnCfg.RPC.GRPCListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", getFreePort())
tnCfg.P2P.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", getFreePort())
appCfg := testnode.DefaultAppConfig()
appCfg.GRPC.Address = fmt.Sprintf("127.0.0.1:%d", getFreePort())
appCfg.API.Address = fmt.Sprintf("tcp://127.0.0.1:%d", getFreePort())
// instructs creating funded accounts
// 10 usually is enough for testing
accounts := make([]string, 10)
for i := range accounts {
accounts[i] = tmrand.Str(9)
}
return &TestConfig{
ConsensusParams: conCfg,
Tendermint: tnCfg,
App: appCfg,
Accounts: accounts,
SuppressLogs: true,
}
}
// 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 *TestConfig) testnode.Context {
state, kr, err := testnode.DefaultGenesisState(cfg.Accounts...)
require.NoError(t, err)
tmNode, app, cctx, err := testnode.New(
t,
cfg.ConsensusParams,
cfg.Tendermint,
cfg.SuppressLogs,
state,
kr,
"private",
)
require.NoError(t, err)
cctx, cleanupCoreNode, err := testnode.StartNode(tmNode, cctx)
require.NoError(t, err)
t.Cleanup(func() {
err := cleanupCoreNode()
require.NoError(t, err)
})
cctx, cleanupGRPCServer, err := StartGRPCServer(app, cfg.App, cctx)
require.NoError(t, err)
t.Cleanup(func() {
err := cleanupGRPCServer()
require.NoError(t, err)
})
// 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 local tendermint client
// if you need to debug something inside of it
ip, port, err := getEndpoint(cfg.Tendermint)
require.NoError(t, err)
client, err := NewRemote(ip, port)
require.NoError(t, err)
err = client.Start()
require.NoError(t, err)
t.Cleanup(func() {
err := client.Stop()
require.NoError(t, err)
})
cctx.WithClient(client)
return cctx
}
func getFreePort() int {
a, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err == nil {
var l *net.TCPListener
if l, err = net.ListenTCP("tcp", a); err == nil {
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
}
panic("while getting free port: " + err.Error())
}
func getEndpoint(cfg *tmconfig.Config) (string, string, error) {
url, err := url.Parse(cfg.RPC.ListenAddress)
if err != nil {
return "", "", err
}
host, _, err := net.SplitHostPort(url.Host)
if err != nil {
return "", "", err
}
return host, url.Port(), nil
}