Skip to content

Commit 614e18a

Browse files
authored
Merge pull request #1110 from oasisprotocol/ptrus/feature/fast-sync-block-signers
analyzer/consensus: Fix block signers during fast-sync
2 parents f59d394 + 3584968 commit 614e18a

File tree

17 files changed

+329880
-9
lines changed

17 files changed

+329880
-9
lines changed

.changelog/1110.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
analyzer/consensus: Fix block signers during fast-sync

analyzer/consensus/consensus.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ func (m *processor) aggregateFastSyncTables(ctx context.Context) error {
200200
m.logger.Info("computing epoch boundaries for epochs scanned during fast-sync")
201201
batch.Queue(queries.ConsensusEpochsRecompute)
202202
batch.Queue("DELETE FROM todo_updates.epochs")
203+
batch.Queue(queries.ConsensusBlockSignersFinalize)
204+
batch.Queue("DELETE FROM todo_updates.block_signers")
203205

204206
if err := m.target.SendBatch(ctx, batch); err != nil {
205207
return err
@@ -455,11 +457,24 @@ func (m *processor) queueBlockInserts(batch *storage.QueryBatch, data *consensus
455457
}
456458
prevSigners = append(prevSigners, entity.String())
457459
}
458-
batch.Queue(
459-
queries.ConsensusBlockAddSigners,
460-
cmtMeta.LastCommit.Height,
461-
prevSigners,
462-
)
460+
switch m.mode {
461+
case analyzer.FastSyncMode:
462+
// During fast-sync, blocks are processed out of order, meaning the parent block may not yet be available.
463+
// To avoid missing dependencies, signers are stored in a temporary table.
464+
// These entries will be finalized during the fast-sync completion phase.
465+
batch.Queue(
466+
queries.ConsensusBlockAddSignersFastSync,
467+
cmtMeta.LastCommit.Height,
468+
prevSigners,
469+
)
470+
471+
case analyzer.SlowSyncMode:
472+
batch.Queue(
473+
queries.ConsensusBlockAddSigners,
474+
cmtMeta.LastCommit.Height,
475+
prevSigners,
476+
)
477+
}
463478
}
464479

465480
return nil

analyzer/queries/queries.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ var (
189189
SET signer_entity_ids = $2
190190
WHERE height = $1`
191191

192+
ConsensusBlockAddSignersFastSync = `
193+
INSERT INTO todo_updates.block_signers (block_height, entity_ids)
194+
VALUES ($1, $2)`
195+
196+
ConsensusBlockSignersFinalize = `
197+
-- Use a CTE to ensure we scan over todo_updates.block_signers, which doesn't have
198+
-- any indexes setup.
199+
WITH updated AS (
200+
SELECT s.block_height, s.entity_ids
201+
FROM todo_updates.block_signers s
202+
)
203+
UPDATE chain.blocks b
204+
SET signer_entity_ids = u.entity_ids
205+
FROM updated u
206+
WHERE b.height = u.block_height`
207+
192208
ConsensusEpochUpsert = `
193209
INSERT INTO chain.epochs AS old (id, start_height, end_height)
194210
VALUES ($1, $2, $2)

storage/migrations/04_fast_sync_temp_tables.up.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ CREATE TABLE todo_updates.evm_tokens( -- Tracks updates to chain.evm_tokens(last
4545
-- error_message TEXT
4646
-- );
4747

48+
-- Added in 47_fast_sync_temp_block_signers.up.sql.
49+
-- CREATE TABLE todo_updates.block_signers( -- Tracks signers for blocks processed during fast-sync.
50+
-- block_height UINT63 NOT NULL,
51+
-- entity_ids TEXT[] NOT NULL
52+
-- );
53+
4854
COMMIT;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BEGIN;
2+
3+
CREATE TABLE todo_updates.block_signers( -- Tracks signers for blocks processed during fast-sync.
4+
block_height UINT63 NOT NULL,
5+
entity_ids TEXT[] NOT NULL
6+
);
7+
8+
COMMIT;

0 commit comments

Comments
 (0)