Skip to content

(VDB-969) Add triggers for re-org safety in ilk history table#26

Merged
gslaughl merged 1 commit intostagingfrom
vdb-969-ilk-state-reorg-safety
Dec 2, 2019
Merged

(VDB-969) Add triggers for re-org safety in ilk history table#26
gslaughl merged 1 commit intostagingfrom
vdb-969-ilk-state-reorg-safety

Conversation

@gslaughl
Copy link
Copy Markdown
Contributor

When a diff record is deleted, these triggers wipe out that diff's value from the historical_ilk_state table.

LANGUAGE sql;

-- +goose StatementBegin
CREATE OR REPLACE FUNCTION maker.delete_redundant_ilk_state(ilk_id INTEGER, block_number BIGINT) RETURNS api.historical_ilk_state
Copy link
Copy Markdown
Contributor Author

@gslaughl gslaughl Nov 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +300 to +402
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;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gslaughl gslaughl force-pushed the vdb-969-ilk-state-reorg-safety branch from cd80a91 to ded507c Compare November 27, 2019 18:33
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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In hindsight I probably should've put the formatting changes into a separate commit :-/

Copy link
Copy Markdown
Contributor

@rmulhol rmulhol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🦃

@gslaughl gslaughl force-pushed the vdb-969-ilk-state-reorg-safety branch from ded507c to 94a9a86 Compare December 2, 2019 20:53
@gslaughl gslaughl merged commit 5453395 into staging Dec 2, 2019
@gslaughl gslaughl deleted the vdb-969-ilk-state-reorg-safety branch December 2, 2019 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants