Skip to content

Commit 5d83851

Browse files
authored
Merge pull request #488 from shutter-network/contract-event-index
Use event index from contract instead of local counter
2 parents f21a5db + 42e7a9b commit 5d83851

File tree

5 files changed

+40
-69
lines changed

5 files changed

+40
-69
lines changed

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

Lines changed: 14 additions & 25 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: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ INSERT INTO transaction_submitted_event (
1111
gas_limit
1212
)
1313
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
14-
ON CONFLICT DO NOTHING;
14+
ON CONFLICT (index, eon) DO UPDATE SET
15+
block_number = $2,
16+
block_hash = $3,
17+
tx_index = $4,
18+
log_index = $5,
19+
identity_prefix = $7,
20+
sender = $8,
21+
gas_limit = $9;
1522

1623
-- name: GetTransactionSubmittedEvents :many
1724
SELECT * FROM transaction_submitted_event
18-
WHERE eon = $1 AND index >= $2
25+
WHERE eon = $1 AND index >= $2 AND index < $2 + $3
1926
ORDER BY index ASC
2027
LIMIT $3;
2128

@@ -27,16 +34,9 @@ SET block_hash = $1, block_number = $2, slot = $3;
2734
-- name: GetTransactionSubmittedEventsSyncedUntil :one
2835
SELECT * FROM transaction_submitted_events_synced_until LIMIT 1;
2936

30-
-- name: SetTransactionSubmittedEventCount :exec
31-
INSERT INTO transaction_submitted_event_count (eon, event_count)
32-
VALUES ($1, $2)
33-
ON CONFLICT (eon) DO UPDATE
34-
SET event_count = $2;
35-
3637
-- name: GetTransactionSubmittedEventCount :one
37-
SELECT event_count FROM transaction_submitted_event_count
38-
WHERE eon = $1
39-
LIMIT 1;
38+
SELECT max(index) + 1 FROM transaction_submitted_event
39+
WHERE eon = $1;
4040

4141
-- name: GetTxPointer :one
4242
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: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,9 @@ 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 {
157-
nextEventIndex, ok := nextEventIndices[event.Eon]
158-
if !ok {
159-
nextEventIndexFromDB, err := queries.GetTransactionSubmittedEventCount(ctx, int64(event.Eon))
160-
if err == pgx.ErrNoRows {
161-
nextEventIndexFromDB = 0
162-
} else if err != nil {
163-
return errors.Wrapf(err, "failed to query count of transaction submitted events for eon %d", event.Eon)
164-
}
165-
nextEventIndices[event.Eon] = nextEventIndexFromDB
166-
nextEventIndex = nextEventIndexFromDB
167-
}
168-
169156
_, err := queries.InsertTransactionSubmittedEvent(ctx, database.InsertTransactionSubmittedEventParams{
170-
Index: nextEventIndex,
157+
Index: int64(event.TxIndex),
171158
BlockNumber: int64(event.Raw.BlockNumber),
172159
BlockHash: event.Raw.BlockHash[:],
173160
TxIndex: int64(event.Raw.TxIndex),
@@ -180,25 +167,15 @@ func (s *SequencerSyncer) insertTransactionSubmittedEvents(
180167
if err != nil {
181168
return errors.Wrap(err, "failed to insert transaction submitted event into db")
182169
}
183-
metricsLatestTxSubmittedEventIndex.WithLabelValues(fmt.Sprint(event.Eon)).Set(float64(nextEventIndex))
184-
nextEventIndices[event.Eon]++
170+
metricsLatestTxSubmittedEventIndex.WithLabelValues(fmt.Sprint(event.Eon)).Set(float64(event.TxIndex))
185171
log.Debug().
186-
Int64("index", nextEventIndex).
172+
Uint64("index", event.TxIndex).
187173
Uint64("block", event.Raw.BlockNumber).
188174
Uint64("eon", event.Eon).
189175
Hex("identityPrefix", event.IdentityPrefix[:]).
190176
Hex("sender", event.Sender.Bytes()).
191177
Uint64("gasLimit", event.GasLimit.Uint64()).
192178
Msg("synced new transaction submitted event")
193179
}
194-
for eon, nextEventIndex := range nextEventIndices {
195-
err := queries.SetTransactionSubmittedEventCount(ctx, database.SetTransactionSubmittedEventCountParams{
196-
Eon: int64(eon),
197-
EventCount: nextEventIndex,
198-
})
199-
if err != nil {
200-
return err
201-
}
202-
}
203180
return nil
204181
}

0 commit comments

Comments
 (0)