Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(starknet_batcher): add getters to the proposal manger #2480

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 30 additions & 10 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 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
Loading