-
Notifications
You must be signed in to change notification settings - Fork 99
chore: Refactor common db capabilities into separate crate #1685
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ae87e93
Initial crate impl and usage in validator
sergerad eee2812
Refactor new()
sergerad e61fd54
RM unused
sergerad d4aa85b
Wire errors together
sergerad a3f5b2d
toml
sergerad eada2c5
Comments
sergerad 4280996
Fix lint
sergerad dd6b51c
RM interact variant
sergerad 1c001cc
RM pool variant
sergerad 7d0bf15
RM conversion variant
sergerad 4d7834a
lint
sergerad 65696b3
Add static
sergerad 60f18b3
Merge branch 'next' of github.com:0xMiden/miden-node into sergerad-db…
sergerad 829821f
move prefix to import statement
sergerad 721732f
RM unused variants
sergerad 43f3fdd
Remove setup error
sergerad d9f561d
RM variant
sergerad d147f02
RM variants
sergerad 6ee4abe
RM more variants
sergerad c568ca7
Merge branch 'next' of github.com:0xMiden/miden-node into sergerad-db…
sergerad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| description = "Shared database capabilities for Miden node" | ||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| keywords = ["database", "miden", "node"] | ||
| license.workspace = true | ||
| name = "miden-node-db" | ||
| repository.workspace = true | ||
| rust-version.workspace = true | ||
| version.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [dependencies] | ||
| deadpool = { default-features = false, workspace = true } | ||
| deadpool-diesel = { features = ["sqlite"], workspace = true } | ||
| deadpool-sync = { default-features = false, workspace = true } | ||
| diesel = { features = ["sqlite"], workspace = true } | ||
| miden-protocol = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| tracing = { workspace = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| //! Central place to define conversion from and to database primitive types | ||
| //! | ||
| //! Eventually, all of them should have types and we can implement a trait for them | ||
| //! rather than function pairs. | ||
| //! | ||
| //! Notice: All of them are infallible. The invariant is a sane content of the database | ||
| //! and humans ensure the sanity of casts. | ||
| //! | ||
| //! Notice: Keep in mind if you _need_ to expand the datatype, only if you require sorting this is | ||
| //! mandatory! | ||
| //! | ||
| //! Notice: Ensure you understand what casting does at the bit-level before changing any. | ||
| //! | ||
| //! Notice: Changing any of these are _backwards-incompatible_ changes that are not caught/covered | ||
| //! by migrations! | ||
|
|
||
| #![expect( | ||
| clippy::inline_always, | ||
| reason = "Just unification helpers of 1-2 lines of casting types" | ||
| )] | ||
| #![expect( | ||
| dead_code, | ||
| reason = "Not all converters are used bidirectionally, however, keeping them is a good thing" | ||
| )] | ||
| #![expect( | ||
| clippy::cast_sign_loss, | ||
| reason = "This is the one file where we map the signed database types to the working types" | ||
| )] | ||
| #![expect( | ||
| clippy::cast_possible_wrap, | ||
| reason = "We will not approach the item count where i64 and usize casting will cause issues | ||
| on relevant platforms" | ||
| )] | ||
|
|
||
| use miden_protocol::Felt; | ||
| use miden_protocol::account::{StorageSlotName, StorageSlotType}; | ||
| use miden_protocol::block::BlockNumber; | ||
| use miden_protocol::note::NoteTag; | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| #[error("failed to convert from database type {from_type} into {into_type}")] | ||
| pub struct DatabaseTypeConversionError { | ||
| source: Box<dyn std::error::Error + Send + Sync>, | ||
| from_type: &'static str, | ||
| into_type: &'static str, | ||
| } | ||
|
|
||
| /// Convert from and to it's database representation and back | ||
| /// | ||
| /// We do not assume sanity of DB types. | ||
| pub trait SqlTypeConvert: Sized { | ||
| type Raw: Sized; | ||
|
|
||
| fn to_raw_sql(self) -> Self::Raw; | ||
| fn from_raw_sql(_raw: Self::Raw) -> Result<Self, DatabaseTypeConversionError>; | ||
|
|
||
| fn map_err<E: std::error::Error + Send + Sync + 'static>( | ||
| source: E, | ||
| ) -> DatabaseTypeConversionError { | ||
| DatabaseTypeConversionError { | ||
| source: Box::new(source), | ||
| from_type: std::any::type_name::<Self::Raw>(), | ||
| into_type: std::any::type_name::<Self>(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SqlTypeConvert for BlockNumber { | ||
| type Raw = i64; | ||
|
|
||
| fn from_raw_sql(raw: Self::Raw) -> Result<Self, DatabaseTypeConversionError> { | ||
| u32::try_from(raw).map(BlockNumber::from).map_err(Self::map_err) | ||
| } | ||
|
|
||
| fn to_raw_sql(self) -> Self::Raw { | ||
| i64::from(self.as_u32()) | ||
| } | ||
| } | ||
|
|
||
| impl SqlTypeConvert for NoteTag { | ||
| type Raw = i32; | ||
|
|
||
| #[inline(always)] | ||
| fn from_raw_sql(raw: Self::Raw) -> Result<Self, DatabaseTypeConversionError> { | ||
| #[expect(clippy::cast_sign_loss)] | ||
| Ok(NoteTag::new(raw as u32)) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn to_raw_sql(self) -> Self::Raw { | ||
| self.as_u32() as i32 | ||
| } | ||
| } | ||
|
|
||
| impl SqlTypeConvert for StorageSlotType { | ||
| type Raw = i32; | ||
|
|
||
| #[inline(always)] | ||
| fn from_raw_sql(raw: Self::Raw) -> Result<Self, DatabaseTypeConversionError> { | ||
| #[derive(Debug, thiserror::Error)] | ||
| #[error("invalid storage slot type value {0}")] | ||
| struct ValueError(i32); | ||
|
|
||
| Ok(match raw { | ||
| 0 => StorageSlotType::Value, | ||
| 1 => StorageSlotType::Map, | ||
| invalid => { | ||
| return Err(Self::map_err(ValueError(invalid))); | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn to_raw_sql(self) -> Self::Raw { | ||
| match self { | ||
| StorageSlotType::Value => 0, | ||
| StorageSlotType::Map => 1, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SqlTypeConvert for StorageSlotName { | ||
| type Raw = String; | ||
|
|
||
| fn from_raw_sql(raw: Self::Raw) -> Result<Self, DatabaseTypeConversionError> { | ||
| StorageSlotName::new(raw).map_err(Self::map_err) | ||
| } | ||
|
|
||
| fn to_raw_sql(self) -> Self::Raw { | ||
| String::from(self) | ||
| } | ||
| } | ||
|
|
||
| // Raw type conversions - eventually introduce wrapper types | ||
| // =========================================================== | ||
|
|
||
| #[inline(always)] | ||
| pub(crate) fn raw_sql_to_nullifier_prefix(raw: i32) -> u16 { | ||
| debug_assert!(raw >= 0); | ||
| raw as u16 | ||
| } | ||
| #[inline(always)] | ||
| pub(crate) fn nullifier_prefix_to_raw_sql(prefix: u16) -> i32 { | ||
| i32::from(prefix) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub(crate) fn raw_sql_to_nonce(raw: i64) -> Felt { | ||
| debug_assert!(raw >= 0); | ||
| Felt::new(raw as u64) | ||
| } | ||
| #[inline(always)] | ||
| pub(crate) fn nonce_to_raw_sql(nonce: Felt) -> i64 { | ||
| nonce.as_int() as i64 | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub(crate) fn raw_sql_to_fungible_delta(raw: i64) -> i64 { | ||
| raw | ||
| } | ||
| #[inline(always)] | ||
| pub(crate) fn fungible_delta_to_raw_sql(delta: i64) -> i64 { | ||
| delta | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| #[expect(clippy::cast_sign_loss)] | ||
| pub(crate) fn raw_sql_to_note_type(raw: i32) -> u8 { | ||
| raw as u8 | ||
| } | ||
| #[inline(always)] | ||
| pub(crate) fn note_type_to_raw_sql(note_type: u8) -> i32 { | ||
| i32::from(note_type) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| pub(crate) fn raw_sql_to_idx(raw: i32) -> usize { | ||
| raw as usize | ||
| } | ||
| #[inline(always)] | ||
| pub(crate) fn idx_to_raw_sql(idx: usize) -> i32 { | ||
| idx as i32 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really struggled formulating these..