Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

minor refactoring #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions db/migrations/00014_create_stored_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ CREATE OR REPLACE FUNCTION was_storage_removed(path BYTEA, height BIGINT, hash V
AS $$
SELECT exists(SELECT 1
FROM eth.storage_cids
INNER JOIN eth.state_cids ON (storage_cids.state_id = state_cids.id)
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
INNER JOIN eth.state_cids ON (storage_cids.state_id = state_cids.id)
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
WHERE storage_path = path
AND block_number > height
AND block_number <= (SELECT block_number
Expand All @@ -23,7 +23,7 @@ CREATE OR REPLACE FUNCTION was_state_removed(path BYTEA, height BIGINT, hash VAR
AS $$
SELECT exists(SELECT 1
FROM eth.state_cids
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
WHERE state_path = path
AND block_number > height
AND block_number <= (SELECT block_number
Expand All @@ -43,26 +43,26 @@ CREATE TYPE child_result AS (
CREATE OR REPLACE FUNCTION has_child(hash VARCHAR(66), height BIGINT) RETURNS child_result AS
$BODY$
DECLARE
child_height INT;
child_height INT;
temp_child eth.header_cids;
new_child_result child_result;
BEGIN
child_height = height + 1;
-- short circuit if there are no children
SELECT exists(SELECT 1
SELECT exists(SELECT 1
FROM eth.header_cids
WHERE parent_hash = hash
AND block_number = child_height
LIMIT 1)
INTO new_child_result.has_child;
-- collect all the children for this header
IF new_child_result.has_child THEN
INTO new_child_result.has_child;
-- collect all the children for this header
IF new_child_result.has_child THEN
FOR temp_child IN
SELECT * FROM eth.header_cids WHERE parent_hash = hash AND block_number = child_height
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;
END IF;
END LOOP;
END IF;
RETURN new_child_result;
END
$BODY$
Expand All @@ -73,7 +73,7 @@ LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION canonical_header_from_array(headers eth.header_cids[]) RETURNS eth.header_cids AS
$BODY$
DECLARE
canonical_header eth.header_cids;
canonical_header eth.header_cids;
canonical_child eth.header_cids;
header eth.header_cids;
current_child_result child_result;
Expand All @@ -92,25 +92,25 @@ BEGIN
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;
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
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
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
-- return the only header with a child
canonical_header = current_header_with_child;
-- if there are multiple headers with children
ELSE
ELSE
-- find the canonical header from the child set
canonical_child = canonical_header_from_array(child_headers);
-- the header that is parent to this header, is the canonical header at this level
SELECT * INTO canonical_header FROM unnest(headers)
WHERE block_hash = canonical_child.parent_hash;
END IF;
RETURN canonical_header;
SELECT * INTO canonical_header FROM unnest(headers)
WHERE block_hash = canonical_child.parent_hash;
END IF;
RETURN canonical_header;
END
$BODY$
LANGUAGE 'plpgsql';
Expand All @@ -120,17 +120,17 @@ LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION canonical_header_id(height BIGINT) RETURNS INTEGER AS
$BODY$
DECLARE
canonical_header eth.header_cids;
canonical_header eth.header_cids;
headers eth.header_cids[];
header_count INT;
temp_header eth.header_cids;
BEGIN
-- collect all headers at this height
FOR temp_header IN
SELECT * FROM eth.header_cids WHERE block_number = height
LOOP
FOR temp_header IN
SELECT * FROM eth.header_cids WHERE block_number = height
LOOP
headers = array_append(headers, temp_header);
END LOOP;
END LOOP;
-- count the number of headers collected
header_count = array_length(headers, 1);
-- if we have less than 1 header, return NULL
Expand All @@ -140,10 +140,10 @@ END LOOP;
ELSIF header_count = 1 THEN
RETURN headers[1].id;
-- if we have multiple headers we need to determine which one is canonical
ELSE
ELSE
canonical_header = canonical_header_from_array(headers);
RETURN canonical_header.id;
END IF;
RETURN canonical_header.id;
END IF;
END;
$BODY$
LANGUAGE 'plpgsql';
Expand Down
60 changes: 30 additions & 30 deletions db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ CREATE FUNCTION public.canonical_header_from_array(headers eth.header_cids[]) RE
LANGUAGE plpgsql
AS $$
DECLARE
canonical_header eth.header_cids;
canonical_header eth.header_cids;
canonical_child eth.header_cids;
header eth.header_cids;
current_child_result child_result;
Expand All @@ -130,25 +130,25 @@ BEGIN
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;
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
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
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
-- return the only header with a child
canonical_header = current_header_with_child;
-- if there are multiple headers with children
ELSE
ELSE
-- find the canonical header from the child set
canonical_child = canonical_header_from_array(child_headers);
-- the header that is parent to this header, is the canonical header at this level
SELECT * INTO canonical_header FROM unnest(headers)
WHERE block_hash = canonical_child.parent_hash;
END IF;
RETURN canonical_header;
SELECT * INTO canonical_header FROM unnest(headers)
WHERE block_hash = canonical_child.parent_hash;
END IF;
RETURN canonical_header;
END
$$;

Expand All @@ -161,17 +161,17 @@ CREATE FUNCTION public.canonical_header_id(height bigint) RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
canonical_header eth.header_cids;
canonical_header eth.header_cids;
headers eth.header_cids[];
header_count INT;
temp_header eth.header_cids;
BEGIN
-- collect all headers at this height
FOR temp_header IN
SELECT * FROM eth.header_cids WHERE block_number = height
LOOP
FOR temp_header IN
SELECT * FROM eth.header_cids WHERE block_number = height
LOOP
headers = array_append(headers, temp_header);
END LOOP;
END LOOP;
-- count the number of headers collected
header_count = array_length(headers, 1);
-- if we have less than 1 header, return NULL
Expand All @@ -181,10 +181,10 @@ END LOOP;
ELSIF header_count = 1 THEN
RETURN headers[1].id;
-- if we have multiple headers we need to determine which one is canonical
ELSE
ELSE
canonical_header = canonical_header_from_array(headers);
RETURN canonical_header.id;
END IF;
RETURN canonical_header.id;
END IF;
END;
$$;

Expand All @@ -197,26 +197,26 @@ CREATE FUNCTION public.has_child(hash character varying, height bigint) RETURNS
LANGUAGE plpgsql
AS $$
DECLARE
child_height INT;
child_height INT;
temp_child eth.header_cids;
new_child_result child_result;
BEGIN
child_height = height + 1;
-- short circuit if there are no children
SELECT exists(SELECT 1
SELECT exists(SELECT 1
FROM eth.header_cids
WHERE parent_hash = hash
AND block_number = child_height
LIMIT 1)
INTO new_child_result.has_child;
-- collect all the children for this header
IF new_child_result.has_child THEN
INTO new_child_result.has_child;
-- collect all the children for this header
IF new_child_result.has_child THEN
FOR temp_child IN
SELECT * FROM eth.header_cids WHERE parent_hash = hash AND block_number = child_height
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;
END IF;
END LOOP;
END IF;
RETURN new_child_result;
END
$$;
Expand All @@ -231,7 +231,7 @@ CREATE FUNCTION public.was_state_removed(path bytea, height bigint, hash charact
AS $$
SELECT exists(SELECT 1
FROM eth.state_cids
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
WHERE state_path = path
AND block_number > height
AND block_number <= (SELECT block_number
Expand All @@ -251,8 +251,8 @@ CREATE FUNCTION public.was_storage_removed(path bytea, height bigint, hash chara
AS $$
SELECT exists(SELECT 1
FROM eth.storage_cids
INNER JOIN eth.state_cids ON (storage_cids.state_id = state_cids.id)
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
INNER JOIN eth.state_cids ON (storage_cids.state_id = state_cids.id)
INNER JOIN eth.header_cids ON (state_cids.header_id = header_cids.id)
WHERE storage_path = path
AND block_number > height
AND block_number <= (SELECT block_number
Expand Down
9 changes: 6 additions & 3 deletions pkg/eth/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ func (in *CIDIndexer) Index(cids CIDPayload) error {
}
}()

headerID, err := in.indexHeaderCID(tx, cids.HeaderCID)
var headerID int64
headerID, err = in.indexHeaderCID(tx, cids.HeaderCID)
if err != nil {
log.Error("eth indexer error when indexing header")
return err
}
for _, uncle := range cids.UncleCIDs {
if err := in.indexUncleCID(tx, uncle, headerID); err != nil {
err = in.indexUncleCID(tx, uncle, headerID)
if err != nil {
log.Error("eth indexer error when indexing uncle")
return err
}
}
if err := in.indexTransactionAndReceiptCIDs(tx, cids, headerID); err != nil {
err = in.indexTransactionAndReceiptCIDs(tx, cids, headerID)
if err != nil {
log.Error("eth indexer error when indexing transactions and receipts")
return err
}
Expand Down
30 changes: 20 additions & 10 deletions pkg/eth/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,21 @@ func (pub *IPLDPublisher) Publish(payload ConvertedPayload) error {

// Publish trie nodes
for _, node := range txTrieNodes {
if err := shared.PublishIPLD(tx, node); err != nil {
err = shared.PublishIPLD(tx, node)
if err != nil {
return err
}
}
for _, node := range rctTrieNodes {
if err := shared.PublishIPLD(tx, node); err != nil {
err = shared.PublishIPLD(tx, node)
if err != nil {
return err
}
}

// Publish and index header
if err := shared.PublishIPLD(tx, headerNode); err != nil {
err = shared.PublishIPLD(tx, headerNode)
if err != nil {
return err
}
reward := CalcEthBlockReward(payload.Block.Header(), payload.Block.Uncles(), payload.Block.Transactions(), payload.Receipts)
Expand All @@ -106,14 +109,16 @@ func (pub *IPLDPublisher) Publish(payload ConvertedPayload) error {
UncleRoot: payload.Block.UncleHash().String(),
Timestamp: payload.Block.Time(),
}
headerID, err := pub.indexer.indexHeaderCID(tx, header)
var headerID int64
headerID, err = pub.indexer.indexHeaderCID(tx, header)
if err != nil {
return err
}

// Publish and index uncles
for _, uncleNode := range uncleNodes {
if err := shared.PublishIPLD(tx, uncleNode); err != nil {
err = shared.PublishIPLD(tx, uncleNode)
if err != nil {
return err
}
uncleReward := CalcUncleMinerReward(payload.Block.Number().Uint64(), uncleNode.Number.Uint64())
Expand All @@ -124,24 +129,28 @@ func (pub *IPLDPublisher) Publish(payload ConvertedPayload) error {
BlockHash: uncleNode.Hash().String(),
Reward: uncleReward.String(),
}
if err := pub.indexer.indexUncleCID(tx, uncle, headerID); err != nil {
err = pub.indexer.indexUncleCID(tx, uncle, headerID)
if err != nil {
return err
}
}

// Publish and index txs and receipts
for i, txNode := range txNodes {
if err := shared.PublishIPLD(tx, txNode); err != nil {
err = shared.PublishIPLD(tx, txNode)
if err != nil {
return err
}
rctNode := rctNodes[i]
if err := shared.PublishIPLD(tx, rctNode); err != nil {
err = shared.PublishIPLD(tx, rctNode)
if err != nil {
return err
}
txModel := payload.TxMetaData[i]
txModel.CID = txNode.Cid().String()
txModel.MhKey = shared.MultihashKeyFromCID(txNode.Cid())
txID, err := pub.indexer.indexTransactionCID(tx, txModel, headerID)
var txID int64
txID, err = pub.indexer.indexTransactionCID(tx, txModel, headerID)
if err != nil {
return err
}
Expand All @@ -153,7 +162,8 @@ func (pub *IPLDPublisher) Publish(payload ConvertedPayload) error {
} else {
rctModel.PostState = common.Bytes2Hex(payload.Receipts[i].PostState)
}
if err := pub.indexer.indexReceiptCID(tx, rctModel, txID); err != nil {
err = pub.indexer.indexReceiptCID(tx, rctModel, txID)
if err != nil {
return err
}
}
Expand Down
Loading