Skip to content

Commit 727ffee

Browse files
committed
Refactor: Replace VRFKey with VrfKeyHash across modules for consistency, update associated type aliases, imports, and serialization logic.
1 parent a7cfb00 commit 727ffee

File tree

15 files changed

+55
-69
lines changed

15 files changed

+55
-69
lines changed

codec/src/map_parameters.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ pub fn to_pool_id(pallas_hash: &pallas_primitives::Hash<28>) -> PoolId {
6262
}
6363

6464
/// Convert a Pallas Hash<32> reference to an Acropolis VRFKey
65-
pub fn to_vrf_key(pallas_hash: &pallas_primitives::Hash<32>) -> VRFKey {
66-
VRFKey::try_from(pallas_hash.as_ref()).unwrap()
65+
pub fn to_vrf_key(pallas_hash: &pallas_primitives::Hash<32>) -> VrfKeyHash {
66+
VrfKeyHash::try_from(pallas_hash.as_ref()).unwrap()
6767
}
6868

6969
/// Convert a Pallas Bytes reference to an Acropolis Hash<N>
@@ -89,20 +89,20 @@ pub fn map_address(address: &addresses::Address) -> Result<Address> {
8989

9090
payment: match shelley_address.payment() {
9191
addresses::ShelleyPaymentPart::Key(hash) => {
92-
ShelleyAddressPaymentPart::PaymentKeyHash(hash.to_vec().try_into().unwrap())
92+
ShelleyAddressPaymentPart::PaymentKeyHash(to_hash(hash))
9393
}
9494
addresses::ShelleyPaymentPart::Script(hash) => {
95-
ShelleyAddressPaymentPart::ScriptHash(hash.to_vec().try_into().unwrap())
95+
ShelleyAddressPaymentPart::ScriptHash(to_hash(hash))
9696
}
9797
},
9898

9999
delegation: match shelley_address.delegation() {
100100
addresses::ShelleyDelegationPart::Null => ShelleyAddressDelegationPart::None,
101101
addresses::ShelleyDelegationPart::Key(hash) => {
102-
ShelleyAddressDelegationPart::StakeKeyHash(hash.to_vec().try_into().unwrap())
102+
ShelleyAddressDelegationPart::StakeKeyHash(to_hash(hash))
103103
}
104104
addresses::ShelleyDelegationPart::Script(hash) => {
105-
ShelleyAddressDelegationPart::ScriptHash(hash.to_vec().try_into().unwrap())
105+
ShelleyAddressDelegationPart::ScriptHash(to_hash(hash))
106106
}
107107
addresses::ShelleyDelegationPart::Pointer(pointer) => {
108108
ShelleyAddressDelegationPart::Pointer(ShelleyAddressPointer {

common/src/address.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,19 @@ 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+
214218
let payment_part = match (header >> 4) & 0x01 {
215-
0 => ShelleyAddressPaymentPart::PaymentKeyHash(
216-
data[1..29].try_into().map_err(|_| anyhow!("Invalid payment key hash size"))?,
217-
),
218-
1 => ShelleyAddressPaymentPart::ScriptHash(
219-
data[1..29].try_into().map_err(|_| anyhow!("Invalid script hash size"))?,
220-
),
219+
0 => ShelleyAddressPaymentPart::PaymentKeyHash(to_28_bytes(&data[1..29])?),
220+
1 => ShelleyAddressPaymentPart::ScriptHash(to_28_bytes(&data[1..29])?),
221221
_ => panic!(),
222222
};
223223

224224
let delegation_part = match (header >> 5) & 0x03 {
225-
0 => ShelleyAddressDelegationPart::StakeKeyHash(
226-
data[29..57].try_into().map_err(|_| anyhow!("Invalid stake key hash size"))?,
227-
),
228-
1 => ShelleyAddressDelegationPart::ScriptHash(
229-
data[29..57]
230-
.try_into()
231-
.map_err(|_| anyhow!("Invalid delegation script hash size"))?,
232-
),
225+
0 => ShelleyAddressDelegationPart::StakeKeyHash(to_28_bytes(&data[29..57])?),
226+
1 => ShelleyAddressDelegationPart::ScriptHash(to_28_bytes(&data[29..57])?),
233227
2 => {
234228
let mut decoder = VarIntDecoder::new(&data[29..]);
235229
let slot = decoder.read()?;

common/src/hash.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,6 @@ impl<'a, C, const BYTES: usize> minicbor::Decode<'a, C> for Hash<BYTES> {
262262
}
263263
}
264264

265-
// Type aliases for common hash sizes in Cardano
266-
/// A 28-byte hash used for scripts in Cardano addresses.
267-
pub type ScriptHash = Hash<28>;
268-
269-
/// A 28-byte hash of an address key in Cardano.
270-
pub type AddrKeyhash = Hash<28>;
271-
272265
/// Declares a type alias for a hash with optional documentation.
273266
///
274267
/// # Examples

common/src/queries/blocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
queries::misc::Order,
33
serialization::{Bech32Conversion, Bech32WithHrp},
4-
Address, BlockHash, GenesisDelegate, HeavyDelegate, KeyHash, TxHash, TxIdentifier, VRFKey,
4+
Address, BlockHash, GenesisDelegate, HeavyDelegate, KeyHash, TxHash, TxIdentifier, VrfKeyHash,
55
};
66
use cryptoxide::hashing::blake2b::Blake2b;
77
use serde::ser::{Serialize, SerializeStruct, Serializer};
@@ -114,7 +114,7 @@ pub struct BlockInfo {
114114
pub tx_count: u64,
115115
pub output: Option<u64>,
116116
pub fees: Option<u64>,
117-
pub block_vrf: Option<VRFKey>,
117+
pub block_vrf: Option<VrfKeyHash>,
118118
pub op_cert: Option<KeyHash>,
119119
pub op_cert_counter: Option<u64>,
120120
pub previous_block: Option<BlockHash>,

common/src/snapshot/pool_params.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
// limitations under the License.
1414

1515
use super::streaming_snapshot::{
16-
cbor, AddrKeyhash, Coin, Nullable, PoolMetadata, Relay, RewardAccount, Set, UnitInterval,
16+
cbor, Coin, Nullable, PoolMetadata, Relay, RewardAccount, Set, UnitInterval,
1717
};
18-
use crate::{PoolId, VRFKey};
18+
use crate::{PoolId, VrfKeyHash};
19+
use crate::types::AddrKeyhash;
1920

2021
#[derive(Debug, Clone, PartialEq, Eq)]
2122
pub struct PoolParams {
2223
pub id: PoolId,
23-
pub vrf: VRFKey,
24+
pub vrf: VrfKeyHash,
2425
pub pledge: Coin,
2526
pub cost: Coin,
2627
pub margin: UnitInterval,

common/src/snapshot/streaming_snapshot.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::fs::File;
2929
use std::io::{Read, Seek, SeekFrom};
3030
use tracing::info;
3131

32-
pub use crate::hash::{AddrKeyhash, Hash, ScriptHash};
32+
pub use crate::hash::Hash;
3333
pub use crate::stake_addresses::{AccountState, StakeAddressState};
3434
pub use crate::StakeCredential;
3535

@@ -74,7 +74,7 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for StakeCredential {
7474
Ok(StakeCredential::ScriptHash(key_hash))
7575
}
7676
1 => {
77-
// AddrKeyHash variant (second in enum) - decode bytes directly
77+
// AddrKeyHash variant (second in enum) - decodes bytes directly
7878
let bytes = d.bytes()?;
7979
let key_hash = KeyHash::try_from(bytes).map_err(|_| {
8080
minicbor::decode::Error::message(
@@ -307,6 +307,8 @@ impl<'b, C> minicbor::Decode<'b, C> for Account {
307307
// Type aliases for pool_params compatibility
308308
// -----------------------------------------------------------------------------
309309

310+
pub use crate::types::AddrKeyhash;
311+
pub use crate::types::ScriptHash;
310312
use crate::{KeyHash, PoolId};
311313
/// Alias minicbor as cbor for pool_params module
312314
pub use minicbor as cbor;
@@ -1086,12 +1088,8 @@ impl StreamingSnapshotParser {
10861088
// Convert DRep delegation from StrictMaybe<DRep> to Option<DRepChoice>
10871089
let delegated_drep = match &account.drep {
10881090
StrictMaybe::Just(drep) => Some(match drep {
1089-
DRep::Key(hash) => crate::DRepChoice::Key(
1090-
KeyHash::try_from(hash.as_ref().to_vec()).unwrap(),
1091-
),
1092-
DRep::Script(hash) => crate::DRepChoice::Script(
1093-
KeyHash::try_from(hash.as_ref().to_vec()).unwrap(),
1094-
),
1091+
DRep::Key(hash) => crate::DRepChoice::Key(*hash),
1092+
DRep::Script(hash) => crate::DRepChoice::Script(*hash),
10951093
DRep::Abstain => crate::DRepChoice::Abstain,
10961094
DRep::NoConfidence => crate::DRepChoice::NoConfidence,
10971095
}),
@@ -1456,12 +1454,8 @@ impl StreamingSnapshotParser {
14561454
// Convert DRep delegation from StrictMaybe<DRep> to Option<DRepChoice>
14571455
let delegated_drep = match &account.drep {
14581456
StrictMaybe::Just(drep) => Some(match drep {
1459-
DRep::Key(hash) => crate::DRepChoice::Key(
1460-
KeyHash::try_from(hash.as_ref().to_vec()).unwrap(),
1461-
),
1462-
DRep::Script(hash) => crate::DRepChoice::Script(
1463-
KeyHash::try_from(hash.as_ref().to_vec()).unwrap(),
1464-
),
1457+
DRep::Key(hash) => crate::DRepChoice::Key(*hash),
1458+
DRep::Script(hash) => crate::DRepChoice::Script(*hash),
14651459
DRep::Abstain => crate::DRepChoice::Abstain,
14661460
DRep::NoConfidence => crate::DRepChoice::NoConfidence,
14671461
}),

common/src/types.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// We don't use these types in the acropolis_common crate itself
33
#![allow(dead_code)]
44

5-
use crate::hash::{AddrKeyhash, Hash, ScriptHash};
5+
use crate::hash::Hash;
66
use crate::{
77
address::{Address, ShelleyAddress, StakeAddress},
88
declare_hash_newtype_with_bech32, declare_hash_type, declare_hash_type_with_bech32,
@@ -437,12 +437,15 @@ impl Default for UTXODelta {
437437

438438
pub type KeyHash = Hash<28>;
439439

440+
pub type ScriptHash = KeyHash;
441+
pub type AddrKeyhash = KeyHash;
442+
440443
/// Script identifier
441444
pub type GenesisKeyhash = Vec<u8>;
442445

443446
declare_hash_type!(BlockHash, 32);
444447
declare_hash_type!(TxHash, 32);
445-
declare_hash_newtype_with_bech32!(VRFKey, 32, "vrf_vk");
448+
declare_hash_newtype_with_bech32!(VrfKeyHash, 32, "vrf_vk");
446449
declare_hash_newtype_with_bech32!(PoolId, 28, "pool");
447450
declare_hash_newtype_with_bech32!(DrepKey, 28, "drep");
448451
declare_hash_type_with_bech32!(DrepScriptKey, 28, "drep_script");
@@ -844,7 +847,7 @@ pub struct PoolRegistration {
844847
/// VRF key hash
845848
#[serde_as(as = "Hex")]
846849
#[n(1)]
847-
pub vrf_key_hash: VRFKey,
850+
pub vrf_key_hash: VrfKeyHash,
848851

849852
/// Pledged Ada
850853
#[n(2)]
@@ -989,7 +992,7 @@ pub struct GenesisKeyDelegation {
989992
pub genesis_delegate_hash: KeyHash,
990993

991994
/// VRF key hash
992-
pub vrf_key_hash: VRFKey,
995+
pub vrf_key_hash: VrfKeyHash,
993996
}
994997

995998
/// Source of a MIR

modules/accounts_state/src/spo_distribution_store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use acropolis_common::hash::AddrKeyhash;
3+
use acropolis_common::types::AddrKeyhash;
44
use acropolis_common::PoolId;
55
use anyhow::Result;
66
use fjall::{Config, Keyspace, PartitionCreateOptions};
@@ -224,7 +224,7 @@ impl SPDDStore {
224224
mod tests {
225225
use super::*;
226226
use acropolis_common::crypto::keyhash_224;
227-
use acropolis_common::hash::AddrKeyhash;
227+
use acropolis_common::types::AddrKeyhash;
228228
use acropolis_common::PoolId;
229229

230230
const DB_PATH: &str = "spdd_db";

modules/accounts_state/src/state.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ mod tests {
996996
Anchor, Committee, Constitution, CostModel, DRepVotingThresholds, NetworkId,
997997
PoolVotingThresholds, Pot, PotDelta, Ratio, Registration, StakeAddress, StakeAddressDelta,
998998
StakeAndVoteDelegation, StakeCredential, StakeRegistrationAndStakeAndVoteDelegation,
999-
StakeRegistrationAndVoteDelegation, VRFKey, VoteDelegation, Withdrawal,
999+
StakeRegistrationAndVoteDelegation, VrfKeyHash, VoteDelegation, Withdrawal,
10001000
};
10011001

10021002
// Helper to create a StakeAddress from a byte slice
@@ -1017,8 +1017,8 @@ mod tests {
10171017
keyhash_224(bytes)
10181018
}
10191019

1020-
fn test_vrf_keyhash(byte: u8) -> VRFKey {
1021-
VRFKey::new(keyhash_256(&vec![byte]))
1020+
fn test_vrf_keyhash(byte: u8) -> VrfKeyHash {
1021+
VrfKeyHash::new(keyhash_256(&vec![byte]))
10221022
}
10231023

10241024
const STAKE_KEY_HASH: [u8; 3] = [0x99, 0x0f, 0x00];

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, VRFKey,
15+
BechOrdAddress, BlockHash, GenesisDelegate, HeavyDelegate, TxHash, VrfKeyHash,
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| VRFKey::try_from(key).ok().unwrap()),
416+
block_vrf: header.vrf_vkey().map(|key| VrfKeyHash::try_from(key).ok().unwrap()),
417417
op_cert,
418418
op_cert_counter,
419419
previous_block: header.previous_hash().map(|h| BlockHash::new(*h)),

0 commit comments

Comments
 (0)