2020-11-09 16:29:04 +00:00
|
|
|
-- +goose Up
|
2021-09-23 10:36:09 +00:00
|
|
|
-- +goose StatementBegin
|
2021-09-24 10:04:09 +00:00
|
|
|
-- returns if a state leaf node was removed within the provided block number
|
2021-09-27 11:44:27 +00:00
|
|
|
CREATE OR REPLACE FUNCTION was_state_leaf_removed(key character varying, hash character varying)
|
|
|
|
RETURNS boolean AS $$
|
|
|
|
SELECT state_cids.node_type = 3
|
|
|
|
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 block_number DESC LIMIT 1;
|
|
|
|
$$
|
|
|
|
language sql;
|
2021-09-23 10:36:09 +00:00
|
|
|
-- +goose StatementEnd
|
|
|
|
|
2020-11-09 16:29:04 +00:00
|
|
|
-- +goose StatementBegin
|
|
|
|
CREATE TYPE child_result AS (
|
2021-03-12 15:50:48 +00:00
|
|
|
has_child BOOLEAN,
|
|
|
|
children eth.header_cids[]
|
2020-11-09 16:29:04 +00:00
|
|
|
);
|
|
|
|
|
2020-11-12 21:33:06 +00:00
|
|
|
CREATE OR REPLACE FUNCTION has_child(hash VARCHAR(66), height BIGINT) RETURNS child_result AS
|
2020-11-09 16:29:04 +00:00
|
|
|
$BODY$
|
|
|
|
DECLARE
|
2021-03-12 15:50:48 +00:00
|
|
|
child_height INT;
|
2020-11-12 21:33:06 +00:00
|
|
|
temp_child eth.header_cids;
|
2020-11-09 16:29:04 +00:00
|
|
|
new_child_result child_result;
|
|
|
|
BEGIN
|
2020-11-12 21:33:06 +00:00
|
|
|
child_height = height + 1;
|
2020-11-09 16:29:04 +00:00
|
|
|
-- short circuit if there are no children
|
2021-03-12 15:50:48 +00:00
|
|
|
SELECT exists(SELECT 1
|
|
|
|
FROM eth.header_cids
|
|
|
|
WHERE parent_hash = hash
|
2020-11-12 21:33:06 +00:00
|
|
|
AND block_number = child_height
|
2021-03-12 15:50:48 +00:00
|
|
|
LIMIT 1)
|
|
|
|
INTO new_child_result.has_child;
|
|
|
|
-- collect all the children for this header
|
|
|
|
IF new_child_result.has_child THEN
|
2020-11-12 21:33:06 +00:00
|
|
|
FOR temp_child IN
|
2021-03-12 15:50:48 +00:00
|
|
|
SELECT * FROM eth.header_cids WHERE parent_hash = hash AND block_number = child_height
|
2020-11-12 21:33:06 +00:00
|
|
|
LOOP
|
|
|
|
new_child_result.children = array_append(new_child_result.children, temp_child);
|
2021-03-12 15:50:48 +00:00
|
|
|
END LOOP;
|
|
|
|
END IF;
|
|
|
|
RETURN new_child_result;
|
2020-11-09 16:29:04 +00:00
|
|
|
END
|
|
|
|
$BODY$
|
|
|
|
LANGUAGE 'plpgsql';
|
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose StatementBegin
|
2020-11-12 21:33:06 +00:00
|
|
|
CREATE OR REPLACE FUNCTION canonical_header_from_array(headers eth.header_cids[]) RETURNS eth.header_cids AS
|
2020-11-09 16:29:04 +00:00
|
|
|
$BODY$
|
|
|
|
DECLARE
|
2021-03-12 15:50:48 +00:00
|
|
|
canonical_header eth.header_cids;
|
2020-11-09 16:29:04 +00:00
|
|
|
canonical_child eth.header_cids;
|
|
|
|
header eth.header_cids;
|
|
|
|
current_child_result child_result;
|
|
|
|
child_headers eth.header_cids[];
|
|
|
|
current_header_with_child eth.header_cids;
|
|
|
|
has_children_count INT DEFAULT 0;
|
|
|
|
BEGIN
|
|
|
|
-- for each header in the provided set
|
2020-11-12 21:33:06 +00:00
|
|
|
FOREACH header IN ARRAY headers
|
2020-11-09 16:29:04 +00:00
|
|
|
LOOP
|
|
|
|
-- check if it has any children
|
2020-11-12 21:33:06 +00:00
|
|
|
current_child_result = has_child(header.block_hash, header.block_number);
|
2020-11-09 16:29:04 +00:00
|
|
|
IF current_child_result.has_child THEN
|
|
|
|
-- if it does, take note
|
|
|
|
has_children_count = has_children_count + 1;
|
|
|
|
current_header_with_child = header;
|
|
|
|
-- and add the children to the growing set of child headers
|
|
|
|
child_headers = array_cat(child_headers, current_child_result.children);
|
2021-03-12 15:50:48 +00:00
|
|
|
END IF;
|
|
|
|
END LOOP;
|
2020-11-09 16:29:04 +00:00
|
|
|
-- if none of the headers had children, none is more canonical than the other
|
|
|
|
IF has_children_count = 0 THEN
|
|
|
|
-- return the first one selected
|
2021-03-12 15:50:48 +00:00
|
|
|
SELECT * INTO canonical_header FROM unnest(headers) LIMIT 1;
|
|
|
|
-- if only one header had children, it can be considered the heaviest/canonical header of the set
|
|
|
|
ELSIF has_children_count = 1 THEN
|
2020-11-09 16:29:04 +00:00
|
|
|
-- return the only header with a child
|
|
|
|
canonical_header = current_header_with_child;
|
|
|
|
-- if there are multiple headers with children
|
2021-03-12 15:50:48 +00:00
|
|
|
ELSE
|
2020-11-12 21:33:06 +00:00
|
|
|
-- find the canonical header from the child set
|
|
|
|
canonical_child = canonical_header_from_array(child_headers);
|
2020-11-09 16:29:04 +00:00
|
|
|
-- the header that is parent to this header, is the canonical header at this level
|
2021-03-12 15:50:48 +00:00
|
|
|
SELECT * INTO canonical_header FROM unnest(headers)
|
|
|
|
WHERE block_hash = canonical_child.parent_hash;
|
|
|
|
END IF;
|
|
|
|
RETURN canonical_header;
|
2020-11-09 16:29:04 +00:00
|
|
|
END
|
|
|
|
$BODY$
|
|
|
|
LANGUAGE 'plpgsql';
|
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose StatementBegin
|
2020-11-12 21:33:06 +00:00
|
|
|
CREATE OR REPLACE FUNCTION canonical_header_id(height BIGINT) RETURNS INTEGER AS
|
2020-11-09 16:29:04 +00:00
|
|
|
$BODY$
|
|
|
|
DECLARE
|
2021-03-12 15:50:48 +00:00
|
|
|
canonical_header eth.header_cids;
|
2020-11-09 16:29:04 +00:00
|
|
|
headers eth.header_cids[];
|
2020-11-12 21:33:06 +00:00
|
|
|
header_count INT;
|
|
|
|
temp_header eth.header_cids;
|
2020-11-09 16:29:04 +00:00
|
|
|
BEGIN
|
2020-11-12 21:33:06 +00:00
|
|
|
-- collect all headers at this height
|
2021-03-12 15:50:48 +00:00
|
|
|
FOR temp_header IN
|
|
|
|
SELECT * FROM eth.header_cids WHERE block_number = height
|
|
|
|
LOOP
|
2020-11-12 21:33:06 +00:00
|
|
|
headers = array_append(headers, temp_header);
|
2021-03-12 15:50:48 +00:00
|
|
|
END LOOP;
|
2020-11-12 21:33:06 +00:00
|
|
|
-- count the number of headers collected
|
|
|
|
header_count = array_length(headers, 1);
|
|
|
|
-- if we have less than 1 header, return NULL
|
|
|
|
IF header_count IS NULL OR header_count < 1 THEN
|
|
|
|
RETURN NULL;
|
|
|
|
-- if we have one header, return its id
|
|
|
|
ELSIF header_count = 1 THEN
|
|
|
|
RETURN headers[1].id;
|
|
|
|
-- if we have multiple headers we need to determine which one is canonical
|
2021-03-12 15:50:48 +00:00
|
|
|
ELSE
|
2020-11-12 21:33:06 +00:00
|
|
|
canonical_header = canonical_header_from_array(headers);
|
2021-03-12 15:50:48 +00:00
|
|
|
RETURN canonical_header.id;
|
|
|
|
END IF;
|
2020-11-12 21:33:06 +00:00
|
|
|
END;
|
2020-11-09 16:29:04 +00:00
|
|
|
$BODY$
|
|
|
|
LANGUAGE 'plpgsql';
|
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
-- +goose Down
|
2021-09-23 10:36:09 +00:00
|
|
|
DROP FUNCTION was_state_leaf_removed;
|
2020-11-12 21:33:06 +00:00
|
|
|
DROP FUNCTION canonical_header_id;
|
|
|
|
DROP FUNCTION canonical_header_from_array;
|
2020-11-09 16:29:04 +00:00
|
|
|
DROP FUNCTION has_child;
|
2021-03-12 15:50:48 +00:00
|
|
|
DROP TYPE child_result;
|