Skip to content

Commit 0669083

Browse files
committed
[consensus] Introduce OptQS::V2 Payload
1 parent 94ea3bd commit 0669083

File tree

8 files changed

+330
-159
lines changed

8 files changed

+330
-159
lines changed

consensus/consensus-types/src/block.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
block_data::{BlockData, BlockType},
77
common::{Author, Payload, Round},
88
opt_block_data::OptBlockData,
9+
payload::{OptQuorumStorePayload, TDataInfo},
910
quorum_cert::QuorumCert,
1011
};
1112
use anyhow::{bail, ensure, format_err, Result};
@@ -143,11 +144,19 @@ impl Block {
143144
proof_with_data.num_txns(),
144145
proof_with_data.num_bytes(),
145146
),
146-
Payload::OptQuorumStore(opt_quorum_store_payload) => (
147-
opt_quorum_store_payload.proof_with_data().num_proofs(),
148-
opt_quorum_store_payload.proof_with_data().num_txns(),
149-
opt_quorum_store_payload.proof_with_data().num_bytes(),
150-
),
147+
Payload::OptQuorumStore(opt_quorum_store_payload) => match opt_quorum_store_payload
148+
{
149+
OptQuorumStorePayload::V1(p) => (
150+
p.proof_with_data().num_proofs(),
151+
p.proof_with_data().num_txns(),
152+
p.proof_with_data().num_bytes(),
153+
),
154+
OptQuorumStorePayload::V2(p) => (
155+
p.proof_with_data().num_proofs(),
156+
p.proof_with_data().num_txns(),
157+
p.proof_with_data().num_bytes(),
158+
),
159+
},
151160
},
152161
}
153162
}
@@ -169,11 +178,19 @@ impl Block {
169178
.map(|(b, _)| b.num_bytes() as usize)
170179
.sum(),
171180
),
172-
Payload::OptQuorumStore(opt_quorum_store_payload) => (
173-
opt_quorum_store_payload.inline_batches().num_batches(),
174-
opt_quorum_store_payload.inline_batches().num_txns(),
175-
opt_quorum_store_payload.inline_batches().num_bytes(),
176-
),
181+
Payload::OptQuorumStore(opt_quorum_store_payload) => match opt_quorum_store_payload
182+
{
183+
OptQuorumStorePayload::V1(p) => (
184+
p.inline_batches().num_batches(),
185+
p.inline_batches().num_txns(),
186+
p.inline_batches().num_bytes(),
187+
),
188+
OptQuorumStorePayload::V2(p) => (
189+
p.inline_batches().num_batches(),
190+
p.inline_batches().num_txns(),
191+
p.inline_batches().num_bytes(),
192+
),
193+
},
177194
_ => (0, 0, 0),
178195
},
179196
}
@@ -184,19 +201,19 @@ impl Block {
184201
match self.block_data.payload() {
185202
None => (0, 0, 0),
186203
Some(payload) => match payload {
187-
Payload::OptQuorumStore(opt_quorum_store_payload) => (
188-
opt_quorum_store_payload.opt_batches().len(),
189-
opt_quorum_store_payload
190-
.opt_batches()
191-
.iter()
192-
.map(|b| b.num_txns() as usize)
193-
.sum(),
194-
opt_quorum_store_payload
195-
.opt_batches()
196-
.iter()
197-
.map(|b| b.num_bytes() as usize)
198-
.sum(),
199-
),
204+
Payload::OptQuorumStore(opt_quorum_store_payload) => match opt_quorum_store_payload
205+
{
206+
OptQuorumStorePayload::V1(p) => (
207+
p.opt_batches().len(),
208+
p.opt_batches().iter().map(|b| b.num_txns() as usize).sum(),
209+
p.opt_batches().iter().map(|b| b.num_bytes() as usize).sum(),
210+
),
211+
OptQuorumStorePayload::V2(p) => (
212+
p.opt_batches().len(),
213+
p.opt_batches().iter().map(|b| b.num_txns() as usize).sum(),
214+
p.opt_batches().iter().map(|b| b.num_bytes() as usize).sum(),
215+
),
216+
},
200217
_ => (0, 0, 0),
201218
},
202219
}

consensus/consensus-types/src/common.rs

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,15 @@ impl Payload {
515515
}
516516
}
517517

