This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrollup.go
214 lines (182 loc) · 5.77 KB
/
rollup.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
package commander
import (
"context"
"time"
"github.com/Worldcoin/hubble-commander/commander/executor"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
)
func (c *Commander) manageRollupLoop(isProposer bool) {
rollupLoopRunning := c.isRollupLoopActive()
if isProposer && !rollupLoopRunning && c.batchCreationEnabled {
log.Debugf("Commander is an active proposer, starting rollupLoop")
c.startRollupLoop()
} else if !isProposer && rollupLoopRunning {
log.Debugf("Commander is no longer an active proposer, stoppping rollupLoop")
c.stopRollupLoop()
}
}
func (c *Commander) startRollupLoop() {
if c.isRollupLoopActive() {
return
}
ctx, cancel := context.WithCancel(c.workersContext)
c.startWorker("Rollup Loop", func() error { return c.rollupLoop(ctx) })
c.cancelRollupLoop = cancel
c.setRollupLoopActive(true)
}
func (c *Commander) stopRollupLoop() {
if !c.isRollupLoopActive() {
return
}
if c.cancelRollupLoop != nil {
c.cancelRollupLoop()
}
c.setRollupLoopActive(false)
}
func (c *Commander) rollupLoop(ctx context.Context) (err error) {
ticker := time.NewTicker(c.cfg.Rollup.BatchLoopInterval)
defer ticker.Stop()
currentBatchType := batchtype.Transfer
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
err = c.rollupLoopIteration(ctx, ¤tBatchType)
if err != nil {
return err
}
}
}
}
func (c *Commander) rollupLoopIteration(ctx context.Context, currentBatchType *batchtype.BatchType) (err error) {
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
err = c.unsafeRollupLoopIteration(ctx, currentBatchType)
if errors.Is(err, executor.ErrNotEnoughDeposits) {
return c.unsafeRollupLoopIteration(ctx, currentBatchType)
}
return err
}
func (c *Commander) unsafeRollupLoopIteration(ctx context.Context, currentBatchType *batchtype.BatchType) (err error) {
spanCtx, span := rollupTracer.Start(ctx, "RollupLoop")
defer span.End()
err = validateStateRoot(c.storage)
if err != nil {
return err
}
rollupCtx := executor.NewRollupLoopContext(c.storage, c.client, c.cfg.Rollup, c.metrics, spanCtx, *currentBatchType)
defer rollupCtx.Rollback(&err)
span.SetAttributes(attribute.String("hubble.batchType", currentBatchType.String()))
// this chooses the type of the next batch, currentBatchType is not read once
// the rollupCtx has been created.
switchBatchType(currentBatchType)
var (
batch *models.Batch
commitmentsCount *int
)
duration, err := metrics.MeasureDuration(func() error {
batch, commitmentsCount, err = rollupCtx.CreateAndSubmitBatch(spanCtx)
return err
})
if errors.Is(err, executor.ErrNotEnoughTxs) || errors.Is(err, executor.ErrNotEnoughDeposits) {
// tell datadog to ignore this trace, we didn't do anything
// this requires custom configuration of the dd agent:
// apm_config.filter_tags.reject = ["manual.drop:true"]
// if we don't do this then ~ every 500µs we emit a new trace
// https://docs.datadoghq.com/tracing/guide/ignoring_apm_resources/?tab=datadogyaml#ignoring-based-on-span-tags
span.SetAttributes(attribute.Bool("manual.drop", true))
}
var rollupError *executor.RollupError
if errors.As(err, &rollupError) {
rollupCtx.Rollback(&err)
return c.handleRollupError(rollupError)
}
if err != nil {
return err
}
metrics.SaveHistogramMeasurement(duration, c.metrics.BatchBuildAndSubmissionDuration, prometheus.Labels{
"type": metrics.BatchTypeToMetricsBatchType(batch.Type),
})
logNewBatch(batch, *commitmentsCount, duration)
err = func() error {
_, span := rollupTracer.Start(spanCtx, "rollupCtx.commit")
defer span.End()
return rollupCtx.Commit()
}()
if err != nil {
return err
}
return nil
}
func (c *Commander) updateMempoolMetrics() error {
allMempoolTxs, err := c.storage.GetAllMempoolTransactions()
if err != nil {
return err
}
transferCount, c2tCount, mmCount := 0, 0, 0
for i := range allMempoolTxs {
switch allMempoolTxs[i].TxType {
case txtype.Transfer:
transferCount += 1
case txtype.Create2Transfer:
c2tCount += 1
case txtype.MassMigration:
mmCount += 1
default:
panic("unknown tx type")
}
}
c.metrics.MempoolSize.Set(float64(len(allMempoolTxs)))
c.metrics.MempoolSizeTransfer.Set(float64(transferCount))
c.metrics.MempoolSizeCreate2Transfer.Set(float64(c2tCount))
c.metrics.MempoolSizeMassMigration.Set(float64(mmCount))
return nil
}
func switchBatchType(batchType *batchtype.BatchType) {
switch *batchType {
case batchtype.Transfer:
*batchType = batchtype.Create2Transfer
case batchtype.Create2Transfer:
*batchType = batchtype.MassMigration
case batchtype.MassMigration:
*batchType = batchtype.Deposit
case batchtype.Deposit:
*batchType = batchtype.Transfer
case batchtype.Genesis:
panic("Not supported")
}
}
func (c *Commander) handleRollupError(err *executor.RollupError) error {
if err.IsLoggable {
log.Warnf("%+v", err)
}
if errors.Is(err, executor.ErrNotEnoughDeposits) {
return err
}
return nil
}
func logNewBatch(batch *models.Batch, commitmentsCount int, duration *time.Duration) {
log.Printf(
"Submitted a %s batch with %d commitment(s) on chain in %s. Batch ID: %d. Transaction hash: %v",
batch.Type.String(),
commitmentsCount,
duration,
batch.ID.Uint64(),
batch.TransactionHash,
)
}
func logLatestCommitment(latestCommitment *models.CommitmentBase) {
fields := log.Fields{
"latestBatchID": latestCommitment.ID.BatchID.String(),
"latestCommitmentID": latestCommitment.ID.IndexInBatch,
}
log.WithFields(fields).Error("rollupLoop: Sanity check on state tree root failed")
}