/
confirmer.go
148 lines (124 loc) · 3.99 KB
/
confirmer.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
package bundle
import (
"cmp"
"database/sql"
"github.com/warp-contracts/syncer/src/utils/config"
"github.com/warp-contracts/syncer/src/utils/listener"
"github.com/warp-contracts/syncer/src/utils/model"
"github.com/warp-contracts/syncer/src/utils/monitoring"
"github.com/warp-contracts/syncer/src/utils/task"
"github.com/jackc/pgtype"
"golang.org/x/exp/slices"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Periodically saves the confirmation state of the bundlet interactions
// This is done to prevent flooding database with bundle_items state updates
type Confirmer struct {
*task.SinkTask[*Confirmation]
monitor monitoring.Monitor
db *gorm.DB
networkMonitor *listener.NetworkMonitor
}
type Confirmation struct {
InteractionID int
BundlerTxID string
Response pgtype.JSONB
Service pgtype.Text
}
func NewConfirmer(config *config.Config) (self *Confirmer) {
self = new(Confirmer)
self.SinkTask = task.NewSinkTask[*Confirmation](config, "confirmer").
WithBatchSize(config.Bundler.ConfirmerBatchSize).
WithOnFlush(config.Bundler.ConfirmerInterval, self.save).
WithBackoff(config.Bundler.ConfirmerBackoffMaxElapsedTime, config.Bundler.ConfirmerBackoffMaxInterval)
return
}
func (self *Confirmer) WithDB(db *gorm.DB) *Confirmer {
self.db = db
return self
}
func (self *Confirmer) WithInputChannel(input chan *Confirmation) *Confirmer {
self.SinkTask.WithInputChannel(input)
return self
}
func (self *Confirmer) WithMonitor(monitor monitoring.Monitor) *Confirmer {
self.monitor = monitor
return self
}
func (self *Confirmer) WithNetworkMonitor(v *listener.NetworkMonitor) *Confirmer {
self.networkMonitor = v
return self
}
func (self *Confirmer) save(confirmations []*Confirmation) error {
if len(confirmations) == 0 {
// Nothing to save
return nil
}
self.Log.WithField("len", len(confirmations)).Trace("Saving confirmations to DB")
// Sort confirmations by interaction ID to minimize deadlocks
slices.SortFunc(confirmations, func(a, b *Confirmation) int {
return cmp.Compare(a.InteractionID, b.InteractionID)
})
// Prepare ids
ids := make([]int, len(confirmations))
for i, confirmation := range confirmations {
ids[i] = confirmation.InteractionID
}
// Network manager updates this value
// NOTE: This can potentially block if NetworkMonitor can't get the first height
currentBlockHeight := self.networkMonitor.GetLastNetworkInfo().Height
// Uses one transaction to do all the updates
// NOTE: It still uses many requests to the database,
// it should be possible to combine updates into batches, but it's not a priority for now.
err := self.db.Transaction(func(tx *gorm.DB) (err error) {
err = tx.Table(model.TableInteraction).
Select("1").
Where("id IN ?", ids).
Clauses(clause.Locking{Strength: "UPDATE"}).
Error
if err != nil {
return
}
err = tx.Table(model.TableBundleItem).
Select("1").
Where("interaction_id IN ?", ids).
Clauses(clause.Locking{Strength: "UPDATE"}).
Error
if err != nil {
return
}
for _, confirmation := range confirmations {
err := tx.Table(model.TableBundleItem).
Where("interaction_id = ?", confirmation.InteractionID).
Where("state = ?", model.BundleStateUploading).
Updates(model.BundleItem{
State: model.BundleStateUploaded,
BlockHeight: sql.NullInt64{Int64: currentBlockHeight, Valid: true},
BundlrResponse: confirmation.Response,
Service: confirmation.Service,
}).
Error
if err != nil {
return err
}
err = tx.Table(model.TableInteraction).
Where("id = ?", confirmation.InteractionID).
Update("bundler_tx_id", confirmation.BundlerTxID).
Error
if err != nil {
return err
}
}
return nil
})
if err != nil {
self.Log.WithError(err).Error("Failed to save bundle items, retrying...")
// Update monitoring
self.monitor.GetReport().Bundler.Errors.ConfirmationsSavedToDbError.Inc()
return err
}
// Update monitoring
self.monitor.GetReport().Bundler.State.ConfirmationsSavedToDb.Inc()
return nil
}