Fix SQL query to check for empty storage value.

This commit is contained in:
Arijit Das
2021-09-24 15:37:04 +05:30
parent f09f665b11
commit 500bba43b4
7 changed files with 25 additions and 29 deletions
+20 -12
View File
@@ -1,18 +1,26 @@
-- +goose Up
-- +goose StatementBegin
-- returns if a storage node at the provided path was removed in the range >= the provided height and <= the provided block hash
CREATE OR REPLACE FUNCTION was_state_leaf_removed(state_leaf_key BYTEA, block_num BIGINT) RETURNS BOOLEAN
-- returns if a state leaf node was removed within the provided block number
CREATE OR REPLACE FUNCTION was_state_leaf_removed(key character varying, hash character varying) RETURNS boolean
LANGUAGE plpgsql
AS $$
SELECT exists(SELECT 1
FROM eth.state_cids
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
WHERE state_leaf_key = state_leaf_key
AND block_number <= block_num
AND state_cids.node_type = 3
LIMIT 1);
$$ LANGUAGE SQL;
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT state_cids.node_type
FROM eth.state_cids
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
WHERE state_leaf_key = key
AND block_number <= (SELECT block_number FROM eth.header_cids WHERE block_hash = hash)
ORDER BY state_cids.id DESC LIMIT 1
LOOP
IF rec.node_type = 3 THEN
RETURN TRUE;
END IF;
END LOOP;
RETURN FALSE;
END;
$$;
-- +goose StatementEnd
-- +goose StatementBegin