Repository navigation
Scanner: backfill the deferred secondary history after fast catch-up #2167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
abd2e90
e3849a7
a95744e
2519c0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1651,6 +1651,92 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |||||||||||||||||
| Ok(()) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The height through which the secondary history tables are complete; | ||||||||||||||||||
| /// `None` when no fast catch-up has deferred them. See the scanner-lib | ||||||||||||||||||
| /// backfill pass. | ||||||||||||||||||
| pub async fn secondary_indexed_height( | ||||||||||||||||||
| &mut self, | ||||||||||||||||||
| ) -> Result<Option<BlockHeight>, ApiServerStorageError> { | ||||||||||||||||||
| let row = self | ||||||||||||||||||
| .tx | ||||||||||||||||||
| .query_opt( | ||||||||||||||||||
| "SELECT value FROM ml.misc_data WHERE name = 'secondary_indexed_height';", | ||||||||||||||||||
| &[], | ||||||||||||||||||
| ) | ||||||||||||||||||
| .await | ||||||||||||||||||
| .map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||||||||||||||||||
|
|
||||||||||||||||||
| let Some(bytes) = row.map(|row| row.get::<_, Vec<u8>>(0)) else { | ||||||||||||||||||
| return Ok(None); | ||||||||||||||||||
| }; | ||||||||||||||||||
| let bytes: [u8; 8] = bytes.try_into().map_err(|_| { | ||||||||||||||||||
| ApiServerStorageError::DeserializationError( | ||||||||||||||||||
| "Invalid secondary indexed height stored in the database".to_owned(), | ||||||||||||||||||
| ) | ||||||||||||||||||
| })?; | ||||||||||||||||||
| Ok(Some(BlockHeight::new(u64::from_be_bytes(bytes)))) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Records the height through which the secondary history tables are | ||||||||||||||||||
| /// complete. | ||||||||||||||||||
| pub async fn set_secondary_indexed_height( | ||||||||||||||||||
| &mut self, | ||||||||||||||||||
| block_height: BlockHeight, | ||||||||||||||||||
| ) -> Result<(), ApiServerStorageError> { | ||||||||||||||||||
| let bytes = u64::to_be_bytes(block_height.into_int()).to_vec(); | ||||||||||||||||||
| self.tx | ||||||||||||||||||
| .execute( | ||||||||||||||||||
| "INSERT INTO ml.misc_data (name, value) VALUES ('secondary_indexed_height', $1) | ||||||||||||||||||
| ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value;", | ||||||||||||||||||
| &[&bytes], | ||||||||||||||||||
| ) | ||||||||||||||||||
| .await | ||||||||||||||||||
| .map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||||||||||||||||||
| Ok(()) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// The `(transaction id, global index)` pairs of all transactions owned by | ||||||||||||||||||
| /// the main-chain block at `block_height`, ascending by index. | ||||||||||||||||||
| pub async fn get_block_transaction_indices( | ||||||||||||||||||
| &mut self, | ||||||||||||||||||
| block_height: BlockHeight, | ||||||||||||||||||
| ) -> Result<Vec<(Id<Transaction>, u64)>, ApiServerStorageError> { | ||||||||||||||||||
| let height = Self::block_height_to_postgres_friendly(block_height); | ||||||||||||||||||
| let rows = self | ||||||||||||||||||
| .tx | ||||||||||||||||||
| .query( | ||||||||||||||||||
| "SELECT t.transaction_id, t.tx_global_index | ||||||||||||||||||
| FROM ml.transactions t | ||||||||||||||||||
| JOIN ml.blocks b ON t.owning_block_id = b.block_id | ||||||||||||||||||
| WHERE b.block_height = $1 | ||||||||||||||||||
| ORDER BY t.tx_global_index ASC;", | ||||||||||||||||||
|
Comment on lines
+1708
to
+1712
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Suggested change
|
||||||||||||||||||
| &[&height], | ||||||||||||||||||
| ) | ||||||||||||||||||
| .await | ||||||||||||||||||
| .map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||||||||||||||||||
|
|
||||||||||||||||||
| let mut result = Vec::with_capacity(rows.len()); | ||||||||||||||||||
| for row in rows { | ||||||||||||||||||
| let tx_id_bytes: Vec<u8> = row.get(0); | ||||||||||||||||||
| let index: i64 = row.get(1); | ||||||||||||||||||
| let tx_id = | ||||||||||||||||||
| Id::<Transaction>::decode_all(&mut tx_id_bytes.as_slice()).map_err(|_| { | ||||||||||||||||||
| ApiServerStorageError::DeserializationError( | ||||||||||||||||||
| "Invalid transaction id in transactions table".to_owned(), | ||||||||||||||||||
| ) | ||||||||||||||||||
| })?; | ||||||||||||||||||
| result.push(( | ||||||||||||||||||
| tx_id, | ||||||||||||||||||
| u64::try_from(index).map_err(|_| { | ||||||||||||||||||
| ApiServerStorageError::DeserializationError( | ||||||||||||||||||
| "Invalid transaction global index".to_owned(), | ||||||||||||||||||
| ) | ||||||||||||||||||
| })?, | ||||||||||||||||||
| )); | ||||||||||||||||||
| } | ||||||||||||||||||
| Ok(result) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| pub async fn initialize_database( | ||||||||||||||||||
| &mut self, | ||||||||||||||||||
| chain_config: &ChainConfig, | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both backends (this upsert and the in-memory setter) write the watermark unconditionally with no monotonic guard. If the value is ever written out of order (e.g. a re-processed/lower block during replay or a concurrent writer), the watermark regresses and the backfill pass will re-index blocks — or worse, if a stale value is written after a newer one within different transactions, secondary tables newer than the watermark could be treated as not yet indexed and skipped on the next catch-up. Consider guarding the update, e.g.
... DO UPDATE SET value = EXCLUDED.value WHERE ...with a comparison on the decoded height, or an application-level monotonic check.Suggestion: