Skip to content

Commit 1b73c2a

Browse files
committed
Store index of transaction submitted events
1 parent 1178aab commit 1b73c2a

File tree

5 files changed

+117
-17
lines changed

5 files changed

+117
-17
lines changed

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

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 6 additions & 0 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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- name: InsertTransactionSubmittedEvent :execresult
22
INSERT INTO transaction_submitted_event (
3+
index,
34
block_number,
45
block_hash,
56
tx_index,
@@ -9,7 +10,7 @@ INSERT INTO transaction_submitted_event (
910
sender,
1011
gas_limit
1112
)
12-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
13+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
1314
ON CONFLICT DO NOTHING;
1415

1516
-- name: SetTransactionSubmittedEventsSyncedUntil :exec
@@ -20,6 +21,17 @@ SET block_number = $1;
2021
-- name: GetTransactionSubmittedEventsSyncedUntil :one
2122
SELECT block_number FROM transaction_submitted_events_synced_until LIMIT 1;
2223

24+
-- name: SetTransactionSubmittedEventCount :exec
25+
INSERT INTO transaction_submitted_event_count (eon, event_count)
26+
VALUES ($1, $2)
27+
ON CONFLICT (eon) DO UPDATE
28+
SET event_count = $2;
29+
30+
-- name: GetTransactionSubmittedEventCount :one
31+
SELECT event_count FROM transaction_submitted_event_count
32+
WHERE eon = $1
33+
LIMIT 1;
34+
2335
-- name: GetLocalTxPointer :one
2436
SELECT local, local_block FROM tx_pointer LIMIT 1;
2537

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
-- the schema. We'll use this to check we're using the right schema.
44

55
CREATE TABLE transaction_submitted_event (
6-
block_number bigint CHECK (block_number >= 0),
7-
block_hash bytea,
8-
tx_index bigint CHECK (tx_index >= 0),
9-
log_index bigint CHECK (log_index >= 0),
6+
index bigint PRIMARY KEY CHECK (index >= 0),
7+
block_number bigint NOT NULL CHECK (block_number >= 0),
8+
block_hash bytea NOT NULL,
9+
tx_index bigint NOT NULL CHECK (tx_index >= 0),
10+
log_index bigint NOT NULL CHECK (log_index >= 0),
1011
eon bigint NOT NULL CHECK (eon >= 0),
1112
identity_prefix bytea NOT NULL,
1213
sender text NOT NULL,
13-
gas_limit bigint NOT NULL CHECK (gas_limit >= 0),
14-
PRIMARY KEY (block_number, block_hash, tx_index, log_index)
14+
gas_limit bigint NOT NULL CHECK (gas_limit >= 0)
1515
);
1616

1717
CREATE TABLE transaction_submitted_events_synced_until(
1818
enforce_one_row bool PRIMARY KEY DEFAULT true,
19-
block_number bigint NOT NULL CHECK (block_number > 0)
19+
block_number bigint NOT NULL CHECK (block_number >= 0)
20+
);
21+
22+
CREATE TABLE transaction_submitted_event_count(
23+
eon bigint PRIMARY KEY,
24+
event_count bigint NOT NULL DEFAULT 0 CHECK (event_count >= 0)
2025
);
2126

2227
-- tx_pointer stores what we know about the current value of the tx pointer. There are two

rolling-shutter/keyperimpl/gnosis/sequencersyncer.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (s *SequencerSyncer) Sync(ctx context.Context, block uint64) error {
3333
if err == pgx.ErrNoRows {
3434
start = 0
3535
} else {
36-
start = uint64(syncedUntilBlock) + 1
36+
start = uint64(syncedUntilBlock + 1)
3737
}
3838

3939
log.Debug().
@@ -69,19 +69,56 @@ func (s *SequencerSyncer) Sync(ctx context.Context, block uint64) error {
6969
if it.Error() != nil {
7070
return errors.Wrap(it.Error(), "failed to iterate transaction submitted events")
7171
}
72-
7372
if len(events) == 0 {
7473
log.Debug().
7574
Uint64("start-block", start).
7675
Uint64("end-block", block).
7776
Msg("no transaction submitted events found")
78-
return nil
7977
}
8078

8179
return s.DBPool.BeginFunc(ctx, func(tx pgx.Tx) error {
80+
err = s.insertTransactionSubmittedEvents(ctx, tx, events)
81+
if err != nil {
82+
return err
83+
}
84+
85+
newSyncedUntilBlock, err := medley.Uint64ToInt64Safe(block)
86+
if err != nil {
87+
return err
88+
}
89+
err = queries.SetTransactionSubmittedEventsSyncedUntil(ctx, newSyncedUntilBlock)
90+
if err != nil {
91+
return err
92+
}
93+
return nil
94+
})
95+
}
96+
97+
// insertTransactionSubmittedEvents inserts the given events into the database and updates the
98+
// transaction submitted event number accordingly.
99+
func (s *SequencerSyncer) insertTransactionSubmittedEvents(
100+
ctx context.Context,
101+
tx pgx.Tx,
102+
events []*sequencerBindings.SequencerTransactionSubmitted,
103+
) error {
104+
if len(events) > 0 {
82105
queries := database.New(tx)
106+
nextEventIndices := make(map[uint64]int64)
83107
for _, event := range events {
108+
nextEventIndex, ok := nextEventIndices[event.Eon]
109+
if !ok {
110+
nextEventIndexFromDB, err := queries.GetTransactionSubmittedEventCount(ctx, int64(event.Eon))
111+
if err == pgx.ErrNoRows {
112+
nextEventIndexFromDB = 0
113+
} else if err != nil {
114+
return errors.Wrapf(err, "failed to query count of transaction submitted events for eon %d", event.Eon)
115+
}
116+
nextEventIndices[event.Eon] = nextEventIndexFromDB
117+
nextEventIndex = nextEventIndexFromDB
118+
}
119+
84120
_, err := queries.InsertTransactionSubmittedEvent(ctx, database.InsertTransactionSubmittedEventParams{
121+
Index: nextEventIndex,
85122
BlockNumber: int64(event.Raw.BlockNumber),
86123
BlockHash: event.Raw.BlockHash[:],
87124
TxIndex: int64(event.Raw.TxIndex),
@@ -94,18 +131,25 @@ func (s *SequencerSyncer) Sync(ctx context.Context, block uint64) error {
94131
if err != nil {
95132
return errors.Wrap(err, "failed to insert transaction submitted event into db")
96133
}
134+
nextEventIndices[event.Eon]++
97135
log.Debug().
136+
Int64("index", nextEventIndex).
98137
Uint64("block", event.Raw.BlockNumber).
99138
Uint64("eon", event.Eon).
100139
Hex("identityPrefix", event.IdentityPrefix[:]).
101140
Hex("sender", event.Sender.Bytes()).
102141
Uint64("gasLimit", event.GasLimit.Uint64()).
103142
Msg("synced new transaction submitted event")
104143
}
105-
newSyncedUntilBlock, err := medley.Uint64ToInt64Safe(block)
106-
if err != nil {
107-
return err
144+
for eon, nextEventIndex := range nextEventIndices {
145+
err := queries.SetTransactionSubmittedEventCount(ctx, database.SetTransactionSubmittedEventCountParams{
146+
Eon: int64(eon),
147+
EventCount: nextEventIndex,
148+
})
149+
if err != nil {
150+
return err
151+
}
108152
}
109-
return queries.SetTransactionSubmittedEventsSyncedUntil(ctx, newSyncedUntilBlock)
110-
})
153+
}
154+
return nil
111155
}

0 commit comments

Comments
 (0)