Skip to content

Commit 42e7a9b

Browse files
committed
Reduce database redundancy
We used to explicitly count the number of txs inserted into the db. This is the value we reset the tx pointer if key generation fails. However, if for some reason, eg due to a reorg, some tx events are missed, this value is a bad choice because the other keypers will likely disagree. Now we use the index of the latest inserted tx instead which is automatically corrected with every new tx, irrespective of what happened in the past.
1 parent 638efb1 commit 42e7a9b

File tree

5 files changed

+19
-50
lines changed

5 files changed

+19
-50
lines changed

rolling-shutter/keyperimpl/gnosis/database/gnosiskeyper.sqlc.gen.go

Lines changed: 5 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rolling-shutter/keyperimpl/gnosis/database/sql/queries/gnosiskeyper.sql

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@ SET block_hash = $1, block_number = $2, slot = $3;
3434
-- name: GetTransactionSubmittedEventsSyncedUntil :one
3535
SELECT * FROM transaction_submitted_events_synced_until LIMIT 1;
3636

37-
-- name: SetTransactionSubmittedEventCount :exec
38-
INSERT INTO transaction_submitted_event_count (eon, event_count)
39-
VALUES ($1, $2)
40-
ON CONFLICT (eon) DO UPDATE
41-
SET event_count = $2;
42-
4337
-- name: GetTransactionSubmittedEventCount :one
44-
SELECT event_count FROM transaction_submitted_event_count
45-
WHERE eon = $1
46-
LIMIT 1;
38+
SELECT max(index) + 1 FROM transaction_submitted_event
39+
WHERE eon = $1;
4740

4841
-- name: GetTxPointer :one
4942
SELECT * FROM tx_pointer

rolling-shutter/keyperimpl/gnosis/newslot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ func getTxPointer(ctx context.Context, db *pgxpool.Pool, eon int64, maxTxPointer
219219
Int64("tx-pointer", txPointer).
220220
Int64("tx-pointer-age", txPointerAge).
221221
Msg("outdated tx pointer")
222-
txPointer, err = gnosisKeyperDB.GetTransactionSubmittedEventCount(ctx, eon)
222+
txPointerInt32, err := gnosisKeyperDB.GetTransactionSubmittedEventCount(ctx, eon)
223+
txPointer = int64(txPointerInt32)
223224
if err == pgx.ErrNoRows {
224225
txPointer = 0
225226
} else if err != nil {

rolling-shutter/keyperimpl/gnosis/newslot_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ func TestGetTxPointerInfiniteIntegration(t *testing.T) {
4848
Value: 5,
4949
})
5050
assert.NilError(t, err)
51-
err = db.SetTransactionSubmittedEventCount(ctx, gnosisDatabase.SetTransactionSubmittedEventCountParams{
52-
Eon: 2,
53-
EventCount: 10,
51+
_, err = db.InsertTransactionSubmittedEvent(ctx, gnosisDatabase.InsertTransactionSubmittedEventParams{
52+
Index: 9,
53+
Eon: 2,
54+
BlockHash: []byte{},
55+
IdentityPrefix: []byte{},
5456
})
5557
assert.NilError(t, err)
5658

@@ -75,9 +77,11 @@ func TestGetTxPointerOutdatedIntegration(t *testing.T) {
7577
Value: 5,
7678
})
7779
assert.NilError(t, err)
78-
err = db.SetTransactionSubmittedEventCount(ctx, gnosisDatabase.SetTransactionSubmittedEventCountParams{
79-
Eon: 2,
80-
EventCount: 10,
80+
_, err = db.InsertTransactionSubmittedEvent(ctx, gnosisDatabase.InsertTransactionSubmittedEventParams{
81+
Index: 9,
82+
Eon: 2,
83+
BlockHash: []byte{},
84+
IdentityPrefix: []byte{},
8185
})
8286
assert.NilError(t, err)
8387

rolling-shutter/keyperimpl/gnosis/sequencersyncer.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ func (s *SequencerSyncer) insertTransactionSubmittedEvents(
152152
events []*sequencerBindings.SequencerTransactionSubmitted,
153153
) error {
154154
queries := database.New(tx)
155-
nextEventIndices := make(map[uint64]int64)
156155
for _, event := range events {
157156
_, err := queries.InsertTransactionSubmittedEvent(ctx, database.InsertTransactionSubmittedEventParams{
158157
Index: int64(event.TxIndex),
@@ -169,7 +168,6 @@ func (s *SequencerSyncer) insertTransactionSubmittedEvents(
169168
return errors.Wrap(err, "failed to insert transaction submitted event into db")
170169
}
171170
metricsLatestTxSubmittedEventIndex.WithLabelValues(fmt.Sprint(event.Eon)).Set(float64(event.TxIndex))
172-
nextEventIndices[event.Eon]++
173171
log.Debug().
174172
Uint64("index", event.TxIndex).
175173
Uint64("block", event.Raw.BlockNumber).
@@ -179,14 +177,5 @@ func (s *SequencerSyncer) insertTransactionSubmittedEvents(
179177
Uint64("gasLimit", event.GasLimit.Uint64()).
180178
Msg("synced new transaction submitted event")
181179
}
182-
for eon, nextEventIndex := range nextEventIndices {
183-
err := queries.SetTransactionSubmittedEventCount(ctx, database.SetTransactionSubmittedEventCountParams{
184-
Eon: int64(eon),
185-
EventCount: nextEventIndex,
186-
})
187-
if err != nil {
188-
return err
189-
}
190-
}
191180
return nil
192181
}

0 commit comments

Comments
 (0)