-
Notifications
You must be signed in to change notification settings - Fork 254
/
config.go
207 lines (180 loc) · 6.73 KB
/
config.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
200
201
202
203
204
205
206
207
package testnode
import (
"fmt"
"time"
"github.com/celestiaorg/celestia-app/cmd/celestia-appd/cmd"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
tmconfig "github.com/tendermint/tendermint/config"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
// Config is the configuration of a test node.
type Config struct {
// ChainID is the chain ID of the network.
ChainID string
// TmConfig is the Tendermint configuration used for the network.
TmConfig *tmconfig.Config
// AppConfig is the application configuration of the test node.
AppConfig *srvconfig.Config
// ConsensusParams are the consensus parameters of the test node.
ConsensusParams *tmproto.ConsensusParams
// AppOptions are the application options of the test node. Portions of the
// app config will automatically be set into the app option when the app
// config is set.
AppOptions *KVAppOptions
// GenesisOptions are the genesis options of the test node.
GenesisOptions []GenesisOption
// Accounts are the accounts of the test node.
Accounts []string
// AppCreator is used to create the application for the testnode.
AppCreator srvtypes.AppCreator
// SupressLogs
SupressLogs bool
}
// WithChainID sets the ChainID and returns the Config.
func (c *Config) WithChainID(s string) *Config {
c.ChainID = s
return c
}
// WithTendermintConfig sets the TmConfig and returns the *Config.
func (c *Config) WithTendermintConfig(conf *tmconfig.Config) *Config {
c.TmConfig = conf
return c
}
// WithAppConfig sets the AppConfig and returns the Config.
//
// Warning: This method will also overwrite relevant portions of the app config
// to the app options. See the SetFromAppConfig method for more information on
// which values are overwritten.
func (c *Config) WithAppConfig(conf *srvconfig.Config) *Config {
c.AppConfig = conf
c.AppOptions.SetFromAppConfig(conf)
return c
}
// WithConsensusParams sets the ConsensusParams and returns the Config.
func (c *Config) WithConsensusParams(params *tmproto.ConsensusParams) *Config {
c.ConsensusParams = params
return c
}
// WithAppOptions sets the AppOptions and returns the Config.
//
// Warning: If the app config is set after this, it could overwrite some values.
// See SetFromAppConfig for more information on which values are overwritten.
func (c *Config) WithAppOptions(opts *KVAppOptions) *Config {
c.AppOptions = opts
return c
}
// WithGenesisOptions sets the GenesisOptions and returns the Config.
func (c *Config) WithGenesisOptions(opts ...GenesisOption) *Config {
c.GenesisOptions = opts
return c
}
// WithAccounts sets the Accounts and returns the Config.
func (c *Config) WithAccounts(accs []string) *Config {
c.Accounts = accs
return c
}
// WithAppCreator sets the AppCreator and returns the Config.
func (c *Config) WithAppCreator(creator srvtypes.AppCreator) *Config {
c.AppCreator = creator
return c
}
// WithSupressLogs sets the SupressLogs and returns the Config.
func (c *Config) WithSupressLogs(sl bool) *Config {
c.SupressLogs = sl
return c
}
// WithTimeoutCommit sets the TimeoutCommit and returns the Config.
func (c *Config) WithTimeoutCommit(d time.Duration) *Config {
c.TmConfig.Consensus.TimeoutCommit = d
return c
}
func DefaultConfig() *Config {
tmcfg := DefaultTendermintConfig()
tmcfg.Consensus.TimeoutCommit = 1 * time.Millisecond
cfg := &Config{}
return cfg.
WithAccounts([]string{}).
WithChainID(tmrand.Str(6)).
WithTendermintConfig(DefaultTendermintConfig()).
WithConsensusParams(DefaultParams()).
WithAppOptions(DefaultAppOptions()).
WithAppConfig(DefaultAppConfig()).
WithGenesisOptions().
WithAppCreator(cmd.NewAppServer).
WithSupressLogs(true)
}
type KVAppOptions struct {
options map[string]interface{}
}
func NewKVAppOptions() *KVAppOptions {
return &KVAppOptions{options: make(map[string]interface{})}
}
// Get implements AppOptions
func (ao *KVAppOptions) Get(o string) interface{} {
return ao.options[o]
}
// Set adds an option to the KVAppOptions
func (ao *KVAppOptions) Set(o string, v interface{}) {
ao.options[o] = v
}
// SetMany adds an option to the KVAppOptions
func (ao *KVAppOptions) SetMany(o map[string]interface{}) {
for k, v := range o {
ao.Set(k, v)
}
}
func (ao *KVAppOptions) SetFromAppConfig(appCfg *srvconfig.Config) {
opts := map[string]interface{}{
server.FlagPruning: appCfg.Pruning,
server.FlagPruningKeepRecent: appCfg.PruningKeepRecent,
server.FlagPruningInterval: appCfg.PruningInterval,
server.FlagMinGasPrices: appCfg.MinGasPrices,
server.FlagMinRetainBlocks: appCfg.MinRetainBlocks,
server.FlagIndexEvents: appCfg.IndexEvents,
server.FlagStateSyncSnapshotInterval: appCfg.StateSync.SnapshotInterval,
server.FlagStateSyncSnapshotKeepRecent: appCfg.StateSync.SnapshotKeepRecent,
server.FlagHaltHeight: appCfg.HaltHeight,
server.FlagHaltTime: appCfg.HaltTime,
}
ao.SetMany(opts)
}
// DefaultAppOptions returns the default application options. The options are
// set using the default app config. If the app config is set after this, it
// will overwrite these values.
func DefaultAppOptions() *KVAppOptions {
opts := NewKVAppOptions()
opts.SetFromAppConfig(DefaultAppConfig())
return opts
}
func DefaultParams() *tmproto.ConsensusParams {
cparams := types.DefaultConsensusParams()
cparams.Block.TimeIotaMs = 1
cparams.Block.MaxBytes = appconsts.DefaultMaxBytes
cparams.Version.AppVersion = appconsts.LatestVersion
return cparams
}
func DefaultTendermintConfig() *tmconfig.Config {
tmCfg := tmconfig.DefaultConfig()
// Reduce the target height duration so that blocks are produced faster
// during tests.
tmCfg.Consensus.TimeoutCommit = 100 * time.Millisecond
tmCfg.Consensus.TimeoutPropose = 200 * time.Millisecond
// set the mempool's MaxTxBytes to allow the testnode to accept a
// transaction that fills the entire square. Any blob transaction larger
// than the square size will still fail no matter what.
tmCfg.Mempool.MaxTxBytes = appconsts.DefaultMaxBytes
// remove all barriers from the testnode being able to accept very large
// transactions and respond to very queries with large responses (~200MB was
// chosen only as an arbitrary large number).
tmCfg.RPC.MaxBodyBytes = 200_000_000
// set all the ports to random open ones
tmCfg.RPC.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
tmCfg.P2P.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
tmCfg.RPC.GRPCListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
return tmCfg
}