2021-08-29 18:55:44 +00:00
|
|
|
-- +goose Up
|
|
|
|
-- +goose StatementBegin
|
2023-03-08 17:10:23 +00:00
|
|
|
-- returns whether the state leaf key is vacated (previously existed but now is empty) at the provided block hash
|
2023-03-08 00:17:35 +00:00
|
|
|
CREATE OR REPLACE FUNCTION was_state_leaf_removed(v_key VARCHAR(66), v_hash VARCHAR)
|
2021-09-30 12:39:48 +00:00
|
|
|
RETURNS boolean AS $$
|
2023-01-23 23:30:23 +00:00
|
|
|
SELECT state_cids.removed = true
|
2021-09-30 12:39:48 +00:00
|
|
|
FROM eth.state_cids
|
2021-11-15 00:06:02 +00:00
|
|
|
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.block_hash)
|
2023-03-08 00:17:35 +00:00
|
|
|
WHERE state_leaf_key = v_key
|
2022-03-15 21:06:13 +00:00
|
|
|
AND state_cids.block_number <= (SELECT block_number
|
2021-09-30 12:39:48 +00:00
|
|
|
FROM eth.header_cids
|
2023-03-08 00:17:35 +00:00
|
|
|
WHERE block_hash = v_hash)
|
|
|
|
ORDER BY state_cids.block_number DESC LIMIT 1;
|
|
|
|
$$
|
|
|
|
language sql;
|
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose StatementBegin
|
2023-03-08 17:10:23 +00:00
|
|
|
-- returns whether the state leaf key is vacated (previously existed but now is empty) at the provided block height
|
2023-03-08 00:17:35 +00:00
|
|
|
CREATE OR REPLACE FUNCTION public.was_state_leaf_removed_by_number(v_key VARCHAR(66), v_block_no BIGINT)
|
|
|
|
RETURNS BOOLEAN AS $$
|
|
|
|
SELECT state_cids.removed = true
|
|
|
|
FROM eth.state_cids
|
|
|
|
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.block_hash)
|
|
|
|
WHERE state_leaf_key = v_key
|
|
|
|
AND state_cids.block_number <= v_block_no
|
2022-03-15 21:06:13 +00:00
|
|
|
ORDER BY state_cids.block_number DESC LIMIT 1;
|
2021-09-30 12:39:48 +00:00
|
|
|
$$
|
|
|
|
language sql;
|
2021-08-29 18:55:44 +00:00
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose StatementBegin
|
2023-04-18 08:43:33 +00:00
|
|
|
-- duplicate of eth.header_cids as a separate type: if we use the table directly, dropping the hypertables
|
|
|
|
-- on downgrade of step 00018 will fail due to the dependency on this type.
|
|
|
|
CREATE TYPE header_result AS (
|
|
|
|
block_number bigint,
|
|
|
|
block_hash character varying(66),
|
|
|
|
parent_hash character varying(66),
|
|
|
|
cid text,
|
|
|
|
td numeric,
|
|
|
|
node_ids character varying(128)[],
|
|
|
|
reward numeric,
|
|
|
|
state_root character varying(66),
|
|
|
|
tx_root character varying(66),
|
|
|
|
receipt_root character varying(66),
|
|
|
|
uncles_hash character varying(66),
|
|
|
|
bloom bytea,
|
|
|
|
"timestamp" bigint,
|
2023-07-18 17:29:27 +00:00
|
|
|
coinbase character varying(66),
|
|
|
|
canonical bool
|
2023-04-18 08:43:33 +00:00
|
|
|
);
|
|
|
|
|
2021-08-29 18:55:44 +00:00
|
|
|
CREATE TYPE child_result AS (
|
|
|
|
has_child BOOLEAN,
|
2023-04-18 08:43:33 +00:00
|
|
|
children header_result[]
|
2021-08-29 18:55:44 +00:00
|
|
|
);
|
|
|
|
|
2023-04-18 08:43:33 +00:00
|
|
|
CREATE OR REPLACE FUNCTION get_child(hash VARCHAR(66), height BIGINT) RETURNS child_result AS
|
2021-08-29 18:55:44 +00:00
|
|
|
$BODY$
|
|
|
|
DECLARE
|
2021-09-10 15:12:54 +00:00
|
|
|
child_height INT;
|
2023-04-18 08:43:33 +00:00
|
|
|
temp_child header_result;
|
2021-08-29 18:55:44 +00:00
|
|
|
new_child_result child_result;
|
|
|
|
BEGIN
|
|
|
|
child_height = height + 1;
|
|
|
|
-- short circuit if there are no children
|
2021-09-10 15:12:54 +00:00
|
|
|
SELECT exists(SELECT 1
|
2021-08-29 18:55:44 +00:00
|
|
|
FROM eth.header_cids
|
|
|
|
WHERE parent_hash = hash
|
|
|
|
AND block_number = child_height
|
2023-07-18 17:29:27 +00:00
|
|
|
AND canonical = true
|
2021-08-29 18:55:44 +00:00
|
|
|
LIMIT 1)
|
2021-09-10 15:12:54 +00:00
|
|
|
INTO new_child_result.has_child;
|
|
|
|
-- collect all the children for this header
|
|
|
|
IF new_child_result.has_child THEN
|
2021-08-29 18:55:44 +00:00
|
|
|
FOR temp_child IN
|
2023-07-18 17:29:27 +00:00
|
|
|
SELECT * FROM eth.header_cids WHERE parent_hash = hash AND block_number = child_height AND canonical = true
|
2021-08-29 18:55:44 +00:00
|
|
|
LOOP
|
|
|
|
new_child_result.children = array_append(new_child_result.children, temp_child);
|
2021-09-10 15:12:54 +00:00
|
|
|
END LOOP;
|
|
|
|
END IF;
|
2021-09-30 12:39:48 +00:00
|
|
|
RETURN new_child_result;
|
2021-08-29 18:55:44 +00:00
|
|
|
END
|
|
|
|
$BODY$
|
|
|
|
LANGUAGE 'plpgsql';
|
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose StatementBegin
|
2021-12-20 08:32:23 +00:00
|
|
|
CREATE OR REPLACE FUNCTION canonical_header_hash(height BIGINT) RETURNS character varying AS
|
2021-08-29 18:55:44 +00:00
|
|
|
$BODY$
|
2023-07-18 17:29:27 +00:00
|
|
|
SELECT block_hash from eth.header_cids WHERE block_number = height AND canonical = true LIMIT 1;
|
2022-03-28 23:20:29 +00:00
|
|
|
$BODY$
|
2023-07-18 17:29:27 +00:00
|
|
|
LANGUAGE sql;
|
2022-03-28 23:20:29 +00:00
|
|
|
-- +goose StatementEnd
|
|
|
|
|
2021-08-29 18:55:44 +00:00
|
|
|
-- +goose Down
|
2021-09-30 12:39:48 +00:00
|
|
|
DROP FUNCTION was_state_leaf_removed;
|
2023-03-08 00:17:35 +00:00
|
|
|
DROP FUNCTION was_state_leaf_removed_by_number;
|
2021-12-20 08:32:23 +00:00
|
|
|
DROP FUNCTION canonical_header_hash;
|
2023-04-18 08:43:33 +00:00
|
|
|
DROP FUNCTION get_child;
|
2021-11-15 00:06:02 +00:00
|
|
|
DROP TYPE child_result;
|