-
Notifications
You must be signed in to change notification settings - Fork 86
/
construction.go
653 lines (571 loc) · 18.2 KB
/
construction.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tester
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
cliErrs "github.com/coinbase/rosetta-cli/pkg/errors"
"github.com/coinbase/rosetta-cli/configuration"
"github.com/coinbase/rosetta-cli/pkg/logger"
"github.com/coinbase/rosetta-cli/pkg/processor"
"github.com/coinbase/rosetta-cli/pkg/results"
"github.com/coinbase/rosetta-sdk-go/constructor/coordinator"
"github.com/coinbase/rosetta-sdk-go/fetcher"
"github.com/coinbase/rosetta-sdk-go/parser"
"github.com/coinbase/rosetta-sdk-go/statefulsyncer"
"github.com/coinbase/rosetta-sdk-go/storage/database"
storageErrs "github.com/coinbase/rosetta-sdk-go/storage/errors"
"github.com/coinbase/rosetta-sdk-go/storage/modules"
"github.com/coinbase/rosetta-sdk-go/syncer"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/coinbase/rosetta-sdk-go/utils"
"github.com/fatih/color"
"golang.org/x/sync/errgroup"
)
const (
// constructionCmdName is used as the prefix on the data directory
// for all data saved using this command.
constructionCmdName = "check-construction"
endConditionsCheckInterval = 10 * time.Second
tipWaitInterval = 10 * time.Second
)
var _ http.Handler = (*ConstructionTester)(nil)
var constructionMetadata string
// ConstructionTester coordinates the `check:construction` test.
type ConstructionTester struct {
network *types.NetworkIdentifier
database database.Database
config *configuration.Configuration
syncer *statefulsyncer.StatefulSyncer
logger *logger.Logger
onlineFetcher *fetcher.Fetcher
broadcastStorage *modules.BroadcastStorage
blockStorage *modules.BlockStorage
jobStorage *modules.JobStorage
counterStorage *modules.CounterStorage
coordinator *coordinator.Coordinator
cancel context.CancelFunc
signalReceived *bool
reachedEndConditions bool
}
// InitializeConstruction initiates the construction API tester.
func InitializeConstruction(
ctx context.Context,
config *configuration.Configuration,
network *types.NetworkIdentifier,
onlineFetcher *fetcher.Fetcher,
cancel context.CancelFunc,
signalReceived *bool,
) (*ConstructionTester, error) {
dataPath, err := utils.CreateCommandPath(config.DataDirectory, constructionCmdName, network)
metadataMap := logger.ConvertStringToMap(config.InfoMetaData)
metadataMap = logger.AddRequestUUIDToMap(metadataMap, config.RequestUUID)
constructionMetadata = logger.ConvertMapToString(metadataMap)
if err != nil {
err = fmt.Errorf("failed to create command path: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
opts := []database.BadgerOption{}
// add constructionMetadata into localStore
opts = append(opts, database.WithMetaData(constructionMetadata))
if config.CompressionDisabled {
opts = append(opts, database.WithoutCompression())
}
if config.L0InMemoryEnabled {
opts = append(
opts,
database.WithCustomSettings(database.PerformanceBadgerOptions(dataPath)),
)
}
// add constructionMetadata into localStore
localStore, err := database.NewBadgerDatabase(ctx, dataPath, opts...)
if err != nil {
err = fmt.Errorf("unable to initialize database: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
networkOptions, fetchErr := onlineFetcher.NetworkOptionsRetry(ctx, network, nil)
if fetchErr != nil {
err := fmt.Errorf("unable to get network options: %w%s", fetchErr.Err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
if len(networkOptions.Allow.BalanceExemptions) > 0 &&
config.Construction.InitialBalanceFetchDisabled {
return nil, cliErrs.ErrBalanceExemptionsWithInitialBalanceFetchDisabled
}
counterStorage := modules.NewCounterStorage(localStore)
//add constructionMetadata into logger
logger, err := logger.NewLogger(
dataPath,
false,
false,
false,
false,
logger.Construction,
network,
metadataMap,
)
if err != nil {
err = fmt.Errorf("unable to initialize logger with error: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
blockStorage := modules.NewBlockStorage(localStore, config.SerialBlockWorkers)
keyStorage := modules.NewKeyStorage(localStore)
coinStorageHelper := processor.NewCoinStorageHelper(blockStorage)
coinStorage := modules.NewCoinStorage(localStore, coinStorageHelper, onlineFetcher.Asserter)
balanceStorage := modules.NewBalanceStorage(localStore)
balanceStorageHelper := processor.NewBalanceStorageHelper(
network,
onlineFetcher,
counterStorage,
false,
nil,
true,
networkOptions.Allow.BalanceExemptions,
config.Construction.InitialBalanceFetchDisabled,
)
balanceStorageHandler := processor.NewBalanceStorageHandler(
logger,
nil,
counterStorage,
false,
nil,
)
balanceStorage.Initialize(balanceStorageHelper, balanceStorageHandler)
broadcastStorage := modules.NewBroadcastStorage(
localStore,
config.Construction.StaleDepth,
config.Construction.BroadcastLimit,
config.TipDelay,
config.Construction.BroadcastBehindTip,
config.Construction.BlockBroadcastLimit,
)
parser := parser.New(onlineFetcher.Asserter, nil, networkOptions.Allow.BalanceExemptions)
broadcastHelper := processor.NewBroadcastStorageHelper(
network,
blockStorage,
onlineFetcher,
)
//add constructionMetadata into fetcher
fetcherOpts := []fetcher.Option{
fetcher.WithMaxConnections(config.Construction.MaxOfflineConnections),
fetcher.WithAsserter(onlineFetcher.Asserter),
fetcher.WithTimeout(time.Duration(config.HTTPTimeout) * time.Second),
fetcher.WithMaxRetries(config.MaxRetries),
fetcher.WithMetaData(constructionMetadata),
}
if config.Construction.ForceRetry {
fetcherOpts = append(fetcherOpts, fetcher.WithForceRetry())
}
offlineFetcher := fetcher.New(
config.Construction.OfflineURL,
fetcherOpts...,
)
// Import prefunded account and save to database
err = keyStorage.ImportAccounts(ctx, config.Construction.PrefundedAccounts)
if err != nil {
err = fmt.Errorf("%w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
// Load all accounts for network
accounts, err := keyStorage.GetAllAccounts(ctx)
if err != nil {
err = fmt.Errorf("unable to load addresses: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
// Track balances on all addresses
for _, account := range accounts {
balanceStorageHelper.AddInterestingAddress(account.Address)
}
color.Cyan("construction tester initialized with %d accounts%s\n", len(accounts), constructionMetadata)
// Load prefunded accounts
var accountBalanceRequests []*utils.AccountBalanceRequest
var acctCoinsReqs []*utils.AccountCoinsRequest
for _, prefundedAcc := range config.Construction.PrefundedAccounts {
accountBalance := &utils.AccountBalanceRequest{
Account: prefundedAcc.AccountIdentifier,
Network: network,
Currency: prefundedAcc.Currency,
}
accountBalanceRequests = append(accountBalanceRequests, accountBalance)
if config.CoinSupported {
acctCoinsReq := &utils.AccountCoinsRequest{
Account: prefundedAcc.AccountIdentifier,
Network: network,
Currencies: []*types.Currency{prefundedAcc.Currency},
IncludeMempool: false,
}
acctCoinsReqs = append(acctCoinsReqs, acctCoinsReq)
}
}
accBalances, err := utils.GetAccountBalances(ctx, onlineFetcher, accountBalanceRequests)
if err != nil {
err = fmt.Errorf("unable to get account balances: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
err = balanceStorage.SetBalanceImported(ctx, nil, accBalances)
if err != nil {
err = fmt.Errorf("unable to set balances: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
// -------------------------------------------------------------------------
// ------------ Get account coins and add them in coins storage ------------
// -------------------------------------------------------------------------
if config.CoinSupported {
acctCoins, errAccCoins := utils.GetAccountCoins(ctx, onlineFetcher, acctCoinsReqs)
if errAccCoins != nil {
err = fmt.Errorf("unable to get account coins: %w%s", errAccCoins, constructionMetadata)
color.Red(err.Error())
return nil, err
}
// Extract accounts from account coins requests
var accts []*types.AccountIdentifier
for _, req := range acctCoinsReqs {
accts = append(accts, req.Account)
}
err = coinStorage.SetCoinsImported(ctx, accts, acctCoins)
if err != nil {
err = fmt.Errorf("unable to set coin balances: %w%s", err, constructionMetadata)
color.Red(err.Error())
return nil, err
}
}
// --------------------------------------------------------------------------
// ---------------------- End of adding account coins -----------------------
// --------------------------------------------------------------------------
jobStorage := modules.NewJobStorage(localStore)
coordinatorHelper := processor.NewCoordinatorHelper(
offlineFetcher,
onlineFetcher,
localStore,
blockStorage,
keyStorage,
balanceStorage,
coinStorage,
broadcastStorage,
balanceStorageHelper,
counterStorage,
config.Construction.Quiet,
)
coordinatorHandler := processor.NewCoordinatorHandler(
counterStorage,
)
coordinator, err := coordinator.New(
jobStorage,
coordinatorHelper,
coordinatorHandler,
parser,
config.Construction.Workflows,
)
if err != nil {
msg := fmt.Sprintf("unable to create coordinator: %s%s", err.Error(), constructionMetadata)
color.Red(msg)
log.Fatalf(msg)
}
broadcastHandler := processor.NewBroadcastStorageHandler(
config,
blockStorage,
counterStorage,
coordinator,
parser,
)
broadcastStorage.Initialize(broadcastHelper, broadcastHandler)
//add constructionMetadata into syncer
syncer := statefulsyncer.New(
ctx,
network,
onlineFetcher,
blockStorage,
counterStorage,
logger,
cancel,
[]modules.BlockWorker{counterStorage, balanceStorage, coinStorage, broadcastStorage},
statefulsyncer.WithCacheSize(syncer.DefaultCacheSize),
statefulsyncer.WithMaxConcurrency(config.MaxSyncConcurrency),
statefulsyncer.WithPastBlockLimit(config.MaxReorgDepth),
statefulsyncer.WithSeenConcurrency(int64(config.SeenBlockWorkers)),
statefulsyncer.WithMetaData(constructionMetadata),
)
return &ConstructionTester{
network: network,
database: localStore,
config: config,
syncer: syncer,
logger: logger,
coordinator: coordinator,
broadcastStorage: broadcastStorage,
blockStorage: blockStorage,
jobStorage: jobStorage,
counterStorage: counterStorage,
onlineFetcher: onlineFetcher,
cancel: cancel,
signalReceived: signalReceived,
}, nil
}
// CloseDatabase closes the database used by ConstructionTester.
func (t *ConstructionTester) CloseDatabase(ctx context.Context) {
if err := t.database.Close(ctx); err != nil {
msg := fmt.Sprintf("error closing database: %s%s", err.Error(), constructionMetadata)
color.Red(msg)
log.Fatalf(msg)
}
}
// StartPeriodicLogger prints out periodic
// stats about a run of `check:construction`.
func (t *ConstructionTester) StartPeriodicLogger(
ctx context.Context,
) error {
tc := time.NewTicker(PeriodicLoggingFrequency)
defer tc.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-tc.C:
status := results.ComputeCheckConstructionStatus(
ctx,
t.config,
t.counterStorage,
t.broadcastStorage,
t.jobStorage,
)
t.logger.LogConstructionStatus(ctx, status)
}
}
}
func (t *ConstructionTester) checkTip(ctx context.Context) (int64, error) {
atTip, blockIdentifier, err := utils.CheckNetworkTip(
ctx,
t.network,
t.config.TipDelay,
t.onlineFetcher,
)
if err != nil {
err = fmt.Errorf("failed to check network tip: %w%s", err, constructionMetadata)
color.Red(err.Error())
return -1, err
}
if atTip {
return blockIdentifier.Index, nil
}
return -1, nil
}
// waitForTip loops until the Rosetta implementation is at tip.
func (t *ConstructionTester) waitForTip(ctx context.Context) (int64, error) {
tc := time.NewTicker(tipWaitInterval)
defer tc.Stop()
for {
// Don't wait any time before first tick if at tip.
tipIndex, err := t.checkTip(ctx)
if err != nil {
err = fmt.Errorf("failed to check tip: %w%s", err, constructionMetadata)
color.Red(err.Error())
return -1, err
}
if tipIndex != -1 {
return tipIndex, nil
}
color.Cyan("waiting for implementation to reach tip before testing...%s", constructionMetadata)
select {
case <-ctx.Done():
return -1, ctx.Err()
case <-tc.C:
continue
}
}
}
// StartSyncer uses the tester's stateful syncer
// to compute balance changes and track transactions
// for confirmation on-chain.
func (t *ConstructionTester) StartSyncer(
ctx context.Context,
cancel context.CancelFunc,
) error {
startIndex := int64(-1)
_, err := t.blockStorage.GetHeadBlockIdentifier(ctx)
if errors.Is(err, storageErrs.ErrHeadBlockNotFound) {
// If no head block exists, ensure we are at tip before starting. Otherwise,
// we will unnecessarily sync tons of blocks before reaching any that matter.
startIndex, err = t.waitForTip(ctx)
if err != nil {
err = fmt.Errorf("unable to wait for tip: %w%s", err, constructionMetadata)
color.Red(err.Error())
return err
}
} else if err != nil {
err = fmt.Errorf("unable to get last block synced: %w%s", err, constructionMetadata)
color.Red(err.Error())
return err
}
return t.syncer.Sync(ctx, startIndex, -1)
}
// StartConstructor uses the tester's constructor
// to begin generating addresses and constructing
// transactions.
func (t *ConstructionTester) StartConstructor(
ctx context.Context,
) error {
if t.config.Construction.ClearBroadcasts {
broadcasts, err := t.broadcastStorage.ClearBroadcasts(ctx)
if err != nil {
err = fmt.Errorf("unable to clear broadcasts: %w%s", err, constructionMetadata)
color.Red(err.Error())
return err
}
color.Cyan("cleared %d broadcasts%s\n", len(broadcasts), constructionMetadata)
}
return t.coordinator.Process(ctx)
}
// ServeHTTP serves a CheckDataStatus response on all paths.
func (t *ConstructionTester) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
status := results.ComputeCheckConstructionStatus(
r.Context(),
t.config,
t.counterStorage,
t.broadcastStorage,
t.jobStorage,
)
if err := json.NewEncoder(w).Encode(status); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// PerformBroadcasts attempts to rebroadcast all pending transactions
// if the RebroadcastAll configuration is set to true.
func (t *ConstructionTester) PerformBroadcasts(ctx context.Context) error {
if !t.config.Construction.RebroadcastAll {
return nil
}
color.Magenta("Rebroadcasting all transactions...%s", constructionMetadata)
if err := t.broadcastStorage.BroadcastAll(ctx, false); err != nil {
err = fmt.Errorf("unable to broadcast all transactions: %w%s", err, constructionMetadata)
color.Red(err.Error())
return err
}
return nil
}
// WatchEndConditions cancels check:construction once
// all end conditions are met (provided workflows
// are executed at least minOccurences).
func (t *ConstructionTester) WatchEndConditions(
ctx context.Context,
) error {
endConditions := t.config.Construction.EndConditions
if endConditions == nil {
return nil
}
tc := time.NewTicker(endConditionsCheckInterval)
defer tc.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-tc.C:
conditionsMet := true
for workflow, minOccurences := range endConditions {
completed, err := t.jobStorage.Completed(ctx, workflow)
if err != nil {
err = fmt.Errorf("unable to fetch completed %s: %w%s", workflow, err, constructionMetadata)
color.Red(err.Error())
return err
}
if len(completed) < minOccurences {
conditionsMet = false
break
}
}
if conditionsMet {
t.reachedEndConditions = true
t.cancel()
return nil
}
}
}
}
func (t *ConstructionTester) returnFunds(
ctx context.Context,
sigListeners *[]context.CancelFunc,
) {
// To cancel all execution, need to call multiple cancel functions.
ctx, cancel := context.WithCancel(ctx)
*sigListeners = append(*sigListeners, cancel)
var returnFundsSuccess bool
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return t.StartSyncer(ctx, cancel)
})
g.Go(func() error {
return t.StartPeriodicLogger(ctx)
})
g.Go(func() error {
err := t.coordinator.ReturnFunds(ctx)
// If the error is nil, we need to cancel the syncer
// or we will sync forever.
if err == nil {
returnFundsSuccess = true // makes error parsing much easier
cancel()
return nil
}
return err
})
err := g.Wait()
if *t.signalReceived {
color.Red("Fund return halted%s", constructionMetadata)
return
}
if !returnFundsSuccess {
color.Cyan("unable to return funds %v%s\n", err, constructionMetadata)
}
}
// HandleErr is called when `check:construction` returns an error.
func (t *ConstructionTester) HandleErr(
err error,
sigListeners *[]context.CancelFunc,
) error {
if *t.signalReceived {
err = fmt.Errorf("%v: %w%s", err.Error(), cliErrs.ErrConstructionCheckHalt, constructionMetadata)
color.Red(err.Error())
return results.ExitConstruction(
t.config,
t.counterStorage,
t.jobStorage,
err,
)
}
if !t.reachedEndConditions {
color.Red("%v%s", err, constructionMetadata)
return results.ExitConstruction(t.config, t.counterStorage, t.jobStorage, err)
}
// We optimistically run the ReturnFunds function on the coordinator
// and only log if it fails. If there is no ReturnFunds workflow defined,
// this will just return nil.
t.returnFunds(
context.Background(),
sigListeners,
)
return results.ExitConstruction(t.config, t.counterStorage, t.jobStorage, nil)
}