-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
182 lines (159 loc) · 5.31 KB
/
main.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
package main
import (
"allora_offchain_node/lib"
"allora_offchain_node/metrics"
usecase "allora_offchain_node/usecase"
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"syscall"
"time"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
)
func ConvertEntrypointsToInstances(userConfig lib.UserConfig) error {
/// Initialize adapters using the factory function
for i, worker := range userConfig.Worker {
if worker.InferenceEntrypointName != "" {
adapter, err := NewAlloraAdapter(worker.InferenceEntrypointName)
if err != nil {
fmt.Println("Error creating inference adapter:", err)
return err
}
userConfig.Worker[i].InferenceEntrypoint = adapter
}
if worker.ForecastEntrypointName != "" {
adapter, err := NewAlloraAdapter(worker.ForecastEntrypointName)
if err != nil {
fmt.Println("Error creating forecast adapter:", err)
return err
}
userConfig.Worker[i].ForecastEntrypoint = adapter
}
}
for i, reputer := range userConfig.Reputer {
if reputer.GroundTruthEntrypointName != "" {
adapter, err := NewAlloraAdapter(reputer.GroundTruthEntrypointName)
if err != nil {
fmt.Println("Error creating reputer adapter:", err)
return err
}
userConfig.Reputer[i].GroundTruthEntrypoint = adapter
}
}
for i, reputer := range userConfig.Reputer {
if reputer.LossFunctionEntrypointName != "" {
adapter, err := NewAlloraAdapter(reputer.LossFunctionEntrypointName)
if err != nil {
fmt.Println("Error creating reputer adapter:", err)
return err
}
userConfig.Reputer[i].LossFunctionEntrypoint = adapter
}
}
return nil
}
func main() {
// Context tree:
// root context (ctx)
// ├── NewUseCaseSuite initialization
// └── signal context (sigCtx)
// └── Spawn process
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCtx, sigCancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer sigCancel()
// Initialize logger
initLogger()
if dotErr := godotenv.Load(); dotErr != nil {
log.Info().Msg("Unable to load .env file")
}
// Set and lock sdk config
config := sdktypes.GetConfig()
config.SetBech32PrefixForAccount(lib.ADDRESS_PREFIX, lib.ADDRESS_PREFIX)
config.Seal()
log.Info().Msg("Starting allora offchain node...")
// Metrics
metrics.InitMetrics(metrics.CounterData)
metricsServer := metrics.GetMetrics()
metricsServer.StartMetricsServer(":2112")
// Load config and do modifications if needed
finalUserConfig := lib.UserConfig{} // nolint: exhaustruct
alloraJsonConfig := os.Getenv(lib.ALLORA_OFFCHAIN_NODE_CONFIG_JSON)
if alloraJsonConfig != "" {
log.Info().Msg("Config using JSON env var")
// completely reset UserConfig
err := json.Unmarshal([]byte(alloraJsonConfig), &finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse JSON config file from Config")
return
}
} else if os.Getenv(lib.ALLORA_OFFCHAIN_NODE_CONFIG_FILE_PATH) != "" {
log.Info().Msg("Config using JSON config file")
// parse file defined in CONFIG_FILE_PATH into UserConfig
file, err := os.Open(os.Getenv(lib.ALLORA_OFFCHAIN_NODE_CONFIG_FILE_PATH))
if err != nil {
log.Fatal().Err(err).Msg("Failed to open JSON config file")
return
}
defer file.Close()
decoder := json.NewDecoder(file)
// completely reset UserConfig
err = decoder.Decode(&finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse JSON config file")
return
}
} else {
log.Fatal().Msg("Could not find config file. Please create a config.json file and pass as environment variable.")
return
}
// Convert entrypoints to instances of adapters
err := ConvertEntrypointsToInstances(finalUserConfig)
if err != nil {
log.Fatal().Err(err).Msg("Failed to convert Entrypoints to instances of adapters - wrong entrypoint name?")
return
}
// Check and set defaults for the user config if any values are not set
finalUserConfig.CheckAndSetDefaults()
// Creates the ConnectionManager and initialises the NodeConfigs
connectionManager, err := lib.NewConnectionManager(sigCtx, finalUserConfig)
if err != nil {
log.Error().Err(err).Msg("Failed to initialize ConnectionManager, exiting")
return
}
// Close the ConnectionManager when the program exits
defer connectionManager.Close()
wallet, err := connectionManager.GetWallet()
if err != nil {
log.Error().Err(err).Msg("Failed to get wallet, exiting")
return
}
spawner, err := usecase.NewUseCaseSuite(sigCtx, finalUserConfig, connectionManager)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize use case, exiting")
return
}
spawner.Metrics = metricsServer // cache the metrics object for ease of access on usecase suite
log.Info().Msg("Starting spawning processes...")
go func() {
err := spawner.Spawn(sigCtx)
if err != nil {
log.Error().Err(err).Msg("Failed to spawn processes, exiting")
cancel()
}
}()
<-sigCtx.Done()
metricsServer.IncrementMetricsCounter(metrics.ApplicationFinishedCount, wallet.Address, 0)
// shutdown metrics server
log.Info().Msg("Shutting down metrics server")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if err := metricsServer.Shutdown(shutdownCtx); err != nil {
log.Error().Err(err).Msg("Error shutting down metrics server")
}
log.Info().Msg("Stopping...")
}