Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
add database trigger to ensure indexers cannot miss any blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Sep 13, 2023
1 parent a4549a8 commit a7c6a2b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP FUNCTION IF EXISTS ensure_block_height_consecutive;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE OR REPLACE FUNCTION ensure_block_height_consecutive()
RETURNS TRIGGER AS $$
DECLARE
block_height integer;
BEGIN
EXECUTE format('SELECT MAX(block_height) FROM %I.%I', TG_TABLE_SCHEMA, TG_TABLE_NAME) INTO block_height;

IF NEW.block_height IS NOT NULL AND block_height IS NOT NULL AND NEW.block_height != block_height + 1 THEN
RAISE EXCEPTION '%.%: attempted to insert value with block_height = % while last indexed block_height = %. block_height values must be consecutive.', TG_TABLE_SCHEMA, TG_TABLE_NAME, NEW.block_height, block_height;
END IF;

RETURN NEW;
END;
$$ LANGUAGE plpgsql;
9 changes: 9 additions & 0 deletions packages/fuel-indexer-schema/src/db/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ impl IndexerSchema {

statements.extend(constraint_stmnts);

statements.push(format!(
"CREATE TRIGGER trigger_ensure_block_height_consecutive
BEFORE INSERT OR UPDATE ON {namespace}_{identifier}.indexmetadataentity
FOR EACH ROW
EXECUTE FUNCTION ensure_block_height_consecutive();",
namespace = self.namespace,
identifier = self.identifier
));

for stmnt in statements.iter() {
queries::execute_query(conn, stmnt.to_owned()).await?;
}
Expand Down

0 comments on commit a7c6a2b

Please sign in to comment.