From 553010ab5795a72fd7993aa7c8212698ac49ff31 Mon Sep 17 00:00:00 2001 From: ClaytonNorthey92 Date: Wed, 5 Jun 2024 13:13:20 -0400 Subject: [PATCH] squashme --- database/bfgd/scripts/0009.sql | 30 +++++++++++++++++++++++------- service/bfg/bfg.go | 4 +--- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/database/bfgd/scripts/0009.sql b/database/bfgd/scripts/0009.sql index f63dbfdd..a4907c78 100644 --- a/database/bfgd/scripts/0009.sql +++ b/database/bfgd/scripts/0009.sql @@ -8,7 +8,7 @@ SET version = 9; DROP MATERIALIZED VIEW btc_blocks_can; -- this materialized view represents the canonical btc_blocks as we know it -CREATE MATERIALIZED VIEW btc_blocks_can AS WITH RECURSIVE bb AS ( +CREATE MATERIALIZED VIEW btc_blocks_can AS explain analyze WITH RECURSIVE bb AS ( -- define the tip as the highest block in __highest, look below -- for definition of this result set SELECT hash, @@ -59,12 +59,16 @@ FROM bb; CREATE VIEW heights_with_no_children AS SELECT height FROM btc_blocks bb1 --- for all blocks, check if there exists no children -WHERE NOT EXISTS ( + + + -- for all blocks, check if there exists no children + WHERE NOT EXISTS ( SELECT * FROM btc_blocks bb2 WHERE substr(bb2.header, 5, 32) = bb1.hash - ) -- then, check if there exist no other blocks at this height with children + ) + + -- then, check if there exist no other blocks at this height with children AND NOT EXISTS ( SELECT * FROM btc_blocks bb3 @@ -74,14 +78,26 @@ WHERE NOT EXISTS ( FROM btc_blocks bb4 WHERE substr(bb4.header, 5, 32) = bb3.hash ) - ) -- exclude the tip, as it will have no children by its nature + ) + +-- exclude the tip, as it will have no children by its nature, +-- since we use electrumx with simple incrementing, +-- we only get 1 block each time the height increases, we SHOULD only need to +-- offset 1 for the tip, but it doesn't hurt to check extra blocks +-- (just potentially slightly noisy) ORDER BY height DESC OFFSET 1; -- only refresh materialized view if there are no heights without children -CREATE OR REPLACE FUNCTION refresh_btc_blocks_can() RETURNS TRIGGER LANGUAGE PLPGSQL AS $$ BEGIN IF NOT EXISTS( +CREATE OR REPLACE FUNCTION refresh_btc_blocks_can() RETURNS TRIGGER LANGUAGE PLPGSQL AS $$ BEGIN + +-- if there exist no heights without children +IF NOT EXISTS( SELECT * FROM heights_with_no_children - ) THEN REFRESH MATERIALIZED VIEW btc_blocks_can; + ) + +-- then we refresh the materialized view of the canonical chain +THEN REFRESH MATERIALIZED VIEW btc_blocks_can; END IF; RETURN NEW; END; diff --git a/service/bfg/bfg.go b/service/bfg/bfg.go index bd4fa662..09b8fb64 100644 --- a/service/bfg/bfg.go +++ b/service/bfg/bfg.go @@ -200,9 +200,7 @@ func (s *Server) invalidBlockChecker(ctx context.Context) { } } - go func() { - s.queueCheckForInvalidBlocks() - }() + s.queueCheckForInvalidBlocks() } } }