-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
389 lines (326 loc) · 13.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/flow-hydraulics/flow-wallet-api/accounts"
"github.com/flow-hydraulics/flow-wallet-api/chain_events"
"github.com/flow-hydraulics/flow-wallet-api/configs"
"github.com/flow-hydraulics/flow-wallet-api/datastore/gorm"
"github.com/flow-hydraulics/flow-wallet-api/handlers"
"github.com/flow-hydraulics/flow-wallet-api/jobs"
"github.com/flow-hydraulics/flow-wallet-api/keys"
"github.com/flow-hydraulics/flow-wallet-api/keys/basic"
"github.com/flow-hydraulics/flow-wallet-api/ops"
"github.com/flow-hydraulics/flow-wallet-api/system"
"github.com/flow-hydraulics/flow-wallet-api/templates"
"github.com/flow-hydraulics/flow-wallet-api/tokens"
"github.com/flow-hydraulics/flow-wallet-api/transactions"
"github.com/gomodule/redigo/redis"
"github.com/gorilla/mux"
access "github.com/onflow/flow-go-sdk/access/grpc"
log "github.com/sirupsen/logrus"
"go.uber.org/ratelimit"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const version = "0.9.0"
var (
sha1ver string // sha1 revision used to build the program
buildTime string // when the executable was built
)
func main() {
var (
printVersion bool
envFilePath string // LEGACY: now used to check if user still is using envFilePath
)
// If we should just print the version number and exit
flag.BoolVar(&printVersion, "version", false, "if true, print version and exit")
flag.StringVar(&envFilePath, "envfile", "", "deprecated")
flag.Parse()
if envFilePath != "" {
panic("'-envfile' is no longer supported, see readme")
}
if printVersion {
fmt.Printf("v%s build on %s from sha1 %s\n", version, buildTime, sha1ver)
os.Exit(0)
}
cfg, err := configs.Parse()
if err != nil {
panic(err)
}
runServer(cfg)
os.Exit(0)
}
func runServer(cfg *configs.Config) {
configs.ConfigureLogger(cfg.LogLevel)
log.Info("Starting server")
// Flow client
// TODO: WithInsecure()?
fc, err := access.NewClient(
cfg.AccessAPIHost,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(cfg.GrpcMaxCallRecvMsgSize)),
)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := fc.Close(); err != nil {
log.Warn(err)
}
log.Info("Closed Flow Client")
}()
// Database
db, err := gorm.New(cfg)
if err != nil {
log.Fatal(err)
}
defer gorm.Close(db)
systemService := system.NewService(
system.NewGormStore(db),
system.WithPauseDuration(cfg.PauseDuration),
)
// Create a worker pool
wp := jobs.NewWorkerPool(
jobs.NewGormStore(db),
cfg.WorkerQueueCapacity,
cfg.WorkerCount,
jobs.WithJobStatusWebhook(cfg.JobStatusWebhookUrl, cfg.JobStatusWebhookTimeout),
jobs.WithSystemService(systemService),
jobs.WithMaxJobErrorCount(cfg.MaxJobErrorCount),
jobs.WithDbJobPollInterval(cfg.DBJobPollInterval),
jobs.WithAcceptedGracePeriod(cfg.AcceptedGracePeriod),
jobs.WithReSchedulableGracePeriod(cfg.ReSchedulableGracePeriod),
)
defer func() {
wp.Stop(true)
log.Info("Stopped workerpool")
}()
txRatelimiter := ratelimit.New(cfg.TransactionMaxSendRate, ratelimit.WithoutSlack)
// Key manager
km := basic.NewKeyManager(cfg, keys.NewGormStore(db), fc)
// Services
templateService, err := templates.NewService(cfg, templates.NewGormStore(db))
if err != nil {
log.Fatal(err)
}
jobsService := jobs.NewService(jobs.NewGormStore(db))
transactionService := transactions.NewService(cfg, transactions.NewGormStore(db), km, fc, wp, transactions.WithTxRatelimiter(txRatelimiter))
accountService := accounts.NewService(cfg, accounts.NewGormStore(db), km, fc, wp, transactionService, templateService, accounts.WithTxRatelimiter(txRatelimiter))
tokenService := tokens.NewService(cfg, tokens.NewGormStore(db), km, fc, wp, transactionService, templateService, accountService)
opsService := ops.NewService(cfg, ops.NewGormStore(db), templateService, transactionService, tokenService)
// Register a handler for account added events
accounts.AccountAdded.Register(&tokens.AccountAddedHandler{
TemplateService: templateService,
TokenService: tokenService,
})
err = accountService.InitAdminAccount(context.Background())
if err != nil {
log.Fatal(err)
}
wp.Start()
log.Info("Started workerpool")
// HTTP handling
systemHandler := handlers.NewSystem(systemService)
templateHandler := handlers.NewTemplates(templateService)
jobsHandler := handlers.NewJobs(jobsService)
accountHandler := handlers.NewAccounts(accountService)
transactionHandler := handlers.NewTransactions(transactionService)
tokenHandler := handlers.NewTokens(tokenService)
opsHandler := handlers.NewOps(opsService)
r := mux.NewRouter()
// Catch the api version
rv := r.PathPrefix("/{apiVersion}").Subrouter()
// Debug
rv.Handle("/debug", handlers.Debug("https://github.com/flow-hydraulics/flow-wallet-api", sha1ver, buildTime)).Methods(http.MethodGet)
// Health
rv.HandleFunc("/health/ready", handlers.HandleHealthReady).Methods(http.MethodGet)
rv.Handle("/health/liveness", handlers.Liveness(func() (interface{}, error) {
return wp.Status()
})).Methods(http.MethodGet)
// System
rv.Handle("/system/settings", systemHandler.GetSettings()).Methods(http.MethodGet)
rv.Handle("/system/settings", systemHandler.SetSettings()).Methods(http.MethodPost)
rv.Handle("/system/sync-account-key-count", accountHandler.SyncAccountKeyCount()).Methods(http.MethodPost)
// Jobs
rv.Handle("/jobs", jobsHandler.List()).Methods(http.MethodGet) // list
rv.Handle("/jobs/{jobId}", jobsHandler.Details()).Methods(http.MethodGet) // details
// Token templates
rv.Handle("/tokens", templateHandler.ListTokens(templates.NotSpecified)).Methods(http.MethodGet) // list
rv.Handle("/tokens", templateHandler.AddToken()).Methods(http.MethodPost) // create
rv.Handle("/tokens/{id_or_name}", templateHandler.GetToken()).Methods(http.MethodGet) // details
rv.Handle("/tokens/{id}", templateHandler.RemoveToken()).Methods(http.MethodDelete) // delete
// List enabled tokens by type
rv.Handle("/fungible-tokens", templateHandler.ListTokens(templates.FT)).Methods(http.MethodGet) // list
rv.Handle("/non-fungible-tokens", templateHandler.ListTokens(templates.NFT)).Methods(http.MethodGet) // list
// Transactions
rv.Handle("/transactions", transactionHandler.List()).Methods(http.MethodGet) // list
rv.Handle("/transactions/{transactionId}", transactionHandler.Details()).Methods(http.MethodGet) // details
// Account
rv.Handle("/accounts", accountHandler.List()).Methods(http.MethodGet) // list
rv.Handle("/accounts", accountHandler.Create()).Methods(http.MethodPost) // create
rv.Handle("/accounts/{address}", accountHandler.Details()).Methods(http.MethodGet) // details
// Account raw transactions
if !cfg.DisableRawTransactions {
rv.Handle("/accounts/{address}/sign", transactionHandler.Sign()).Methods(http.MethodPost) // sign
rv.Handle("/accounts/{address}/transactions", transactionHandler.List()).Methods(http.MethodGet) // list
rv.Handle("/accounts/{address}/transactions", transactionHandler.Create()).Methods(http.MethodPost) // create
rv.Handle("/accounts/{address}/transactions/{transactionId}", transactionHandler.Details()).Methods(http.MethodGet) // details
} else {
log.Info("raw transactions disabled")
}
// Non-custodial watchlist accounts
rv.Handle("/watchlist/accounts", accountHandler.AddNonCustodialAccount()).Methods(http.MethodPost) // add
rv.Handle("/watchlist/accounts/{address}", accountHandler.DeleteNonCustodialAccount()).Methods(http.MethodDelete) // delete
// Scripts
rv.Handle("/scripts", transactionHandler.ExecuteScript()).Methods(http.MethodPost) // execute
// Fungible tokens
if !cfg.DisableFungibleTokens {
rv.Handle("/accounts/{address}/fungible-tokens", tokenHandler.AccountTokens(templates.FT)).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}", tokenHandler.Details()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}", tokenHandler.Setup()).Methods(http.MethodPost)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}/withdrawals", tokenHandler.ListWithdrawals()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}/withdrawals", tokenHandler.CreateWithdrawal()).Methods(http.MethodPost)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}/withdrawals/{transactionId}", tokenHandler.GetWithdrawal()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}/deposits", tokenHandler.ListDeposits()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/fungible-tokens/{tokenName}/deposits/{transactionId}", tokenHandler.GetDeposit()).Methods(http.MethodGet)
} else {
log.Info("fungible tokens disabled")
}
// Non-Fungible tokens
if !cfg.DisableNonFungibleTokens {
rv.Handle("/accounts/{address}/non-fungible-tokens", tokenHandler.AccountTokens(templates.NFT)).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}", tokenHandler.Details()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}", tokenHandler.Setup()).Methods(http.MethodPost)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}/withdrawals", tokenHandler.ListWithdrawals()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}/withdrawals", tokenHandler.CreateWithdrawal()).Methods(http.MethodPost)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}/withdrawals/{transactionId}", tokenHandler.GetWithdrawal()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}/deposits", tokenHandler.ListDeposits()).Methods(http.MethodGet)
rv.Handle("/accounts/{address}/non-fungible-tokens/{tokenName}/deposits/{transactionId}", tokenHandler.GetDeposit()).Methods(http.MethodGet)
} else {
log.Info("non-fungible tokens disabled")
}
// Ops
rv.Handle("/ops/missing-fungible-token-vaults/start", opsHandler.InitMissingFungibleVaults()).Methods(http.MethodGet) // start retroactive init job
rv.Handle("/ops/missing-fungible-token-vaults/stats", opsHandler.GetMissingFungibleVaults()).Methods(http.MethodGet) // get number of accounts with missing fungible token vaults
h := http.TimeoutHandler(r, cfg.ServerRequestTimeout, "request timed out")
h = handlers.UseCors(h)
h = handlers.UseLogging(h)
h = handlers.UseCompress(h)
// Setup idempotency key middleware if it's enabled
// redis for idempotency key handling
if !cfg.DisableIdempotencyMiddleware {
var is handlers.IdempotencyStore
switch cfg.IdempotencyMiddlewareDatabaseType {
// Shared SQL/Gorm store (same as for main app)
case handlers.IdempotencyStoreTypeShared.String():
is = handlers.NewIdempotencyStoreGorm(db)
// Redis, separate from app db
case handlers.IdempotencyStoreTypeRedis.String():
if cfg.IdempotencyMiddlewareRedisURL == "" {
log.Fatal("idempotency middleware db set to redis but Redis URL is empty")
}
pool := &redis.Pool{
MaxIdle: 80,
MaxActive: 12000,
Dial: func() (redis.Conn, error) {
c, err := redis.DialURL(cfg.IdempotencyMiddlewareRedisURL)
if err != nil {
panic(err.Error())
}
return c, err
},
}
client := pool.Get()
defer func() {
log.Info("Closing Redis client..")
if err := client.Close(); err != nil {
log.Warn(err)
}
}()
is = handlers.NewIdempotencyStoreRedis(client)
case handlers.IdempotencyStoreTypeLocal.String():
is = handlers.NewIdempotencyStoreLocal()
}
h = handlers.UseIdempotency(h, handlers.IdempotencyHandlerOptions{
Expiry: 1 * time.Hour,
IgnorePaths: []string{"/v1/scripts"}, // Scripts are read-only
}, is)
}
// Server boilerplate
srv := &http.Server{
Handler: h,
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
WriteTimeout: 0, // Disabled, set cfg.ServerRequestTimeout instead
ReadTimeout: 0, // Disabled, set cfg.ServerRequestTimeout instead
}
// Run our server in a goroutine so that it doesn't block.
go func() {
log.
WithFields(log.Fields{
"host": cfg.Host,
"port": cfg.Port,
}).
Info("Server listening")
if err := srv.ListenAndServe(); err != nil {
log.Warn(err)
}
}()
// Chain event listener
if !cfg.DisableChainEvents {
store := chain_events.NewGormStore(db)
getTypes := func() ([]string, error) {
// Get all enabled tokens
tt, err := templateService.ListTokens(templates.NotSpecified)
if err != nil {
return nil, err
}
token_count := len(tt)
event_types := make([]string, token_count)
// Listen for enabled tokens deposit events
for i, token := range tt {
event_types[i] = templates.DepositEventTypeFromToken(token)
}
return event_types, nil
}
listener := chain_events.NewListener(
fc, store, getTypes,
cfg.ChainListenerMaxBlocks,
cfg.ChainListenerInterval,
cfg.ChainListenerStartingHeight,
chain_events.WithSystemService(systemService),
)
defer func() {
listener.Stop()
log.Info("Stopped chain events listener")
}()
// Register a handler for chain events
chain_events.ChainEvent.Register(&tokens.ChainEventHandler{
AccountService: accountService,
ChainListener: listener,
TemplateService: templateService,
TokenService: tokenService,
})
listener.Start()
log.Info("Started chain events listener")
}
// Trap interupt or sigterm and gracefully shutdown the server
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
sig := <-c
log.Infof("Got signal: %s. Shutting down..", sig)
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Warnf("Error in server shutdown: %s", err)
}
}