518-
fn verify_with_cache(
519-
proofs: &[ProofOfStore<BatchInfo>],
518+
fn verify_with_cache<T>(
519+
proofs: &[ProofOfStore<T>],
520520
validator: &ValidatorVerifier,
521521
proof_cache: &ProofCache,
522-
) -> anyhow::Result<()> {
522+
) -> anyhow::Result<()>
523+
where
524+
T: TBatchInfo + Send + Sync + 'static,
525+
BatchInfoExt: From<T>,
526+
{
523527
let unverified: Vec<_> = proofs
524528
.iter()
525529
.filter(|proof| {
@@ -535,15 +539,15 @@ impl Payload {
535539
Ok(())
536540
}
537541

538-
pub fn verify_inline_batches<'a>(
539-
inline_batches: impl Iterator<Item = (&'a BatchInfo, &'a Vec<SignedTransaction>)>,
542+
pub fn verify_inline_batches<'a, T: TBatchInfo + 'a>(
543+
inline_batches: impl Iterator<Item = (&'a T, &'a Vec<SignedTransaction>)>,
540544
) -> anyhow::Result<()> {
541545
for (batch, payload) in inline_batches {
542546
// TODO: Can cloning be avoided here?
543547
let computed_digest = BatchPayload::new(batch.author(), payload.clone()).hash();
544548
ensure!(
545549
computed_digest == *batch.digest(),
546-
"Hash of the received inline batch doesn't match the digest value for batch {}: {} != {}",
550+
"Hash of the received inline batch doesn't match the digest value for batch {:?}: {} != {}",
547551
batch,
548552
computed_digest,
549553
batch.digest()
@@ -552,9 +556,9 @@ impl Payload {
552556
Ok(())
553557
}
554558

555-
pub fn verify_opt_batches(
559+
pub fn verify_opt_batches<T: TBatchInfo>(
556560
verifier: &ValidatorVerifier,
557-
opt_batches: &OptBatches,
561+
opt_batches: &OptBatches<T>,
558562
) -> anyhow::Result<()> {
559563
let authors = verifier.address_to_validator_index();
560564
for batch in &opt_batches.batch_summary {
@@ -592,16 +596,26 @@ impl Payload {
592596
)?;
593597
Ok(())
594598
},
595-
(true, Payload::OptQuorumStore(opt_quorum_store)) => {
596-
let proof_with_data = opt_quorum_store.proof_with_data();
599+
(true, Payload::OptQuorumStore(OptQuorumStorePayload::V1(p))) => {
600+
let proof_with_data = p.proof_with_data();
601+
Self::verify_with_cache(&proof_with_data.batch_summary, verifier, proof_cache)?;
602+
Self::verify_inline_batches(
603+
p.inline_batches()
604+
.iter()
605+
.map(|batch| (batch.info(), batch.transactions())),
606+
)?;
607+
Self::verify_opt_batches(verifier, p.opt_batches())?;
608+
Ok(())
609+
},
610+
(true, Payload::OptQuorumStore(OptQuorumStorePayload::V2(p))) => {
611+
let proof_with_data = p.proof_with_data();
597612
Self::verify_with_cache(&proof_with_data.batch_summary, verifier, proof_cache)?;
598613
Self::verify_inline_batches(
599-
opt_quorum_store
600-
.inline_batches()
614+
p.inline_batches()
601615
.iter()
602616
.map(|batch| (batch.info(), batch.transactions())),
603617
)?;
604-
Self::verify_opt_batches(verifier, opt_quorum_store.opt_batches())?;
618+
Self::verify_opt_batches(verifier, p.opt_batches())?;
605619
Ok(())
606620
},
607621
(_, _) => Err(anyhow::anyhow!(
@@ -792,17 +806,28 @@ impl From<&Vec<&Payload>> for PayloadFilter {
792806
Payload::DirectMempool(_) => {
793807
error!("DirectMempool payload in InQuorumStore filter");
794808
},
795-
Payload::OptQuorumStore(opt_qs_payload) => {
796-
for batch in opt_qs_payload.inline_batches().iter() {
809+
Payload::OptQuorumStore(OptQuorumStorePayload::V1(p)) => {
810+
for batch in p.inline_batches().iter() {
797811
exclude_batches.insert(batch.info().clone().into());
798812
}
799-
for batch_info in &opt_qs_payload.opt_batches().batch_summary {
813+
for batch_info in &p.opt_batches().batch_summary {
800814
exclude_batches.insert(batch_info.clone().into());
801815
}
802-
for proof in &opt_qs_payload.proof_with_data().batch_summary {
816+
for proof in &p.proof_with_data().batch_summary {
803817
exclude_batches.insert(proof.info().clone().into());
804818
}
805819
},
820+
Payload::OptQuorumStore(OptQuorumStorePayload::V2(p)) => {
821+
for batch in p.inline_batches().iter() {
822+
exclude_batches.insert(batch.info().clone());
823+
}
824+
for batch_info in &p.opt_batches().batch_summary {
825+
exclude_batches.insert(batch_info.clone());
826+
}
827+
for proof in &p.proof_with_data().batch_summary {
828+
exclude_batches.insert(proof.info().clone());
829+
}
830+
},
806831
}
807832
}
808833
PayloadFilter::InQuorumStore(exclude_batches)

0 commit comments

Comments
 (0)