This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
model_sync_transactions.go
886 lines (766 loc) · 25.1 KB
/
model_sync_transactions.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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
package bux
import (
"context"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/notifications"
"github.com/BuxOrg/bux/taskmanager"
"github.com/libsv/go-bt/v2"
"github.com/mrz1836/go-datastore"
customTypes "github.com/mrz1836/go-datastore/custom_types"
"github.com/tonicpow/go-paymail"
)
// SyncTransaction is an object representing the chain-state sync configuration and results for a given transaction
//
// Gorm related models & indexes: https://gorm.io/docs/models.html - https://gorm.io/docs/indexes.html
type SyncTransaction struct {
// Base model
Model `bson:",inline"`
// Model specific fields
ID string `json:"id" toml:"id" yaml:"id" gorm:"<-:create;type:char(64);primaryKey;comment:This is the unique transaction id" bson:"_id"`
Configuration SyncConfig `json:"configuration" toml:"configuration" yaml:"configuration" gorm:"<-;type:text;comment:This is the configuration struct in JSON" bson:"configuration"`
LastAttempt customTypes.NullTime `json:"last_attempt" toml:"last_attempt" yaml:"last_attempt" gorm:"<-;comment:When the last broadcast occurred" bson:"last_attempt,omitempty"`
Results SyncResults `json:"results" toml:"results" yaml:"results" gorm:"<-;type:text;comment:This is the results struct in JSON" bson:"results"`
BroadcastStatus SyncStatus `json:"broadcast_status" toml:"broadcast_status" yaml:"broadcast_status" gorm:"<-;type:varchar(10);index;comment:This is the status of the broadcast" bson:"broadcast_status"`
P2PStatus SyncStatus `json:"p2p_status" toml:"p2p_status" yaml:"p2p_status" gorm:"<-;column:p2p_status;type:varchar(10);index;comment:This is the status of the p2p paymail requests" bson:"p2p_status"`
SyncStatus SyncStatus `json:"sync_status" toml:"sync_status" yaml:"sync_status" gorm:"<-;type:varchar(10);index;comment:This is the status of the on-chain sync" bson:"sync_status"`
// internal fields
transaction *Transaction
}
// newSyncTransaction will start a new model (config is required)
func newSyncTransaction(txID string, config *SyncConfig, opts ...ModelOps) *SyncTransaction {
// Do not allow making a model without the configuration
if config == nil {
return nil
}
// Broadcasting
bs := SyncStatusReady
if !config.Broadcast {
bs = SyncStatusSkipped
}
// Notify Paymail P2P
ps := SyncStatusPending
if !config.PaymailP2P {
ps = SyncStatusSkipped
}
// Sync
ss := SyncStatusReady
if !config.SyncOnChain {
ss = SyncStatusSkipped
}
return &SyncTransaction{
BroadcastStatus: bs,
Configuration: *config,
ID: txID,
Model: *NewBaseModel(ModelSyncTransaction, opts...),
P2PStatus: ps,
SyncStatus: ss,
}
}
// GetSyncTransactionByID will get a sync transaction
func GetSyncTransactionByID(ctx context.Context, id string, opts ...ModelOps) (*SyncTransaction, error) {
// Get the records by status
txs, err := getSyncTransactionsByConditions(ctx,
map[string]interface{}{
idField: id,
},
nil, opts...,
)
if err != nil {
return nil, err
}
if len(txs) != 1 {
return nil, nil
}
return txs[0], nil
}
// getTransactionsToBroadcast will get the sync transactions to broadcast
func getTransactionsToBroadcast(ctx context.Context, queryParams *datastore.QueryParams,
opts ...ModelOps) (map[string][]*SyncTransaction, error) {
// Get the records by status
txs, err := getSyncTransactionsByConditions(
ctx,
map[string]interface{}{
broadcastStatusField: SyncStatusReady.String(),
},
queryParams, opts...,
)
if err != nil {
return nil, err
}
// group transactions by xpub and return including the tx itself
txsByXpub := make(map[string][]*SyncTransaction)
for _, tx := range txs {
if tx.transaction, err = getTransactionByID(
ctx, "", tx.ID, opts...,
); err != nil {
return nil, err
}
var parentsBroadcast bool
parentsBroadcast, err = areParentsBroadcast(ctx, tx, opts...)
if err != nil {
return nil, err
}
if !parentsBroadcast {
// if all parents are not broadcast, then we cannot broadcast this tx
continue
}
xPubID := "" // fallback if we have no input xpubs
if len(tx.transaction.XpubInIDs) > 0 {
// use the first xpub for the grouping
// in most cases when we are broadcasting, there should be only 1 xpub in
xPubID = tx.transaction.XpubInIDs[0]
}
if txsByXpub[xPubID] == nil {
txsByXpub[xPubID] = make([]*SyncTransaction, 0)
}
txsByXpub[xPubID] = append(txsByXpub[xPubID], tx)
}
return txsByXpub, nil
}
func areParentsBroadcast(ctx context.Context, syncTx *SyncTransaction, opts ...ModelOps) (bool, error) {
tx, err := getTransactionByID(ctx, "", syncTx.ID, opts...)
if err != nil {
return false, err
}
if tx == nil {
return false, ErrMissingTransaction
}
// get the sync transaction of all inputs
var btTx *bt.Tx
btTx, err = bt.NewTxFromString(tx.Hex)
if err != nil {
return false, err
}
// check that all inputs we handled have been broadcast, or are not handled by Bux
parentsBroadcast := true
for _, input := range btTx.Inputs {
var parentTx *SyncTransaction
previousTxID := hex.EncodeToString(bt.ReverseBytes(input.PreviousTxID()))
parentTx, err = GetSyncTransactionByID(ctx, previousTxID, opts...)
if err != nil {
return false, err
}
// if we have a sync transaction, and it is not complete, then we cannot broadcast
if parentTx != nil && parentTx.BroadcastStatus != SyncStatusComplete {
parentsBroadcast = false
}
}
return parentsBroadcast, nil
}
// getTransactionsToNotifyP2P will get the sync transactions to notify p2p paymail providers
func getTransactionsToNotifyP2P(ctx context.Context, queryParams *datastore.QueryParams,
opts ...ModelOps) ([]*SyncTransaction, error) {
// Get the records by status
txs, err := getSyncTransactionsByConditions(
ctx,
map[string]interface{}{
p2pStatusField: SyncStatusReady.String(),
},
queryParams, opts...,
)
if err != nil {
return nil, err
}
return txs, nil
}
// getTransactionsToSync will get the sync transactions to sync
func getTransactionsToSync(ctx context.Context, queryParams *datastore.QueryParams,
opts ...ModelOps) ([]*SyncTransaction, error) {
// Get the records by status
txs, err := getSyncTransactionsByConditions(
ctx,
map[string]interface{}{
syncStatusField: SyncStatusReady.String(),
},
queryParams, opts...,
)
if err != nil {
return nil, err
}
return txs, nil
}
// getTransactionsToSync will get the sync transactions to sync
func getSyncTransactionsByConditions(ctx context.Context, conditions map[string]interface{},
queryParams *datastore.QueryParams, opts ...ModelOps) ([]*SyncTransaction, error) {
if queryParams == nil {
queryParams = &datastore.QueryParams{
OrderByField: createdAtField,
SortDirection: datastore.SortAsc,
}
} else if queryParams.OrderByField == "" || queryParams.SortDirection == "" {
queryParams.OrderByField = createdAtField
queryParams.SortDirection = datastore.SortAsc
}
// Get the records
var models []SyncTransaction
if err := getModels(
ctx, NewBaseModel(ModelNameEmpty, opts...).Client().Datastore(),
&models, conditions, queryParams, defaultDatabaseReadTimeout,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
// Loop and enrich
txs := make([]*SyncTransaction, 0)
for index := range models {
models[index].enrich(ModelSyncTransaction, opts...)
txs = append(txs, &models[index])
}
return txs, nil
}
// isSkipped will return true if Broadcasting, P2P and SyncOnChain are all skipped
func (m *SyncTransaction) isSkipped() bool {
return m.BroadcastStatus == SyncStatusSkipped &&
m.SyncStatus == SyncStatusSkipped &&
m.P2PStatus == SyncStatusSkipped
}
// GetModelName will get the name of the current model
func (m *SyncTransaction) GetModelName() string {
return ModelSyncTransaction.String()
}
// GetModelTableName will get the db table name of the current model
func (m *SyncTransaction) GetModelTableName() string {
return tableSyncTransactions
}
// Save will save the model into the Datastore
func (m *SyncTransaction) Save(ctx context.Context) error {
return Save(ctx, m)
}
// GetID will get the ID
func (m *SyncTransaction) GetID() string {
return m.ID
}
// BeforeCreating will fire before the model is being inserted into the Datastore
func (m *SyncTransaction) BeforeCreating(_ context.Context) error {
m.DebugLog("starting: [" + m.name.String() + "] BeforeCreating hook...")
// Make sure ID is valid
if len(m.ID) == 0 {
return ErrMissingFieldID
}
m.DebugLog("end: " + m.Name() + " BeforeCreating hook")
return nil
}
// AfterCreated will fire after the model is created in the Datastore
func (m *SyncTransaction) AfterCreated(ctx context.Context) error {
m.DebugLog("starting: " + m.Name() + " AfterCreated hook...")
// Should we broadcast immediately?
if m.Configuration.Broadcast &&
m.Configuration.BroadcastInstant {
if err := processBroadcastTransaction(
ctx, m,
); err != nil {
// return err (do not return and fail the record creation)
m.Client().Logger().Error(ctx, "error running broadcast tx: "+err.Error())
}
}
m.DebugLog("end: " + m.Name() + " AfterCreated hook")
return nil
}
// RegisterTasks will register the model specific tasks on client initialization
func (m *SyncTransaction) RegisterTasks() error {
// No task manager loaded?
tm := m.Client().Taskmanager()
if tm == nil {
return nil
}
// Register the task locally (cron task - set the defaults)
syncTask := m.Name() + "_" + syncActionSync
ctx := context.Background()
// Register the task
if err := tm.RegisterTask(&taskmanager.Task{
Name: syncTask,
RetryLimit: 1,
Handler: func(client ClientInterface) error {
if taskErr := taskSyncTransactions(ctx, client.Logger(), WithClient(client)); taskErr != nil {
client.Logger().Error(ctx, "error running "+syncTask+" task: "+taskErr.Error())
}
return nil
},
}); err != nil {
return err
}
// Run the task periodically
err := tm.RunTask(ctx, &taskmanager.TaskOptions{
Arguments: []interface{}{m.Client()},
RunEveryPeriod: m.Client().GetTaskPeriod(syncTask),
TaskName: syncTask,
})
if err != nil {
return err
}
// Register the task locally (cron task - set the defaults)
broadcastTask := m.Name() + "_" + syncActionBroadcast
// Register the task
if err = tm.RegisterTask(&taskmanager.Task{
Name: broadcastTask,
RetryLimit: 1,
Handler: func(client ClientInterface) error {
if taskErr := taskBroadcastTransactions(ctx, client.Logger(), WithClient(client)); taskErr != nil {
client.Logger().Error(ctx, "error running "+broadcastTask+" task: "+taskErr.Error())
}
return nil
},
}); err != nil {
return err
}
// Run the task periodically
if err = tm.RunTask(ctx, &taskmanager.TaskOptions{
Arguments: []interface{}{m.Client()},
RunEveryPeriod: m.Client().GetTaskPeriod(broadcastTask),
TaskName: broadcastTask,
}); err != nil {
return err
}
// Register the task locally (cron task - set the defaults)
p2pTask := m.Name() + "_" + syncActionP2P
// Register the task
if err = tm.RegisterTask(&taskmanager.Task{
Name: p2pTask,
RetryLimit: 1,
Handler: func(client ClientInterface) error {
if taskErr := taskNotifyP2P(ctx, client.Logger(), WithClient(client)); taskErr != nil {
client.Logger().Error(ctx, "error running "+p2pTask+" task: "+taskErr.Error())
}
return nil
},
}); err != nil {
return err
}
// Run the task periodically
return tm.RunTask(ctx, &taskmanager.TaskOptions{
Arguments: []interface{}{m.Client()},
RunEveryPeriod: m.Client().GetTaskPeriod(p2pTask),
TaskName: p2pTask,
})
}
// Migrate model specific migration on startup
func (m *SyncTransaction) Migrate(client datastore.ClientInterface) error {
return client.IndexMetadata(client.GetTableName(tableSyncTransactions), metadataField)
}
// processSyncTransactions will process sync transaction records
func processSyncTransactions(ctx context.Context, maxTransactions int, opts ...ModelOps) error {
queryParams := &datastore.QueryParams{
Page: 1,
PageSize: maxTransactions,
OrderByField: "created_at",
SortDirection: "asc",
}
// Get x records
records, err := getTransactionsToSync(
ctx, queryParams, opts...,
)
if err != nil {
return err
} else if len(records) == 0 {
return nil
}
// Process the incoming transaction
for index := range records {
if err = processSyncTransaction(
ctx, records[index], nil,
); err != nil {
return err
}
}
return nil
}
// processBroadcastTransactions will process sync transaction records
func processBroadcastTransactions(ctx context.Context, maxTransactions int, opts ...ModelOps) error {
queryParams := &datastore.QueryParams{
Page: 1,
PageSize: maxTransactions,
OrderByField: createdAtField,
SortDirection: datastore.SortAsc,
}
// Get maxTransactions records, grouped by xpub
txsByXpub, err := getTransactionsToBroadcast(
ctx, queryParams, opts...,
)
if err != nil {
return err
} else if len(txsByXpub) == 0 {
return nil
}
wg := new(sync.WaitGroup)
// we limit the number of concurrent broadcasts to the number of cpus*2, since there is lots of IO wait
limit := make(chan bool, runtime.NumCPU()*2)
// Process the transactions per xpub, in parallel
for xPubID := range txsByXpub {
limit <- true // limit the number of routines running at the same time
wg.Add(1)
go func(xPubID string) {
defer wg.Done()
defer func() { <-limit }()
for _, tx := range txsByXpub[xPubID] {
if err = processBroadcastTransaction(
ctx, tx,
); err != nil {
tx.Client().Logger().Error(ctx,
fmt.Sprintf("error running broadcast tx for xpub %s, tx %s: %s", xPubID, tx.ID, err.Error()),
)
return // stop processing transactions for this xpub if we found an error
}
}
}(xPubID)
}
wg.Wait()
return nil
}
// processBroadcastTransaction will process a sync transaction record and broadcast it
func processBroadcastTransaction(ctx context.Context, syncTx *SyncTransaction) error {
// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
syncTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()
// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessBroadcastTx, syncTx.GetID()), syncTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
return err
}
// Get the transaction
var transaction *Transaction
var incomingTransaction *IncomingTransaction
var txHex string
if syncTx.transaction != nil && syncTx.transaction.Hex != "" {
// the transaction has already been retrieved and added to the syncTx object, just use that
transaction = syncTx.transaction
txHex = transaction.Hex
} else {
if transaction, err = getTransactionByID(
ctx, "", syncTx.ID, syncTx.GetOptions(false)...,
); err != nil {
return err
} else if transaction == nil {
// maybe this is only an incoming transaction, let's try to find that and broadcast
// the processing of incoming transactions should then pick it up in the next job run
if incomingTransaction, err = getIncomingTransactionByID(ctx, syncTx.ID, syncTx.GetOptions(false)...); err != nil {
return err
} else if incomingTransaction == nil {
return errors.New("transaction was expected but not found, using ID: " + syncTx.ID)
}
txHex = incomingTransaction.Hex
} else {
txHex = transaction.Hex
}
}
// Broadcast
var provider string
if provider, err = syncTx.Client().Chainstate().Broadcast(
ctx, syncTx.ID, txHex, defaultBroadcastTimeout,
); err != nil {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusError, syncActionBroadcast, provider, "broadcast error: "+err.Error(),
)
return nil //nolint:nolintlint,nilerr // error is not needed
}
// Create status message
message := "broadcast success"
// process the incoming transaction before finishing the sync
if incomingTransaction != nil {
// give the transaction some time to propagate through the network
time.Sleep(3 * time.Second)
// we don't need to handle the error here, this is only to speed up the processing
// job will pick it up later if needed
if err = processIncomingTransaction(ctx, nil, incomingTransaction); err == nil {
// again ignore the error, if an error occurs the transaction will be set and will be processed further
transaction, _ = getTransactionByID(ctx, "", syncTx.ID, syncTx.GetOptions(false)...)
}
}
// Update the sync information
syncTx.BroadcastStatus = SyncStatusComplete
syncTx.Results.LastMessage = message
syncTx.LastAttempt = customTypes.NullTime{
NullTime: sql.NullTime{
Time: time.Now().UTC(),
Valid: true,
},
}
// Trim the results to the last 20
if len(syncTx.Results.Results) >= 19 {
syncTx.Results.Results = syncTx.Results.Results[1:]
}
syncTx.Results.Results = append(syncTx.Results.Results, &SyncResult{
Action: syncActionBroadcast,
ExecutedAt: time.Now().UTC(),
Provider: provider,
StatusMessage: message,
})
// Update the P2P status
if syncTx.P2PStatus == SyncStatusPending {
syncTx.P2PStatus = SyncStatusReady
}
// Update sync status to be ready now
if syncTx.SyncStatus == SyncStatusPending {
syncTx.SyncStatus = SyncStatusReady
}
// Update the sync transaction record
if err = syncTx.Save(ctx); err != nil {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusError, syncActionBroadcast, "internal", err.Error(),
)
return err
}
// Fire a notification
notify(notifications.EventTypeBroadcast, syncTx)
// Notify any P2P paymail providers associated to the transaction
// but only if we actually found the transaction in the transactions' collection, otherwise this was an incoming
// transaction that needed to be broadcast and was not successfully processed after the broadcast
if transaction != nil {
if syncTx.P2PStatus == SyncStatusReady {
return processP2PTransaction(ctx, syncTx, transaction)
} else if syncTx.P2PStatus == SyncStatusSkipped && syncTx.SyncStatus == SyncStatusReady {
return processSyncTransaction(ctx, syncTx, transaction)
}
}
return nil
}
// processSyncTransaction will process the sync transaction record, or save the failure
func processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, transaction *Transaction) error {
// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
syncTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()
// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessSyncTx, syncTx.GetID()), syncTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
return err
}
// Find on-chain
var txInfo *chainstate.TransactionInfo
if txInfo, err = syncTx.Client().Chainstate().QueryTransactionFastest(
ctx, syncTx.ID, chainstate.RequiredOnChain, defaultQueryTxTimeout,
); err != nil {
if errors.Is(err, chainstate.ErrTransactionNotFound) {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusReady, syncActionSync, "all", "transaction not found on-chain",
)
return nil
}
return err
}
// Get the transaction
if transaction == nil {
if transaction, err = getTransactionByID(
ctx, "", syncTx.ID, syncTx.GetOptions(false)...,
); err != nil {
return err
}
}
if transaction == nil {
return ErrMissingTransaction
}
// Add additional information (if found on-chain)
transaction.BlockHash = txInfo.BlockHash
transaction.BlockHeight = uint64(txInfo.BlockHeight)
// Create status message
message := "transaction was found on-chain by " + txInfo.Provider
// Save the transaction (should NOT error)
if err = transaction.Save(ctx); err != nil {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusError, syncActionSync, "internal", err.Error(),
)
return err
}
// Update the sync status
syncTx.SyncStatus = SyncStatusComplete
syncTx.Results.LastMessage = message
syncTx.Results.Results = append(syncTx.Results.Results, &SyncResult{
Action: syncActionSync,
ExecutedAt: time.Now().UTC(),
Provider: txInfo.Provider,
StatusMessage: message,
})
// Update the sync transaction record
if err = syncTx.Save(ctx); err != nil {
bailAndSaveSyncTransaction(ctx, syncTx, SyncStatusError, syncActionSync, "internal", err.Error())
return err
}
// Done!
return nil
}
// processP2PTransactions will process transactions for p2p notifications
func processP2PTransactions(ctx context.Context, maxTransactions int, opts ...ModelOps) error {
queryParams := &datastore.QueryParams{
Page: 1,
PageSize: maxTransactions,
OrderByField: "created_at",
SortDirection: "asc",
}
// Get x records
records, err := getTransactionsToNotifyP2P(
ctx, queryParams, opts...,
)
if err != nil {
return err
} else if len(records) == 0 {
return nil
}
// Process the incoming transaction
for index := range records {
if err = processP2PTransaction(
ctx, records[index], nil,
); err != nil {
return err
}
}
return nil
}
// processP2PTransaction will process the sync transaction record, or save the failure
func processP2PTransaction(ctx context.Context, syncTx *SyncTransaction, transaction *Transaction) error {
// Successfully capture any panics, convert to readable string and log the error
defer func() {
if err := recover(); err != nil {
syncTx.Client().Logger().Error(ctx,
fmt.Sprintf(
"panic: %v - stack trace: %v", err,
strings.ReplaceAll(string(debug.Stack()), "\n", ""),
),
)
}
}()
// Create the lock and set the release for after the function completes
unlock, err := newWriteLock(
ctx, fmt.Sprintf(lockKeyProcessP2PTx, syncTx.GetID()), syncTx.Client().Cachestore(),
)
defer unlock()
if err != nil {
return err
}
// Get the transaction
if transaction == nil {
if transaction, err = getTransactionByID(
ctx, "", syncTx.ID, syncTx.GetOptions(false)...,
); err != nil {
return err
}
}
// No draft?
if len(transaction.DraftID) == 0 {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusComplete, syncActionP2P, "all", "no draft found, cannot complete p2p",
)
return nil
}
// Notify any P2P paymail providers associated to the transaction
var results []*SyncResult
if results, err = notifyPaymailProviders(ctx, transaction); err != nil {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusReady, syncActionP2P, "", err.Error(),
)
return err
}
// Update if we have some results
if len(results) > 0 {
syncTx.Results.Results = append(syncTx.Results.Results, results...)
syncTx.Results.LastMessage = fmt.Sprintf("notified %d paymail provider(s)", len(results))
}
// Save the record
syncTx.P2PStatus = SyncStatusComplete
if err = syncTx.Save(ctx); err != nil {
bailAndSaveSyncTransaction(
ctx, syncTx, SyncStatusError, syncActionP2P, "internal", err.Error(),
)
return err
}
// Done!
return nil
}
// bailAndSaveSyncTransaction will save the error message for a sync tx
func bailAndSaveSyncTransaction(ctx context.Context, syncTx *SyncTransaction, status SyncStatus,
action, provider, message string) {
if action == syncActionSync {
syncTx.SyncStatus = status
} else if action == syncActionP2P {
syncTx.P2PStatus = status
} else if action == syncActionBroadcast {
syncTx.BroadcastStatus = status
}
syncTx.LastAttempt = customTypes.NullTime{
NullTime: sql.NullTime{
Time: time.Now().UTC(),
Valid: true,
},
}
syncTx.Results.LastMessage = message
syncTx.Results.Results = append(syncTx.Results.Results, &SyncResult{
Action: action,
ExecutedAt: time.Now().UTC(),
Provider: provider,
StatusMessage: message,
})
_ = syncTx.Save(ctx)
}
// notifyPaymailProviders will notify any associated Paymail providers
func notifyPaymailProviders(ctx context.Context, transaction *Transaction) ([]*SyncResult, error) {
// First get the draft tx
draftTx, err := getDraftTransactionID(
ctx,
transaction.xPubID,
transaction.DraftID,
transaction.GetOptions(false)...,
)
if err != nil {
return nil, err
} else if draftTx == nil {
return nil, errors.New("draft not found: " + transaction.DraftID)
}
// Loop each output looking for paymail outputs
var attempts []*SyncResult
pm := transaction.Client().PaymailClient()
var payload *paymail.P2PTransactionPayload
for _, out := range draftTx.Configuration.Outputs {
if out.PaymailP4 != nil && out.PaymailP4.ResolutionType == ResolutionTypeP2P {
// Notify each provider with the transaction
if payload, err = finalizeP2PTransaction(
pm,
out.PaymailP4.Alias,
out.PaymailP4.Domain,
out.PaymailP4.ReceiveEndpoint,
out.PaymailP4.ReferenceID,
out.PaymailP4.Note,
out.PaymailP4.FromPaymail,
transaction.Hex,
); err != nil {
return nil, err
}
attempts = append(attempts, &SyncResult{
Action: syncActionP2P,
ExecutedAt: time.Now().UTC(),
Provider: out.PaymailP4.ReceiveEndpoint,
StatusMessage: "success: " + payload.TxID,
})
}
}
return attempts, nil
}