Skip to content

Commit

Permalink
Traits and data structs for reachability services (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsutton authored Jul 20, 2022
1 parent ca78dd5 commit b461014
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 3 deletions.
1 change: 1 addition & 0 deletions kaspad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ include = [
async-std = "1.11.0"
regex = "1"
hex = "0.4.3"
thiserror = "1.0.31"
kaspa-core = {path="../core"}

[features]
Expand Down
8 changes: 8 additions & 0 deletions kaspad/src/domain/consensus/model/api/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ use std::fmt::Debug;

const DOMAIN_HASH_SIZE: usize = 32;

/**
* TODO: consider the right design for passing domain hashes around. Some options:
* 1. Pass around by value. Pros: simple concurrency management; no indirect access to heap. Cons: x4 size of a pointer
* 2. Use Box<DomainHash> or Arc<DomainHash> or a combination per need.
*
* Ideally we manage to wrap the type correctly and make easy conventions for using it.
*/

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct DomainHash {
byte_array: [u8; DOMAIN_HASH_SIZE],
Expand Down
2 changes: 2 additions & 0 deletions kaspad/src/domain/consensus/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod api;
pub mod services;
pub mod staging_area;
pub mod stores;
1 change: 1 addition & 0 deletions kaspad/src/domain/consensus/model/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod reachability_service;
38 changes: 38 additions & 0 deletions kaspad/src/domain/consensus/model/services/reachability_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use thiserror::Error;

use crate::domain::consensus::model::{
api::hash::DomainHash, staging_area::StagingArea, stores::errors::StoreError,
};

#[derive(Error, Debug)]
pub enum ReachabilityError {
#[error("data store error")]
ReachabilityStoreError(#[from] StoreError),
}

pub trait ReachabilityService {
fn init(&mut self, staging: &dyn StagingArea) -> Result<(), ReachabilityError>;
fn add(
&mut self,
staging: &dyn StagingArea,
block: DomainHash,
selected_parent: DomainHash,
mergeset: &[DomainHash],
is_selected_leaf: bool,
) -> Result<(), ReachabilityError>;
fn is_chain_ancestor_of(
&self,
low: DomainHash,
high: DomainHash,
) -> Result<bool, ReachabilityError>;
fn is_dag_ancestor_of(
&self,
low: DomainHash,
high: DomainHash,
) -> Result<bool, ReachabilityError>;
fn get_next_chain_ancestor(
&self,
descendant: DomainHash,
ancestor: DomainHash,
) -> Result<DomainHash, ReachabilityError>;
}
12 changes: 11 additions & 1 deletion kaspad/src/domain/consensus/model/staging_area.rs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
pub struct StagingArea {}
pub trait StagingArea {
fn commit(&mut self);
}

pub struct InMemoryStagingArea {}

impl StagingArea for InMemoryStagingArea {
fn commit(&mut self) {
todo!()
}
}
20 changes: 20 additions & 0 deletions kaspad/src/domain/consensus/model/stores/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum StoreError {
#[error("Key not found in store")]
KeyNotFound,
// More usage examples:
//
// #[error("data store disconnected")]
// Disconnect(#[from] io::Error),
// #[error("the data for key `{0}` is not available")]
// Redaction(String),
// #[error("invalid header (expected {expected:?}, found {found:?})")]
// InvalidHeader {
// expected: String,
// found: String,
// },
// #[error("unknown data store error")]
// Unknown,
}
2 changes: 2 additions & 0 deletions kaspad/src/domain/consensus/model/stores/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod errors;
pub mod reachability_store;
43 changes: 43 additions & 0 deletions kaspad/src/domain/consensus/model/stores/reachability_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::errors::StoreError;
use crate::domain::consensus::{
model::{api::hash::DomainHash, staging_area::StagingArea},
processes::reachability::interval::Interval,
};

pub struct ReachabilityData {
pub children: Vec<DomainHash>,
pub parent: DomainHash,
pub interval: Interval,
pub future_covering_set: Vec<DomainHash>,
}

impl ReachabilityData {
pub fn new(
children: Vec<DomainHash>,
parent: DomainHash,
interval: Interval,
future_covering_set: Vec<DomainHash>,
) -> Self {
Self {
children,
parent,
interval,
future_covering_set,
}
}
}

pub trait ReachabilityStore {
fn stage(
&mut self,
staging: &dyn StagingArea,
hash: &DomainHash,
data: &ReachabilityData,
) -> Result<(), StoreError>;
fn get(
&self,
staging: &dyn StagingArea,
hash: &DomainHash,
) -> Result<ReachabilityData, StoreError>;
fn has(&self, staging: &dyn StagingArea, hash: &DomainHash) -> Result<bool, StoreError>;
}
2 changes: 1 addition & 1 deletion kaspad/src/domain/consensus/processes/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod reachabilitymanager;
pub mod reachability;
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl Interval {
return self.split_exact(sizes);
}

//
// Add a fractional bias to every size in the provided sizes
//

let mut remaining_bias = interval_size - sizes_sum;
let total_bias = remaining_bias as f64;
Expand Down
64 changes: 64 additions & 0 deletions kaspad/src/domain/consensus/processes/reachability/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::domain::consensus::model::services::reachability_service::ReachabilityService;

pub struct ReachabilityManager {}

impl ReachabilityManager {}

impl ReachabilityService for ReachabilityManager {
fn init(
&mut self,
staging: &dyn crate::domain::consensus::model::staging_area::StagingArea,
) -> Result<
(),
crate::domain::consensus::model::services::reachability_service::ReachabilityError,
> {
todo!()
}

fn add(
&mut self,
staging: &dyn crate::domain::consensus::model::staging_area::StagingArea,
block: crate::domain::consensus::model::api::hash::DomainHash,
selected_parent: crate::domain::consensus::model::api::hash::DomainHash,
mergeset: &[crate::domain::consensus::model::api::hash::DomainHash],
is_selected_leaf: bool,
) -> Result<
(),
crate::domain::consensus::model::services::reachability_service::ReachabilityError,
> {
todo!()
}

fn is_chain_ancestor_of(
&self,
low: crate::domain::consensus::model::api::hash::DomainHash,
high: crate::domain::consensus::model::api::hash::DomainHash,
) -> Result<
bool,
crate::domain::consensus::model::services::reachability_service::ReachabilityError,
> {
todo!()
}

fn is_dag_ancestor_of(
&self,
low: crate::domain::consensus::model::api::hash::DomainHash,
high: crate::domain::consensus::model::api::hash::DomainHash,
) -> Result<
bool,
crate::domain::consensus::model::services::reachability_service::ReachabilityError,
> {
todo!()
}

fn get_next_chain_ancestor(
&self,
descendant: crate::domain::consensus::model::api::hash::DomainHash,
ancestor: crate::domain::consensus::model::api::hash::DomainHash,
) -> Result<
crate::domain::consensus::model::api::hash::DomainHash,
crate::domain::consensus::model::services::reachability_service::ReachabilityError,
> {
todo!()
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod interval;
pub mod manager;
2 changes: 1 addition & 1 deletion kaspad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use kaspa_core::*;
mod domain;

use domain::consensus::model::api::hash::DomainHash;
use domain::consensus::processes::reachabilitymanager::interval;
use domain::consensus::processes::reachability::interval;

const SERVICE_THREADS : usize = 1;
// if sleep time is < 0, sleep is skipped
Expand Down

0 comments on commit b461014

Please sign in to comment.