Skip to content

Commit 2faea48

Browse files
authored
Merge pull request #265 from input-output-hk/lowhung/163-replace-reward-account-with-stake-address-block-info-network-id
Add `NetworkId` into `BlockInfo`
2 parents 32045af + 39e0f13 commit 2faea48

File tree

21 files changed

+304
-295
lines changed

21 files changed

+304
-295
lines changed

common/src/address.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use crate::cip19::{VarIntDecoder, VarIntEncoder};
66
use crate::types::{KeyHash, ScriptHash};
7-
use crate::Credential;
7+
use crate::{Credential, NetworkId};
88
use anyhow::{anyhow, bail, Result};
99
use serde_with::{hex::Hex, serde_as};
10-
use std::borrow::Borrow;
1110
use std::fmt::{Display, Formatter};
12-
use std::hash::{Hash, Hasher};
1311

1412
/// a Byron-era address
1513
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -28,6 +26,15 @@ pub enum AddressNetwork {
2826
Test,
2927
}
3028

29+
impl From<NetworkId> for AddressNetwork {
30+
fn from(network: NetworkId) -> Self {
31+
match network {
32+
NetworkId::Mainnet => Self::Main,
33+
NetworkId::Testnet => Self::Test,
34+
}
35+
}
36+
}
37+
3138
impl Default for AddressNetwork {
3239
fn default() -> Self {
3340
Self::Main
@@ -201,7 +208,7 @@ impl StakeAddressPayload {
201208
}
202209

203210
/// A stake address
204-
#[derive(Debug, Clone, Eq, serde::Serialize, serde::Deserialize)]
211+
#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
205212
pub struct StakeAddress {
206213
/// Network id
207214
pub network: AddressNetwork,
@@ -211,7 +218,7 @@ pub struct StakeAddress {
211218
}
212219

213220
impl StakeAddress {
214-
pub fn new(network: AddressNetwork, payload: StakeAddressPayload) -> Self {
221+
pub fn new(payload: StakeAddressPayload, network: AddressNetwork) -> Self {
215222
StakeAddress { network, payload }
216223
}
217224

@@ -305,26 +312,6 @@ impl Display for StakeAddress {
305312
}
306313
}
307314

308-
impl Hash for StakeAddress {
309-
fn hash<H: Hasher>(&self, state: &mut H) {
310-
self.network.hash(state);
311-
std::mem::discriminant(&self.payload).hash(state);
312-
self.get_hash().hash(state);
313-
}
314-
}
315-
316-
impl Borrow<[u8]> for StakeAddress {
317-
fn borrow(&self) -> &[u8] {
318-
self.get_hash()
319-
}
320-
}
321-
322-
impl PartialEq for StakeAddress {
323-
fn eq(&self, other: &Self) -> bool {
324-
self.network == other.network && self.payload == other.payload
325-
}
326-
}
327-
328315
impl<C> minicbor::Encode<C> for StakeAddress {
329316
fn encode<W: minicbor::encode::Write>(
330317
&self,

common/src/queries/governance.rs

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

3-
use crate::{Anchor, Credential, DRepCredential, GovActionId, Lovelace, ProposalProcedure, TxHash, Vote, Voter, VotingProcedure};
3+
use crate::{Anchor, DRepCredential, GovActionId, Lovelace, ProposalProcedure, StakeAddress, TxHash, Vote, Voter, VotingProcedure};
44

55
pub const DEFAULT_DREPS_QUERY_TOPIC: (&str, &str) =
66
("drep-state-query-topic", "cardano.query.dreps");
@@ -58,12 +58,12 @@ pub struct DRepInfo {
5858
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
5959
pub struct DRepInfoWithDelegators {
6060
pub info: DRepInfo,
61-
pub delegators: Vec<Credential>,
61+
pub delegators: Vec<StakeAddress>,
6262
}
6363

6464
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
6565
pub struct DRepDelegatorAddresses {
66-
pub addresses: Vec<Credential>,
66+
pub addresses: Vec<StakeAddress>,
6767
}
6868

6969
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]

common/src/stake_addresses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,10 @@ mod tests {
528528

529529
fn create_stake_address(hash: &[u8]) -> StakeAddress {
530530
StakeAddress::new(
531-
AddressNetwork::Main,
532531
StakeAddressPayload::StakeKeyHash(
533532
hash.to_vec().try_into().expect("Invalid hash length"),
534533
),
534+
AddressNetwork::Main,
535535
)
536536
}
537537

common/src/types.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::{
66
address::{Address, ShelleyAddress, StakeAddress},
77
protocol_params,
88
rational_number::RationalNumber,
9-
AddressNetwork, StakeAddressPayload,
109
};
1110
use anyhow::{anyhow, bail, Error, Result};
1211
use bech32::{Bech32, Hrp};
@@ -26,6 +25,16 @@ pub enum NetworkId {
2625
Mainnet,
2726
}
2827

28+
impl From<String> for NetworkId {
29+
fn from(s: String) -> Self {
30+
match s.as_str() {
31+
"testnet" => NetworkId::Testnet,
32+
"mainnet" => NetworkId::Mainnet,
33+
_ => NetworkId::Mainnet,
34+
}
35+
}
36+
}
37+
2938
/// Protocol era
3039
#[derive(
3140
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
@@ -613,20 +622,6 @@ impl Credential {
613622

614623
pub type StakeCredential = Credential;
615624

616-
impl StakeCredential {
617-
pub fn to_stake_address(&self, network: Option<AddressNetwork>) -> StakeAddress {
618-
let payload = match self {
619-
StakeCredential::AddrKeyHash(hash) => StakeAddressPayload::StakeKeyHash(
620-
hash.clone().try_into().expect("Invalid hash length"),
621-
),
622-
StakeCredential::ScriptHash(hash) => StakeAddressPayload::ScriptHash(
623-
hash.clone().try_into().expect("Invalid hash length"),
624-
),
625-
};
626-
StakeAddress::new(network.unwrap_or(AddressNetwork::Main), payload)
627-
}
628-
}
629-
630625
/// Relay single host address
631626
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
632627
pub struct SingleHostAddr {
@@ -739,7 +734,7 @@ pub struct PoolRegistration {
739734
/// Pool owners by their key hash
740735
// #[serde_as(as = "Vec<Hex>")]
741736
#[n(6)]
742-
pub pool_owners: Vec<Credential>,
737+
pub pool_owners: Vec<StakeAddress>,
743738

744739
// Relays
745740
#[n(7)]
@@ -824,8 +819,8 @@ pub struct PoolEpochState {
824819
/// Stake delegation data
825820
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
826821
pub struct StakeDelegation {
827-
/// Stake credential
828-
pub credential: StakeCredential,
822+
/// Stake address
823+
pub stake_address: StakeAddress,
829824

830825
/// Pool ID to delegate to
831826
pub operator: KeyHash,
@@ -877,7 +872,7 @@ pub enum InstantaneousRewardSource {
877872
/// Target of a MIR
878873
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
879874
pub enum InstantaneousRewardTarget {
880-
StakeCredentials(Vec<(StakeCredential, i64)>),
875+
StakeAddresses(Vec<(StakeAddress, i64)>),
881876
OtherAccountingPot(u64),
882877
}
883878

@@ -894,8 +889,8 @@ pub struct MoveInstantaneousReward {
894889
/// Register stake (Conway version) = 'reg_cert'
895890
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
896891
pub struct Registration {
897-
/// Stake credential
898-
pub credential: StakeCredential,
892+
/// Stake address
893+
pub stake_address: StakeAddress,
899894

900895
/// Deposit paid
901896
pub deposit: Lovelace,
@@ -904,8 +899,8 @@ pub struct Registration {
904899
/// Deregister stake (Conway version) = 'unreg_cert'
905900
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
906901
pub struct Deregistration {
907-
/// Stake credential
908-
pub credential: StakeCredential,
902+
/// Stake address
903+
pub stake_address: StakeAddress,
909904

910905
/// Deposit to be refunded
911906
pub refund: Lovelace,
@@ -930,8 +925,8 @@ pub enum DRepChoice {
930925
/// Vote delegation (simple, existing registration) = vote_deleg_cert
931926
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
932927
pub struct VoteDelegation {
933-
/// Stake credential
934-
pub credential: StakeCredential,
928+
/// Stake address
929+
pub stake_address: StakeAddress,
935930

936931
// DRep choice
937932
pub drep: DRepChoice,
@@ -940,8 +935,8 @@ pub struct VoteDelegation {
940935
/// Stake+vote delegation (to SPO and DRep) = stake_vote_deleg_cert
941936
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
942937
pub struct StakeAndVoteDelegation {
943-
/// Stake credential
944-
pub credential: StakeCredential,
938+
/// Stake address
939+
pub stake_address: StakeAddress,
945940

946941
/// Pool
947942
pub operator: KeyHash,
@@ -953,8 +948,8 @@ pub struct StakeAndVoteDelegation {
953948
/// Stake delegation to SPO + registration = stake_reg_deleg_cert
954949
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
955950
pub struct StakeRegistrationAndDelegation {
956-
/// Stake credential
957-
pub credential: StakeCredential,
951+
/// Stake address
952+
pub stake_address: StakeAddress,
958953

959954
/// Pool
960955
pub operator: KeyHash,
@@ -966,8 +961,8 @@ pub struct StakeRegistrationAndDelegation {
966961
/// Vote delegation to DRep + registration = vote_reg_deleg_cert
967962
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
968963
pub struct StakeRegistrationAndVoteDelegation {
969-
/// Stake credential
970-
pub credential: StakeCredential,
964+
/// Stake address
965+
pub stake_address: StakeAddress,
971966

972967
/// DRep choice
973968
pub drep: DRepChoice,
@@ -982,7 +977,7 @@ pub struct StakeRegistrationAndVoteDelegation {
982977
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
983978
pub struct StakeRegistrationAndStakeAndVoteDelegation {
984979
/// Stake credential
985-
pub credential: StakeCredential,
980+
pub stake_address: StakeAddress,
986981

987982
/// Pool
988983
pub operator: KeyHash,
@@ -1660,8 +1655,8 @@ pub struct GovernanceOutcome {
16601655
}
16611656

16621657
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
1663-
pub struct StakeCredentialWithPos {
1664-
pub stake_credential: StakeCredential,
1658+
pub struct StakeAddressWithPos {
1659+
pub stake_address: StakeAddress,
16651660
pub tx_index: u64,
16661661
pub cert_index: u64,
16671662
}
@@ -1673,10 +1668,10 @@ pub enum TxCertificate {
16731668
None(()),
16741669

16751670
/// Stake registration
1676-
StakeRegistration(StakeCredentialWithPos),
1671+
StakeRegistration(StakeAddressWithPos),
16771672

16781673
/// Stake de-registration
1679-
StakeDeregistration(StakeCredential),
1674+
StakeDeregistration(StakeAddress),
16801675

16811676
/// Stake Delegation to a pool
16821677
StakeDelegation(StakeDelegation),

modules/accounts_state/src/rewards.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ fn calculate_spo_rewards(
335335
if !to_delegators.is_zero() {
336336
let total_stake = BigDecimal::from(spo.total_stake);
337337
for (delegator_stake_address, stake) in &spo.delegators {
338-
let delegator_credential = delegator_stake_address.get_credential();
339338
let proportion = BigDecimal::from(stake) / &total_stake;
340339

341340
// and hence how much of the total reward they get
@@ -346,7 +345,7 @@ fn calculate_spo_rewards(
346345
delegator_stake_address);
347346

348347
// Pool owners don't get member rewards (seems unfair!)
349-
if spo.pool_owners.contains(&delegator_credential) {
348+
if spo.pool_owners.contains(&delegator_stake_address) {
350349
debug!(
351350
"Skipping pool owner reward account {}, losing {to_pay}",
352351
delegator_stake_address

modules/accounts_state/src/snapshot.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Acropolis AccountsState: snapshot for rewards calculations
22
33
use crate::state::{Pots, RegistrationChange};
4-
use acropolis_common::{stake_addresses::StakeAddressMap, Credential, KeyHash, Lovelace, PoolRegistration, Ratio, StakeAddress};
4+
use acropolis_common::{stake_addresses::StakeAddressMap, KeyHash, Lovelace, PoolRegistration, Ratio, StakeAddress};
55
use imbl::OrdMap;
66
use std::collections::HashMap;
77
use std::sync::Arc;
@@ -35,7 +35,7 @@ pub struct SnapshotSPO {
3535
pub two_previous_reward_account_is_registered: bool,
3636

3737
/// Pool owners
38-
pub pool_owners: Vec<Credential>,
38+
pub pool_owners: Vec<StakeAddress>,
3939
}
4040

4141
/// Snapshot of stake distribution taken at the end of an particular epoch
@@ -159,18 +159,18 @@ impl Snapshot {
159159
pub fn get_stake_delegated_to_spo_by_addresses(
160160
&self,
161161
spo: &KeyHash,
162-
addresses: &[Credential],
162+
addresses: &[StakeAddress],
163163
) -> Lovelace {
164164
let Some(snapshot_spo) = self.spos.get(spo) else {
165165
return 0;
166166
};
167167

168-
let addr_set: std::collections::HashSet<_> = addresses.iter().collect();
168+
let address_set: std::collections::HashSet<_> = addresses.iter().collect();
169169
snapshot_spo
170170
.delegators
171171
.iter()
172-
.filter_map(|(addr, amount)| {
173-
if addr_set.contains(&addr.get_credential()) {
172+
.filter_map(|(address, amount)| {
173+
if address_set.contains(&address) {
174174
Some(*amount)
175175
} else {
176176
None
@@ -321,9 +321,9 @@ mod tests {
321321

322322
// Extract key hashes from stake addresses for the API call
323323
let addresses = vec![
324-
addr2.get_credential(),
325-
addr3.get_credential(),
326-
addr4.get_credential(),
324+
addr2,
325+
addr3,
326+
addr4,
327327
];
328328
let result = snapshot.get_stake_delegated_to_spo_by_addresses(&spo1, &addresses);
329329
assert_eq!(result, 500);
@@ -347,7 +347,7 @@ mod tests {
347347
);
348348

349349
// Extract key hash from stake address for the API call
350-
let addresses = vec![addr_x.get_credential()];
350+
let addresses = vec![addr_x];
351351
let result = snapshot.get_stake_delegated_to_spo_by_addresses(&spo1, &addresses);
352352
assert_eq!(result, 0);
353353
}

0 commit comments

Comments
 (0)