Skip to content

Commit 87b2ea6

Browse files
committed
Refactor: Replace PoolId::new and KeyHash::new with .into() for hash conversions across modules
1 parent b2cd604 commit 87b2ea6

File tree

10 files changed

+59
-49
lines changed

10 files changed

+59
-49
lines changed

codec/src/map_parameters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn to_hash_opt<const N: usize>(
5858

5959
/// Convert a Pallas Hash<28> reference to an Acropolis PoolId
6060
pub fn to_pool_id(pallas_hash: &pallas_primitives::Hash<28>) -> PoolId {
61-
PoolId::from(to_hash(pallas_hash))
61+
to_hash(pallas_hash).into()
6262
}
6363

6464
/// Convert a Pallas Hash<32> reference to an Acropolis VRFKey
@@ -72,7 +72,7 @@ pub fn bytes_to_hash<const N: usize>(bytes: &pallas_primitives::Bytes) -> Hash<N
7272
let slice: &[u8] = bytes.as_ref();
7373
let mut array = [0u8; N];
7474
array.copy_from_slice(&slice[..N]);
75-
Hash::from(array)
75+
array.into()
7676
}
7777

7878
/// Derive our Address from a Pallas address

common/src/address.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub enum ShelleyAddressPaymentPart {
105105

106106
impl Default for ShelleyAddressPaymentPart {
107107
fn default() -> Self {
108-
Self::PaymentKeyHash(KeyHash::new([0u8; 28]))
108+
Self::PaymentKeyHash([0u8; 28].into())
109109
}
110110
}
111111

@@ -211,19 +211,31 @@ impl ShelleyAddress {
211211

212212
let header = *header;
213213

214-
let to_28_bytes = |slice: &[u8]| -> Result<KeyHash> {
215-
slice.try_into().map_err(|_| anyhow!("Invalid hash"))
216-
};
217-
218214
let payment_part = match (header >> 4) & 0x01 {
219-
0 => ShelleyAddressPaymentPart::PaymentKeyHash(to_28_bytes(&data[1..29])?),
220-
1 => ShelleyAddressPaymentPart::ScriptHash(to_28_bytes(&data[1..29])?),
215+
0 => ShelleyAddressPaymentPart::PaymentKeyHash(
216+
data[1..29]
217+
.try_into()
218+
.map_err(|e| anyhow!("Failed to parse payment key hash: {}", e))?,
219+
),
220+
1 => ShelleyAddressPaymentPart::ScriptHash(
221+
data[1..29]
222+
.try_into()
223+
.map_err(|e| anyhow!("Failed to parse payment script hash: {}", e))?,
224+
),
221225
_ => panic!(),
222226
};
223227

224228
let delegation_part = match (header >> 5) & 0x03 {
225-
0 => ShelleyAddressDelegationPart::StakeKeyHash(to_28_bytes(&data[29..57])?),
226-
1 => ShelleyAddressDelegationPart::ScriptHash(to_28_bytes(&data[29..57])?),
229+
0 => ShelleyAddressDelegationPart::StakeKeyHash(
230+
data[29..57]
231+
.try_into()
232+
.map_err(|e| anyhow!("Failed to parse stake key hash: {}", e))?,
233+
),
234+
1 => ShelleyAddressDelegationPart::ScriptHash(
235+
data[29..57]
236+
.try_into()
237+
.map_err(|e| anyhow!("Failed to parse stake script hash: {}", e))?,
238+
),
227239
2 => {
228240
let mut decoder = VarIntDecoder::new(&data[29..]);
229241
let slot = decoder.read()?;
@@ -301,24 +313,24 @@ impl ShelleyAddress {
301313
ShelleyAddressDelegationPart::None => {
302314
let header = build_header(3);
303315
data.push(header);
304-
data.extend(payment_hash.as_ref());
316+
data.extend_from_slice(payment_hash.as_ref());
305317
}
306318
ShelleyAddressDelegationPart::StakeKeyHash(hash) => {
307319
let header = build_header(0);
308320
data.push(header);
309-
data.extend(payment_hash.as_ref());
310-
data.extend(hash.as_ref());
321+
data.extend_from_slice(payment_hash.as_ref());
322+
data.extend_from_slice(hash.as_ref());
311323
}
312324
ShelleyAddressDelegationPart::ScriptHash(hash) => {
313325
let header = build_header(1);
314326
data.push(header);
315-
data.extend(payment_hash.as_ref());
316-
data.extend(hash.as_ref());
327+
data.extend_from_slice(payment_hash.as_ref());
328+
data.extend_from_slice(hash.as_ref());
317329
}
318330
ShelleyAddressDelegationPart::Pointer(pointer) => {
319331
let header = build_header(2);
320332
data.push(header);
321-
data.extend(payment_hash.as_ref());
333+
data.extend_from_slice(payment_hash.as_ref());
322334

323335
let mut encoder = VarIntEncoder::new();
324336
encoder.push(pointer.slot);
@@ -413,10 +425,12 @@ impl StakeAddress {
413425

414426
let credential = match (header >> 4) & 0x0Fu8 {
415427
0b1110 => StakeCredential::AddrKeyHash(
416-
data[1..].try_into().map_err(|_| anyhow!("Invalid key hash size"))?,
428+
data[1..].try_into().map_err(|e| anyhow!("Failed to parse key hash: {}", e))?,
417429
),
418430
0b1111 => StakeCredential::ScriptHash(
419-
data[1..].try_into().map_err(|_| anyhow!("Invalid script hash size"))?,
431+
data[1..]
432+
.try_into()
433+
.map_err(|e| anyhow!("Failed to parse script hash: {}", e))?,
420434
),
421435
_ => return Err(anyhow!("Unknown header {header} in stake address")),
422436
};
@@ -437,9 +451,9 @@ impl StakeAddress {
437451
NetworkId::Testnet => 0b0u8,
438452
};
439453

440-
let (stake_bits, stake_hash): (u8, &Vec<u8>) = match &self.credential {
441-
StakeCredential::AddrKeyHash(data) => (0b1110, &data.to_vec()),
442-
StakeCredential::ScriptHash(data) => (0b1111, &data.to_vec()),
454+
let (stake_bits, stake_hash) = match &self.credential {
455+
StakeCredential::AddrKeyHash(data) => (0b1110, data.as_ref()),
456+
StakeCredential::ScriptHash(data) => (0b1111, data.as_ref()),
443457
};
444458

445459
let mut data = vec![network_bits | (stake_bits << 4)];
@@ -526,7 +540,7 @@ impl Default for StakeAddress {
526540
fn default() -> Self {
527541
StakeAddress {
528542
network: NetworkId::Mainnet,
529-
credential: StakeCredential::AddrKeyHash(KeyHash::new([0u8; 28])),
543+
credential: StakeCredential::AddrKeyHash([0u8; 28].into()),
530544
}
531545
}
532546
}

common/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ mod tests {
20902090
let mut test_hash_bytes = [0u8; 28];
20912091
test_hash_bytes[0..4].copy_from_slice(&[1, 2, 3, 4]);
20922092
voting.votes.insert(
2093-
Voter::StakePoolKey(PoolId::new(Hash::new(test_hash_bytes))),
2093+
Voter::StakePoolKey(test_hash_bytes.into()),
20942094
SingleVoterVotes::default(),
20952095
);
20962096

modules/accounts_state/src/snapshot.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,21 @@ mod tests {
195195
use super::*;
196196
use acropolis_common::stake_addresses::StakeAddressState;
197197
use acropolis_common::NetworkId::Mainnet;
198-
use acropolis_common::{KeyHash, PoolId, StakeAddress, StakeCredential};
198+
use acropolis_common::{PoolId, StakeAddress, StakeCredential};
199199

200-
// Helper function to create stake addresses for testing
201200
fn create_test_stake_address(id: u8) -> StakeAddress {
202201
let mut hash = [0u8; 28];
203202
hash[0] = id;
204203
StakeAddress {
205204
network: Mainnet,
206-
credential: StakeCredential::AddrKeyHash(KeyHash::new(hash)),
205+
credential: StakeCredential::AddrKeyHash(hash.into()),
207206
}
208207
}
209208

210-
// Helper function to create SPO key hashes for testing
211209
fn create_test_spo_hash(id: u8) -> PoolId {
212-
let mut hash = vec![0u8; 28];
210+
let mut hash = [0u8; 28];
213211
hash[0] = id;
214-
PoolId::try_from(hash).unwrap()
212+
hash.into()
215213
}
216214

217215
#[test]

modules/accounts_state/src/spo_distribution_store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ fn encode_epoch_pool_prefix(epoch: u64, pool_id: &PoolId) -> Vec<u8> {
3232

3333
fn decode_key(key: &[u8]) -> Result<(u64, PoolId, AddrKeyhash)> {
3434
let epoch = u64::from_be_bytes(key[..EPOCH_LEN].try_into()?);
35-
let pool_id: PoolId = key[EPOCH_LEN..EPOCH_LEN + POOL_ID_LENGTH].try_into()?;
36-
let stake_key: AddrKeyhash = key[EPOCH_LEN + POOL_ID_LENGTH..].try_into()?;
35+
let pool_id = key[EPOCH_LEN..EPOCH_LEN + POOL_ID_LENGTH].try_into()?;
36+
let stake_key = key[EPOCH_LEN + POOL_ID_LENGTH..].try_into()?;
3737
Ok((epoch, pool_id, stake_key))
3838
}
3939

@@ -230,7 +230,7 @@ mod tests {
230230
const DB_PATH: &str = "spdd_db";
231231

232232
fn test_pool_hash(byte: u8) -> PoolId {
233-
PoolId::new(keyhash_224(&vec![byte]))
233+
keyhash_224(&vec![byte]).into()
234234
}
235235

236236
fn test_addr_hash(byte: u8) -> AddrKeyhash {

modules/accounts_state/src/state.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ mod tests {
10221022
}
10231023

10241024
fn test_vrf_keyhash(byte: u8) -> VrfKeyHash {
1025-
VrfKeyHash::new(keyhash_256(&vec![byte]))
1025+
keyhash_256(&vec![byte]).into()
10261026
}
10271027

10281028
const STAKE_KEY_HASH: [u8; 3] = [0x99, 0x0f, 0x00];
@@ -1078,8 +1078,8 @@ mod tests {
10781078
fn spdd_from_delegation_with_utxo_values_and_pledge() {
10791079
let mut state = State::default();
10801080

1081-
let spo1: PoolId = PoolId::new(test_keyhash(0x01));
1082-
let spo2: PoolId = PoolId::new(test_keyhash(0x02));
1081+
let spo1 = test_keyhash(0x01).into();
1082+
let spo2 = test_keyhash(0x02).into();
10831083

10841084
let vrf_key_hash_1 = test_vrf_keyhash(0x03);
10851085
let vrf_key_hash_2 = test_vrf_keyhash(0x04);
@@ -1090,7 +1090,7 @@ mod tests {
10901090
epoch: 1,
10911091
spos: vec![
10921092
PoolRegistration {
1093-
operator: spo1.clone(),
1093+
operator: spo1,
10941094
vrf_key_hash: vrf_key_hash_1,
10951095
pledge: 26,
10961096
cost: 0,
@@ -1104,7 +1104,7 @@ mod tests {
11041104
pool_metadata: None,
11051105
},
11061106
PoolRegistration {
1107-
operator: spo2.clone(),
1107+
operator: spo2,
11081108
vrf_key_hash: vrf_key_hash_2,
11091109
pledge: 47,
11101110
cost: 10,
@@ -1393,7 +1393,7 @@ mod tests {
13931393
// Get the KeyHash once
13941394
let spo1_hash = spo1.get_hash();
13951395
let drep_key_hash = test_keyhash_from_bytes(&DREP_HASH);
1396-
let pool_id_1 = PoolId::new(*spo1_hash);
1396+
let pool_id_1 = (*spo1_hash).into();
13971397

13981398
let certificates = vec![
13991399
// register the first two SPOs separately from their delegation

modules/epochs_state/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl State {
188188
self.last_block_time = block_info.timestamp;
189189
self.last_block_height = block_info.number;
190190
self.epoch_blocks += 1;
191-
let spo_id = PoolId::new(keyhash_224(issuer_vkey));
191+
let spo_id = PoolId::from(keyhash_224(issuer_vkey));
192192

193193
// Count one on this hash
194194
*(self.blocks_minted.entry(spo_id.clone()).or_insert(0)) += 1;

modules/spo_state/src/epochs_history.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ mod tests {
214214

215215
#[test]
216216
fn get_pool_history_returns_none_when_spo_is_not_found() {
217-
let key_hash = KeyHash::new([1; 28]);
217+
let key_hash = [1; 28].into();
218218
let epochs_history = EpochsHistoryState::new(save_history_store_config());
219219
let pool_history = epochs_history.get_pool_history(&key_hash);
220220
assert!(pool_history.is_none());
@@ -223,8 +223,8 @@ mod tests {
223223
#[test]
224224
fn get_pool_history_returns_data() {
225225
let epochs_history = EpochsHistoryState::new(save_history_store_config());
226-
let pool_id = PoolId::new(KeyHash::new([1; 28]));
227-
let spo_block_key_hash = PoolId::new(KeyHash::new([2; 28]));
226+
let pool_id = [1; 28].into();
227+
let spo_block_key_hash = [2; 28].into();
228228

229229
let block = new_block(2);
230230
let mut spdd_msg = new_spdd_message(1);

modules/spo_state/src/retired_pools_history.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ impl RetiredPoolsHistoryState {
6666
mod tests {
6767
use super::*;
6868
use crate::test_utils::*;
69-
use acropolis_common::KeyHash;
7069

7170
#[test]
7271
fn retired_pools_history_is_none_when_store_retired_pools_is_false() {
@@ -91,8 +90,8 @@ mod tests {
9190
let state = RetiredPoolsHistoryState::new(save_retired_pools_store_config());
9291

9392
let block = new_block(2);
94-
let spo_1 = PoolId::new(KeyHash::new([1; 28]));
95-
let spo_2 = PoolId::new(KeyHash::new([2; 28]));
93+
let spo_1 = [1; 28].into();
94+
let spo_2 = [2; 28].into();
9695
let retired_spos = vec![spo_1, spo_2];
9796
state.handle_deregistrations(&block, &retired_spos);
9897

modules/spo_state/src/state.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,6 @@ impl State {
668668
mod tests {
669669
use super::*;
670670
use crate::test_utils::*;
671-
use acropolis_common::hash::Hash;
672671
use acropolis_common::{
673672
state_history::{StateHistory, StateHistoryStore},
674673
PoolRetirement, Ratio, StakeAddress, TxCertificate, TxCertificateWithPos, TxIdentifier,
@@ -677,11 +676,11 @@ mod tests {
677676
use tokio::sync::Mutex;
678677

679678
fn test_pool_id(byte: u8) -> PoolId {
680-
PoolId::new(Hash::new([byte; 28]))
679+
[byte; 28].into()
681680
}
682681

683682
fn test_pool_id_from_bytes(bytes: &[u8]) -> PoolId {
684-
PoolId::new(keyhash_224(bytes))
683+
keyhash_224(bytes).into()
685684
}
686685

687686
fn default_pool_registration(

0 commit comments

Comments
 (0)