Skip to content

Commit

Permalink
update canonicality with a lock
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Jan 31, 2025
1 parent 3302878 commit c5909d1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
20 changes: 14 additions & 6 deletions pkg/db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ WHERE

-- name: GetLatestCursor :many
SELECT
originator_node_id,
MAX(originator_sequence_id)::BIGINT AS max_sequence_id
originator_node_id,
MAX(originator_sequence_id)::BIGINT AS max_sequence_id
FROM
gateway_envelopes
gateway_envelopes
GROUP BY
originator_node_id;
originator_node_id;

-- name: InsertBlockchainMessage :exec
INSERT INTO blockchain_messages(block_number, block_hash, originator_node_id, originator_sequence_id, is_canonical)
Expand Down Expand Up @@ -157,9 +157,17 @@ ORDER BY

-- name: UpdateBlocksCanonicalityInRange :exec
UPDATE
blockchain_messages
blockchain_messages AS bm
SET
is_canonical = FALSE
FROM (
SELECT
block_number
FROM
blockchain_messages
WHERE
bm.block_number BETWEEN @start_block_number AND @end_block_number
FOR UPDATE) AS locked_rows
WHERE
block_number >= @reorg_block_number;
bm.block_number = locked_rows.block_number;

31 changes: 22 additions & 9 deletions pkg/db/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func indexLogs(
storedBlockNumber uint64
storedBlockHash []byte
lastBlockSeen uint64
reorgCheckInterval uint64 = 10 // TODO: Adapt based on blocks per batch
reorgCheckInterval uint64 = 10 // TODO(borja): Adapt based on blocks per batch
reorgCheckAt uint64
reorgDetectedAt uint64
reorgBeginsAt uint64
Expand Down
13 changes: 8 additions & 5 deletions pkg/indexer/reorgHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
ErrGetBlock = errors.New("failed to get block")
)

// TODO: Make this configurable?
// TODO(borja): Make this configurable?
const BLOCK_RANGE_SIZE uint64 = 1000

func NewChainReorgHandler(
Expand All @@ -42,7 +42,7 @@ func NewChainReorgHandler(
}
}

// TODO: When reorg range has been calculated, alert clients (TBD)
// TODO(borja): When reorg range has been calculated, alert clients (TBD)
func (r *ReorgHandler) FindReorgPoint(detectedAt uint64) (uint64, []byte, error) {
startBlock, endBlock := blockRange(detectedAt)

Expand Down Expand Up @@ -84,16 +84,19 @@ func (r *ReorgHandler) FindReorgPoint(detectedAt uint64) (uint64, []byte, error)
}

// Oldest block matches, reorg happened in this range
blockNumber, blockHash, err := r.searchInRange(storedBlocks)
startedAt, blockHash, err := r.searchInRange(storedBlocks)
if err != nil {
return 0, nil, fmt.Errorf("failed to search reorg block in range: %w", err)
}

if err := r.queries.UpdateBlocksCanonicalityInRange(r.ctx, blockNumber); err != nil {
if err := r.queries.UpdateBlocksCanonicalityInRange(r.ctx, queries.UpdateBlocksCanonicalityInRangeParams{
StartBlockNumber: startedAt,
EndBlockNumber: detectedAt,
}); err != nil {
return 0, nil, fmt.Errorf("failed to update block range canonicality: %w", err)
}

return blockNumber, blockHash, nil
return startedAt, blockHash, nil
}
}

Expand Down

0 comments on commit c5909d1

Please sign in to comment.