Skip to content

Commit a9990f2

Browse files
committed
chore: more cleanup for indexing_slicing
1 parent 7b331b3 commit a9990f2

File tree

12 files changed

+179
-173
lines changed

12 files changed

+179
-173
lines changed

stacks-common/src/util/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ where
580580
// borrowed from Andrew Poelstra's rust-bitcoin library
581581
/// Convert a hexadecimal-encoded string to its corresponding bytes
582582
pub fn hex_bytes(s: &str) -> Result<Vec<u8>, HexError> {
583-
let mut v = vec![];
583+
let mut v = Vec::with_capacity(s.len() / 2);
584584
let mut iter = s.chars().pair();
585585
// Do the parsing
586586
iter.by_ref()

stackslib/src/chainstate/stacks/index/bits.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,12 @@ pub fn ptrs_from_bytes<R: Read>(
143143
let ptr_bytes = bytes
144144
.get(1..)
145145
.ok_or_else(|| Error::CorruptionError("Failed to read >1 bytes from bytes array".into()))?;
146-
147-
for i in 0..num_ptrs {
148-
let ptr_slot = ptrs_buf
149-
.get_mut(i)
150-
.ok_or_else(|| Error::CorruptionError("ptrs_buff smaller than num_ptrs".into()))?;
151-
let next_ptr_bytes = ptr_bytes
152-
.get(i * TRIEPTR_SIZE..(i + 1) * TRIEPTR_SIZE)
153-
.ok_or_else(|| Error::CorruptionError("ptr_bytes malformed".into()))?;
146+
// iterate over the read-in bytes in chunks of TRIEPTR_SIZE and store them
147+
// to `ptrs_buf`
148+
let reading_ptrs = ptr_bytes
149+
.chunks_exact(TRIEPTR_SIZE)
150+
.zip(ptrs_buf.iter_mut());
151+
for (next_ptr_bytes, ptr_slot) in reading_ptrs {
154152
*ptr_slot = TriePtr::from_bytes(next_ptr_bytes);
155153
}
156154
Ok(*nid)
@@ -171,9 +169,7 @@ pub fn get_node_hash<M, T: ConsensusSerializable<M> + std::fmt::Debug>(
171169
hasher.update(child_hash.as_ref());
172170
}
173171

174-
let mut res = [0u8; 32];
175-
res.copy_from_slice(hasher.finalize().as_slice());
176-
172+
let res = hasher.finalize().into();
177173
let ret = TrieHash(res);
178174

179175
trace!(
@@ -191,9 +187,7 @@ pub fn get_leaf_hash(node: &TrieLeaf) -> TrieHash {
191187
node.write_bytes(&mut hasher)
192188
.expect("IO Failure pushing to hasher.");
193189

194-
let mut res = [0u8; 32];
195-
res.copy_from_slice(hasher.finalize().as_slice());
196-
190+
let res = hasher.finalize().into();
197191
let ret = TrieHash(res);
198192

199193
trace!("get_leaf_hash: hash {:?} = {:?} + []", &ret, node);

stackslib/src/chainstate/stacks/index/mod.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,12 @@ macro_rules! impl_clarity_marf_trie_id {
121121
fn from(m: MARFValue) -> Self {
122122
let h = m.0;
123123
let mut d = [0u8; 32];
124-
d.copy_from_slice(h.get(..32).expect("Failed to convert MARF value: MARFValue did not have 32 bytes"));
125-
if let Some(remainder) = h.get(32..) {
126-
for h_i in remainder {
127-
if *h_i != 0 {
128-
panic!(
129-
"Failed to convert MARF value into BHH: data stored after 32nd byte"
130-
);
131-
}
124+
d.copy_from_slice(&h[..32]);
125+
for x in &h[32..] {
126+
if *x != 0 {
127+
panic!(
128+
"Failed to convert MARF value into BHH: data stored after 32nd byte"
129+
);
132130
}
133131
}
134132
Self(d)
@@ -192,13 +190,11 @@ impl From<MARFValue> for u32 {
192190
let h = m.0;
193191
let mut d = [0u8; 4];
194192

195-
d.copy_from_slice(h.get(..4).expect("FATAL: MARFValue does not have 4 bytes"));
193+
d.copy_from_slice(&h[..4]);
196194

197-
if let Some(remainder) = h.get(4..) {
198-
for h_i in remainder {
199-
if *h_i != 0 {
200-
panic!("Failed to convert MARF value into u32: data stored after 4th byte");
201-
}
195+
for h_i in &h[4..] {
196+
if *h_i != 0 {
197+
panic!("Failed to convert MARF value into u32: data stored after 4th byte");
202198
}
203199
}
204200
u32::from_le_bytes(d)
@@ -220,11 +216,9 @@ impl MARFValue {
220216

221217
/// Construct from a String that encodes a value inserted into the underlying data store
222218
pub fn from_value(s: &str) -> MARFValue {
223-
let mut tmp = [0u8; 32];
224-
225219
let mut hasher = TrieHasher::new();
226220
hasher.update(s.as_bytes());
227-
tmp.copy_from_slice(hasher.finalize().as_slice());
221+
let tmp = hasher.finalize().into();
228222

229223
MARFValue::from_value_hash_bytes(&tmp)
230224
}

0 commit comments

Comments
 (0)