Skip to content

Commit

Permalink
better error handling + sql update
Browse files Browse the repository at this point in the history
* do not stop parsing a btc block if encountering a parse error, we may have a parse error partially down the block but we shouldn't halt parsing the rest of the txs for that

* sql update; delete invalid pop basis upon re-org
  • Loading branch information
ClaytonNorthey92 committed Jan 15, 2025
1 parent 5d1bf8f commit 6c3d601
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion database/bfgd/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

const (
bfgdVersion = 15
bfgdVersion = 16

logLevel = "INFO"
verbose = false
Expand Down
2 changes: 1 addition & 1 deletion database/bfgd/scripts/0015.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ ALTER TABLE btc_blocks ADD UNIQUE (height);
-- when a btc block becomes "invalid" (orphaned), delete it and all pop_bases
-- that referenced it
ALTER TABLE pop_basis DROP CONSTRAINT pop_basis_btc_block_hash_fkey;
ALTER TABLE pop_basis ADD CONSTRAINT pop_basis_btc_block_hash_fkey FOREIGN KEY (btc_block_hash) REFERENCES btc_blocks (hash) ON DELETE CASCADE;
ALTER TABLE pop_basis ADD CONSTRAINT pop_basis_btc_block_hash_fkey FOREIGN KEY (btc_block_hash) REFERENCES btc_blocks (hash) ON UPDATE CASCADE;

COMMIT;
25 changes: 25 additions & 0 deletions database/bfgd/scripts/0016.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Copyright (c) 2025 Hemi Labs, Inc.
-- Use of this source code is governed by the MIT License,
-- which can be found in the LICENSE file.

BEGIN;

UPDATE version SET version = 16;

ALTER TABLE pop_basis DROP CONSTRAINT pop_basis_btc_block_hash_fkey;

-- upon a re-org, set all references to deleted btc block to NULL, then delete those rows via a trigger

ALTER TABLE pop_basis ADD CONSTRAINT pop_basis_btc_block_hash_fkey FOREIGN KEY (btc_block_hash) REFERENCES btc_blocks (hash) ON UPDATE SET NULL;

CREATE FUNCTION clean_pop_basis() RETURNS TRIGGER AS $clean_pop_basis$
BEGIN
DELETE FROM pop_basis WHERE btc_block_hash IS NULL;
RETURN NULL;
END;
$clean_pop_basis$ LANGUAGE plpgsql;

CREATE TRIGGER btc_blocks_clean AFTER INSERT OR UPDATE OR DELETE
ON btc_blocks EXECUTE FUNCTION clean_pop_basis();

COMMIT;
3 changes: 2 additions & 1 deletion service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ func (s *Server) processBitcoinBlock(ctx context.Context, height uint64) error {

publicKeyUncompressed, err := pop.ParsePublicKeyFromSignatureScript(mtx.TxIn[0].SignatureScript)
if err != nil {
return fmt.Errorf("could not parse signature script: %w", err)
log.Errorf("could not parse signature script: %w", err)
continue
}

popTxIdFull := []byte{}
Expand Down

0 comments on commit 6c3d601

Please sign in to comment.