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

feat(clean): dc_db: rename DbBlockId::BlockN to DbBlockId::Number #310

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next release

- fix(sync): pending block retrying mechanism
- feat(clean): dc_db: rename `DbBlockId::BlockN` to `DbBlockId::Number`
- fix:(tests): Add testing feature to mc-db dev dependency (#294)
- feat: new crate gateway client & server
- test: Starknet-js basic tests added
Expand Down
16 changes: 8 additions & 8 deletions crates/client/db/src/block_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,17 @@ impl MadaraBackend {

pub(crate) fn id_to_storage_type(&self, id: &BlockId) -> Result<Option<DbBlockId>> {
match id {
BlockId::Hash(hash) => Ok(self.block_hash_to_block_n(hash)?.map(DbBlockId::BlockN)),
BlockId::Number(block_n) => Ok(Some(DbBlockId::BlockN(*block_n))),
BlockId::Tag(BlockTag::Latest) => Ok(self.get_latest_block_n()?.map(DbBlockId::BlockN)),
BlockId::Hash(hash) => Ok(self.block_hash_to_block_n(hash)?.map(DbBlockId::Number)),
BlockId::Number(block_n) => Ok(Some(DbBlockId::Number(*block_n))),
BlockId::Tag(BlockTag::Latest) => Ok(self.get_latest_block_n()?.map(DbBlockId::Number)),
BlockId::Tag(BlockTag::Pending) => Ok(Some(DbBlockId::Pending)),
}
}

fn storage_to_info(&self, id: &DbBlockId) -> Result<Option<MadaraMaybePendingBlockInfo>> {
match id {
DbBlockId::Pending => Ok(Some(MadaraMaybePendingBlockInfo::Pending(self.get_pending_block_info()?))),
DbBlockId::BlockN(block_n) => {
DbBlockId::Number(block_n) => {
Ok(self.get_block_info_from_block_n(*block_n)?.map(MadaraMaybePendingBlockInfo::NotPending))
}
}
Expand All @@ -283,7 +283,7 @@ impl MadaraBackend {
fn storage_to_inner(&self, id: &DbBlockId) -> Result<Option<MadaraBlockInner>> {
match id {
DbBlockId::Pending => Ok(Some(self.get_pending_block_inner()?)),
DbBlockId::BlockN(block_n) => self.get_block_inner_from_block_n(*block_n),
DbBlockId::Number(block_n) => self.get_block_inner_from_block_n(*block_n),
}
}

Expand All @@ -292,7 +292,7 @@ impl MadaraBackend {
pub fn get_block_n(&self, id: &impl DbBlockIdResolvable) -> Result<Option<u64>> {
let Some(ty) = id.resolve_db_block_id(self)? else { return Ok(None) };
match &ty {
DbBlockId::BlockN(block_id) => Ok(Some(*block_id)),
DbBlockId::Number(block_id) => Ok(Some(*block_id)),
DbBlockId::Pending => Ok(None),
}
}
Expand All @@ -301,7 +301,7 @@ impl MadaraBackend {
let Some(ty) = id.resolve_db_block_id(self)? else { return Ok(None) };
match &ty {
// TODO: fast path if id is already a block hash..
DbBlockId::BlockN(block_n) => Ok(self.get_block_info_from_block_n(*block_n)?.map(|b| b.block_hash)),
DbBlockId::Number(block_n) => Ok(self.get_block_info_from_block_n(*block_n)?.map(|b| b.block_hash)),
DbBlockId::Pending => Ok(None),
}
}
Expand All @@ -310,7 +310,7 @@ impl MadaraBackend {
let Some(ty) = id.resolve_db_block_id(self)? else { return Ok(None) };
match ty {
DbBlockId::Pending => Ok(Some(self.get_pending_block_state_update()?)),
DbBlockId::BlockN(block_n) => self.get_state_update(block_n),
DbBlockId::Number(block_n) => self.get_state_update(block_n),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/client/db/src/class_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl MadaraBackend {

let valid = match (requested_id, info.block_id) {
(DbBlockId::Pending, _) => true,
(DbBlockId::BlockN(block_n), DbBlockId::BlockN(real_block_n)) => real_block_n <= block_n,
(DbBlockId::Number(block_n), DbBlockId::Number(real_block_n)) => real_block_n <= block_n,
_ => false,
};
if !valid {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl MadaraBackend {
block_number: u64,
converted_classes: &[ConvertedClass],
) -> Result<(), MadaraStorageError> {
self.store_classes(DbBlockId::BlockN(block_number), converted_classes, Column::ClassInfo, Column::ClassCompiled)
self.store_classes(DbBlockId::Number(block_number), converted_classes, Column::ClassInfo, Column::ClassCompiled)
}

/// NB: This functions needs to run on the rayon thread pool
Expand Down
2 changes: 1 addition & 1 deletion crates/client/db/src/contract_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl MadaraBackend {
let Some(block_n) = self.get_latest_block_n()? else { return Ok(None) };
block_n
}
DbBlockId::BlockN(block_n) => block_n,
DbBlockId::Number(block_n) => block_n,
};

// We try to find history values.
Expand Down
6 changes: 3 additions & 3 deletions crates/client/db/src/db_block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{MadaraBackend, MadaraStorageError};
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum DbBlockId {
Pending,
BlockN(u64),
Number(u64),
}

impl DbBlockId {
Expand Down Expand Up @@ -42,7 +42,7 @@ impl fmt::Display for DbBlockId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Pending => write!(f, "#<pending>"),
Self::BlockN(block_n) => write!(f, "#{block_n}"),
Self::Number(block_n) => write!(f, "#{block_n}"),
}
}
}
Expand All @@ -60,6 +60,6 @@ mod test {
#[test]
fn test_db_block_id() {
assert!(DbBlockId::Pending.is_pending());
assert!(!DbBlockId::BlockN(0).is_pending());
assert!(!DbBlockId::Number(0).is_pending());
}
}
6 changes: 3 additions & 3 deletions crates/client/db/src/tests/test_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ mod block_tests {
backend.store_block(block.clone(), state_diff.clone(), vec![]).unwrap();
backend.store_block(pending_block_one(), pending_state_diff_one(), vec![]).unwrap();

assert_eq!(backend.resolve_block_id(&BlockId::Hash(block_hash)).unwrap().unwrap(), DbBlockId::BlockN(0));
assert_eq!(backend.resolve_block_id(&BlockId::Number(0)).unwrap().unwrap(), DbBlockId::BlockN(0));
assert_eq!(backend.resolve_block_id(&BlockId::Hash(block_hash)).unwrap().unwrap(), DbBlockId::Number(0));
assert_eq!(backend.resolve_block_id(&BlockId::Number(0)).unwrap().unwrap(), DbBlockId::Number(0));
assert_eq!(backend.resolve_block_id(&DbBlockId::Pending).unwrap().unwrap(), DbBlockId::Pending);
}

Expand All @@ -43,7 +43,7 @@ mod block_tests {

#[tokio::test]
async fn test_store_block() {
const BLOCK_ID_0: DbBlockId = DbBlockId::BlockN(0);
const BLOCK_ID_0: DbBlockId = DbBlockId::Number(0);

let db = temp_db().await;
let backend = db.backend();
Expand Down
6 changes: 3 additions & 3 deletions crates/client/exec/src/block_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ impl ExecutionContext {
pub fn init_cached_state(&self) -> CachedState<BlockifierStateAdapter> {
let on_top_of = match self.db_id {
DbBlockId::Pending => Some(DbBlockId::Pending),
DbBlockId::BlockN(block_n) => {
DbBlockId::Number(block_n) => {
// We exec on top of the previous block. None means we are executing genesis.
block_n.checked_sub(1).map(DbBlockId::BlockN)
block_n.checked_sub(1).map(DbBlockId::Number)
}
};

Expand Down Expand Up @@ -70,7 +70,7 @@ impl ExecutionContext {
block.header.l1_da_mode,
),
MadaraMaybePendingBlockInfo::NotPending(block) => (
DbBlockId::BlockN(block.header.block_number),
DbBlockId::Number(block.header.block_number),
block.header.protocol_version,
block.header.block_number,
block.header.block_timestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn get_state_update(starknet: &Starknet, block_id: BlockId) -> StarknetRpcRe

// Get the old root from the previous block if it exists, otherwise default to zero.
let old_root = if let Some(val) = block_info.header.block_number.checked_sub(1) {
let prev_block_info = &starknet.get_block_info(&DbBlockId::BlockN(val))?;
let prev_block_info = &starknet.get_block_info(&DbBlockId::Number(val))?;
let prev_block_info =
prev_block_info.as_nonpending().ok_or_internal_server_error("Block should not be pending")?;

Expand Down
2 changes: 1 addition & 1 deletion crates/client/sync/src/l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ mod tests {
Err(_) => panic!("Timeout reached while waiting for task completion"),
}

let applied_block = backend.get_block(&DbBlockId::BlockN(0)).unwrap();
let applied_block = backend.get_block(&DbBlockId::Number(0)).unwrap();
assert!(applied_block.is_some(), "The block was not applied correctly");
let applied_block = MadaraBlock::try_from(applied_block.unwrap()).unwrap();

Expand Down
Loading