From 8468f09e17390b7b62eedb374520db1ebc3a2a92 Mon Sep 17 00:00:00 2001 From: Dvir Yosef Date: Tue, 17 Dec 2024 12:19:02 +0200 Subject: [PATCH] feat(sequencing): add cende module and main types --- .../src/cende/mod.rs | 80 +++++++++++++++++++ .../papyrus_consensus_orchestrator/src/lib.rs | 5 ++ 2 files changed, 85 insertions(+) create mode 100644 crates/sequencing/papyrus_consensus_orchestrator/src/cende/mod.rs diff --git a/crates/sequencing/papyrus_consensus_orchestrator/src/cende/mod.rs b/crates/sequencing/papyrus_consensus_orchestrator/src/cende/mod.rs new file mode 100644 index 0000000000..ded1b69142 --- /dev/null +++ b/crates/sequencing/papyrus_consensus_orchestrator/src/cende/mod.rs @@ -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>; + + // 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`. + // `None` indicates that there is no blob to write, and therefore, the node can't be the + // proposer. + prev_height_blob: Arc>>, +} + +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> { + 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 for AerospikeBlob { + fn from(_nfb: NeededForBlob) -> Self { + // TODO(yael): make the full creation of blob. + AerospikeBlob {} + } +} diff --git a/crates/sequencing/papyrus_consensus_orchestrator/src/lib.rs b/crates/sequencing/papyrus_consensus_orchestrator/src/lib.rs index ff7de38c70..72e91009d4 100644 --- a/crates/sequencing/papyrus_consensus_orchestrator/src/lib.rs +++ b/crates/sequencing/papyrus_consensus_orchestrator/src/lib.rs @@ -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;