Skip to content

Commit

Permalink
refactor: pass around block refs (#1221)
Browse files Browse the repository at this point in the history
* Add a new function for getting the block ref from the database
* Minor clean up of block hash and block height stuff in the TxSigner
* Ignore presign requests from non-coordinators
* Don't accept WSTS messages from signers with outdated chain tips
  • Loading branch information
djordon authored Feb 4, 2025
1 parent 138f911 commit 7aafe07
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 110 deletions.
12 changes: 12 additions & 0 deletions signer/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ impl super::DbRead for SharedStore {
.map(|block| block.block_hash))
}

async fn get_bitcoin_canonical_chain_tip_ref(
&self,
) -> Result<Option<model::BitcoinBlockRef>, Error> {
Ok(self
.lock()
.await
.bitcoin_blocks
.values()
.max_by_key(|block| (block.block_height, block.block_hash))
.map(model::BitcoinBlockRef::from))
}

async fn get_stacks_chain_tip(
&self,
bitcoin_chain_tip: &model::BitcoinBlockHash,
Expand Down
5 changes: 5 additions & 0 deletions signer/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub trait DbRead {
&self,
) -> impl Future<Output = Result<Option<model::BitcoinBlockHash>, Error>> + Send;

/// Get the bitcoin canonical chain tip.
fn get_bitcoin_canonical_chain_tip_ref(
&self,
) -> impl Future<Output = Result<Option<model::BitcoinBlockRef>, Error>> + Send;

/// Get the stacks chain tip, defined as the highest stacks block
/// confirmed by the bitcoin chain tip.
fn get_stacks_chain_tip(
Expand Down
4 changes: 3 additions & 1 deletion signer/src/storage/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,11 @@ impl std::fmt::Display for BitcoinBlockHash {

/// A struct that references a specific bitcoin block is identifier and its
/// position in the blockchain.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, sqlx::FromRow)]
#[cfg_attr(feature = "testing", derive(fake::Dummy))]
pub struct BitcoinBlockRef {
/// The height of the block in the bitcoin blockchain.
#[sqlx(try_from = "i64")]
pub block_height: u64,
/// Bitcoin block hash. It uniquely identifies the bitcoin block.
pub block_hash: BitcoinBlockHash,
Expand Down
16 changes: 16 additions & 0 deletions signer/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,22 @@ impl super::DbRead for PgStore {
.map_err(Error::SqlxQuery)
}

async fn get_bitcoin_canonical_chain_tip_ref(
&self,
) -> Result<Option<model::BitcoinBlockRef>, Error> {
sqlx::query_as::<_, model::BitcoinBlockRef>(
"SELECT
block_hash
, block_height
FROM sbtc_signer.bitcoin_blocks
ORDER BY block_height DESC, block_hash DESC
LIMIT 1",
)
.fetch_optional(&self.0)
.await
.map_err(Error::SqlxQuery)
}

async fn get_stacks_chain_tip(
&self,
bitcoin_chain_tip: &model::BitcoinBlockHash,
Expand Down
Loading

0 comments on commit 7aafe07

Please sign in to comment.