This repository has been 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 pathbatches.go
206 lines (172 loc) · 5.64 KB
/
batches.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
package commander
import (
"context"
"errors"
"github.com/Worldcoin/hubble-commander/commander/disputer"
"github.com/Worldcoin/hubble-commander/commander/executor"
"github.com/Worldcoin/hubble-commander/commander/syncer"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
)
var ErrSyncedFraudulentBatch = errors.New("commander synced fraudulent batch")
func (c *Commander) syncBatches(ctx context.Context, startBlock, endBlock uint64) error {
spanCtx, span := newBlockTracer.Start(ctx, "syncBatches")
defer span.End()
c.stateMutex.Lock()
defer c.stateMutex.Unlock()
duration, err := metrics.MeasureDuration(func() error {
return c.unsafeSyncBatches(spanCtx, startBlock, endBlock)
})
if err != nil {
return err
}
metrics.SaveHistogramMeasurement(duration, c.metrics.SyncingMethodDuration, prometheus.Labels{
"method": metrics.SyncBatchesMethod,
})
return nil
}
func (c *Commander) unsafeSyncBatches(ctx context.Context, startBlock, endBlock uint64) error {
latestBatchID, err := c.getLatestBatchID()
if err != nil {
return err
}
if c.invalidBatchID != nil && latestBatchID.Cmp(c.invalidBatchID) >= 0 {
return ErrSyncedFraudulentBatch
}
filter := func(batchID *models.Uint256) bool {
if batchID.Cmp(latestBatchID) <= 0 {
log.Printf("Batch #%d already synced. Skipping...", batchID.Uint64())
return false
}
if c.invalidBatchID != nil && batchID.Cmp(c.invalidBatchID) >= 0 {
log.Printf("Batch #%d after dispute. Skipping...", batchID.Uint64())
return false
}
return true
}
newRemoteBatches, err := c.client.GetBatches(
ctx,
ð.BatchesFilters{
StartBlockInclusive: startBlock,
EndBlockInclusive: &endBlock,
FilterByBatchID: filter,
},
)
if err != nil {
return err
}
for _, remoteBatch := range newRemoteBatches {
err = c.syncRemoteBatch(ctx, remoteBatch)
if err != nil {
return err
}
err = c.syncPendingStakeWithdrawal(remoteBatch)
if err != nil {
return err
}
select {
case <-c.workersContext.Done():
return ErrIncompleteBlockRangeSync
default:
}
}
return nil
}
func (c *Commander) syncRemoteBatch(ctx context.Context, remoteBatch eth.DecodedBatch) error {
var icError *syncer.InconsistentBatchError
_, span := newBlockTracer.Start(ctx, "syncRemoteBatch")
defer span.End()
span.SetAttributes(
attribute.Int64("hubble.batchID", int64(remoteBatch.GetBase().ID.Uint64())),
attribute.String("hubble.batchType", remoteBatch.GetBase().Type.String()),
attribute.String("hubble.batchHash", remoteBatch.GetBase().Hash.String()),
attribute.Int("hubble.commitmentCount", remoteBatch.GetCommitmentsLength()),
)
err := c.syncOrDisputeRemoteBatch(remoteBatch)
if errors.As(err, &icError) {
return c.replaceBatch(icError.LocalBatch, remoteBatch)
}
return err
}
func (c *Commander) syncOrDisputeRemoteBatch(remoteBatch eth.DecodedBatch) error {
var disputableErr *syncer.DisputableError
err := c.syncBatch(remoteBatch)
if errors.As(err, &disputableErr) {
logFraudulentBatch(&remoteBatch.GetBase().ID, disputableErr.Reason)
return c.disputeFraudulentBatch(remoteBatch.ToDecodedTxBatch(), disputableErr)
}
return err
}
func (c *Commander) syncBatch(remoteBatch eth.DecodedBatch) (err error) {
syncCtx := syncer.NewContext(c.storage, c.client, c.cfg.Rollup, remoteBatch.GetBase().Type)
defer syncCtx.Rollback(&err)
err = syncCtx.SyncBatch(remoteBatch)
if err != nil {
return err
}
return syncCtx.Commit()
}
func (c *Commander) syncPendingStakeWithdrawal(remoteBatch eth.DecodedBatch) error {
batchBase := remoteBatch.GetBase()
if batchBase.Committer == c.blockchain.GetAccount().From {
err := c.storage.AddPendingStakeWithdrawal(&models.PendingStakeWithdrawal{
BatchID: batchBase.ID,
FinalisationBlock: batchBase.FinalisationBlock,
})
if err != nil {
return err
}
}
return nil
}
func (c *Commander) replaceBatch(localBatch *models.Batch, remoteBatch eth.DecodedBatch) error {
log.Warnf("Reverting local batch(es) with ID(s) greater or equal to %s", localBatch.ID.String())
err := c.revertBatches(localBatch)
if err != nil {
return err
}
return c.syncOrDisputeRemoteBatch(remoteBatch)
}
func (c *Commander) disputeFraudulentBatch(
remoteBatch *eth.DecodedTxBatch,
disputableErr *syncer.DisputableError,
) (err error) {
disputeCtx := disputer.NewContext(c.storage, c.client)
switch disputableErr.Type {
case syncer.Transition:
err = disputeCtx.DisputeTransition(remoteBatch, disputableErr.CommitmentIndex, disputableErr.Proofs)
case syncer.Signature:
err = disputeCtx.DisputeSignature(remoteBatch, disputableErr.CommitmentIndex, disputableErr.Proofs)
}
if err != nil {
return err
}
return ErrRollbackInProgress
}
func (c *Commander) revertBatches(startBatch *models.Batch) (err error) {
executionCtx := executor.NewExecutionContext(c.storage, c.client, c.cfg.Rollup, c.metrics, context.Background())
defer executionCtx.Rollback(&err)
err = executionCtx.RevertBatches(startBatch)
if err != nil {
return err
}
return executionCtx.Commit()
}
func (c *Commander) getLatestBatchID() (*models.Uint256, error) {
latestBatch, err := c.storage.GetLatestSubmittedBatch()
if st.IsNotFoundError(err) {
return models.NewUint256(0), nil
} else if err != nil {
return nil, err
}
return &latestBatch.ID, nil
}
func logFraudulentBatch(batchID *models.Uint256, reason string) {
log.WithFields(log.Fields{"batchID": batchID.String()}).
Infof("Found fraudulent batch. Reason: %s", reason)
}