Skip to content

Commit

Permalink
Remove redundant ChildKey
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Dec 5, 2023
1 parent 0fad12f commit 26302c7
Showing 1 changed file with 0 additions and 45 deletions.
45 changes: 0 additions & 45 deletions consensus/src/model/stores/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use kaspa_database::prelude::StoreResult;
use kaspa_database::prelude::DB;
use kaspa_database::registry::DatabaseStorePrefixes;
use kaspa_hashes::Hash;
use kaspa_hashes::HASH_SIZE;
use rocksdb::WriteBatch;
use std::fmt::Display;
use std::sync::Arc;

pub trait ChildrenStoreReader {
Expand All @@ -24,49 +22,6 @@ pub trait ChildrenStore {
fn delete_child(&mut self, writer: impl DbWriter, parent: Hash, child: Hash) -> Result<(), StoreError>;
}

struct ChildKey {
parent: Hash,
child: Hash,
}

const KEY_SIZE: usize = 2 * HASH_SIZE;

#[derive(Eq, Hash, PartialEq, Debug, Copy, Clone)]
struct DbChildKey([u8; KEY_SIZE]);

impl AsRef<[u8]> for DbChildKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl Display for DbChildKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let key: ChildKey = (*self).into();
write!(f, "{}:{}", key.parent, key.child)
}
}

impl From<ChildKey> for DbChildKey {
fn from(key: ChildKey) -> Self {
let mut bytes = [0; KEY_SIZE];
bytes[..HASH_SIZE].copy_from_slice(&key.parent.as_bytes());
bytes[HASH_SIZE..].copy_from_slice(&key.child.as_bytes());
Self(bytes)
}
}

impl From<DbChildKey> for ChildKey {
fn from(k: DbChildKey) -> Self {
let parent_bytes: [u8; HASH_SIZE] = k.0[..HASH_SIZE].try_into().unwrap();
let parent: Hash = parent_bytes.into();

let child_bytes: [u8; HASH_SIZE] = k.0[HASH_SIZE..].try_into().unwrap();
let child: Hash = child_bytes.into();
Self { parent, child }
}
}

/// A DB + cache implementation of `DbChildrenStore` trait, with concurrency support.
#[derive(Clone)]
pub struct DbChildrenStore {
Expand Down

0 comments on commit 26302c7

Please sign in to comment.