-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
126 lines (106 loc) · 2.95 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
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
package main
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/cartesi/rollups-espresso-reader/internal/config"
"github.com/cartesi/rollups-espresso-reader/internal/espressoreader"
"github.com/cartesi/rollups-espresso-reader/internal/model"
"github.com/cartesi/rollups-espresso-reader/internal/repository"
"github.com/cartesi/rollups-espresso-reader/internal/repository/factory"
"github.com/cartesi/rollups-espresso-reader/internal/services/retry"
"github.com/cartesi/rollups-espresso-reader/internal/services/startup"
"github.com/spf13/cobra"
)
var (
// Should be overridden during the final release build with ldflags
// to contain the actual version number
buildVersion = "devel"
)
const (
CMD_NAME = "espresso-sequencer"
)
var Cmd = &cobra.Command{
Use: CMD_NAME,
Short: "Runs Espresso Reader",
Long: `Runs Espresso Reader`,
Run: run,
}
type loadNodeConfigArgs struct {
ctx context.Context
database repository.Repository
configKey string
}
func run(cmd *cobra.Command, args []string) {
startTime := time.Now()
ctx := cmd.Context()
c := config.FromEnv()
// setup log
startup.ConfigLogs(c.LogLevel, c.LogPrettyEnabled)
slog.Info("Starting the Cartesi Rollups Node Espresso Reader", "config", c)
// connect to database
database, err := factory.NewRepositoryFromConnectionString(ctx, c.PostgresEndpoint.Value)
if err != nil {
slog.Error("Espresso Reader couldn't connect to the database", "error", err)
os.Exit(1)
}
// load node configuration
max_attempts := 10
var config *model.NodeConfig[model.NodeConfigValue]
config, err = retry.CallFunctionWithRetryPolicy(
loadNodeConfig,
loadNodeConfigArgs{
ctx: ctx,
database: database,
configKey: model.BaseConfigKey,
},
c.MaxRetries,
c.MaxDelay,
"Main::LoadNodeConfig",
)
if err != nil {
slog.Error(fmt.Sprintf("Failed to load configuration after %d attempts", max_attempts), "error", err)
os.Exit(1)
}
// create Espresso Reader Service
service := espressoreader.NewEspressoReaderService(
c.BlockchainHttpEndpoint.Value,
c.BlockchainWsEndpoint.Value,
database,
c.EspressoBaseUrl,
c.EspressoStartingBlock,
c.EspressoNamespace,
config.Value.ChainID,
config.Value.InputBoxDeploymentBlock,
c.EspressoServiceEndpoint,
c.MaxRetries,
c.MaxDelay,
)
// logs startup time
ready := make(chan struct{}, 1)
go func() {
select {
case <-ready:
duration := time.Since(startTime)
slog.Info("EVM Reader is ready", "after", duration)
case <-ctx.Done():
}
}()
// start service
if err := service.Start(ctx, ready); err != nil {
slog.Error("Espresso Reader exited with an error", "error", err)
os.Exit(1)
}
}
func loadNodeConfig(args loadNodeConfigArgs) (*model.NodeConfig[model.NodeConfigValue], error) {
return repository.LoadNodeConfig[model.NodeConfigValue](args.ctx, args.database, args.configKey)
}
func main() {
err := Cmd.Execute()
if err != nil {
os.Exit(1)
}
}