Skip to content

Commit

Permalink
Move LiteCertificate to certificate/lite.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
deuszx committed Nov 13, 2024
1 parent 4fb928f commit 38732e8
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 119 deletions.
97 changes: 97 additions & 0 deletions linera-chain/src/certificate/lite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::borrow::Cow;

use linera_base::{crypto::Signature, data_types::Round};
use linera_execution::committee::{Committee, ValidatorName};
use serde::{Deserialize, Serialize};

use super::{Certificate, HashedCertificateValue};
use crate::{
data_types::{check_signatures, LiteValue, LiteVote},
ChainError,
};

/// A certified statement from the committee, without the value.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(with_testing, derive(Eq, PartialEq))]
pub struct LiteCertificate<'a> {
/// Hash and chain ID of the certified value (used as key for storage).
pub value: LiteValue,
/// The round in which the value was certified.
pub round: Round,
/// Signatures on the value.
pub signatures: Cow<'a, [(ValidatorName, Signature)]>,
}

impl<'a> LiteCertificate<'a> {
pub fn new(
value: LiteValue,
round: Round,
mut signatures: Vec<(ValidatorName, Signature)>,
) -> Self {
signatures.sort_by_key(|&(validator_name, _)| validator_name);

let signatures = Cow::Owned(signatures);
Self {
value,
round,
signatures,
}
}

/// Creates a `LiteCertificate` from a list of votes, without cryptographically checking the
/// signatures. Returns `None` if the votes are empty or don't have matching values and rounds.
pub fn try_from_votes(votes: impl IntoIterator<Item = LiteVote>) -> Option<Self> {
let mut votes = votes.into_iter();
let LiteVote {
value,
round,
validator,
signature,
} = votes.next()?;
let mut signatures = vec![(validator, signature)];
for vote in votes {
if vote.value.value_hash != value.value_hash || vote.round != round {
return None;
}
signatures.push((vote.validator, vote.signature));
}
Some(LiteCertificate::new(value, round, signatures))
}

/// Verifies the certificate.
pub fn check(&self, committee: &Committee) -> Result<&LiteValue, ChainError> {
check_signatures(
self.value.value_hash,
self.round,
&self.signatures,
committee,
)?;
Ok(&self.value)
}

/// Returns the `Certificate` with the specified value, if it matches.
pub fn with_value(self, value: HashedCertificateValue) -> Option<Certificate> {
if self.value.chain_id != value.inner().chain_id() || self.value.value_hash != value.hash()
{
return None;
}
Some(Certificate::new(
value,
self.round,
self.signatures.into_owned(),
))
}

/// Returns a `LiteCertificate` that owns the list of signatures.
pub fn cloned(&self) -> LiteCertificate<'static> {
LiteCertificate {
value: self.value.clone(),
round: self.round,
signatures: Cow::Owned(self.signatures.clone().into_owned()),
}
}
}
4 changes: 3 additions & 1 deletion linera-chain/src/certificate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod confirmed;
mod generic;
mod hashed;
mod lite;
mod timeout;
mod validated;
mod value;
Expand All @@ -19,11 +20,12 @@ use linera_base::{
identifiers::{BlobId, ChainId},
};
use linera_execution::committee::{Epoch, ValidatorName};
pub use lite::LiteCertificate;
use serde::{Deserialize, Deserializer, Serialize};
pub use value::{CertificateValue, HashedCertificateValue};

use crate::{
data_types::{is_strictly_ordered, LiteCertificate, LiteValue, Medium, MessageBundle},
data_types::{is_strictly_ordered, LiteValue, Medium, MessageBundle},
types::{ConfirmedBlock, Timeout, ValidatedBlock},
};

Expand Down
6 changes: 3 additions & 3 deletions linera-chain/src/certificate/validated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use serde::{
};

use super::{
generic::GenericCertificate, hashed::Hashed, Certificate, CertificateValue,
HashedCertificateValue,
generic::GenericCertificate, hashed::Hashed, lite::LiteCertificate, Certificate,
CertificateValue, HashedCertificateValue,
};
use crate::{
block::ValidatedBlock,
data_types::{ExecutedBlock, LiteCertificate, LiteValue},
data_types::{ExecutedBlock, LiteValue},
};

