|
2 | 2 | // Copyright (c) Zefchain Labs, Inc. |
3 | 3 | // SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -use linera_base::identifiers::{BlobId, ChainId, MessageId}; |
6 | | -use linera_execution::committee::Epoch; |
7 | | - |
8 | | -use super::{ |
9 | | - generic::GenericCertificate, hashed::Hashed, Certificate, CertificateValue, |
10 | | - HashedCertificateValue, |
| 5 | +use linera_base::{ |
| 6 | + crypto::Signature, |
| 7 | + data_types::Round, |
| 8 | + identifiers::{BlobId, ChainId, MessageId}, |
11 | 9 | }; |
| 10 | +use linera_execution::committee::{Epoch, ValidatorName}; |
| 11 | +use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize}; |
| 12 | + |
| 13 | +use super::{generic::GenericCertificate, hashed::Hashed, Certificate}; |
12 | 14 | use crate::{ |
13 | | - block::{ConfirmedBlock, ConversionError, ValidatedBlock}, |
| 15 | + block::{ConfirmedBlock, ConversionError}, |
14 | 16 | data_types::{ExecutedBlock, Medium, MessageBundle}, |
15 | 17 | }; |
16 | 18 |
|
17 | 19 | impl GenericCertificate<ConfirmedBlock> { |
18 | | - /// Creates a new `ConfirmedBlockCertificate` from a `ValidatedBlockCertificate`. |
19 | | - pub fn from_validated(validated: GenericCertificate<ValidatedBlock>) -> Self { |
20 | | - let round = validated.round; |
21 | | - let validated_block = validated.into_inner(); |
22 | | - // To keep the signature checks passing, we need to obtain a hash over the old type. |
23 | | - let old_confirmed = HashedCertificateValue::new_confirmed(validated_block.inner().clone()); |
24 | | - let confirmed = ConfirmedBlock::from_validated(validated_block); |
25 | | - let hashed = Hashed::unchecked_new(confirmed, old_confirmed.hash()); |
26 | | - |
27 | | - Self::new(hashed, round, vec![]) |
28 | | - } |
29 | | - |
30 | 20 | /// Returns reference to the `ExecutedBlock` contained in this certificate. |
31 | 21 | pub fn executed_block(&self) -> &ExecutedBlock { |
32 | 22 | self.inner().executed_block() |
@@ -54,31 +44,61 @@ impl GenericCertificate<ConfirmedBlock> { |
54 | 44 | pub fn requires_blob(&self, blob_id: &BlobId) -> bool { |
55 | 45 | self.executed_block().requires_blob(blob_id) |
56 | 46 | } |
| 47 | + |
| 48 | + #[cfg(with_testing)] |
| 49 | + pub fn outgoing_message_count(&self) -> usize { |
| 50 | + self.executed_block().messages().iter().map(Vec::len).sum() |
| 51 | + } |
57 | 52 | } |
58 | 53 |
|
59 | 54 | impl TryFrom<Certificate> for GenericCertificate<ConfirmedBlock> { |
60 | 55 | type Error = ConversionError; |
61 | 56 |
|
62 | 57 | fn try_from(cert: Certificate) -> Result<Self, Self::Error> { |
63 | | - let hash = cert.hash(); |
64 | | - let (value, round, signatures) = cert.destructure(); |
65 | | - match value.into_inner() { |
66 | | - CertificateValue::ConfirmedBlock(confirmed) => Ok(Self::new( |
67 | | - Hashed::unchecked_new(confirmed, hash), |
68 | | - round, |
69 | | - signatures, |
70 | | - )), |
| 58 | + match cert { |
| 59 | + Certificate::Confirmed(confirmed) => Ok(confirmed), |
71 | 60 | _ => Err(ConversionError::ConfirmedBlock), |
72 | 61 | } |
73 | 62 | } |
74 | 63 | } |
75 | 64 |
|
76 | 65 | impl From<GenericCertificate<ConfirmedBlock>> for Certificate { |
77 | 66 | fn from(cert: GenericCertificate<ConfirmedBlock>) -> Certificate { |
78 | | - let (value, round, signatures) = cert.destructure(); |
79 | | - let hash = value.hash(); |
80 | | - let value = |
81 | | - Hashed::unchecked_new(CertificateValue::ConfirmedBlock(value.into_inner()), hash); |
82 | | - Certificate::new(value, round, signatures) |
| 67 | + Certificate::Confirmed(cert) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl Serialize for GenericCertificate<ConfirmedBlock> { |
| 72 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 73 | + where |
| 74 | + S: serde::Serializer, |
| 75 | + { |
| 76 | + let mut state = serializer.serialize_struct("ConfirmedBlockCertificate", 3)?; |
| 77 | + state.serialize_field("value", self.inner())?; |
| 78 | + state.serialize_field("round", &self.round)?; |
| 79 | + state.serialize_field("signatures", self.signatures())?; |
| 80 | + state.end() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<'de> Deserialize<'de> for GenericCertificate<ConfirmedBlock> { |
| 85 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 86 | + where |
| 87 | + D: Deserializer<'de>, |
| 88 | + { |
| 89 | + #[derive(Debug, Deserialize)] |
| 90 | + #[serde(rename = "ConfirmedBlockCertificate")] |
| 91 | + struct Helper { |
| 92 | + value: Hashed<ConfirmedBlock>, |
| 93 | + round: Round, |
| 94 | + signatures: Vec<(ValidatorName, Signature)>, |
| 95 | + } |
| 96 | + |
| 97 | + let helper = Helper::deserialize(deserializer)?; |
| 98 | + if !crate::data_types::is_strictly_ordered(&helper.signatures) { |
| 99 | + Err(serde::de::Error::custom("Vector is not strictly sorted")) |
| 100 | + } else { |
| 101 | + Ok(Self::new(helper.value, helper.round, helper.signatures)) |
| 102 | + } |
83 | 103 | } |
84 | 104 | } |
0 commit comments