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

feat(code/app): Add persistence to the example app #746

Merged
merged 3 commits into from
Jan 9, 2025
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
4 changes: 4 additions & 0 deletions code/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions code/crates/app-channel/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use malachitebft_engine::network::Msg as NetworkActorMsg;

use crate::app::types::core::{CommitCertificate, Context, Round, ValueId};
use crate::app::types::streaming::StreamMessage;
use crate::app::types::sync::DecidedValue;
use crate::app::types::sync::RawDecidedValue;
use crate::app::types::{LocallyProposedValue, PeerId, ProposedValue};

pub type Reply<T> = oneshot::Sender<T>;
Expand Down Expand Up @@ -126,7 +126,7 @@ pub enum AppMsg<Ctx: Context> {
/// Height of the decided value to retrieve
height: Ctx::Height,
/// Channel for sending back the decided value
reply: Reply<Option<DecidedValue<Ctx>>>,
reply: Reply<Option<RawDecidedValue<Ctx>>>,
},

/// Notifies the application that a value has been synced from the network.
Expand Down
2 changes: 1 addition & 1 deletion code/crates/app/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod streaming {
}

pub mod sync {
pub use malachitebft_sync::{DecidedValue, Metrics, Request, Response, Status};
pub use malachitebft_sync::{Metrics, RawDecidedValue, Request, Response, Status};
}

