-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
137 lines (117 loc) · 4.96 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
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=models/models.cfg.yaml api/openapi.yaml
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"strconv"
"github.com/Taraxa-project/taraxa-indexer/api"
"github.com/Taraxa-project/taraxa-indexer/internal/common"
"github.com/Taraxa-project/taraxa-indexer/internal/indexer"
"github.com/Taraxa-project/taraxa-indexer/internal/logging"
"github.com/Taraxa-project/taraxa-indexer/internal/metrics"
"github.com/Taraxa-project/taraxa-indexer/internal/storage/pebble"
migration "github.com/Taraxa-project/taraxa-indexer/internal/storage/pebble/migrations"
"github.com/labstack/echo/v4"
echomiddleware "github.com/oapi-codegen/echo-middleware"
log "github.com/sirupsen/logrus"
)
var (
http_port *int
metrics_port *int
blockchain_ws *string
data_dir *string
log_level *string
yield_saving_interval *int
validators_yield_saving_interval *int
sync_queue_limit *int
)
func init() {
http_port = flag.Int("http_port", 8080, "port to listen")
metrics_port = flag.Int("metrics_port", 2112, "metrics http port")
blockchain_ws = flag.String("blockchain_ws", "wss://ws.testnet.taraxa.io", "ws url to connect to blockchain")
data_dir = flag.String("data_dir", "./data", "path to directory where indexer database will be saved")
log_level = flag.String("log_level", "info", "minimum log level. could be only [trace, debug, info, warn, error, fatal]")
yield_saving_interval = flag.Int("yield_saving_interval", 150000, "interval for saving total yield")
validators_yield_saving_interval = flag.Int("validators_yield_saving_interval", 150000, "interval for saving validators yield")
sync_queue_limit = flag.Int("sync_queue_limit", 10, "limit of blocks in the sync queue")
flag.Parse()
logging.Config(filepath.Join(*data_dir, "logs"), *log_level)
log.Print("\n\n\n")
log.WithFields(log.Fields{
"http_port": *http_port,
"blockchain_ws": *blockchain_ws,
"data_dir": *data_dir,
"log_level": *log_level}).
Info("Application initialized")
}
func setupCloseHandler(fn func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGABRT)
go func() {
<-c
fn()
os.Exit(0)
}()
}
func main() {
log.Info("Starting Taraxa Indexer")
st := pebble.NewStorage(filepath.Join(*data_dir, "db"))
setupCloseHandler(func() { st.Close() })
fin := st.GetFinalizationData()
// fromKey := storage.FormatIntToKey(fin.PbftCount - uint64(distributionFrequency))
// stats_map := make(map[uint64]*storage.RewardsStats)
// st.ForEachFromKey([]byte(pebble.GetPrefix(storage.RewardsStats{})), []byte{}, func(key, res []byte) (stop bool) {
// rs := new(storage.RewardsStats)
// err := rlp.DecodeBytes(res, rs)
// if err != nil {
// log.WithError(err).Fatal("Error decoding data from db")
// }
// stats_map[common.ParseUInt(strings.TrimLeft(string(key)[3:], "0"))] = rs
// // pr := r.rewardsFromStats(totalStake, rs)
// // for validator, reward := range pr.ValidatorRewards {
// // if intervalRewards.ValidatorRewards[validator] == nil {
// // intervalRewards.ValidatorRewards[validator] = big.NewInt(0)
// // }
// // intervalRewards.ValidatorRewards[validator].Add(intervalRewards.ValidatorRewards[validator], reward)
// // }
// // intervalRewards.TotalReward.Add(intervalRewards.TotalReward, pr.TotalReward)
// // intervalRewards.BlockFee.Add(intervalRewards.BlockFee, pr.BlockFee)
// // r.batch.Remove(key)
// return false
// })
// smj, _ := json.Marshal(stats_map)
// fmt.Println(string(smj))
// return
swagger, err := api.GetSwagger()
if err != nil {
log.WithError(err).Fatal("Error loading swagger spec")
}
manager := migration.NewManager(st, *blockchain_ws)
err = manager.ApplyAll()
if err != nil {
log.WithError(err).Fatal("Error applying migrations")
}
swagger.Servers = nil
e := echo.New()
e.Use(echomiddleware.OapiRequestValidator(swagger))
// Add http error handler to return a proper error JSON on request error
e.HTTPErrorHandler = func(err error, ctx echo.Context) {
_ = ctx.JSON(http.StatusInternalServerError, map[string]any{"message": err.Error()})
}
c := common.DefaultConfig()
c.TotalYieldSavingInterval = uint64(*yield_saving_interval)
c.ValidatorsYieldSavingInterval = uint64(*validators_yield_saving_interval)
c.SyncQueueLimit = uint64(*sync_queue_limit)
log.WithFields(log.Fields{"pbft_count": fin.PbftCount, "dag_count": fin.DagCount, "trx_count": fin.TrxCount}).Info("Loaded db with")
apiHandler := api.NewApiHandler(st, c)
api.RegisterHandlers(e, apiHandler)
go indexer.MakeAndRun(*blockchain_ws, st, c)
// start a http server for prometheus on a separate go routine
go metrics.RunPrometheusServer(":" + strconv.FormatInt(int64(*metrics_port), 10))
err = e.Start(":" + strconv.FormatInt(int64(*http_port), 10))
log.WithError(err).Fatal("Can't start http server")
}