2020-11-09 16:29:04 +00:00
|
|
|
-- +goose Up
|
|
|
|
-- +goose StatementBegin
|
|
|
|
CREATE TYPE child_result AS (
|
|
|
|
has_child BOOLEAN,
|
|
|
|
children eth.header_cids[]
|
|
|
|
);
|
|
|
|
|
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
|
2020-11-12 21:33:06 +00:00
|
|
|
child_height INT;
|
|
|
|
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
|
|
|
|
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
|
2020-11-09 16:29:04 +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
|
|
|
|
SELECT * FROM eth.header_cids WHERE parent_hash = hash AND block_number = child_height
|
|
|
|
LOOP
|
|
|
|
new_child_result.children = array_append(new_child_result.children, temp_child);
|
|
|
|
END LOOP;
|
2020-11-09 16:29:04 +00:00
|
|
|
END IF;
|
|
|
|
RETURN new_child_result;
|
|
|
|
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
|
|
|
|
canonical_header eth.header_cids;
|
|
|
|
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);
|
|
|
|
END IF;
|
|
|
|
END LOOP;
|
|
|
|
-- 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
|
2020-11-12 21:33:06 +00:00
|
|
|
SELECT * INTO canonical_header FROM unnest(headers) LIMIT 1;
|
2020-11-09 16:29:04 +00:00
|
|
|
-- if only one header had children, it can be considered the heaviest/canonical header of the set
|
|
|
|
ELSIF has_children_count = 1 THEN
|
|
|
|
-- return the only header with a child
|
|
|
|
canonical_header = current_header_with_child;
|
|
|
|
-- if there are multiple headers with children
|
|
|
|
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
|
2020-11-12 21:33:06 +00:00
|
|
|
SELECT * INTO canonical_header FROM unnest(headers)
|
2020-11-09 16:29:04 +00:00
|
|
|
WHERE block_hash = canonical_child.parent_hash;
|
|
|
|
END IF;
|
|
|
|
RETURN canonical_header;
|
|
|
|
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
|
2020-11-12 21:33:06 +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
|
|
|
|
FOR temp_header IN
|
|
|
|
SELECT * FROM eth.header_cids WHERE block_number = height
|
|
|
|
LOOP
|
|
|
|
headers = array_append(headers, temp_header);
|
|
|
|
END LOOP;
|
|
|
|
-- 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
|
|
|
|
ELSE
|
|
|
|
canonical_header = canonical_header_from_array(headers);
|
|
|
|
RETURN canonical_header.id;
|
2020-11-09 16:29:04 +00:00
|
|
|
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
|
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;
|
|
|
|
DROP TYPE child_result;
|