Skip to content

Commit

Permalink
Fix Ahmet lints
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet committed Aug 15, 2024
1 parent 2997f53 commit 843bf1c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 45 deletions.
31 changes: 13 additions & 18 deletions crates/bitcoin-da/src/helpers/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -124,7 +119,7 @@ impl BitcoinMerkleTree {
combined_hash = calculate_double_sha256(&preimage);
}
level += 1;
index = index / 2;
index /= 2;
}
combined_hash
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bitcoin-da/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion crates/bitcoin-da/src/helpers/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
9 changes: 1 addition & 8 deletions crates/bitcoin-da/src/helpers/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
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;
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<Transaction> {
Expand Down
4 changes: 2 additions & 2 deletions crates/bitcoin-da/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion crates/bitcoin-da/src/spec/header.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
19 changes: 5 additions & 14 deletions crates/bitcoin-da/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 843bf1c

Please sign in to comment.