这是indexloc提供的服务,不要输入任何密码
Skip to content
Draft
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
63 changes: 40 additions & 23 deletions consensus/consensus-types/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
block_data::{BlockData, BlockType},
common::{Author, Payload, Round},
opt_block_data::OptBlockData,
payload::{OptQuorumStorePayload, TDataInfo},
quorum_cert::QuorumCert,
};
use anyhow::{bail, ensure, format_err, Result};
Expand Down Expand Up @@ -143,11 +144,19 @@ impl Block {
proof_with_data.num_txns(),
proof_with_data.num_bytes(),
),
Payload::OptQuorumStore(opt_quorum_store_payload) => (
opt_quorum_store_payload.proof_with_data().num_proofs(),
opt_quorum_store_payload.proof_with_data().num_txns(),
opt_quorum_store_payload.proof_with_data().num_bytes(),
),
Payload::OptQuorumStore(opt_quorum_store_payload) => match opt_quorum_store_payload
{
OptQuorumStorePayload::V1(p) => (
p.proof_with_data().num_proofs(),
p.proof_with_data().num_txns(),
p.proof_with_data().num_bytes(),
),
OptQuorumStorePayload::V2(p) => (
p.proof_with_data().num_proofs(),
p.proof_with_data().num_txns(),
p.proof_with_data().num_bytes(),
),
},
},
}
}
Expand All @@ -169,11 +178,19 @@ impl Block {
.map(|(b, _)| b.num_bytes() as usize)
.sum(),
),
Payload::OptQuorumStore(opt_quorum_store_payload) => (
opt_quorum_store_payload.inline_batches().num_batches(),
opt_quorum_store_payload.inline_batches().num_txns(),
opt_quorum_store_payload.inline_batches().num_bytes(),
),
Payload::OptQuorumStore(opt_quorum_store_payload) => match opt_quorum_store_payload
{
OptQuorumStorePayload::V1(p) => (
p.inline_batches().num_batches(),
p.inline_batches().num_txns(),
p.inline_batches().num_bytes(),
),
OptQuorumStorePayload::V2(p) => (
p.inline_batches().num_batches(),
p.inline_batches().num_txns(),
p.inline_batches().num_bytes(),
),
},
_ => (0, 0, 0),
},
}
Expand All @@ -184,19 +201,19 @@ impl Block {
match self.block_data.payload() {
None => (0, 0, 0),
Some(payload) => match payload {
Payload::OptQuorumStore(opt_quorum_store_payload) => (
opt_quorum_store_payload.opt_batches().len(),
opt_quorum_store_payload
.opt_batches()
.iter()
.map(|b| b.num_txns() as usize)
.sum(),
opt_quorum_store_payload
.opt_batches()
.iter()
.map(|b| b.num_bytes() as usize)
.sum(),
),
Payload::OptQuorumStore(opt_quorum_store_payload) => match opt_quorum_store_payload
{
OptQuorumStorePayload::V1(p) => (
p.opt_batches().len(),
p.opt_batches().iter().map(|b| b.num_txns() as usize).sum(),
p.opt_batches().iter().map(|b| b.num_bytes() as usize).sum(),
),
OptQuorumStorePayload::V2(p) => (
p.opt_batches().len(),
p.opt_batches().iter().map(|b| b.num_txns() as usize).sum(),
p.opt_batches().iter().map(|b| b.num_bytes() as usize).sum(),
),
},
_ => (0, 0, 0),
},
}
Expand Down
59 changes: 42 additions & 17 deletions consensus/consensus-types/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,15 @@ impl Payload {
}
}

