/
full_node.go
199 lines (163 loc) · 6.58 KB
/
full_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
193
194
195
196
197
198
199
package testnode
import (
"context"
"encoding/json"
"net"
"os"
"path/filepath"
"testing"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proxy"
dbm "github.com/tendermint/tm-db"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/test/util/testfactory"
qgbtypes "github.com/celestiaorg/celestia-app/x/qgb/types"
)
// NewCometNode creates a ready to use comet node that operates a single
// validator celestia-app network. It expects that all configuration files are
// already initialized and saved to the baseDir.
func NewCometNode(t testing.TB, baseDir string, cfg *Config) (*node.Node, srvtypes.Application, error) {
var logger log.Logger
if cfg.SupressLogs {
logger = log.NewNopLogger()
} else {
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger = log.NewFilter(logger, log.AllowError())
}
dbPath := filepath.Join(cfg.TmConfig.RootDir, "data")
db, err := dbm.NewGoLevelDB("application", dbPath)
require.NoError(t, err)
cfg.AppOptions.Set(flags.FlagHome, baseDir)
app := cfg.AppCreator(logger, db, nil, cfg.AppOptions)
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.TmConfig.NodeKeyFile())
if err != nil {
return nil, nil, err
}
tmNode, err := node.NewNode(
cfg.TmConfig,
privval.LoadOrGenFilePV(cfg.TmConfig.PrivValidatorKeyFile(), cfg.TmConfig.PrivValidatorStateFile()),
nodeKey,
proxy.NewLocalClientCreator(app),
node.DefaultGenesisDocProviderFunc(cfg.TmConfig),
node.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.TmConfig.Instrumentation),
logger,
)
return tmNode, app, err
}
// InitFiles initializes the files for a new tendermint node with the provided
// genesis state and consensus parameters. The provided keyring is used to
// create a validator key and the chainID is used to initialize the genesis
// state. The keyring is returned with the validator account added.
func InitFiles(
t testing.TB,
cparams *tmproto.ConsensusParams,
tmCfg *config.Config,
genState map[string]json.RawMessage,
kr keyring.Keyring,
chainID string,
) (string, keyring.Keyring, error) {
baseDir, err := initFileStructure(t, tmCfg)
if err != nil {
return baseDir, kr, err
}
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
nodeID, pubKey, err := genutil.InitializeNodeValidatorFiles(tmCfg)
if err != nil {
return baseDir, kr, err
}
err = createValidator(kr, encCfg, pubKey, "validator", nodeID, chainID, baseDir)
if err != nil {
return baseDir, kr, err
}
err = initGenFiles(cparams, genState, encCfg.Codec, tmCfg.GenesisFile(), chainID)
if err != nil {
return baseDir, kr, err
}
return baseDir, kr, collectGenFiles(tmCfg, encCfg, pubKey, nodeID, chainID, baseDir)
}
// DefaultGenesisState returns a default genesis state and a keyring with
// accounts that have coins. It adds a default "validator" account that is
// funded and used for the valop address of the single validator. The keyring
// accounts are based on the fundedAccounts parameter.
func DefaultGenesisState(fundedAccounts ...string) (map[string]json.RawMessage, keyring.Keyring, error) {
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
state := app.ModuleBasics.DefaultGenesis(encCfg.Codec)
fundedAccounts = append(fundedAccounts, "validator")
kr, bankBals, authAccs := testfactory.FundKeyringAccounts(fundedAccounts...)
// set the accounts in the genesis state
var authGenState authtypes.GenesisState
encCfg.Codec.MustUnmarshalJSON(state[authtypes.ModuleName], &authGenState)
accounts, err := authtypes.PackAccounts(authAccs)
if err != nil {
return nil, nil, err
}
authGenState.Accounts = append(authGenState.Accounts, accounts...)
state[authtypes.ModuleName] = encCfg.Codec.MustMarshalJSON(&authGenState)
// set the balances in the genesis state
var bankGenState banktypes.GenesisState
encCfg.Codec.MustUnmarshalJSON(state[banktypes.ModuleName], &bankGenState)
bankGenState.Balances = append(bankGenState.Balances, bankBals...)
state[banktypes.ModuleName] = encCfg.Codec.MustMarshalJSON(&bankGenState)
// use the minimum data commitment window (100)
var qgbGenState qgbtypes.GenesisState
encCfg.Codec.MustUnmarshalJSON(state[qgbtypes.ModuleName], &qgbGenState)
qgbGenState.Params.DataCommitmentWindow = qgbtypes.MinimumDataCommitmentWindow
state[qgbtypes.ModuleName] = encCfg.Codec.MustMarshalJSON(&qgbGenState)
return state, kr, nil
}
// NewNetwork starts a single valiator celestia-app network using the provided
// configurations. Configured accounts will be funded and their keys can be
// accessed in keyring returned client.Context. All rpc, p2p, and grpc addresses
// in the provided configs are overwritten to use open ports. The node can be
// accessed via the returned client.Context or via the returned rpc and grpc
// addresses. Configured genesis options will be applied after all accounts have
// been initialized.
func NewNetwork(t testing.TB, cfg *Config) (cctx Context, rpcAddr, grpcAddr string) {
t.Helper()
genState, kr, err := DefaultGenesisState(cfg.Accounts...)
require.NoError(t, err)
for _, opt := range cfg.GenesisOptions {
genState = opt(genState)
}
chainID := cfg.ChainID
baseDir, kr, err := InitFiles(t, cfg.ConsensusParams, cfg.TmConfig, genState, kr, chainID)
require.NoError(t, err)
tmNode, app, err := NewCometNode(t, baseDir, cfg)
require.NoError(t, err)
cctx = NewContext(context.TODO(), kr, cfg.TmConfig, chainID)
cctx, stopNode, err := StartNode(tmNode, cctx)
require.NoError(t, err)
cctx, cleanupGRPC, err := StartGRPCServer(app, cfg.AppConfig, cctx)
require.NoError(t, err)
t.Cleanup(func() {
t.Log("tearing down testnode")
require.NoError(t, stopNode())
require.NoError(t, cleanupGRPC())
})
return cctx, cfg.TmConfig.RPC.ListenAddress, cfg.AppConfig.GRPC.Address
}
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())
}