Skip to content

Commit

Permalink
Convert dyn to generics in `verified_blob_content (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet authored Nov 26, 2024
1 parent 61f962e commit d7f5037
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
27 changes: 11 additions & 16 deletions crates/bitcoin-da/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use citrea_primitives::compression::decompress_blob;
use crypto_bigint::{Encoding, U256};
use itertools::Itertools;
use sov_rollup_interface::da::{
BlockHeaderTrait, CountedBufReader, DaNamespace, DaSpec, DaVerifier, UpdatedDaState,
BlobReaderTrait, BlockHeaderTrait, DaNamespace, DaSpec, DaVerifier, UpdatedDaState,
};
use sov_rollup_interface::zk::LightClientCircuitOutput;

Expand All @@ -12,7 +12,7 @@ use crate::helpers::parsers::{
ParsedLightClientTransaction, VerifyParsed,
};
use crate::helpers::{calculate_double_sha256, merkle_tree};
use crate::spec::blob::{BlobBuf, BlobWithSender};
use crate::spec::blob::BlobWithSender;
use crate::spec::BitcoinSpec;

pub const WITNESS_COMMITMENT_PREFIX: &[u8] = &[0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed];
Expand Down Expand Up @@ -110,8 +110,6 @@ impl DaVerifier for BitcoinVerifier {
if let Some(blob_content) =
verified_blob_content(&seq_comm, &mut blobs_iter)?
{
let blob_content = blob_content.accumulator();

// assert tx content is not modified
if blob_content != seq_comm.body {
return Err(ValidationError::BlobContentWasModified);
Expand All @@ -128,8 +126,6 @@ impl DaVerifier for BitcoinVerifier {
if let Some(blob_content) =
verified_blob_content(&complete, &mut blobs_iter)?
{
let blob_content = blob_content.accumulator();

// assert tx content is not modified
let body = decompress_blob(&complete.body);
if blob_content != body {
Expand All @@ -141,8 +137,6 @@ impl DaVerifier for BitcoinVerifier {
if let Some(blob_content) =
verified_blob_content(&aggregate, &mut blobs_iter)?
{
let blob_content = blob_content.accumulator();

// assert tx content is not modified
if blob_content != aggregate.body {
return Err(ValidationError::BlobContentWasModified);
Expand Down Expand Up @@ -314,10 +308,14 @@ impl DaVerifier for BitcoinVerifier {
}

// Get associated blob content only if signatures, hashes and public keys match
fn verified_blob_content(
tx: &dyn VerifyParsed,
blobs_iter: &mut dyn Iterator<Item = &BlobWithSender>,
) -> Result<Option<CountedBufReader<BlobBuf>>, ValidationError> {
fn verified_blob_content<'a, T, I>(
tx: &T,
blobs_iter: &mut I,
) -> Result<Option<&'a [u8]>, ValidationError>
where
T: VerifyParsed,
I: Iterator<Item = &'a BlobWithSender>,
{
if let Some(blob_hash) = tx.get_sig_verified_hash() {
let blob = blobs_iter.next();

Expand All @@ -333,10 +331,7 @@ fn verified_blob_content(
return Err(ValidationError::IncorrectSenderInBlob);
}

// read the supplied blob from txs
let mut blob_content = blob.blob.clone();
blob_content.advance(blob_content.total_len());
Ok(Some(blob_content))
Ok(Some(blob.verified_data()))
} else {
Ok(None)
}
Expand Down
14 changes: 11 additions & 3 deletions crates/bitcoin-da/tests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use citrea_e2e::framework::TestFramework;
use citrea_e2e::test_case::{TestCase, TestCaseRunner};
use citrea_e2e::Result;
use citrea_primitives::{TO_BATCH_PROOF_PREFIX, TO_LIGHT_CLIENT_PREFIX};
use sov_rollup_interface::da::{DaNamespace, DaVerifier};
use sov_rollup_interface::da::{BlobReaderTrait, DaNamespace, DaVerifier};
use sov_rollup_interface::services::da::DaService;
use test_utils::{
generate_mock_txs, get_citrea_path, get_default_service, get_mock_false_signature_txs_block,
Expand Down Expand Up @@ -57,7 +57,7 @@ impl TestCase for BitcoinServiceTest {

// Extracts relevant batch proof blobs with proof correctly
{
let (txs, inclusion_proof, completeness_proof) =
let (mut txs, inclusion_proof, completeness_proof) =
service.extract_relevant_blobs_with_proof(&block, DaNamespace::ToBatchProver);
assert_eq!(inclusion_proof.wtxids.len(), 29);
assert_eq!(inclusion_proof.wtxids[1..], block_wtxids[1..]);
Expand All @@ -71,6 +71,10 @@ impl TestCase for BitcoinServiceTest {
completeness_proof.len()
);

txs.iter_mut().for_each(|t| {
t.full_data();
});

// Since only one of the transactions has a malformed sender, we have to find the
// tx that is not malformed, and get its public key
pubkey = if txs[0].sender == txs[1].sender || txs[0].sender == txs[2].sender {
Expand All @@ -94,7 +98,7 @@ impl TestCase for BitcoinServiceTest {

// Extracts relevant light client proof blobs with proof correctly
{
let (txs, inclusion_proof, completeness_proof) =
let (mut txs, inclusion_proof, completeness_proof) =
service.extract_relevant_blobs_with_proof(&block, DaNamespace::ToLightClientProver);
assert_eq!(inclusion_proof.wtxids.len(), 29);
assert_eq!(inclusion_proof.wtxids[1..], block_wtxids[1..]);
Expand All @@ -108,6 +112,10 @@ impl TestCase for BitcoinServiceTest {
completeness_proof.len()
);

txs.iter_mut().for_each(|t| {
t.full_data();
});

// Ensure that the produced outputs are verifiable by the verifier
assert_eq!(
verifier.verify_transactions(
Expand Down
12 changes: 9 additions & 3 deletions crates/bitcoin-da/tests/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use citrea_e2e::framework::TestFramework;
use citrea_e2e::test_case::{TestCase, TestCaseRunner};
use citrea_e2e::Result;
use citrea_primitives::{TO_BATCH_PROOF_PREFIX, TO_LIGHT_CLIENT_PREFIX};
use sov_rollup_interface::da::{DaNamespace, DaVerifier};
use sov_rollup_interface::da::{BlobReaderTrait, DaNamespace, DaVerifier};
use sov_rollup_interface::services::da::DaService;
use test_utils::macros::assert_panic;
use test_utils::{
Expand All @@ -42,10 +42,16 @@ impl TestCase for BitcoinVerifierTest {
let service = get_default_service(&mut task_manager, &da_node.config).await;
let (block, _, _) = generate_mock_txs(&service, da_node, &mut task_manager).await;

let (b_txs, b_inclusion_proof, b_completeness_proof) =
let (mut b_txs, b_inclusion_proof, b_completeness_proof) =
service.extract_relevant_blobs_with_proof(&block, DaNamespace::ToBatchProver);
let (l_txs, l_inclusion_proof, l_completeness_proof) =
let (mut l_txs, l_inclusion_proof, l_completeness_proof) =
service.extract_relevant_blobs_with_proof(&block, DaNamespace::ToLightClientProver);
b_txs.iter_mut().for_each(|t| {
t.full_data();
});
l_txs.iter_mut().for_each(|t| {
t.full_data();
});

let verifier = BitcoinVerifier::new(RollupParams {
to_batch_proof_prefix: TO_BATCH_PROOF_PREFIX.to_vec(),
Expand Down

0 comments on commit d7f5037

Please sign in to comment.