summaryrefslogtreecommitdiff
path: root/src/hash_chain.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-10-22 14:59:43 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-10-23 22:00:57 +0800
commit71d5eadbc5ebe366da3af11dd8a010f55a83fd05 (patch)
tree813bca711b6bf0bdb10d7a1e332d80a7267f1b74 /src/hash_chain.rs
parent7aa4a2704299a8fa4872f90836dac864dcf24222 (diff)
refactor(hashchain): hash item with the original &(a, b) form instead of (&a, &b)
Diffstat (limited to 'src/hash_chain.rs')
-rw-r--r--src/hash_chain.rs18
1 files changed, 7 insertions, 11 deletions
diff --git a/src/hash_chain.rs b/src/hash_chain.rs
index 9beb60b..0fe2c1d 100644
--- a/src/hash_chain.rs
+++ b/src/hash_chain.rs
@@ -63,10 +63,7 @@ impl<T: Encode> HashChain<T> {
.iter()
.enumerate()
.all(|(prev_hash, (pred_prev_hash, _))| {
- let prev_hash = Self::hash_item({
- let prev_hash = &self.data[prev_hash - 1];
- (&prev_hash.0, &prev_hash.1)
- });
+ let prev_hash = Self::hash_o_item(&self.data[prev_hash - 1]);
prev_hash == *pred_prev_hash
})
}
@@ -81,12 +78,7 @@ impl<T: Encode> HashChain<T> {
let prev_hashes: Vec<_> = (pos..self
.data
.len())
- .map(|i| {
- Self::hash_item({
- let prev_hash = &self.data[i - 1];
- (&prev_hash.0, &prev_hash.1)
- })
- })
+ .map(|i| Self::hash_o_item(&self.data[i - 1]))
.collect();
self.data[pos..]
@@ -100,13 +92,17 @@ impl<T: Encode> HashChain<T> {
});
}
- fn hash_item(data: (&Vec<u8>, &T)) -> Vec<u8> {
+ pub fn hash_item(data: (&Vec<u8>, &T)) -> Vec<u8> {
let mut hasher = Sha256::new();
hasher.update(bincode::encode_to_vec(data, bincode::config::standard()).unwrap());
hasher
.finalize()
.to_vec()
}
+
+ pub fn hash_o_item(data: &(Vec<u8>, T)) -> Vec<u8> {
+ Self::hash_item((&data.0, &data.1))
+ }
}
impl<T: Encode> Deref for HashChain<T> {