fn verify_with_cache(
proofs: &[ProofOfStore<BatchInfo>],
fn verify_with_cache<T>(
proofs: &[ProofOfStore<T>],
validator: &ValidatorVerifier,
proof_cache: &ProofCache,
) -> anyhow::Result<()> {
) -> anyhow::Result<()>
where
T: TBatchInfo + Send + Sync + 'static,
BatchInfoExt: From<T>,
{
let unverified: Vec<_> = proofs
.iter()
.filter(|proof| {
Expand All @@ -535,15 +539,15 @@ impl Payload {
Ok(())
}

pub fn verify_inline_batches<'a>(
inline_batches: impl Iterator<Item = (&'a BatchInfo, &'a Vec<SignedTransaction>)>,
pub fn verify_inline_batches<'a, T: TBatchInfo + 'a>(
inline_batches: impl Iterator<Item = (&'a T, &'a Vec<SignedTransaction>)>,
) -> anyhow::Result<()> {
for (batch, payload) in inline_batches {
// TODO: Can cloning be avoided here?
let computed_digest = BatchPayload::new(batch.author(), payload.clone()).hash();
ensure!(
computed_digest == *batch.digest(),
"Hash of the received inline batch doesn't match the digest value for batch {}: {} != {}",
"Hash of the received inline batch doesn't match the digest value for batch {:?}: {} != {}",
batch,
computed_digest,
batch.digest()
Expand All @@ -552,9 +556,9 @@ impl Payload {
Ok(())
}

pub fn verify_opt_batches(
pub fn verify_opt_batches<T: TBatchInfo>(
verifier: &ValidatorVerifier,
opt_batches: &OptBatches,
opt_batches: &OptBatches<T>,
) -> anyhow::Result<()> {
let authors = verifier.address_to_validator_index();
for batch in &opt_batches.batch_summary {
Expand Down Expand Up @@ -592,16 +596,26 @@ impl Payload {
)?;
Ok(())
},
(true, Payload::OptQuorumStore(opt_quorum_store)) => {
let proof_with_data = opt_quorum_store.proof_with_data();
(true, Payload::OptQuorumStore(OptQuorumStorePayload::V1(p))) => {
let proof_with_data = p.proof_with_data();
Self::verify_with_cache(&proof_with_data.batch_summary, verifier, proof_cache)?;
Self::verify_inline_batches(
p.inline_batches()
.iter()
.map(|batch| (batch.info(), batch.transactions())),
)?;
Self::verify_opt_batches(verifier, p.opt_batches())?;
Ok(())
},
(true, Payload::OptQuorumStore(OptQuorumStorePayload::V2(p))) => {
let proof_with_data = p.proof_with_data();
Self::verify_with_cache(&proof_with_data.batch_summary, verifier, proof_cache)?;
Self::verify_inline_batches(
opt_quorum_store
.inline_batches()
p.inline_batches()
.iter()
.map(|batch| (batch.info(), batch.transactions())),
)?;
Self::verify_opt_batches(verifier, opt_quorum_store.opt_batches())?;
Self::verify_opt_batches(verifier, p.opt_batches())?;
Ok(())
},
(_, _) => Err(anyhow::anyhow!(
Expand Down Expand Up @@ -792,17 +806,28 @@ impl From<&Vec<&Payload>> for PayloadFilter {
Payload::DirectMempool(_) => {
error!("DirectMempool payload in InQuorumStore filter");
},
Payload::OptQuorumStore(opt_qs_payload) => {
for batch in opt_qs_payload.inline_batches().iter() {
Payload::OptQuorumStore(OptQuorumStorePayload::V1(p)) => {
for batch in p.inline_batches().iter() {
exclude_batches.insert(batch.info().clone().into());
}
for batch_info in &opt_qs_payload.opt_batches().batch_summary {
for batch_info in &p.opt_batches().batch_summary {
exclude_batches.insert(batch_info.clone().into());
}
for proof in &opt_qs_payload.proof_with_data().batch_summary {
for proof in &p.proof_with_data().batch_summary {
exclude_batches.insert(proof.info().clone().into());
}
},
Payload::OptQuorumStore(OptQuorumStorePayload::V2(p)) => {
for batch in p.inline_batches().iter() {
exclude_batches.insert(batch.info().clone());
}
for batch_info in &p.opt_batches().batch_summary {
exclude_batches.insert(batch_info.clone());
}
for proof in &p.proof_with_data().batch_summary {
exclude_batches.insert(proof.info().clone());
}
},
}
}
PayloadFilter::InQuorumStore(exclude_batches)
Expand Down
Loading
Loading