-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move LiteCertificate to certificate/lite.rs
- Loading branch information
Showing
18 changed files
with
138 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.