def insert_missed_blocks(store):
 missed = []
 for row in store.selectall('\n        SELECT b.block_id\n          FROM block b\n          LEFT JOIN chain_candidate cc ON (b.block_id = cc.block_id)\n         WHERE chain_id IS NULL\n         ORDER BY b.block_height\n    '):
  missed.append(row[0])
 if (not missed):
  return
 store.log.info('Attempting to repair %d missed blocks.', len(missed))
 inserted = 0
 for block_id in missed:
  store.sql('\n            INSERT INTO chain_candidate (\n                chain_id, block_id, block_height, in_longest)\n            SELECT cc.chain_id, b.block_id, b.block_height, 0\n              FROM chain_candidate cc\n              JOIN block prev ON (cc.block_id = prev.block_id)\n              JOIN block b ON (b.prev_block_id = prev.block_id)\n             WHERE b.block_id = ?', (block_id,))
  inserted += store.rowcount()
  store.commit()
 store.log.info('Inserted %d rows into chain_candidate.', inserted)

def insert_missed_blocks ( store ) : missed = [ ] for row in store . selectall ( '\n        SELECT b.block_id\n          FROM block b\n          LEFT JOIN chain_candidate cc ON (b.block_id = cc.block_id)\n         WHERE chain_id IS NULL\n         ORDER BY b.block_height\n    ' ) : missed . append ( row [ 0 ] ) if ( not missed ) : return store . log . info ( 'Attempting to repair %d missed blocks.' , len ( missed ) ) inserted = 0 for block_id in missed : store . sql ( '\n            INSERT INTO chain_candidate (\n                chain_id, block_id, block_height, in_longest)\n            SELECT cc.chain_id, b.block_id, b.block_height, 0\n              FROM chain_candidate cc\n              JOIN block prev ON (cc.block_id = prev.block_id)\n              JOIN block b ON (b.prev_block_id = prev.block_id)\n             WHERE b.block_id = ?' , ( block_id , ) ) inserted += store . rowcount ( ) store . commit ( ) store . log . info ( 'Inserted %d rows into chain_candidate.' , inserted )
rescanning doesnt always work due to timeouts and resource constraints .