Skip to content

feat: note the canonical bitcoin blocks in the database#1891

Open
djordon wants to merge 39 commits intomainfrom
1889-note-the-canonical-bitcoin-blocks-in-the-database
Open

feat: note the canonical bitcoin blocks in the database#1891
djordon wants to merge 39 commits intomainfrom
1889-note-the-canonical-bitcoin-blocks-in-the-database

Conversation

@djordon
Copy link
Contributor

@djordon djordon commented Jan 13, 2026

Description

Closes #1889

Changes

  1. Add a new column to the bitcoin blocks table that notes whether a given bitcoin block is canonical.
  2. Add a new query that updates the database with a given chain tip to set whether the blocks in the database are canonical with respect to the given chain tip block hash.
  3. Update the block observer to note whether the blocks in the database are canonical or not.

Testing Information

I added a few integration tests, let me know if you can think of some scenarios that you want added.

Checklist

  • I have performed a self-review of my code

@djordon djordon added this to the sBTC: v1.3.0 milestone Jan 13, 2026
@djordon djordon self-assigned this Jan 13, 2026
@djordon djordon added the sbtc signer binary The sBTC Bootstrap Signer. label Jan 13, 2026
@djordon djordon added this to sBTC Jan 13, 2026
@github-project-automation github-project-automation bot moved this to Needs Triage in sBTC Jan 13, 2026
@djordon djordon moved this from Needs Triage to In Review in sBTC Jan 14, 2026
@MCJOHN974
Copy link
Contributor

I added a few integration tests, let me know if you can think of some scenarios that you want added.

Probably test for case when two forks have no common blocks

MCJOHN974
MCJOHN974 previously approved these changes Jan 29, 2026
/// non-canonical if they are part of the chain identified by the given
/// chain tip.
#[tokio::test]
async fn test_set_canonical_bitcoin_blockchain_with_fork_known_later() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe use something like TestData + get_rng() or so in testing to generate various blockchains (and for example just test that in-memory and postgres canonical-related functions behave similarly)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c8d7896

Copy link
Contributor

@matteojug matteojug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Should we add a PR label for DB migrations? Mostly to then have them easily listed in the release notes

};
let blockchains = TestData::generate(&mut rng, &signer_keys, &params);

// Insert all blocks from into the database
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: from?

assert_eq!(db.is_block_canonical(&new_block).await.unwrap(), Some(true));
}

// Check that the new blocks have is_canonical = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: old comment, should probably be moved up in the for

Comment on lines +8227 to +8230
// Create two blockchains, one of length 10 and another of length
// 20, where one forks the other at height 3 (the block with height
// 3 differs from one to the other but their earlier blocks are the
// same)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: now fork chain is 17 blocks long, fork block is at height 2

@djordon djordon added breaking-local Breaking changes for the local signer (i.e. config, db) db-migrations This PR introduces a schema change to the database and removed breaking-local Breaking changes for the local signer (i.e. config, db) labels Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-local Breaking changes for the local signer (i.e. config, db) db-migrations This PR introduces a schema change to the database sbtc signer binary The sBTC Bootstrap Signer.

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

[Feature]: Note the canonical bitcoin blocks in the database

3 participants