impl GenericCertificate<ValidatedBlock> {
Expand Down
89 changes: 5 additions & 84 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::{borrow::Cow, collections::HashSet, fmt};
use std::{collections::HashSet, fmt};

use async_graphql::SimpleObject;
use linera_base::{
Expand All @@ -22,7 +22,10 @@ use linera_execution::{
use serde::{Deserialize, Serialize};

use crate::{
types::{Certificate, CertificateValue, HashedCertificateValue, ValidatedBlockCertificate},
types::{
Certificate, CertificateValue, HashedCertificateValue, LiteCertificate,
ValidatedBlockCertificate,
},
ChainError,
};

Expand Down Expand Up @@ -466,88 +469,6 @@ impl LiteVote {
}
}

/// A certified statement from the committee, without the value.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(with_testing, derive(Eq, PartialEq))]
pub struct LiteCertificate<'a> {
/// Hash and chain ID of the certified value (used as key for storage).
pub value: LiteValue,
/// The round in which the value was certified.
pub round: Round,
/// Signatures on the value.
pub signatures: Cow<'a, [(ValidatorName, Signature)]>,
}

impl<'a> LiteCertificate<'a> {
pub fn new(
value: LiteValue,
round: Round,
mut signatures: Vec<(ValidatorName, Signature)>,
) -> Self {
signatures.sort_by_key(|&(validator_name, _)| validator_name);

let signatures = Cow::Owned(signatures);
Self {
value,
round,
signatures,
}
}

/// Creates a `LiteCertificate` from a list of votes, without cryptographically checking the
/// signatures. Returns `None` if the votes are empty or don't have matching values and rounds.
pub fn try_from_votes(votes: impl IntoIterator<Item = LiteVote>) -> Option<Self> {
let mut votes = votes.into_iter();
let LiteVote {
value,
round,
validator,
signature,
} = votes.next()?;
let mut signatures = vec![(validator, signature)];
for vote in votes {
if vote.value.value_hash != value.value_hash || vote.round != round {
return None;
}
signatures.push((vote.validator, vote.signature));
}
Some(LiteCertificate::new(value, round, signatures))
}

/// Verifies the certificate.
pub fn check(&self, committee: &Committee) -> Result<&LiteValue, ChainError> {
check_signatures(
self.value.value_hash,
self.round,
&self.signatures,
committee,
)?;
Ok(&self.value)
}

/// Returns the `Certificate` with the specified value, if it matches.
pub fn with_value(self, value: HashedCertificateValue) -> Option<Certificate> {
if self.value.chain_id != value.inner().chain_id() || self.value.value_hash != value.hash()
{
return None;
}
Some(Certificate::new(
value,
self.round,
self.signatures.into_owned(),
))
}

/// Returns a `LiteCertificate` that owns the list of signatures.
pub fn cloned(&self) -> LiteCertificate<'static> {
LiteCertificate {
value: self.value.clone(),
round: self.round,
signatures: Cow::Owned(self.signatures.clone().into_owned()),
}
}
}

