Skip to content

Commit 0b69ae2

Browse files
authored
Merge pull request #304 from input-output-hk/lowhung/unify-hash-types
Refactor: Leverage new Hash macros + Hash type instead of previous KeyHash / vec implementation
2 parents dc556bf + dc38a71 commit 0b69ae2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+873
-654
lines changed

codec/src/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use acropolis_common::{
2-
GenesisDelegate, HeavyDelegate, crypto::keyhash_224, queries::blocks::BlockIssuer,
2+
GenesisDelegate, HeavyDelegate, PoolId, crypto::keyhash_224, queries::blocks::BlockIssuer,
33
};
44
use pallas_primitives::byron::BlockSig::DlgSig;
55
use pallas_traverse::MultiEraHeader;
66
use std::collections::HashMap;
77

88
pub fn map_to_block_issuer(
99
header: &MultiEraHeader,
10-
byron_heavy_delegates: &HashMap<Vec<u8>, HeavyDelegate>,
11-
shelley_genesis_delegates: &HashMap<Vec<u8>, GenesisDelegate>,
10+
byron_heavy_delegates: &HashMap<PoolId, HeavyDelegate>,
11+
shelley_genesis_delegates: &HashMap<PoolId, GenesisDelegate>,
1212
) -> Option<BlockIssuer> {
1313
match header.issuer_vkey() {
1414
Some(vkey) => match header {

codec/src/map_parameters.rs

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use pallas::ledger::{
1212
*,
1313
};
1414

15+
use acropolis_common::hash::Hash;
1516
use acropolis_common::{
1617
protocol_params::{Nonce, NonceVariant, ProtocolVersion},
1718
rational_number::RationalNumber,
@@ -28,6 +29,34 @@ pub fn map_network(network: addresses::Network) -> Result<NetworkId> {
2829
}
2930
}
3031

32+
/// Convert a Pallas Hash reference to an Acropolis Hash (owned)
33+
/// Works for any hash size N
34+
pub fn to_hash<const N: usize>(pallas_hash: &pallas_primitives::Hash<N>) -> Hash<N> {
35+
Hash::try_from(pallas_hash.as_ref()).unwrap()
36+
}
37+
38+
/// Convert a Pallas Hash reference to an Acropolis Hash (owned)
39+
/// Works for any hash size N
40+
pub fn genesis_to_hash(pallas_hash: &pallas_primitives::Genesishash) -> Hash<32> {
41+
Hash::try_from(pallas_hash.as_ref()).unwrap()
42+
}
43+
44+
/// Convert a Pallas Hash reference to an Acropolis Hash (owned)
45+
/// Works for any hash size N
46+
pub fn genesis_delegate_to_hash(pallas_hash: &pallas_primitives::GenesisDelegateHash) -> PoolId {
47+
PoolId::try_from(pallas_hash.as_ref()).unwrap()
48+
}
49+
50+
/// Convert a Pallas Hash<28> reference to an Acropolis PoolId
51+
pub fn to_pool_id(pallas_hash: &pallas_primitives::Hash<28>) -> PoolId {
52+
to_hash(pallas_hash).into()
53+
}
54+
55+
/// Convert a Pallas Hash<32> reference to an Acropolis VRFKey
56+
pub fn to_vrf_key(pallas_hash: &pallas_primitives::Hash<32>) -> VrfKeyHash {
57+
VrfKeyHash::try_from(pallas_hash.as_ref()).unwrap()
58+
}
59+
3160
/// Derive our Address from a Pallas address
3261
// This is essentially a 1:1 mapping but makes the Message definitions independent
3362
// of Pallas
@@ -42,20 +71,20 @@ pub fn map_address(address: &addresses::Address) -> Result<Address> {
4271

4372
payment: match shelley_address.payment() {
4473
addresses::ShelleyPaymentPart::Key(hash) => {
45-
ShelleyAddressPaymentPart::PaymentKeyHash(hash.to_vec())
74+
ShelleyAddressPaymentPart::PaymentKeyHash(to_hash(hash))
4675
}
4776
addresses::ShelleyPaymentPart::Script(hash) => {
48-
ShelleyAddressPaymentPart::ScriptHash(hash.to_vec())
77+
ShelleyAddressPaymentPart::ScriptHash(to_hash(hash))
4978
}
5079
},
5180

5281
delegation: match shelley_address.delegation() {
5382
addresses::ShelleyDelegationPart::Null => ShelleyAddressDelegationPart::None,
5483
addresses::ShelleyDelegationPart::Key(hash) => {
55-
ShelleyAddressDelegationPart::StakeKeyHash(hash.to_vec())
84+
ShelleyAddressDelegationPart::StakeKeyHash(to_hash(hash))
5685
}
5786
addresses::ShelleyDelegationPart::Script(hash) => {
58-
ShelleyAddressDelegationPart::ScriptHash(hash.to_vec())
87+
ShelleyAddressDelegationPart::ScriptHash(to_hash(hash))
5988
}
6089
addresses::ShelleyDelegationPart::Pointer(pointer) => {
6190
ShelleyAddressDelegationPart::Pointer(ShelleyAddressPointer {
@@ -70,8 +99,8 @@ pub fn map_address(address: &addresses::Address) -> Result<Address> {
7099
addresses::Address::Stake(stake_address) => Ok(Address::Stake(StakeAddress {
71100
network: map_network(stake_address.network())?,
72101
credential: match stake_address.payload() {
73-
addresses::StakePayload::Stake(hash) => StakeCredential::AddrKeyHash(hash.to_vec()),
74-
addresses::StakePayload::Script(hash) => StakeCredential::ScriptHash(hash.to_vec()),
102+
addresses::StakePayload::Stake(hash) => StakeCredential::AddrKeyHash(to_hash(hash)),
103+
addresses::StakePayload::Script(hash) => StakeCredential::ScriptHash(to_hash(hash)),
75104
},
76105
})),
77106
}
@@ -81,10 +110,10 @@ pub fn map_address(address: &addresses::Address) -> Result<Address> {
81110
pub fn map_stake_credential(cred: &PallasStakeCredential) -> StakeCredential {
82111
match cred {
83112
PallasStakeCredential::AddrKeyhash(key_hash) => {
84-
StakeCredential::AddrKeyHash(key_hash.to_vec())
113+
StakeCredential::AddrKeyHash(to_hash(key_hash))
85114
}
86115
PallasStakeCredential::ScriptHash(script_hash) => {
87-
StakeCredential::ScriptHash(script_hash.to_vec())
116+
StakeCredential::ScriptHash(to_hash(script_hash))
88117
}
89118
}
90119
}
@@ -93,10 +122,10 @@ pub fn map_stake_credential(cred: &PallasStakeCredential) -> StakeCredential {
93122
pub fn map_stake_address(cred: &PallasStakeCredential, network_id: NetworkId) -> StakeAddress {
94123
let payload = match cred {
95124
PallasStakeCredential::AddrKeyhash(key_hash) => {
96-
StakeCredential::AddrKeyHash(key_hash.to_vec())
125+
StakeCredential::AddrKeyHash(to_hash(key_hash))
97126
}
98127
PallasStakeCredential::ScriptHash(script_hash) => {
99-
StakeCredential::ScriptHash(script_hash.to_vec())
128+
StakeCredential::ScriptHash(to_hash(script_hash))
100129
}
101130
};
102131

@@ -106,8 +135,8 @@ pub fn map_stake_address(cred: &PallasStakeCredential, network_id: NetworkId) ->
106135
/// Map a Pallas DRep to our DRepChoice
107136
pub fn map_drep(drep: &conway::DRep) -> DRepChoice {
108137
match drep {
109-
conway::DRep::Key(key_hash) => DRepChoice::Key(key_hash.to_vec()),
110-
conway::DRep::Script(script_hash) => DRepChoice::Script(script_hash.to_vec()),
138+
conway::DRep::Key(key_hash) => DRepChoice::Key(to_hash(key_hash)),
139+
conway::DRep::Script(script_hash) => DRepChoice::Script(to_hash(script_hash)),
111140
conway::DRep::Abstain => DRepChoice::Abstain,
112141
conway::DRep::NoConfidence => DRepChoice::NoConfidence,
113142
}
@@ -169,7 +198,7 @@ pub fn map_nullable_gov_action_id(
169198
fn map_constitution(constitution: &conway::Constitution) -> Constitution {
170199
Constitution {
171200
anchor: map_anchor(&constitution.anchor),
172-
guardrail_script: map_nullable(|x| x.to_vec(), &constitution.guardrail_script),
201+
guardrail_script: map_nullable(|x| to_hash(x), &constitution.guardrail_script),
173202
}
174203
}
175204

@@ -231,7 +260,7 @@ pub fn map_certificate(
231260
alonzo::Certificate::StakeDelegation(cred, pool_key_hash) => Ok(TxCertificateWithPos {
232261
cert: TxCertificate::StakeDelegation(StakeDelegation {
233262
stake_address: map_stake_address(cred, network_id),
234-
operator: pool_key_hash.to_vec(),
263+
operator: to_pool_id(pool_key_hash),
235264
}),
236265
tx_identifier,
237266
cert_index: cert_index.try_into().unwrap(),
@@ -248,8 +277,8 @@ pub fn map_certificate(
248277
pool_metadata,
249278
} => Ok(TxCertificateWithPos {
250279
cert: TxCertificate::PoolRegistration(PoolRegistration {
251-
operator: operator.to_vec(),
252-
vrf_key_hash: vrf_keyhash.to_vec(),
280+
operator: to_pool_id(operator),
281+
vrf_key_hash: to_vrf_key(vrf_keyhash),
253282
pledge: *pledge,
254283
cost: *cost,
255284
margin: Ratio {
@@ -261,7 +290,7 @@ pub fn map_certificate(
261290
.iter()
262291
.map(|v| {
263292
StakeAddress::new(
264-
StakeCredential::AddrKeyHash(v.to_vec()),
293+
StakeCredential::AddrKeyHash(to_hash(v)),
265294
network_id.clone(),
266295
)
267296
})
@@ -280,7 +309,7 @@ pub fn map_certificate(
280309
}),
281310
alonzo::Certificate::PoolRetirement(pool_key_hash, epoch) => Ok(TxCertificateWithPos {
282311
cert: TxCertificate::PoolRetirement(PoolRetirement {
283-
operator: pool_key_hash.to_vec(),
312+
operator: to_pool_id(pool_key_hash),
284313
epoch: *epoch,
285314
}),
286315
tx_identifier,
@@ -292,9 +321,9 @@ pub fn map_certificate(
292321
vrf_key_hash,
293322
) => Ok(TxCertificateWithPos {
294323
cert: TxCertificate::GenesisKeyDelegation(GenesisKeyDelegation {
295-
genesis_hash: genesis_hash.to_vec(),
296-
genesis_delegate_hash: genesis_delegate_hash.to_vec(),
297-
vrf_key_hash: vrf_key_hash.to_vec(),
324+
genesis_hash: genesis_to_hash(genesis_hash),
325+
genesis_delegate_hash: genesis_delegate_to_hash(genesis_delegate_hash),
326+
vrf_key_hash: to_vrf_key(vrf_key_hash),
298327
}),
299328
tx_identifier,
300329
cert_index: cert_index as u64,
@@ -348,7 +377,7 @@ pub fn map_certificate(
348377
Ok(TxCertificateWithPos {
349378
cert: TxCertificate::StakeDelegation(StakeDelegation {
350379
stake_address: map_stake_address(cred, network_id),
351-
operator: pool_key_hash.to_vec(),
380+
operator: to_pool_id(pool_key_hash),
352381
}),
353382
tx_identifier,
354383
cert_index: cert_index.try_into().unwrap(),
@@ -368,8 +397,8 @@ pub fn map_certificate(
368397
pool_metadata,
369398
} => Ok(TxCertificateWithPos {
370399
cert: TxCertificate::PoolRegistration(PoolRegistration {
371-
operator: operator.to_vec(),
372-
vrf_key_hash: vrf_keyhash.to_vec(),
400+
operator: to_pool_id(operator),
401+
vrf_key_hash: to_vrf_key(vrf_keyhash),
373402
pledge: *pledge,
374403
cost: *cost,
375404
margin: Ratio {
@@ -388,7 +417,7 @@ pub fn map_certificate(
388417
.into_iter()
389418
.map(|v| {
390419
StakeAddress::new(
391-
StakeCredential::AddrKeyHash(v.to_vec()),
420+
StakeCredential::AddrKeyHash(to_hash(v)),
392421
network_id.clone(),
393422
)
394423
})
@@ -408,7 +437,7 @@ pub fn map_certificate(
408437
conway::Certificate::PoolRetirement(pool_key_hash, epoch) => {
409438
Ok(TxCertificateWithPos {
410439
cert: TxCertificate::PoolRetirement(PoolRetirement {
411-
operator: pool_key_hash.to_vec(),
440+
operator: to_pool_id(pool_key_hash),
412441
epoch: *epoch,
413442
}),
414443
tx_identifier,
@@ -447,7 +476,7 @@ pub fn map_certificate(
447476
Ok(TxCertificateWithPos {
448477
cert: TxCertificate::StakeAndVoteDelegation(StakeAndVoteDelegation {
449478
stake_address: map_stake_address(cred, network_id),
450-
operator: pool_key_hash.to_vec(),
479+
operator: to_pool_id(pool_key_hash),
451480
drep: map_drep(drep),
452481
}),
453482
tx_identifier,
@@ -460,7 +489,7 @@ pub fn map_certificate(
460489
cert: TxCertificate::StakeRegistrationAndDelegation(
461490
StakeRegistrationAndDelegation {
462491
stake_address: map_stake_address(cred, network_id),
463-
operator: pool_key_hash.to_vec(),
492+
operator: to_pool_id(pool_key_hash),
464493
deposit: *coin,
465494
},
466495
),
@@ -486,7 +515,7 @@ pub fn map_certificate(
486515
cert: TxCertificate::StakeRegistrationAndStakeAndVoteDelegation(
487516
StakeRegistrationAndStakeAndVoteDelegation {
488517
stake_address: map_stake_address(cred, network_id),
489-
operator: pool_key_hash.to_vec(),
518+
operator: to_pool_id(pool_key_hash),
490519
drep: map_drep(drep),
491520
deposit: *coin,
492521
},
@@ -881,14 +910,14 @@ pub fn map_governance_proposals_procedures(
881910
fn map_voter(voter: &conway::Voter) -> Voter {
882911
match voter {
883912
conway::Voter::ConstitutionalCommitteeKey(key_hash) => {
884-
Voter::ConstitutionalCommitteeKey(key_hash.to_vec())
913+
Voter::ConstitutionalCommitteeKey(to_hash(key_hash).into())
885914
}
886915
conway::Voter::ConstitutionalCommitteeScript(script_hash) => {
887-
Voter::ConstitutionalCommitteeScript(script_hash.to_vec())
916+
Voter::ConstitutionalCommitteeScript(to_hash(script_hash).into())
888917
}
889-
conway::Voter::DRepKey(addr_key_hash) => Voter::DRepKey(addr_key_hash.to_vec()),
890-
conway::Voter::DRepScript(script_hash) => Voter::DRepScript(script_hash.to_vec()),
891-
conway::Voter::StakePoolKey(key_hash) => Voter::StakePoolKey(key_hash.to_vec()),
918+
conway::Voter::DRepKey(addr_key_hash) => Voter::DRepKey(to_hash(addr_key_hash).into()),
919+
conway::Voter::DRepScript(script_hash) => Voter::DRepScript(to_hash(script_hash).into()),
920+
conway::Voter::StakePoolKey(key_hash) => Voter::StakePoolKey(to_pool_id(key_hash)),
892921
}
893922
}
894923

0 commit comments

Comments
 (0)