Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore]: use wrapper structs for heights #1448

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
I beat sqlx 🎉
  • Loading branch information
MCJOHN974 committed Mar 3, 2025
commit fbce9ccf6d4c15423d0e445d24cbcba45841e986
40 changes: 40 additions & 0 deletions signer/src/storage/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,46 @@ impl Deref for StacksBlockHeight {
}
}

use sqlx::{Decode, Type};

// Implement sqlx::Decode by delegating to i64.
impl<'r> Decode<'r, sqlx::Postgres> for BitcoinBlockHeight {
fn decode(value: <sqlx::Postgres as sqlx::Database>::ValueRef<'r>)
-> Result<Self, sqlx::error::BoxDynError>
{
// First decode the raw value as an i64.
let int_value = <i64 as Decode<sqlx::Postgres>>::decode(value)?;
// Then convert i64 into BitcoinBlockHeight.
BitcoinBlockHeight::try_from(int_value).map_err(Into::into)
}
}

// Implement sqlx::Type for BitcoinBlockHeight by delegating to i64.
impl Type<sqlx::Postgres> for BitcoinBlockHeight {
fn type_info() -> <sqlx::Postgres as sqlx::Database>::TypeInfo {
<i64 as Type<sqlx::Postgres>>::type_info()
}
}

// Implement sqlx::Decode by delegating to i64.
impl<'r> Decode<'r, sqlx::Postgres> for StacksBlockHeight {
fn decode(value: <sqlx::Postgres as sqlx::Database>::ValueRef<'r>)
-> Result<Self, sqlx::error::BoxDynError>
{
// First decode the raw value as an i64.
let int_value = <i64 as Decode<sqlx::Postgres>>::decode(value)?;
// Then convert i64 into StacksBlockHeight.
StacksBlockHeight::try_from(int_value).map_err(Into::into)
}
}

// Implement sqlx::Type for StacksBlockHeight by delegating to i64.
impl Type<sqlx::Postgres> for StacksBlockHeight {
fn type_info() -> <sqlx::Postgres as sqlx::Database>::TypeInfo {
<i64 as Type<sqlx::Postgres>>::type_info()
}
}

implement_int!(BitcoinBlockHeight, u64);
implement_int!(StacksBlockHeight, u64);

Expand Down
Loading