Skip to content

Commit

Permalink
indexer: add ReindexBlocks and make indexerdb.CreateBlock an UPSERT
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Aug 27, 2024
1 parent 78dc107 commit 5086384
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions vochain/indexer/db/blocks.sql.go

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

25 changes: 25 additions & 0 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,31 @@ func (idx *Indexer) AfterSyncBootstrap(inTest bool) {
log.Infof("live results recovery computation finished, took %s", time.Since(startTime))
}

func (idx *Indexer) ReindexBlocks() {
queries := idx.blockTxQueries()

for i := idx.App.Node.BlockStore().Base(); i <= idx.App.Node.BlockStore().Height(); i++ {
if b := idx.App.GetBlockByHeight(int64(i)); b != nil {
idxBlock, err := idx.readOnlyQuery.GetBlockByHeight(context.TODO(), i)
if err == nil && idxBlock.Time != b.Time {
log.Errorf("while reindexing blocks, block %d timestamp in db (%s) differs from blockstore (%s), leaving untouched", i, idxBlock.Time, b.Time)
continue
}
// if we got here, the block doesn't exist
if _, err := queries.CreateBlock(context.TODO(), indexerdb.CreateBlockParams{
ChainID: b.ChainID,
Height: b.Height,
Time: b.Time,
Hash: nonNullBytes(b.Hash()),
ProposerAddress: nonNullBytes(b.ProposerAddress),
LastBlockHash: nonNullBytes(b.LastBlockID.Hash),
}); err != nil {
log.Errorw(err, "cannot index new block")
}
}
}
}

// Commit is called by the APP when a block is confirmed and included into the chain
func (idx *Indexer) Commit(height uint32) error {
idx.blockMu.Lock()
Expand Down
8 changes: 7 additions & 1 deletion vochain/indexer/queries/blocks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ INSERT INTO blocks(
chain_id, height, time, hash, proposer_address, last_block_hash
) VALUES (
?, ?, ?, ?, ?, ?
);
)
ON CONFLICT(height) DO UPDATE
SET chain_id = sqlc.arg(chain_id),
time = sqlc.arg(time),
hash = sqlc.arg(hash),
proposer_address = sqlc.arg(proposer_address),
last_block_hash = sqlc.arg(last_block_hash);

-- name: GetBlockByHeight :one
SELECT * FROM blocks
Expand Down

0 comments on commit 5086384

Please sign in to comment.