extern crate alloc;
extern crate core;
extern crate self as consensus_core;
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hasher};
pub use kaspa_hashes::Hash;
pub mod acceptance_data;
pub mod api;
pub mod block;
pub mod blockhash;
pub mod blockstatus;
pub mod coinbase;
pub mod config;
pub mod constants;
pub mod daa_score_timestamp;
pub mod errors;
pub mod hashing;
pub mod header;
pub mod mass;
pub mod merkle;
pub mod muhash;
pub mod network;
pub mod pruning;
pub mod sign;
pub mod subnets;
pub mod trusted;
pub mod tx;
pub mod utxo;
pub type BlueWorkType = kaspa_math::Uint192;
pub type KType = u16;
pub type HashKTypeMap = std::sync::Arc<BlockHashMap<KType>>;
pub type BlockHashMap<V> = HashMap<Hash, V, BlockHasher>;
pub type BlockHashSet = HashSet<Hash, BlockHasher>;
pub trait HashMapCustomHasher {
fn new() -> Self;
fn with_capacity(capacity: usize) -> Self;
}
impl<V> HashMapCustomHasher for BlockHashMap<V> {
#[inline(always)]
fn new() -> Self {
Self::with_hasher(BlockHasher::new())
}
#[inline(always)]
fn with_capacity(cap: usize) -> Self {
Self::with_capacity_and_hasher(cap, BlockHasher::new())
}
}
impl HashMapCustomHasher for BlockHashSet {
#[inline(always)]
fn new() -> Self {
Self::with_hasher(BlockHasher::new())
}
#[inline(always)]
fn with_capacity(cap: usize) -> Self {
Self::with_capacity_and_hasher(cap, BlockHasher::new())
}
}
#[derive(Default, Debug)]
pub struct ChainPath {
pub added: Vec<Hash>,
pub removed: Vec<Hash>,
}
#[derive(Default, Clone, Copy)]
pub struct BlockHasher(u64);
impl BlockHasher {
#[inline(always)]
pub const fn new() -> Self {
Self(0)
}
}
impl Hasher for BlockHasher {
#[inline(always)]
fn finish(&self) -> u64 {
self.0
}
#[inline(always)]
fn write_u64(&mut self, v: u64) {
self.0 = v;
}
#[cold]
fn write(&mut self, _: &[u8]) {
unimplemented!("use write_u64")
}
}
impl BuildHasher for BlockHasher {
type Hasher = Self;
#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
Self(0)
}
}
pub type BlockLevel = u8;
#[cfg(test)]
mod tests {
use super::BlockHasher;
use kaspa_hashes::Hash;
use std::hash::{Hash as _, Hasher as _};
#[test]
fn test_block_hasher() {
let hash = Hash::from_le_u64([1, 2, 3, 4]);
let mut hasher = BlockHasher::default();
hash.hash(&mut hasher);
assert_eq!(hasher.finish(), 4);
}
}