-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (111 loc) · 3.43 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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/tez-capital/protocol-rewards/api"
"github.com/tez-capital/protocol-rewards/configuration"
"github.com/tez-capital/protocol-rewards/constants"
"github.com/tez-capital/protocol-rewards/core"
"github.com/tez-capital/protocol-rewards/test"
"github.com/trilitech/tzgo/tezos"
)
func run_test(ctx context.Context, testFlag string, config *configuration.Runtime, cacheId *string) {
options := core.TestEngineOptions
if cacheId != nil {
var err error
options.Transport, err = test.NewTestTransport(http.DefaultTransport, *cacheId, *cacheId+".gob.lz4")
if err != nil {
slog.Error("failed to create caching transport", "error", err)
return
}
slog.Info("using caching transport", "cacheId", *cacheId)
}
engine, err := core.NewEngine(ctx, config, core.TestEngineOptions)
if err != nil {
slog.Error("failed to create engine", "error", err.Error())
os.Exit(1)
}
params := strings.Split(testFlag, ":")
if len(params) > 1 {
address := params[0]
cycle, err := strconv.ParseInt(params[1], 10, 64)
if err != nil {
slog.Error("cycle is not int", "error", err)
showTestExample()
return
}
engine.FetchDelegateDelegationState(ctx, tezos.MustParseAddress(address), cycle, 0, &core.DebugFetchOptions)
return
}
cycle, err := strconv.ParseInt(params[0], 10, 64)
if err != nil {
slog.Error("cycle is not int", "error", err)
showTestExample()
return
}
engine.FetchCycleDelegationStates(ctx, cycle, 0, &core.ForceFetchOptions)
}
func main() {
configPath := flag.String("config", "config.hjson", "path to the configuration file")
logLevel := flag.String("log", "", "set the desired log level")
isTest := flag.String("test", "", "run tests")
cacheId := flag.String("cache", "", "cache id")
versionFlag := flag.Bool("version", false, "print version")
ctx, cancel := context.WithCancel(context.Background())
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Println("\nExamples:")
fmt.Printf("%s -config <path/to/config.json>\n", os.Args[0])
fmt.Printf("%s -log <logLevel> (debug, info, warn, error)\n", os.Args[0])
fmt.Printf("%s -test <address>:<cycle> or <cycle>\n", os.Args[0])
fmt.Printf("%s -cache test/data/745 (only in combination with -test)\n", os.Args[0])
}
flag.Parse()
if *versionFlag {
fmt.Println("protocol-rewards version " + constants.VERSION)
os.Exit(0)
}
config, err := configuration.LoadConfiguration(*configPath)
if err != nil {
panic(err)
}
if *logLevel != "" {
config.LogLevel = configuration.GetLogLevel(*logLevel)
}
slog.SetLogLoggerLevel(config.LogLevel)
switch {
case *isTest != "":
run_test(ctx, *isTest, config, cacheId)
return
}
engine, err := core.NewEngine(ctx, config, core.DefaultEngineOptions)
if err != nil {
slog.Error("failed to create engine", "error", err.Error())
os.Exit(1)
}
publicApiApp := api.CreatePublicApi(config, engine)
privateApiApp := api.CreatePrivateApi(config, engine)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
publicApiApp.Shutdown()
if privateApiApp != nil {
privateApiApp.Shutdown()
}
cancel()
}
func showTestExample() {
slog.Error("check test parameters again")
fmt.Println("\nExamples:")
fmt.Printf("%s -test <address>:<cycle>\n", os.Args[0])
fmt.Printf("%s -test <cycle>\n", os.Args[0])
}