-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
185 lines (158 loc) · 5.9 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
183
184
185
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/deso-protocol/core/lib"
"github.com/deso-protocol/postgres-data-handler/handler"
"github.com/deso-protocol/postgres-data-handler/migrations/initial_migrations"
"github.com/deso-protocol/postgres-data-handler/migrations/post_sync_migrations"
"github.com/deso-protocol/state-consumer/consumer"
"github.com/golang/glog"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/spf13/viper"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/profiler"
)
func main() {
// Initialize flags and get config values.
setupFlags()
pgURI, stateChangeDir, consumerProgressDir, batchBytes, threadLimit, logQueries, readOnlyUserPassword,
explorerStatistics, datadogProfiler, isTestnet, isRegtest, isAcceleratedRegtest, syncMempool := getConfigValues()
// Print all the config values in a single printf call broken up
// with newlines and make it look pretty both printed out and in code
glog.Infof(`
PostgresDataHandler Config Values:
---------------------------------
DB_HOST: %s
DB_PORT: %s
DB_USERNAME: %s
STATE_CHANGE_DIR: %s
CONSUMER_PROGRESS_DIR: %s
BATCH_BYTES: %d
THREAD_LIMIT: %d
LOG_QUERIES: %t
CALCULATE_EXPLORER_STATISTICS: %t
DATA_DOG_PROFILER: %t
TESTNET: %t
`, viper.GetString("DB_HOST"), viper.GetString("DB_PORT"),
viper.GetString("DB_USERNAME"),
stateChangeDir, consumerProgressDir, batchBytes, threadLimit,
logQueries, explorerStatistics, datadogProfiler, isTestnet)
// Initialize the DB.
db, err := setupDb(pgURI, threadLimit, logQueries, readOnlyUserPassword, explorerStatistics)
if err != nil {
glog.Fatalf("Error setting up DB: %v", err)
}
// Setup profiler if enabled.
if datadogProfiler {
tracer.Start()
err = profiler.Start(profiler.WithProfileTypes(profiler.CPUProfile, profiler.BlockProfile, profiler.MutexProfile, profiler.GoroutineProfile, profiler.HeapProfile))
if err != nil {
glog.Fatal(err)
}
}
params := &lib.DeSoMainnetParams
if isTestnet {
params = &lib.DeSoTestnetParams
if isRegtest {
params.EnableRegtest(isAcceleratedRegtest)
}
}
lib.GlobalDeSoParams = *params
cachedEntries, err := lru.New[string, []byte](int(handler.EntryCacheSize))
if err != nil {
glog.Fatalf("Error creating LRU cache: %v", err)
}
// Initialize and run a state syncer consumer.
stateSyncerConsumer := &consumer.StateSyncerConsumer{}
err = stateSyncerConsumer.InitializeAndRun(
stateChangeDir,
consumerProgressDir,
batchBytes,
threadLimit,
syncMempool,
&handler.PostgresDataHandler{
DB: db,
Params: params,
CachedEntries: cachedEntries,
},
)
if err != nil {
glog.Fatal(err)
}
}
func setupFlags() {
// Set glog flags
flag.Set("log_dir", viper.GetString("log_dir"))
flag.Set("v", viper.GetString("glog_v"))
flag.Set("vmodule", viper.GetString("glog_vmodule"))
flag.Set("alsologtostderr", "true")
flag.Parse()
glog.CopyStandardLogTo("INFO")
viper.SetConfigFile(".env")
viper.ReadInConfig()
viper.AutomaticEnv()
}
func getConfigValues() (pgURI string, stateChangeDir string, consumerProgressDir string, batchBytes uint64, threadLimit int, logQueries bool, readonlyUserPassword string, explorerStatistics bool, datadogProfiler bool, isTestnet bool, isRegtest bool, isAcceleratedRegtest bool, syncMempool bool) {
dbHost := viper.GetString("DB_HOST")
dbPort := viper.GetString("DB_PORT")
dbUsername := viper.GetString("DB_USERNAME")
dbPassword := viper.GetString("DB_PASSWORD")
pgURI = fmt.Sprintf("postgres://%s:%s@%s:%s/postgres?sslmode=disable&timeout=18000s", dbUsername, dbPassword, dbHost, dbPort)
stateChangeDir = viper.GetString("STATE_CHANGE_DIR")
if stateChangeDir == "" {
stateChangeDir = "/tmp/state-changes"
}
// Set the state change dir flag that core uses, so DeSoEncoders properly encode and decode state change metadata.
viper.Set("state-change-dir", stateChangeDir)
consumerProgressDir = viper.GetString("CONSUMER_PROGRESS_DIR")
if consumerProgressDir == "" {
consumerProgressDir = "/tmp/consumer-progress"
}
batchBytes = viper.GetUint64("BATCH_BYTES")
if batchBytes == 0 {
batchBytes = 5000000
}
threadLimit = viper.GetInt("THREAD_LIMIT")
if threadLimit == 0 {
threadLimit = 25
}
syncMempool = viper.GetBool("SYNC_MEMPOOL")
logQueries = viper.GetBool("LOG_QUERIES")
readonlyUserPassword = viper.GetString("READONLY_USER_PASSWORD")
explorerStatistics = viper.GetBool("CALCULATE_EXPLORER_STATISTICS")
datadogProfiler = viper.GetBool("DATADOG_PROFILER")
isTestnet = viper.GetBool("IS_TESTNET")
isRegtest = viper.GetBool("REGTEST")
isAcceleratedRegtest = viper.GetBool("ACCELERATED_REGTEST")
return pgURI, stateChangeDir, consumerProgressDir, batchBytes, threadLimit, logQueries, readonlyUserPassword, explorerStatistics, datadogProfiler, isTestnet, isRegtest, isAcceleratedRegtest, syncMempool
}
func setupDb(pgURI string, threadLimit int, logQueries bool, readonlyUserPassword string, calculateExplorerStatistics bool) (*bun.DB, error) {
// Open a PostgreSQL database.
pgdb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(pgURI)))
if pgdb == nil {
glog.Fatalf("Error connecting to postgres db at URI: %v", pgURI)
}
// Create a Bun db on top of postgres for querying.
db := bun.NewDB(pgdb, pgdialect.New())
db.SetConnMaxLifetime(0)
db.SetMaxIdleConns(threadLimit * 2)
//Print all queries to stdout for debugging.
if logQueries {
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
}
// Set the readonly user password for the initial migrations.
initial_migrations.SetQueryUserPassword(readonlyUserPassword)
post_sync_migrations.SetCalculateExplorerStatistics(calculateExplorerStatistics)
// Apply db migrations.
err := handler.RunMigrations(db, false, handler.MigrationTypeInitial)
if err != nil {
return nil, err
}
return db, nil
}