-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
524 lines (466 loc) · 13.8 KB
/
config_test.go
File metadata and controls
524 lines (466 loc) · 13.8 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package seiconfig
import (
"os"
"path/filepath"
"testing"
"time"
)
const testRPCAddr = "tcp://0.0.0.0:26657"
func TestDefaultForMode_AllModesValid(t *testing.T) {
modes := []NodeMode{ModeValidator, ModeFull, ModeSeed, ModeArchive}
for _, mode := range modes {
cfg := DefaultForMode(mode)
if cfg.Mode != mode {
t.Errorf("DefaultForMode(%s): got mode %s", mode, cfg.Mode)
}
if cfg.Version != CurrentVersion {
t.Errorf("DefaultForMode(%s): got version %d, want %d", mode, cfg.Version, CurrentVersion)
}
result := Validate(cfg)
if result.HasErrors() {
t.Errorf("DefaultForMode(%s) produced validation errors: %v", mode, result.Errors())
}
}
}
func TestDefaultForMode_ValidatorDisablesServices(t *testing.T) {
cfg := DefaultForMode(ModeValidator)
if cfg.API.REST.Enable {
t.Error("validator should have REST API disabled")
}
if cfg.API.GRPC.Enable {
t.Error("validator should have gRPC disabled")
}
if cfg.EVM.HTTPEnabled {
t.Error("validator should have EVM HTTP disabled")
}
if cfg.EVM.WSEnabled {
t.Error("validator should have EVM WS disabled")
}
if cfg.Storage.StateStore.Enable {
t.Error("validator should have state store disabled")
}
}
func TestDefaultForMode_SeedHighConnections(t *testing.T) {
cfg := DefaultForMode(ModeSeed)
if cfg.Network.P2P.MaxConnections != 1000 {
t.Errorf("seed max_connections: got %d, want 1000", cfg.Network.P2P.MaxConnections)
}
if !cfg.Network.P2P.AllowDuplicateIP {
t.Error("seed should allow duplicate IPs")
}
if cfg.Storage.PruningStrategy != PruningEverything {
t.Errorf("seed pruning: got %s, want everything", cfg.Storage.PruningStrategy)
}
}
func TestDefaultForMode_ArchiveKeepsAll(t *testing.T) {
cfg := DefaultForMode(ModeArchive)
if cfg.Storage.PruningStrategy != PruningNothing {
t.Errorf("archive pruning: got %s, want nothing", cfg.Storage.PruningStrategy)
}
if cfg.Storage.StateStore.KeepRecent != 0 {
t.Errorf("archive state_store.keep_recent: got %d, want 0", cfg.Storage.StateStore.KeepRecent)
}
if cfg.Chain.MinRetainBlocks != 0 {
t.Errorf("archive min_retain_blocks: got %d, want 0", cfg.Chain.MinRetainBlocks)
}
if cfg.EVM.MaxTraceLookbackBlocks != -1 {
t.Errorf("archive max_trace_lookback_blocks: got %d, want -1", cfg.EVM.MaxTraceLookbackBlocks)
}
}
func TestDefaultForMode_FullEnablesServices(t *testing.T) {
cfg := DefaultForMode(ModeFull)
if !cfg.API.REST.Enable {
t.Error("full should have REST API enabled")
}
if !cfg.API.GRPC.Enable {
t.Error("full should have gRPC enabled")
}
if !cfg.EVM.HTTPEnabled {
t.Error("full should have EVM HTTP enabled")
}
if cfg.Network.RPC.ListenAddress != testRPCAddr {
t.Errorf("full RPC listen: got %s, want %s", cfg.Network.RPC.ListenAddress, testRPCAddr)
}
}
func TestValidate_InvalidMode(t *testing.T) {
cfg := Default()
cfg.Mode = "bogus"
result := Validate(cfg)
if !result.HasErrors() {
t.Error("expected error for invalid mode")
}
}
func TestValidate_EmptyMinGasPrices(t *testing.T) {
cfg := Default()
cfg.Chain.MinGasPrices = ""
result := Validate(cfg)
if !result.HasErrors() {
t.Error("expected error for empty min_gas_prices")
}
}
func TestValidate_InvalidPruningStrategy(t *testing.T) {
cfg := Default()
cfg.Storage.PruningStrategy = "aggressive"
result := Validate(cfg)
if !result.HasErrors() {
t.Error("expected error for invalid pruning strategy")
}
}
func TestValidate_PruningEverythingWithSnapshots(t *testing.T) {
cfg := Default()
cfg.Storage.PruningStrategy = PruningEverything
cfg.Storage.SnapshotInterval = 1000
result := Validate(cfg)
if !result.HasErrors() {
t.Error("expected error for snapshots with everything pruning")
}
}
func TestValidate_InvalidLogFormat(t *testing.T) {
cfg := Default()
cfg.Logging.Format = "xml"
result := Validate(cfg)
if !result.HasErrors() {
t.Error("expected error for invalid log format")
}
}
func TestValidate_EVMOnValidator(t *testing.T) {
cfg := DefaultForMode(ModeValidator)
cfg.EVM.HTTPEnabled = true
result := Validate(cfg)
hasWarning := false
for _, d := range result.Diagnostics {
if d.Severity == SeverityWarning && d.Field == "evm" {
hasWarning = true
break
}
}
if !hasWarning {
t.Error("expected warning for EVM on validator")
}
}
func TestWriteReadRoundTrip(t *testing.T) {
dir := t.TempDir()
original := DefaultForMode(ModeFull)
// Note: ChainID is stored in genesis.json, not config.toml/app.toml,
// so it does not round-trip through the legacy two-file format.
original.Chain.Moniker = "test-node"
original.EVM.HTTPPort = 9545
original.Storage.StateStore.KeepRecent = 50000
if err := WriteConfigToDir(original, dir); err != nil {
t.Fatalf("WriteConfigToDir: %v", err)
}
configPath := filepath.Join(dir, "config", "config.toml")
appPath := filepath.Join(dir, "config", "app.toml")
if _, err := os.Stat(configPath); err != nil {
t.Fatalf("config.toml not created: %v", err)
}
if _, err := os.Stat(appPath); err != nil {
t.Fatalf("app.toml not created: %v", err)
}
loaded, err := ReadConfigFromDir(dir)
if err != nil {
t.Fatalf("ReadConfigFromDir: %v", err)
}
if loaded.Chain.Moniker != "test-node" {
t.Errorf("moniker: got %q, want %q", loaded.Chain.Moniker, "test-node")
}
if loaded.EVM.HTTPPort != 9545 {
t.Errorf("evm.http_port: got %d, want 9545", loaded.EVM.HTTPPort)
}
if loaded.Storage.StateStore.KeepRecent != 50000 {
t.Errorf("state_store.keep_recent: got %d, want 50000", loaded.Storage.StateStore.KeepRecent)
}
if loaded.Network.RPC.ListenAddress != testRPCAddr {
t.Errorf("rpc.listen_address: got %q", loaded.Network.RPC.ListenAddress)
}
}
func TestWriteReadRoundTrip_AllModes(t *testing.T) {
modes := []NodeMode{ModeValidator, ModeFull, ModeSeed, ModeArchive}
for _, mode := range modes {
t.Run(string(mode), func(t *testing.T) {
dir := t.TempDir()
original := DefaultForMode(mode)
if err := WriteConfigToDir(original, dir); err != nil {
t.Fatalf("WriteConfigToDir: %v", err)
}
loaded, err := ReadConfigFromDir(dir)
if err != nil {
t.Fatalf("ReadConfigFromDir: %v", err)
}
if loaded.Chain.MinGasPrices != original.Chain.MinGasPrices {
t.Errorf("min_gas_prices: got %q, want %q",
loaded.Chain.MinGasPrices, original.Chain.MinGasPrices)
}
if loaded.Storage.PruningStrategy != original.Storage.PruningStrategy {
t.Errorf("pruning: got %q, want %q",
loaded.Storage.PruningStrategy, original.Storage.PruningStrategy)
}
})
}
}
func TestApplyOverrides(t *testing.T) {
cfg := Default()
overrides := map[string]string{
"evm.http_port": "9545",
"chain.min_gas_prices": "0.1usei",
"storage.pruning": "custom",
}
if err := ApplyOverrides(cfg, overrides); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if cfg.EVM.HTTPPort != 9545 {
t.Errorf("evm.http_port: got %d, want 9545", cfg.EVM.HTTPPort)
}
if cfg.Chain.MinGasPrices != "0.1usei" {
t.Errorf("chain.min_gas_prices: got %q, want %q", cfg.Chain.MinGasPrices, "0.1usei")
}
if cfg.Storage.PruningStrategy != "custom" {
t.Errorf("storage.pruning: got %q, want %q", cfg.Storage.PruningStrategy, "custom")
}
}
func TestApplyOverrides_Bool(t *testing.T) {
cfg := Default()
if err := ApplyOverrides(cfg, map[string]string{
"network.p2p.allow_duplicate_ip": "true",
}); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if !cfg.Network.P2P.AllowDuplicateIP {
t.Error("expected AllowDuplicateIP to be true")
}
if err := ApplyOverrides(cfg, map[string]string{
"network.p2p.allow_duplicate_ip": "false",
}); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if cfg.Network.P2P.AllowDuplicateIP {
t.Error("expected AllowDuplicateIP to be false")
}
}
func TestApplyOverrides_Uint(t *testing.T) {
cfg := Default()
if err := ApplyOverrides(cfg, map[string]string{
"chain.halt_height": "999999",
}); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if cfg.Chain.HaltHeight != 999999 {
t.Errorf("halt_height: got %d, want 999999", cfg.Chain.HaltHeight)
}
}
func TestApplyOverrides_Float(t *testing.T) {
cfg := Default()
if err := ApplyOverrides(cfg, map[string]string{
"mempool.drop_priority_threshold": "0.75",
}); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if cfg.Mempool.DropPriorityThreshold != 0.75 {
t.Errorf("drop_priority_threshold: got %f, want 0.75", cfg.Mempool.DropPriorityThreshold)
}
}
func TestApplyOverrides_Duration(t *testing.T) {
cfg := Default()
if err := ApplyOverrides(cfg, map[string]string{
"network.rpc.timeout_broadcast_tx_commit": "30s",
}); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if cfg.Network.RPC.TimeoutBroadcastTxCommit.Duration != 30*time.Second {
t.Errorf("timeout_broadcast_tx_commit: got %v, want 30s",
cfg.Network.RPC.TimeoutBroadcastTxCommit.Duration)
}
}
func TestApplyOverrides_Int64(t *testing.T) {
cfg := Default()
if err := ApplyOverrides(cfg, map[string]string{
"state_sync.backfill_blocks": "500",
}); err != nil {
t.Fatalf("ApplyOverrides: %v", err)
}
if cfg.StateSync.BackfillBlocks != 500 {
t.Errorf("backfill_blocks: got %d, want 500", cfg.StateSync.BackfillBlocks)
}
}
func TestApplyOverrides_UnknownKey(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"totally.fake.key": "value",
})
if err == nil {
t.Fatal("expected error for unknown key")
}
}
func TestApplyOverrides_InvalidBool(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"network.p2p.allow_duplicate_ip": "maybe",
})
if err == nil {
t.Fatal("expected error for invalid bool value")
}
}
func TestApplyOverrides_InvalidInt(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"evm.http_port": "not_a_number",
})
if err == nil {
t.Fatal("expected error for non-numeric int value")
}
}
func TestApplyOverrides_InvalidDuration(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"network.rpc.timeout_broadcast_tx_commit": "not_a_duration",
})
if err == nil {
t.Fatal("expected error for invalid duration value")
}
}
func TestApplyOverrides_Uint16Overflow(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"network.p2p.max_connections": "70000",
})
if err == nil {
t.Fatal("expected error for uint16 overflow (65535 max)")
}
}
func TestApplyOverrides_Int32Overflow(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"state_sync.fetchers": "3000000000",
})
if err == nil {
t.Fatal("expected error for int32 overflow")
}
}
func TestApplyOverrides_NegativeUint(t *testing.T) {
cfg := Default()
err := ApplyOverrides(cfg, map[string]string{
"chain.halt_height": "-1",
})
if err == nil {
t.Fatal("expected error for negative uint value")
}
}
func TestApplyOverrides_Empty(t *testing.T) {
cfg := Default()
original := cfg.EVM.HTTPPort
if err := ApplyOverrides(cfg, nil); err != nil {
t.Fatalf("ApplyOverrides(nil): %v", err)
}
if cfg.EVM.HTTPPort != original {
t.Error("nil overrides should not change config")
}
}
func TestResolveEnv(t *testing.T) {
cfg := Default()
t.Setenv("SEI_CHAIN_MIN_GAS_PRICES", "0.5usei")
warnings := ResolveEnv(cfg)
if cfg.Chain.MinGasPrices != "0.5usei" {
t.Errorf("after ResolveEnv: got %q, want %q", cfg.Chain.MinGasPrices, "0.5usei")
}
for _, w := range warnings {
t.Logf("warning: %s", w)
}
}
func TestResolveEnv_LegacyPrefix(t *testing.T) {
cfg := Default()
t.Setenv("SEID_CHAIN_MIN_GAS_PRICES", "0.3usei")
warnings := ResolveEnv(cfg)
if cfg.Chain.MinGasPrices != "0.3usei" {
t.Errorf("after ResolveEnv with SEID_: got %q, want %q", cfg.Chain.MinGasPrices, "0.3usei")
}
hasDeprecation := false
for _, w := range warnings {
if w != "" {
hasDeprecation = true
}
}
if !hasDeprecation {
t.Error("expected deprecation warning for SEID_ prefix")
}
}
func TestResolveEnv_SEIPrecedence(t *testing.T) {
cfg := Default()
t.Setenv("SEI_CHAIN_MIN_GAS_PRICES", "0.5usei")
t.Setenv("SEID_CHAIN_MIN_GAS_PRICES", "0.3usei")
ResolveEnv(cfg)
if cfg.Chain.MinGasPrices != "0.5usei" {
t.Errorf("SEI_ should take precedence: got %q, want %q", cfg.Chain.MinGasPrices, "0.5usei")
}
}
func TestDuration_MarshalUnmarshal(t *testing.T) {
d := Dur(10 * time.Second)
text, err := d.MarshalText()
if err != nil {
t.Fatalf("MarshalText: %v", err)
}
if string(text) != "10s" {
t.Errorf("MarshalText: got %q, want %q", string(text), "10s")
}
var d2 Duration
if err := d2.UnmarshalText(text); err != nil {
t.Fatalf("UnmarshalText: %v", err)
}
if d2.Duration != d.Duration {
t.Errorf("round-trip: got %v, want %v", d2.Duration, d.Duration)
}
}
func TestDuration_InvalidParse(t *testing.T) {
var d Duration
if err := d.UnmarshalText([]byte("not-a-duration")); err == nil {
t.Error("expected error for invalid duration")
}
}
func TestNodeMode_Validity(t *testing.T) {
tests := []struct {
mode NodeMode
valid bool
}{
{ModeValidator, true},
{ModeFull, true},
{ModeSeed, true},
{ModeArchive, true},
{"rpc", false},
{"indexer", false},
{"bogus", false},
{"", false},
}
for _, tt := range tests {
if got := tt.mode.IsValid(); got != tt.valid {
t.Errorf("NodeMode(%q).IsValid() = %v, want %v", tt.mode, got, tt.valid)
}
}
}
func TestNodeMode_IsFullnodeType(t *testing.T) {
fullnodeTypes := []NodeMode{ModeFull, ModeArchive}
for _, m := range fullnodeTypes {
if !m.IsFullnodeType() {
t.Errorf("%s should be fullnode type", m)
}
}
nonFullnodeTypes := []NodeMode{ModeValidator, ModeSeed}
for _, m := range nonFullnodeTypes {
if m.IsFullnodeType() {
t.Errorf("%s should not be fullnode type", m)
}
}
}
func TestWriteMode_Validity(t *testing.T) {
if !WriteModeCosmosOnly.IsValid() {
t.Error("cosmos_only should be valid")
}
if WriteMode("invalid").IsValid() {
t.Error("'invalid' should not be valid")
}
}
func TestLegacyTendermintMode_ArchiveMapped(t *testing.T) {
cfg := DefaultForMode(ModeArchive)
tm := cfg.toLegacyTendermint()
if tm.Mode != "full" {
t.Errorf("archive should map to tendermint mode 'full', got %q", tm.Mode)
}
}