(VDB-969) Add triggers for re-org safety in ilk history table#26
(VDB-969) Add triggers for re-org safety in ilk history table#26
Conversation
| LANGUAGE sql; | ||
|
|
||
| -- +goose StatementBegin | ||
| CREATE OR REPLACE FUNCTION maker.delete_redundant_ilk_state(ilk_id INTEGER, block_number BIGINT) RETURNS api.historical_ilk_state |
There was a problem hiding this comment.
This function is responsible for determining whether a record in historical_ilk_state is the same as the one before it, and deleting it if so. It's necessary because a single record in historical_ilk_state can have data from multiple storage tables, so we can't know whether it's safe to delete a row until we've verified that the only new data it held was from a re-orged diff.
| IF (TG_OP IN ('INSERT', 'UPDATE')) THEN | ||
| PERFORM maker.insert_new_rate(NEW); | ||
| PERFORM maker.update_rates_until_next_diff(NEW, NEW.rate); | ||
| ELSIF (TG_OP = 'DELETE') THEN | ||
| PERFORM maker.update_rates_until_next_diff(OLD, ilk_rate_before_block(OLD.ilk_id, OLD.block_number)); | ||
| PERFORM maker.delete_redundant_ilk_state(OLD.ilk_id, OLD.block_number); | ||
| END IF; |
There was a problem hiding this comment.
Not sure what the best practices are for triggers (i.e. whether it's better to have multiple triggers on the same table or use conditional logic like this), but this seemed preferable to me. Happy to change if we want to.
cd80a91 to
ded507c
Compare
| CREATE FUNCTION ilk_rate_before_block(ilk_id INTEGER, header_id INTEGER) RETURNS NUMERIC AS | ||
| $$ | ||
| WITH passed_block_number AS ( | ||
| SELECT block_number FROM public.headers WHERE id = header_id |
There was a problem hiding this comment.
In hindsight I probably should've put the formatting changes into a separate commit :-/
ded507c to
94a9a86
Compare
When a diff record is deleted, these triggers wipe out that diff's value from the
historical_ilk_statetable.