Skip to content

Commit

Permalink
feat(sequencing): add cende module and main types
Browse files Browse the repository at this point in the history
  • Loading branch information
DvirYo-starkware committed Dec 18, 2024
1 parent 5217539 commit 8468f09
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
80 changes: 80 additions & 0 deletions crates/sequencing/papyrus_consensus_orchestrator/src/cende/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::sync::{Arc, OnceLock};

use async_trait::async_trait;
use tokio::sync::Mutex;
use tokio::task::{self};
use tracing::debug;

// TODO(dvir): add tests when will have more logic.

/// A chunk of all the data to write to Aersopike.
#[derive(Debug)]
pub(crate) struct AerospikeBlob {
// TODO(yael, dvir): add the blob fields.
}

#[async_trait]
pub(crate) trait CendeContext: Send + Sync {
/// Write the previous height blob to Aerospike. Returns a cell with an inner boolean indicating
/// whether the write was successful.
fn write_prev_height_blob(&self) -> Arc<OnceLock<bool>>;

// Prepares the previous height blob that will be written in the next height.
async fn prepare_blob_for_next_height(&self, nfb: NeededForBlob);
}

#[derive(Clone, Debug)]
pub(crate) struct CendeAmbassador {
// TODO(dvir): consider creating enum varaiant instead of the `Option<AerospikeBlob>`.
// `None` indicates that there is no blob to write, and therefore, the node can't be the
// proposer.
prev_height_blob: Arc<Mutex<Option<AerospikeBlob>>>,
}

impl CendeAmbassador {
pub(crate) fn new() -> Self {
CendeAmbassador { prev_height_blob: Arc::new(Mutex::new(None)) }
}
}

#[async_trait]
impl CendeContext for CendeAmbassador {
fn write_prev_height_blob(&self) -> Arc<OnceLock<bool>> {
let cell = Arc::new(OnceLock::new());
let prev_height_blob = self.prev_height_blob.clone();
let cloned_cell = cell.clone();
task::spawn(async move {
let Some(ref _blob) = *prev_height_blob.lock().await else {
debug!("No blob to write to Aerospike.");
cell.set(false).expect("Cell should be empty");
return;
};
// TODO(dvir): write blob to AS.
// TODO(dvir): consider set `prev_height_blob` to `None` after writing to AS.
debug!("Writing blob to Aerospike.");
cell.set(true).expect("Cell should be empty");
debug!("Blob writing to Aerospike completed.");
});

cloned_cell
}

async fn prepare_blob_for_next_height(&self, nfb: NeededForBlob) {
// TODO(dvir, yael): make the full creation of blob.
// TODO(dvir): as optimization, call the `into` and other preperation when writing to AS.
*self.prev_height_blob.lock().await = Some(nfb.into());
}
}

#[derive(Clone, Debug, Default)]
pub(crate) struct NeededForBlob {
// TODO(dvir): add here all the information needed for creating the blob: tranasctions, classes,
// block info, BlockExecutionArtifacts.
}

impl From<NeededForBlob> for AerospikeBlob {
fn from(_nfb: NeededForBlob) -> Self {
// TODO(yael): make the full creation of blob.
AerospikeBlob {}
}
}
5 changes: 5 additions & 0 deletions crates/sequencing/papyrus_consensus_orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
pub mod papyrus_consensus_context;
#[allow(missing_docs)]
pub mod sequencer_consensus_context;

/// Centralized and decentralized communication types and functionallity.
// TODO(dvir): delete this when using the types in consensus.
#[allow(dead_code)]
mod cende;

0 comments on commit 8468f09

Please sign in to comment.