Skip to content

Commit

Permalink
Add default implementations for new storage methods (#4006)
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-es authored Jan 7, 2025
1 parent 2f9b534 commit f62b9b6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
14 changes: 14 additions & 0 deletions crates/types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,20 @@ impl<TYPES: NodeType> Leaf2<TYPES> {

Ok(())
}

/// Converts a `Leaf2` to a `Leaf`. This operation is fundamentally unsafe and should not be used.
pub fn to_leaf_unsafe(self) -> Leaf<TYPES> {
let bytes: [u8; 32] = self.parent_commitment.into();

Leaf {
view_number: self.view_number,
justify_qc: self.justify_qc.to_qc(),
parent_commitment: Commitment::from_raw(bytes),
block_header: self.block_header,
upgrade_certificate: self.upgrade_certificate,
block_payload: self.block_payload,
}
}
}

impl<TYPES: NodeType> Committable for Leaf2<TYPES> {
Expand Down
55 changes: 43 additions & 12 deletions crates/types/src/traits/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::collections::BTreeMap;

use anyhow::Result;
use async_trait::async_trait;
use committable::Commitment;
use jf_vid::VidScheme;

use super::node_implementation::NodeType;
Expand All @@ -23,7 +24,7 @@ use crate::{
VidDisperseShare2,
},
event::HotShotAction,
message::Proposal,
message::{convert_proposal, Proposal},
simple_certificate::{
NextEpochQuorumCertificate2, QuorumCertificate, QuorumCertificate2, UpgradeCertificate,
},
Expand All @@ -36,8 +37,12 @@ pub trait Storage<TYPES: NodeType>: Send + Sync + Clone {
/// Add a proposal to the stored VID proposals.
async fn append_vid(&self, proposal: &Proposal<TYPES, VidDisperseShare<TYPES>>) -> Result<()>;
/// Add a proposal to the stored VID proposals.
async fn append_vid2(&self, proposal: &Proposal<TYPES, VidDisperseShare2<TYPES>>)
-> Result<()>;
async fn append_vid2(
&self,
proposal: &Proposal<TYPES, VidDisperseShare2<TYPES>>,
) -> Result<()> {
self.append_vid2(&convert_proposal(proposal.clone())).await
}
/// Add a proposal to the stored DA proposals.
async fn append_da(
&self,
Expand All @@ -49,7 +54,10 @@ pub trait Storage<TYPES: NodeType>: Send + Sync + Clone {
&self,
proposal: &Proposal<TYPES, DaProposal2<TYPES>>,
vid_commit: <VidSchemeType as VidScheme>::Commit,
) -> Result<()>;
) -> Result<()> {
self.append_da(&convert_proposal(proposal.clone()), vid_commit)
.await
}
/// Add a proposal we sent to the store
async fn append_proposal(
&self,
Expand All @@ -59,18 +67,25 @@ pub trait Storage<TYPES: NodeType>: Send + Sync + Clone {
async fn append_proposal2(
&self,
proposal: &Proposal<TYPES, QuorumProposal2<TYPES>>,
) -> Result<()>;
) -> Result<()> {
self.append_proposal(&convert_proposal(proposal.clone()))
.await
}
/// Record a HotShotAction taken.
async fn record_action(&self, view: TYPES::View, action: HotShotAction) -> Result<()>;
/// Update the current high QC in storage.
async fn update_high_qc(&self, high_qc: QuorumCertificate<TYPES>) -> Result<()>;
/// Update the current high QC in storage.
async fn update_high_qc2(&self, high_qc: QuorumCertificate2<TYPES>) -> Result<()>;
async fn update_high_qc2(&self, high_qc: QuorumCertificate2<TYPES>) -> Result<()> {
self.update_high_qc(high_qc.to_qc()).await
}
/// Update the current high QC in storage.
async fn update_next_epoch_high_qc2(
&self,
next_epoch_high_qc: NextEpochQuorumCertificate2<TYPES>,
) -> Result<()>;
_next_epoch_high_qc: NextEpochQuorumCertificate2<TYPES>,
) -> Result<()> {
Ok(())
}
/// Update the currently undecided state of consensus. This includes the undecided leaf chain,
/// and the undecided state.
async fn update_undecided_state(
Expand All @@ -84,7 +99,21 @@ pub trait Storage<TYPES: NodeType>: Send + Sync + Clone {
&self,
leaves: CommitmentMap<Leaf2<TYPES>>,
state: BTreeMap<TYPES::View, View<TYPES>>,
) -> Result<()>;
) -> Result<()> {
self.update_undecided_state(
leaves
.iter()
.map(|(&commitment, leaf)| {
(
Commitment::from_raw(commitment.into()),
leaf.clone().to_leaf_unsafe(),
)
})
.collect(),
state,
)
.await
}
/// Upgrade the current decided upgrade certificate in storage.
async fn update_decided_upgrade_certificate(
&self,
Expand All @@ -93,9 +122,11 @@ pub trait Storage<TYPES: NodeType>: Send + Sync + Clone {
/// Migrate leaves from `Leaf` to `Leaf2`, and proposals from `QuorumProposal` to `QuorumProposal2`
async fn migrate_consensus(
&self,
convert_leaf: fn(Leaf<TYPES>) -> Leaf2<TYPES>,
convert_proposal: fn(
_convert_leaf: fn(Leaf<TYPES>) -> Leaf2<TYPES>,
_convert_proposal: fn(
Proposal<TYPES, QuorumProposal<TYPES>>,
) -> Proposal<TYPES, QuorumProposal2<TYPES>>,
) -> Result<()>;
) -> Result<()> {
Ok(())
}
}

0 comments on commit f62b9b6

Please sign in to comment.