Skip to content

Commit 068822b

Browse files
committed
refactor: use Hash primitive
1 parent a9b88df commit 068822b

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

dash/src/sml/masternode_list/merkle_roots.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::transaction::special_transaction::TransactionPayload;
2020
/// - `Some([u8; 32])`: The computed Merkle root if at least one hash is provided.
2121
/// - `None`: If the input vector is empty.
2222
#[inline]
23-
pub fn merkle_root_from_hashes(hashes: Vec<[u8; 32]>) -> Option<[u8; 32]> {
23+
pub fn merkle_root_from_hashes(hashes: Vec<sha256d::Hash>) -> Option<sha256d::Hash> {
2424
let length = hashes.len();
2525
let mut level = hashes;
2626
match length {
@@ -29,12 +29,12 @@ pub fn merkle_root_from_hashes(hashes: Vec<[u8; 32]>) -> Option<[u8; 32]> {
2929
while level.len() != 1 {
3030
let len = level.len();
3131
let mut higher_level =
32-
Vec::<[u8; 32]>::with_capacity((0.5 * len as f64).ceil() as usize);
32+
Vec::<sha256d::Hash>::with_capacity((0.5 * len as f64).ceil() as usize);
3333
for pair in level.chunks(2) {
3434
let mut buffer = Vec::with_capacity(64);
35-
buffer.extend_from_slice(&pair[0]);
36-
buffer.extend_from_slice(pair.get(1).unwrap_or(&pair[0]));
37-
higher_level.push(sha256d::Hash::hash(&buffer).to_byte_array());
35+
buffer.extend_from_slice(pair[0].as_byte_array());
36+
buffer.extend_from_slice(pair.get(1).unwrap_or(&pair[0]).as_byte_array());
37+
higher_level.push(sha256d::Hash::hash(&buffer));
3838
}
3939
level = higher_level;
4040
}
@@ -126,7 +126,7 @@ impl MasternodeList {
126126
) -> Option<MerkleRootMasternodeList> {
127127
self.hashes_for_merkle_root(block_height)
128128
.and_then(merkle_root_from_hashes)
129-
.map(|hash| MerkleRootMasternodeList::from_byte_array(hash))
129+
.map(MerkleRootMasternodeList::from_raw_hash)
130130
}
131131

132132
/// Computes the Merkle root for the LLMQ (Long-Living Masternode Quorum) list.
@@ -140,7 +140,7 @@ impl MasternodeList {
140140
/// - `None`: If no quorum commitment hashes are available.
141141
pub fn calculate_llmq_merkle_root(&self) -> Option<MerkleRootQuorums> {
142142
merkle_root_from_hashes(self.hashes_for_quorum_merkle_root())
143-
.map(|hash| MerkleRootQuorums::from_byte_array(hash))
143+
.map(MerkleRootQuorums::from_raw_hash)
144144
}
145145

146146
/// Retrieves the list of hashes required to compute the masternode list Merkle root.
@@ -154,9 +154,9 @@ impl MasternodeList {
154154
///
155155
/// # Returns
156156
///
157-
/// - `Some(Vec<[u8; 32]>)`: A sorted list of masternode entry hashes.
157+
/// - `Some(Vec<sha256d::Hash>)`: A sorted list of masternode entry hashes.
158158
/// - `None`: If the block height is invalid (`u32::MAX`).
159-
pub fn hashes_for_merkle_root(&self, block_height: u32) -> Option<Vec<[u8; 32]>> {
159+
pub fn hashes_for_merkle_root(&self, block_height: u32) -> Option<Vec<sha256d::Hash>> {
160160
(block_height != u32::MAX).then_some({
161161
let mut pro_tx_hashes = self.reversed_pro_reg_tx_hashes();
162162
pro_tx_hashes.sort_by(|&s1, &s2| s1.reverse().cmp(&s2.reverse()));
@@ -180,11 +180,11 @@ impl MasternodeList {
180180
/// # Returns
181181
///
182182
/// - `Vec<[u8; 32]>`: A sorted list of quorum commitment hashes.
183-
pub fn hashes_for_quorum_merkle_root(&self) -> Vec<[u8; 32]> {
183+
pub fn hashes_for_quorum_merkle_root(&self) -> Vec<sha256d::Hash> {
184184
let mut llmq_commitment_hashes = self
185185
.quorums
186186
.values()
187-
.flat_map(|q_map| q_map.values().map(|entry| entry.entry_hash.to_byte_array()))
187+
.flat_map(|q_map| q_map.values().map(|entry| entry.entry_hash.to_raw_hash()))
188188
.collect::<Vec<_>>();
189189
llmq_commitment_hashes.sort();
190190
llmq_commitment_hashes
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use hashes::{Hash, sha256d};
1+
use hashes::{sha256d, Hash};
22

33
use crate::consensus::Encodable;
44
use crate::sml::masternode_list_entry::MasternodeListEntry;
55

66
impl MasternodeListEntry {
7-
pub fn calculate_entry_hash(&self) -> [u8; 32] {
7+
pub fn calculate_entry_hash(&self) -> sha256d::Hash {
88
let mut writer = Vec::new();
99

1010
self.consensus_encode(&mut writer).expect("encoding failed");
11-
sha256d::Hash::hash(&writer).to_byte_array()
11+
sha256d::Hash::hash(&writer)
1212
}
1313
}

dash/src/sml/masternode_list_entry/qualified_masternode_list_entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp::Ordering;
22

33
#[cfg(feature = "bincode")]
44
use bincode::{Decode, Encode};
5-
use hashes::Hash;
5+
use hashes::{sha256d, Hash};
66

77
use crate::hash_types::ConfirmedHashHashedWithProRegTx;
88
use crate::sml::masternode_list_entry::MasternodeListEntry;
@@ -18,7 +18,7 @@ pub struct QualifiedMasternodeListEntry {
1818
/// The underlying masternode list entry
1919
pub masternode_list_entry: MasternodeListEntry,
2020
/// The computed entry hash
21-
pub entry_hash: [u8; 32],
21+
pub entry_hash: sha256d::Hash,
2222
/// The confirmed hash hashed with the pro_reg_tx if the confirmed hash is set
2323
pub confirmed_hash_hashed_with_pro_reg_tx: Option<ConfirmedHashHashedWithProRegTx>,
2424
}

0 commit comments

Comments
 (0)