Skip to content

Commit 119426f

Browse files
committed
Refactor: Replace KeyHash::try_from with .try_into() for consistent hash conversion across modules
1 parent 87b2ea6 commit 119426f

File tree

8 files changed

+16
-36
lines changed

8 files changed

+16
-36
lines changed

codec/src/map_parameters.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ pub fn genesis_delegate_to_hash<const N: usize>(
4949
Hash::try_from(pallas_hash.as_ref()).unwrap()
5050
}
5151

52-
/// Convert an optional Pallas Hash reference to an optional Acropolis Hash
53-
pub fn to_hash_opt<const N: usize>(
54-
pallas_hash: Option<&pallas_primitives::Hash<N>>,
55-
) -> Option<Hash<N>> {
56-
pallas_hash.map(|h| to_hash(h))
57-
}
58-
5952
/// Convert a Pallas Hash<28> reference to an Acropolis PoolId
6053
pub fn to_pool_id(pallas_hash: &pallas_primitives::Hash<28>) -> PoolId {
6154
to_hash(pallas_hash).into()
@@ -66,15 +59,6 @@ pub fn to_vrf_key(pallas_hash: &pallas_primitives::Hash<32>) -> VrfKeyHash {
6659
VrfKeyHash::try_from(pallas_hash.as_ref()).unwrap()
6760
}
6861

69-
/// Convert a Pallas Bytes reference to an Acropolis Hash<N>
70-
/// Useful for genesis hash fields that might be stored as Bytes
71-
pub fn bytes_to_hash<const N: usize>(bytes: &pallas_primitives::Bytes) -> Hash<N> {
72-
let slice: &[u8] = bytes.as_ref();
73-
let mut array = [0u8; N];
74-
array.copy_from_slice(&slice[..N]);
75-
array.into()
76-
}
77-
7862
/// Derive our Address from a Pallas address
7963
// This is essentially a 1:1 mapping but makes the Message definitions independent
8064
// of Pallas

common/src/snapshot/streaming_snapshot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for StakeCredential {
6666
0 => {
6767
// ScriptHash variant (first in enum) - decode bytes directly
6868
let bytes = d.bytes()?;
69-
let key_hash = KeyHash::try_from(bytes).map_err(|_| {
69+
let key_hash = bytes.try_into().map_err(|_| {
7070
minicbor::decode::Error::message(
7171
"invalid length for ScriptHash in StakeCredential",
7272
)
@@ -76,7 +76,7 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for StakeCredential {
7676
1 => {
7777
// AddrKeyHash variant (second in enum) - decodes bytes directly
7878
let bytes = d.bytes()?;
79-
let key_hash = KeyHash::try_from(bytes).map_err(|_| {
79+
let key_hash = bytes.try_into().map_err(|_| {
8080
minicbor::decode::Error::message(
8181
"invalid length for AddrKeyHash in StakeCredential",
8282
)
@@ -309,7 +309,7 @@ impl<'b, C> minicbor::Decode<'b, C> for Account {
309309

310310
pub use crate::types::AddrKeyhash;
311311
pub use crate::types::ScriptHash;
312-
use crate::{KeyHash, PoolId};
312+
use crate::{PoolId};
313313
/// Alias minicbor as cbor for pool_params module
314314
pub use minicbor as cbor;
315315

common/src/types.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,10 @@ impl Credential {
653653
hex_str
654654
))
655655
} else {
656-
KeyHash::try_from(key_hash.as_slice())
657-
.map_err(|_| anyhow!("Failed to convert to KeyHash"))
656+
key_hash
657+
.as_slice()
658+
.try_into()
659+
.map_err(|e| anyhow!("Failed to convert to KeyHash {}", e))
658660
}
659661
}
660662

@@ -695,12 +697,7 @@ impl Credential {
695697
));
696698
}
697699

698-
let hash = KeyHash::try_from(data).map_err(|v| {
699-
anyhow!(
700-
"Failed to convert to KeyHash: expected 28 bytes, got {}",
701-
v.len()
702-
)
703-
})?;
700+
let hash = data.try_into().expect("failed to convert to fixed-size array");
704701

705702
match hrp.as_str() {
706703
"drep" => Ok(Credential::AddrKeyHash(hash)),

modules/accounts_state/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ mod tests {
10091009
full_hash[..hash.len().min(28)].copy_from_slice(&hash[..hash.len().min(28)]);
10101010
StakeAddress {
10111011
network: NetworkId::Mainnet,
1012-
credential: StakeCredential::AddrKeyHash(KeyHash::try_from(full_hash).unwrap()),
1012+
credential: StakeCredential::AddrKeyHash(full_hash.try_into().unwrap()),
10131013
}
10141014
}
10151015

modules/accounts_state/src/verifier.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Verifier {
166166
continue;
167167
};
168168

169-
expected_rewards.entry(KeyHash::try_from(spo).unwrap()).or_default().push(
169+
expected_rewards.entry(spo.try_into().unwrap()).or_default().push(
170170
RewardDetail {
171171
account: stake_address,
172172
rtype,

modules/chain_store/src/chain_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use acropolis_common::{
1212
},
1313
queries::misc::Order,
1414
state_history::{StateHistory, StateHistoryStore},
15-
BechOrdAddress, BlockHash, GenesisDelegate, HeavyDelegate, TxHash, VrfKeyHash,
15+
BechOrdAddress, BlockHash, GenesisDelegate, HeavyDelegate, TxHash,
1616
};
1717
use anyhow::{bail, Result};
1818
use caryatid_sdk::{module, Context, Module};
@@ -413,7 +413,7 @@ impl ChainStore {
413413
tx_count: decoded.tx_count() as u64,
414414
output,
415415
fees,
416-
block_vrf: header.vrf_vkey().map(|key| VrfKeyHash::try_from(key).ok().unwrap()),
416+
block_vrf: header.vrf_vkey().map(|key| key.try_into().ok().unwrap()),
417417
op_cert,
418418
op_cert_counter,
419419
previous_block: header.previous_hash().map(|h| BlockHash::from(*h)),

modules/parameters_state/src/genesis_params.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::alonzo_genesis;
2-
use acropolis_common::types::ScriptHash;
32
use acropolis_common::{
43
protocol_params::{AlonzoParams, BabbageParams, ByronParams, ConwayParams, ShelleyParams},
54
rational_number::{rational_number_from_f32, RationalNumber},
@@ -109,7 +108,7 @@ pub fn map_constitution(constitution: &conway::Constitution) -> Result<Constitut
109108
Ok(Constitution {
110109
anchor: map_anchor(&constitution.anchor)?,
111110
guardrail_script: Some(
112-
ScriptHash::try_from(decode_hex_string(&constitution.script, 28)?).unwrap(),
111+
decode_hex_string(&constitution.script, 28)?.try_into().unwrap(),
113112
),
114113
})
115114
}

modules/stake_delta_filter/src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,15 @@ mod test {
430430
}
431431

432432
pub fn script_to_hash<const N: usize>(pallas_hash: ScriptHash) -> Hash<N> {
433-
Hash::try_from(pallas_hash.as_ref()).unwrap()
433+
pallas_hash.as_ref().try_into().unwrap()
434434
}
435435

436436
pub fn stake_to_hash<const N: usize>(pallas_hash: StakeKeyHash) -> Hash<N> {
437-
Hash::try_from(pallas_hash.as_ref()).unwrap()
437+
pallas_hash.as_ref().try_into().unwrap()
438438
}
439439

440440
pub fn payment_to_hash<const N: usize>(pallas_hash: PaymentKeyHash) -> Hash<N> {
441-
Hash::try_from(pallas_hash.as_ref()).unwrap()
441+
pallas_hash.as_ref().try_into().unwrap()
442442
}
443443

444444
/// Derive our Address from a Pallas address

0 commit comments

Comments
 (0)