-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathdefaults.go
More file actions
105 lines (95 loc) · 3.14 KB
/
defaults.go
File metadata and controls
105 lines (95 loc) · 3.14 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
package config
import (
"math/rand"
"os"
"path/filepath"
"time"
)
const (
// ConfigFileName is the base name of the evolve configuration file without extension.
ConfigFileName = "evnode"
// ConfigExtension is the file extension for the configuration file without the leading dot.
ConfigExtension = "yaml"
// ConfigPath is the filename for the evolve configuration file.
ConfigName = ConfigFileName + "." + ConfigExtension
// AppConfigDir is the directory name for the app configuration.
AppConfigDir = "config"
)
// DefaultRootDir returns the default root directory for evolve
var DefaultRootDir = DefaultRootDirWithName(ConfigFileName)
// DefaultRootDirWithName returns the default root directory for an application,
// based on the app name and the user's home directory
func DefaultRootDirWithName(appName string) string {
if appName == "" {
appName = ConfigFileName
}
home, err := os.UserHomeDir()
if err != nil {
return ""
}
return filepath.Join(home, "."+appName)
}
// calculateReadinessMaxBlocksBehind calculates how many blocks represent the readiness window
// based on the given block time and window duration in seconds. This allows for normal
// batch-sync latency while detecting stuck nodes.
func calculateReadinessMaxBlocksBehind(blockTime time.Duration, windowSeconds uint64) uint64 {
if blockTime == 0 {
return 30 // fallback to safe default if blockTime is not set
}
if windowSeconds == 0 {
windowSeconds = 15 // fallback to default 15s window
}
return uint64(time.Duration(windowSeconds) * time.Second / blockTime)
}
// DefaultConfig keeps default values of NodeConfig
func DefaultConfig() Config {
defaultBlockTime := DurationWrapper{1 * time.Second}
defaultReadinessWindowSeconds := uint64(15)
return Config{
RootDir: DefaultRootDir,
DBPath: "data",
P2P: P2PConfig{
ListenAddress: "/ip4/0.0.0.0/tcp/7676",
Peers: "",
},
Node: NodeConfig{
Aggregator: false,
BlockTime: defaultBlockTime,
LazyMode: false,
LazyBlockInterval: DurationWrapper{60 * time.Second},
Light: false,
ReadinessWindowSeconds: defaultReadinessWindowSeconds,
ReadinessMaxBlocksBehind: calculateReadinessMaxBlocksBehind(defaultBlockTime.Duration, defaultReadinessWindowSeconds),
},
DA: DAConfig{
Address: "http://localhost:7980",
BlockTime: DurationWrapper{6 * time.Second},
MaxSubmitAttempts: 30,
Namespace: randString(10),
DataNamespace: "",
},
Instrumentation: DefaultInstrumentationConfig(),
Log: LogConfig{
Level: "info",
Format: "text",
Trace: false,
},
Signer: SignerConfig{
SignerType: "file",
SignerPath: "config",
},
RPC: RPCConfig{
Address: "127.0.0.1:7331",
EnableDAVisualization: false,
},
}
}
func randString(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
result := make([]byte, length)
rng := rand.New(rand.NewSource(time.Now().Unix())) //nolint:gosec // even half random is good enough here.
for i := range result {
result[i] = charset[rng.Intn(len(charset))]
}
return string(result)
}