impl fmt::Display for Origin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.medium {
Expand Down
7 changes: 4 additions & 3 deletions linera-core/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ use linera_base::{
};
use linera_chain::{
data_types::{
Block, BlockProposal, ChainAndHeight, ExecutedBlock, IncomingBundle, LiteCertificate,
LiteVote, MessageAction,
Block, BlockProposal, ChainAndHeight, ExecutedBlock, IncomingBundle, LiteVote,
MessageAction,
},
manager::ChainManagerInfo,
types::{
Certificate, ConfirmedBlockCertificate, HashedCertificateValue, ValidatedBlockCertificate,
Certificate, ConfirmedBlockCertificate, HashedCertificateValue, LiteCertificate,
ValidatedBlockCertificate,
},
ChainError, ChainExecutionContext, ChainStateView,
};
Expand Down
4 changes: 2 additions & 2 deletions linera-core/src/local_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use linera_base::{
identifiers::{BlobId, ChainId, MessageId, UserApplicationId},
};
use linera_chain::{
data_types::{Block, BlockProposal, ExecutedBlock, LiteCertificate},
types::{Certificate, CertificateValue, ConfirmedBlockCertificate},
data_types::{Block, BlockProposal, ExecutedBlock},
types::{Certificate, CertificateValue, ConfirmedBlockCertificate, LiteCertificate},
ChainError, ChainStateView,
};
use linera_execution::{ExecutionError, Query, Response, SystemExecutionError};
Expand Down
4 changes: 2 additions & 2 deletions linera-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use linera_base::{
identifiers::{BlobId, ChainId},
};
use linera_chain::{
data_types::{BlockProposal, LiteCertificate, Origin},
types::{Certificate, HashedCertificateValue},
data_types::{BlockProposal, Origin},
types::{Certificate, HashedCertificateValue, LiteCertificate},
ChainError,
};
use linera_execution::{
Expand Down
4 changes: 2 additions & 2 deletions linera-core/src/remote_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use linera_base::{
identifiers::{BlobId, ChainId},
};
use linera_chain::{
data_types::{BlockProposal, LiteCertificate},
types::{Certificate, ConfirmedBlockCertificate},
data_types::BlockProposal,
types::{Certificate, ConfirmedBlockCertificate, LiteCertificate},
};
use linera_execution::committee::ValidatorName;
use rand::seq::SliceRandom as _;
Expand Down
4 changes: 2 additions & 2 deletions linera-core/src/unit_tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use linera_base::{
identifiers::{BlobId, ChainDescription, ChainId, UserApplicationId},
};
use linera_chain::{
data_types::{BlockProposal, LiteCertificate},
types::{Certificate, ConfirmedBlockCertificate, HashedCertificateValue},
data_types::BlockProposal,
types::{Certificate, ConfirmedBlockCertificate, HashedCertificateValue, LiteCertificate},
};
use linera_execution::{
committee::{Committee, ValidatorName},
Expand Down
5 changes: 1 addition & 4 deletions linera-core/src/value_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use std::{any::type_name, sync::LazyLock};
use std::{borrow::Cow, hash::Hash, num::NonZeroUsize};

use linera_base::{crypto::CryptoHash, data_types::Blob, identifiers::BlobId};
use linera_chain::{
data_types::LiteCertificate,
types::{Certificate, HashedCertificateValue},
};
use linera_chain::types::{Certificate, HashedCertificateValue, LiteCertificate};
use lru::LruCache;
use tokio::sync::Mutex;
#[cfg(with_metrics)]
Expand Down
5 changes: 2 additions & 3 deletions linera-core/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ use linera_base::{
};
use linera_chain::{
data_types::{
Block, BlockExecutionOutcome, BlockProposal, ExecutedBlock, LiteCertificate, MessageBundle,
Origin, Target,
Block, BlockExecutionOutcome, BlockProposal, ExecutedBlock, MessageBundle, Origin, Target,
},
types::{
Certificate, CertificateValue, ConfirmedBlockCertificate, HashedCertificateValue,
TimeoutCertificate, ValidatedBlockCertificate,
LiteCertificate, TimeoutCertificate, ValidatedBlockCertificate,
},
ChainStateView,
};
Expand Down
4 changes: 2 additions & 2 deletions linera-rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use linera_base::{
identifiers::{BlobId, ChainId},
};
use linera_chain::{
data_types::{BlockProposal, LiteCertificate},
types::{Certificate, HashedCertificateValue},
data_types::BlockProposal,
types::{Certificate, HashedCertificateValue, LiteCertificate},
};
use linera_core::{
data_types::{ChainInfoQuery, ChainInfoResponse},
Expand Down
4 changes: 2 additions & 2 deletions linera-rpc/src/grpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use linera_base::{
};
use linera_chain::{
data_types::{self},
types::{Certificate, CertificateValue, HashedCertificateValue},
types::{self, Certificate, CertificateValue, HashedCertificateValue},
};
use linera_core::{
node::{CrossChainMessageDelivery, NodeError, NotificationStream, ValidatorNode},
Expand Down Expand Up @@ -187,7 +187,7 @@ impl ValidatorNode for GrpcClient {
#[instrument(target = "grpc_client", skip_all, fields(address = self.address))]
async fn handle_lite_certificate(
&self,
certificate: data_types::LiteCertificate<'_>,
certificate: types::LiteCertificate<'_>,
delivery: CrossChainMessageDelivery,
) -> Result<linera_core::data_types::ChainInfoResponse, NodeError> {
let wait_for_outgoing_messages = delivery.wait_for_outgoing_messages();
Expand Down
4 changes: 2 additions & 2 deletions linera-rpc/src/grpc/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use linera_base::{
identifiers::{BlobId, ChainId, Owner},
};
use linera_chain::{
data_types::{BlockProposal, LiteCertificate, LiteValue, ProposalContent},
types::{Certificate, CertificateValue, HashedCertificateValue},
data_types::{BlockProposal, LiteValue, ProposalContent},
types::{Certificate, CertificateValue, HashedCertificateValue, LiteCertificate},
};
use linera_core::{
data_types::{ChainInfoQuery, ChainInfoResponse, CrossChainRequest},
Expand Down
2 changes: 1 addition & 1 deletion linera-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use node_provider::NodeOptions;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[cfg_attr(with_testing, derive(Eq, PartialEq))]
pub struct HandleLiteCertRequest<'a> {
pub certificate: linera_chain::data_types::LiteCertificate<'a>,
pub certificate: linera_chain::types::LiteCertificate<'a>,
pub wait_for_outgoing_messages: bool,
}

Expand Down
4 changes: 2 additions & 2 deletions linera-rpc/src/simple/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use linera_base::{
time::{timer, Duration},
};
use linera_chain::{
data_types::{BlockProposal, LiteCertificate},
types::{Certificate, CertificateValue, HashedCertificateValue},
data_types::BlockProposal,
types::{Certificate, CertificateValue, HashedCertificateValue, LiteCertificate},
};
use linera_core::{
data_types::{ChainInfoQuery, ChainInfoResponse},
Expand Down
Loading

0 comments on commit 38732e8

Please sign in to comment.