diff --git a/crates/bitcoin-da/src/helpers/merkle_tree.rs b/crates/bitcoin-da/src/helpers/merkle_tree.rs index 4f2fcb3cd7..5cc94f2348 100644 --- a/crates/bitcoin-da/src/helpers/merkle_tree.rs +++ b/crates/bitcoin-da/src/helpers/merkle_tree.rs @@ -11,7 +11,7 @@ pub struct BitcoinMerkleTree { impl BitcoinMerkleTree { pub fn new(transactions: Vec<[u8; 32]>) -> Self { - if transactions.len() == 0 { + if transactions.is_empty() { return BitcoinMerkleTree { depth: 0, nodes: vec![], @@ -40,7 +40,7 @@ impl BitcoinMerkleTree { tree.nodes.push(vec![]); for i in 0..(prev_level_size / 2) { preimage[..32].copy_from_slice( - &tree.nodes[curr_level_offset - 1 as usize][prev_level_index_offset + i * 2], + &tree.nodes[curr_level_offset - 1_usize][prev_level_index_offset + i * 2], ); preimage[32..].copy_from_slice( &tree.nodes[curr_level_offset - 1][prev_level_index_offset + i * 2 + 1], @@ -70,38 +70,33 @@ impl BitcoinMerkleTree { // Returns the Merkle root pub fn root(&self) -> Option<[u8; 32]> { - if self.nodes.len() == 0 { + if self.nodes.is_empty() { return None; } - return Some(self.nodes[self.nodes.len() - 1][0]); + Some(self.nodes[self.nodes.len() - 1][0]) } pub fn get_element(&self, level: u32, index: u32) -> [u8; 32] { - return self.nodes[level as usize][index as usize]; + self.nodes[level as usize][index as usize] } pub fn get_idx_path(&self, index: u32) -> Vec<[u8; 32]> { - assert!( - index <= self.nodes[0].len() as u32 - 1, - "Index out of bounds" - ); + assert!(index < self.nodes[0].len() as u32, "Index out of bounds"); let mut path = vec![]; let mut level = 0; let mut i = index; while level < self.nodes.len() as u32 - 1 { if i % 2 == 1 { path.push(self.nodes[level as usize][i as usize - 1]); + } else if (self.nodes[level as usize].len() - 1) as u32 == i { + path.push(self.nodes[level as usize][i as usize]); } else { - if (self.nodes[level as usize].len() - 1) as u32 == i { - path.push(self.nodes[level as usize][i as usize]); - } else { - path.push(self.nodes[level as usize][(i + 1) as usize]); - } + path.push(self.nodes[level as usize][(i + 1) as usize]); } level += 1; - i = i / 2; + i /= 2; } - return path; + path } pub fn calculate_root_with_merkle_proof( @@ -110,7 +105,7 @@ impl BitcoinMerkleTree { merkle_proof: Vec<[u8; 32]>, ) -> [u8; 32] { let mut preimage: [u8; 64] = [0; 64]; - let mut combined_hash: [u8; 32] = txid.clone(); + let mut combined_hash: [u8; 32] = txid; let mut index = idx; let mut level: u32 = 0; while level < merkle_proof.len() as u32 { @@ -124,7 +119,7 @@ impl BitcoinMerkleTree { combined_hash = calculate_double_sha256(&preimage); } level += 1; - index = index / 2; + index /= 2; } combined_hash } diff --git a/crates/bitcoin-da/src/helpers/mod.rs b/crates/bitcoin-da/src/helpers/mod.rs index 7411b1f1a3..4ec27d493f 100644 --- a/crates/bitcoin-da/src/helpers/mod.rs +++ b/crates/bitcoin-da/src/helpers/mod.rs @@ -113,5 +113,5 @@ pub fn calculate_double_sha256(input: &[u8]) -> [u8; 32] { hasher.update(input); let result = hasher.finalize_reset(); hasher.update(result); - hasher.finalize().try_into().unwrap() + hasher.finalize().into() } diff --git a/crates/bitcoin-da/src/helpers/parsers.rs b/crates/bitcoin-da/src/helpers/parsers.rs index 048880a5b6..84a7aa86d6 100644 --- a/crates/bitcoin-da/src/helpers/parsers.rs +++ b/crates/bitcoin-da/src/helpers/parsers.rs @@ -2,7 +2,7 @@ use core::num::NonZeroU16; use bitcoin::blockdata::script::Instruction; use bitcoin::hashes::Hash; -use bitcoin::opcodes::all::{OP_CHECKSIGVERIFY, OP_DROP}; +use bitcoin::opcodes::all::OP_CHECKSIGVERIFY; use bitcoin::script::Instruction::{Op, PushBytes}; use bitcoin::script::{Error as ScriptError, PushBytes as StructPushBytes}; use bitcoin::secp256k1::{ecdsa, Message, Secp256k1}; diff --git a/crates/bitcoin-da/src/helpers/test_utils.rs b/crates/bitcoin-da/src/helpers/test_utils.rs index b5a6b02186..ac289cd87c 100644 --- a/crates/bitcoin-da/src/helpers/test_utils.rs +++ b/crates/bitcoin-da/src/helpers/test_utils.rs @@ -1,9 +1,4 @@ -use core::str::FromStr; - -use bitcoin::block::{Header, Version}; -use bitcoin::hash_types::{TxMerkleNode, WitnessMerkleNode}; -use bitcoin::hashes::Hash; -use bitcoin::{BlockHash, CompactTarget, Transaction}; +use bitcoin::Transaction; use sov_rollup_interface::da::{DaSpec, DaVerifier}; use super::calculate_double_sha256; @@ -11,8 +6,6 @@ use super::parsers::ParsedLightClientTransaction; use crate::helpers::compression::decompress_blob; use crate::helpers::parsers::{parse_hex_transaction, parse_transaction}; use crate::spec::blob::BlobWithSender; -use crate::spec::header::HeaderWrapper; -use crate::spec::proof::InclusionMultiProof; use crate::verifier::BitcoinVerifier; pub(crate) fn get_mock_txs() -> Vec { diff --git a/crates/bitcoin-da/src/service.rs b/crates/bitcoin-da/src/service.rs index 29365e51e9..73236fa4d9 100644 --- a/crates/bitcoin-da/src/service.rs +++ b/crates/bitcoin-da/src/service.rs @@ -28,10 +28,10 @@ use crate::helpers::builders::{ create_seqcommitment_transactions, create_zkproof_transactions, sign_blob_with_private_key, write_inscription_txs, BatchProvingTxs, LightClientTxs, TxWithId, }; -use crate::helpers::compression::{compress_blob, decompress_blob}; +use crate::helpers::compression::compress_blob; +use crate::helpers::merkle_tree; use crate::helpers::merkle_tree::BitcoinMerkleTree; use crate::helpers::parsers::{parse_batch_proof_transaction, ParsedBatchProofTransaction}; -use crate::helpers::{calculate_double_sha256, merkle_tree}; use crate::spec::blob::BlobWithSender; use crate::spec::block::BitcoinBlock; use crate::spec::header::HeaderWrapper; diff --git a/crates/bitcoin-da/src/spec/header.rs b/crates/bitcoin-da/src/spec/header.rs index c148cb60be..12f2b9c01e 100644 --- a/crates/bitcoin-da/src/spec/header.rs +++ b/crates/bitcoin-da/src/spec/header.rs @@ -1,7 +1,6 @@ use core::ops::Deref; use bitcoin::block::{Header as BitcoinHeader, Version}; -use bitcoin::hash_types::WitnessMerkleNode; use bitcoin::hashes::Hash; use bitcoin::{BlockHash, CompactTarget, TxMerkleNode}; use borsh::{BorshDeserialize, BorshSerialize}; diff --git a/crates/bitcoin-da/src/verifier.rs b/crates/bitcoin-da/src/verifier.rs index 56ca2b0aad..3158b01ad2 100644 --- a/crates/bitcoin-da/src/verifier.rs +++ b/crates/bitcoin-da/src/verifier.rs @@ -200,8 +200,9 @@ impl DaVerifier for BitcoinVerifier { } } Some(mut commitment_idx) => { - let merkle_root = - merkle_tree::BitcoinMerkleTree::new(inclusion_proof.wtxids).root(); + let merkle_root = merkle_tree::BitcoinMerkleTree::new(inclusion_proof.wtxids) + .root() + .expect("Merkle root should not fail"); let input_witness_value = coinbase_tx.input[0].witness.iter().next().unwrap(); @@ -254,25 +255,15 @@ mod tests { // Transactions for testing is prepared with 2 leading zeros // So verifier takes in [0, 0] - use core::str::FromStr; - - use bitcoin::block::{Header, Version}; - use bitcoin::hash_types::{TxMerkleNode, WitnessMerkleNode}; use bitcoin::hashes::Hash; - use bitcoin::{BlockHash, CompactTarget, ScriptBuf, Witness}; use sov_rollup_interface::da::DaVerifier; use super::BitcoinVerifier; use crate::helpers::parsers::{parse_batch_proof_transaction, ParsedBatchProofTransaction}; - use crate::helpers::test_utils::{ - get_blob_with_sender, get_mock_data, get_mock_txs, get_non_segwit_mock_txs, - }; + use crate::helpers::test_utils::{get_mock_data, get_mock_txs}; use crate::spec::blob::BlobWithSender; - use crate::spec::header::HeaderWrapper; - use crate::spec::proof::InclusionMultiProof; - use crate::spec::transaction::TransactionWrapper; use crate::spec::RollupParams; - use crate::verifier::{ChainValidityCondition, ValidationError, WITNESS_COMMITMENT_PREFIX}; + use crate::verifier::ValidationError; #[test] fn correct() {