pub mod codec {
Expand Down
6 changes: 6 additions & 0 deletions code/crates/core-types/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ impl Validity {
self == Validity::Valid
}

/// Converts the validity to a boolean:
/// `true` if the proposal is valid, `false` otherwise.
pub fn to_bool(self) -> bool {
self.is_valid()
}

/// Returns `Valid` if given true, `Invalid` if given false.
pub fn from_bool(valid: bool) -> Self {
if valid {
Expand Down
4 changes: 2 additions & 2 deletions code/crates/engine/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ractor::{ActorRef, RpcReplyPort};

use malachitebft_core_consensus::PeerId;
use malachitebft_core_types::{CommitCertificate, Context, Round, SignedExtension, ValueId};
use malachitebft_sync::DecidedValue;
use malachitebft_sync::RawDecidedValue;

use crate::consensus::ConsensusRef;
use crate::util::streaming::StreamMessage;
Expand Down Expand Up @@ -100,7 +100,7 @@ pub enum HostMsg<Ctx: Context> {
// Retrieve decided value from the block store
GetDecidedValue {
height: Ctx::Height,
reply_to: RpcReplyPort<Option<DecidedValue<Ctx>>>,
reply_to: RpcReplyPort<Option<RawDecidedValue<Ctx>>>,
},

// Process a value synced from another node via the ValueSync protocol.
Expand Down
4 changes: 2 additions & 2 deletions code/crates/engine/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use malachitebft_codec as codec;
use malachitebft_core_consensus::PeerId;
use malachitebft_core_types::{CertificateError, CommitCertificate, Context, Height, Round};
use malachitebft_sync::{self as sync, InboundRequestId, OutboundRequestId, Response};
use malachitebft_sync::{DecidedValue, Request};
use malachitebft_sync::{RawDecidedValue, Request};

use crate::host::{HostMsg, HostRef};
use crate::network::{NetworkEvent, NetworkMsg, NetworkRef, Status};
Expand Down Expand Up @@ -86,7 +86,7 @@ pub enum Msg<Ctx: Context> {
StartedHeight(Ctx::Height),

/// Host has a response for the blocks request
GotDecidedBlock(InboundRequestId, Ctx::Height, Option<DecidedValue<Ctx>>),
GotDecidedBlock(InboundRequestId, Ctx::Height, Option<RawDecidedValue<Ctx>>),

/// A timeout has elapsed
TimeoutElapsed(TimeoutElapsed<Timeout>),
Expand Down
6 changes: 3 additions & 3 deletions code/crates/starknet/host/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use malachitebft_engine::host::{LocallyProposedValue, ProposedValue};
use malachitebft_engine::network::{NetworkMsg, NetworkRef};
use malachitebft_engine::util::streaming::{StreamContent, StreamMessage};
use malachitebft_metrics::Metrics;
use malachitebft_sync::DecidedValue;
use malachitebft_sync::RawDecidedValue;

use crate::host::proposal::compute_proposal_signature;
use crate::host::state::HostState;
Expand Down Expand Up @@ -454,7 +454,7 @@ fn on_process_synced_value(
async fn on_get_decided_block(
height: Height,
state: &mut HostState,
reply_to: RpcReplyPort<Option<DecidedValue<MockContext>>>,
reply_to: RpcReplyPort<Option<RawDecidedValue<MockContext>>>,
) -> Result<(), ActorProcessingErr> {
debug!(%height, "Received request for block");

Expand All @@ -469,7 +469,7 @@ async fn on_get_decided_block(
}

Ok(Some(block)) => {
let block = DecidedValue {
let block = RawDecidedValue {
value_bytes: block.block.to_bytes().unwrap(),
certificate: block.certificate,
};
Expand Down
12 changes: 6 additions & 6 deletions code/crates/starknet/host/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ impl Codec<CommitCertificate<MockContext>> for ProtobufCodec {
}

pub fn encode_synced_value(
synced_value: &sync::DecidedValue<MockContext>,
synced_value: &sync::RawDecidedValue<MockContext>,
) -> Result<proto::sync::SyncedValue, ProtoError> {
Ok(proto::sync::SyncedValue {
value_bytes: synced_value.value_bytes.clone(),
Expand All @@ -574,28 +574,28 @@ pub fn encode_synced_value(

pub fn decode_synced_value(
proto: proto::sync::SyncedValue,
) -> Result<sync::DecidedValue<MockContext>, ProtoError> {
) -> Result<sync::RawDecidedValue<MockContext>, ProtoError> {
let Some(certificate) = proto.certificate else {
return Err(ProtoError::missing_field::<proto::sync::SyncedValue>(
"certificate",
));
};

Ok(sync::DecidedValue {
Ok(sync::RawDecidedValue {
value_bytes: proto.value_bytes,
certificate: decode_certificate(certificate)?,
})
}

impl Codec<sync::DecidedValue<MockContext>> for ProtobufCodec {
impl Codec<sync::RawDecidedValue<MockContext>> for ProtobufCodec {
type Error = ProtoError;

fn decode(&self, bytes: Bytes) -> Result<sync::DecidedValue<MockContext>, Self::Error> {
fn decode(&self, bytes: Bytes) -> Result<sync::RawDecidedValue<MockContext>, Self::Error> {
let proto = proto::sync::SyncedValue::decode(bytes).map_err(ProtoError::Decode)?;
decode_synced_value(proto)
}

fn encode(&self, msg: &sync::DecidedValue<MockContext>) -> Result<Bytes, Self::Error> {
fn encode(&self, msg: &sync::RawDecidedValue<MockContext>) -> Result<Bytes, Self::Error> {
Ok(Bytes::from(encode_synced_value(msg)?.encode_to_vec()))
}
}
Expand Down
6 changes: 3 additions & 3 deletions code/crates/sync/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use malachitebft_core_types::{CertificateError, CommitCertificate, Context, Heig

use crate::co::Co;
use crate::{
perform, DecidedValue, InboundRequestId, Metrics, OutboundRequestId, PeerId, Request, State,
perform, InboundRequestId, Metrics, OutboundRequestId, PeerId, RawDecidedValue, Request, State,
Status, ValueRequest, ValueResponse, VoteSetRequest, VoteSetResponse,
};

Expand Down Expand Up @@ -71,7 +71,7 @@ pub enum Input<Ctx: Context> {
ValueResponse(OutboundRequestId, PeerId, ValueResponse<Ctx>),

/// Got a response from the application to our `GetValue` request
GotDecidedValue(InboundRequestId, Ctx::Height, Option<DecidedValue<Ctx>>),
GotDecidedValue(InboundRequestId, Ctx::Height, Option<RawDecidedValue<Ctx>>),

/// A request for a value or vote set timed out
SyncRequestTimedOut(PeerId, Request<Ctx>),
Expand Down Expand Up @@ -281,7 +281,7 @@ pub async fn on_value<Ctx>(
metrics: &Metrics,
request_id: InboundRequestId,
height: Ctx::Height,
value: Option<DecidedValue<Ctx>>,
value: Option<RawDecidedValue<Ctx>>,
) -> Result<(), Error<Ctx>>
where
Ctx: Context,
Expand Down
8 changes: 4 additions & 4 deletions code/crates/sync/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ impl<Ctx: Context> ValueRequest<Ctx> {
#[derive_where(Clone, Debug, PartialEq, Eq)]
pub struct ValueResponse<Ctx: Context> {
pub height: Ctx::Height,
pub value: Option<DecidedValue<Ctx>>,
pub value: Option<RawDecidedValue<Ctx>>,
}

impl<Ctx: Context> ValueResponse<Ctx> {
pub fn new(height: Ctx::Height, value: Option<DecidedValue<Ctx>>) -> Self {
pub fn new(height: Ctx::Height, value: Option<RawDecidedValue<Ctx>>) -> Self {
Self { height, value }
}
}

#[derive_where(Clone, Debug, PartialEq, Eq)]
pub struct DecidedValue<Ctx: Context> {
pub struct RawDecidedValue<Ctx: Context> {
pub value_bytes: Bytes,
pub certificate: CommitCertificate<Ctx>,
}

impl<Ctx: Context> DecidedValue<Ctx> {
impl<Ctx: Context> RawDecidedValue<Ctx> {
pub fn new(value_bytes: Bytes, certificate: CommitCertificate<Ctx>) -> Self {
Self {
value_bytes,
Expand Down
Loading
Loading