Skip to content

Commit

Permalink
refactor(starknet_batcher): add getters to the proposal manger
Browse files Browse the repository at this point in the history
  • Loading branch information
dafnamatsry committed Dec 5, 2024
1 parent fa2439b commit f37c942
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
22 changes: 22 additions & 0 deletions crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ trait ProposalManagerTraitWrapper: Send + Sync {
proposal_id: ProposalId,
) -> BoxFuture<'_, ProposalResult<ProposalOutput>>;

fn wrap_get_active_proposal(&self) -> BoxFuture<'_, Option<ProposalId>>;

fn wrap_get_completed_proposals(
&self,
) -> BoxFuture<'_, Arc<tokio::sync::Mutex<HashMap<ProposalId, ProposalResult<ProposalOutput>>>>>;

fn wrap_await_active_proposal(&mut self) -> BoxFuture<'_, bool>;

fn wrap_get_proposal_status(
&self,
proposal_id: ProposalId,
Expand Down Expand Up @@ -613,6 +621,20 @@ impl<T: ProposalManagerTraitWrapper> ProposalManagerTrait for T {
self.wrap_take_proposal_result(proposal_id).await
}

async fn get_active_proposal(&self) -> Option<ProposalId> {
self.wrap_get_active_proposal().await
}

async fn get_completed_proposals(
&self,
) -> Arc<tokio::sync::Mutex<HashMap<ProposalId, ProposalResult<ProposalOutput>>>> {
self.wrap_get_completed_proposals().await
}

async fn await_active_proposal(&mut self) -> bool {
self.wrap_await_active_proposal().await
}

async fn get_proposal_status(&self, proposal_id: ProposalId) -> InternalProposalStatus {
self.wrap_get_proposal_status(proposal_id).await
}
Expand Down
46 changes: 33 additions & 13 deletions crates/starknet_batcher/src/proposal_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ pub trait ProposalManagerTrait: Send + Sync {
proposal_id: ProposalId,
) -> ProposalResult<ProposalOutput>;

#[allow(dead_code)]
async fn get_active_proposal(&self) -> Option<ProposalId>;

#[allow(dead_code)]
async fn get_completed_proposals(
&self,
) -> Arc<Mutex<HashMap<ProposalId, ProposalResult<ProposalOutput>>>>;

async fn await_active_proposal(&mut self) -> bool;

async fn get_proposal_status(&self, proposal_id: ProposalId) -> InternalProposalStatus;

async fn await_proposal_commitment(
Expand All @@ -77,9 +87,9 @@ pub trait ProposalManagerTrait: Send + Sync {
}

// Represents a spawned task of building new block proposal.
struct ProposalTask {
abort_signal_sender: tokio::sync::oneshot::Sender<()>,
join_handle: tokio::task::JoinHandle<()>,
pub struct ProposalTask {
pub abort_signal_sender: tokio::sync::oneshot::Sender<()>,
pub join_handle: tokio::task::JoinHandle<()>,
}

/// Main struct for handling block proposals.
Expand Down Expand Up @@ -162,6 +172,26 @@ impl ProposalManagerTrait for ProposalManager {
.ok_or(GetProposalResultError::ProposalDoesNotExist { proposal_id })?
}

async fn get_active_proposal(&self) -> Option<ProposalId> {
*self.active_proposal.lock().await
}

async fn get_completed_proposals(
&self,
) -> Arc<Mutex<HashMap<ProposalId, ProposalResult<ProposalOutput>>>> {
self.executed_proposals.clone()
}

// Awaits the active proposal.
// Returns true if there was an active proposal, and false otherwise.
async fn await_active_proposal(&mut self) -> bool {
if let Some(proposal_task) = self.active_proposal_task.take() {
proposal_task.join_handle.await.ok();
return true;
}
false
}

// Returns None if the proposal does not exist, otherwise, returns the status of the proposal.
async fn get_proposal_status(&self, proposal_id: ProposalId) -> InternalProposalStatus {
match self.executed_proposals.lock().await.get(&proposal_id) {
Expand Down Expand Up @@ -245,16 +275,6 @@ impl ProposalManager {
Ok(())
}

// Awaits the active proposal.
// Returns true if there was an active proposal, and false otherwise.
pub async fn await_active_proposal(&mut self) -> bool {
if let Some(proposal_task) = self.active_proposal_task.take() {
proposal_task.join_handle.await.ok();
return true;
}
false
}

// Ends the current active proposal.
// This call is non-blocking.
async fn abort_active_proposal(&mut self) {
Expand Down

0 comments on commit f37c942

Please sign in to comment.