Skip to content

Commit 7e1fe2c

Browse files
committed
Replace RewardAccount with StakeAddress for consistency and simplify related logic
- Update `RewardDetail` and reward handling logic to use `StakeAddress` directly. - Replace `reward_account` field with `.get_hash()` where necessary.
1 parent 19580f0 commit 7e1fe2c

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

modules/accounts_state/src/rewards.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::snapshot::{Snapshot, SnapshotSPO};
44
use acropolis_common::{
55
protocol_params::ShelleyParams, rational_number::RationalNumber, KeyHash, Lovelace,
6-
RewardAccount, SPORewards,
6+
RewardAccount, SPORewards, StakeAddress,
77
};
88
use anyhow::{bail, Result};
99
use bigdecimal::{BigDecimal, One, ToPrimitive, Zero};
@@ -24,7 +24,7 @@ pub enum RewardType {
2424
#[derive(Debug, Clone)]
2525
pub struct RewardDetail {
2626
/// Account reward paid to
27-
pub account: RewardAccount,
27+
pub account: StakeAddress,
2828

2929
/// Type of reward
3030
pub rtype: RewardType,
@@ -134,7 +134,8 @@ pub fn calculate_rewards(
134134
);
135135

136136
// Note we use the staking reward account - it could have changed
137-
pay_to_pool_reward_account = registrations.contains(staking_spo.reward_account.get_hash())
137+
pay_to_pool_reward_account =
138+
registrations.contains(staking_spo.reward_account.get_hash())
138139
}
139140

140141
// There was a bug in the original node from Shelley until Allegra where if multiple SPOs
@@ -376,7 +377,7 @@ fn calculate_spo_rewards(
376377

377378
// Transfer from reserves to this account
378379
rewards.push(RewardDetail {
379-
account: hash.clone(),
380+
account: spo.reward_account.clone(),
380381
rtype: RewardType::Member,
381382
amount: to_pay,
382383
});
@@ -393,16 +394,15 @@ fn calculate_spo_rewards(
393394

394395
if pay_to_pool_reward_account {
395396
rewards.push(RewardDetail {
396-
// TODO Hack to remove e1 header - needs resolving properly with StakeAddress
397-
account: RewardAccount::from(spo.reward_account.get_hash()),
397+
account: spo.reward_account.clone(),
398398
rtype: RewardType::Leader,
399399
amount: spo_benefit,
400400
});
401401
} else {
402402
info!(
403403
"SPO {}'s reward account {} not paid {}",
404404
hex::encode(&operator_id),
405-
hex::encode(&spo.reward_account.get_hash()),
405+
hex::encode(spo.reward_account.get_hash()),
406406
spo_benefit,
407407
);
408408
}

modules/accounts_state/src/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl State {
663663
rewards
664664
.iter()
665665
.map(|reward| StakeRewardDelta {
666-
hash: reward.account.clone(),
666+
hash: reward.account.get_hash().to_vec(),
667667
delta: reward.amount as i64,
668668
})
669669
.collect::<Vec<_>>(),
@@ -677,7 +677,7 @@ impl State {
677677
let mut stake_addresses = self.stake_addresses.lock().unwrap();
678678
for (_, rewards) in reward_result.rewards {
679679
for reward in rewards {
680-
stake_addresses.add_to_reward(&reward.account, reward.amount);
680+
stake_addresses.add_to_reward(&reward.account.get_hash().to_vec(), reward.amount);
681681
}
682682
}
683683

@@ -784,14 +784,14 @@ impl State {
784784

785785
// Schedule to retire - we need them to still be in place when we count
786786
// blocks for the previous epoch
787-
self.retiring_spos.push(id.clone());
787+
self.retiring_spos.push(id.to_vec());
788788
}
789789

790790
self.spos = new_spos;
791791
Ok(())
792792
}
793793

794-
/// Register a stake address, with specified deposit if known
794+
/// Register a stake address, with a specified deposit if known
795795
fn register_stake_address(&mut self, credential: &StakeCredential, deposit: Option<Lovelace>) {
796796
// Stake addresses can be registered after being used in UTXOs
797797
let mut stake_addresses = self.stake_addresses.lock().unwrap();

modules/accounts_state/src/verifier.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Verification of calculated values against captured CSV from Haskell node / DBSync
22
use crate::rewards::{RewardDetail, RewardType, RewardsResult};
33
use crate::state::Pots;
4-
use acropolis_common::{KeyHash, RewardAccount};
4+
use acropolis_common::{KeyHash, RewardAccount, StakeAddress};
55
use hex::FromHex;
66
use itertools::EitherOrBoth::{Both, Left, Right};
77
use itertools::Itertools;
@@ -114,7 +114,7 @@ impl Verifier {
114114
match (&left.rtype, &right.rtype) {
115115
(RewardType::Leader, RewardType::Member) => Ordering::Less,
116116
(RewardType::Member, RewardType::Leader) => Ordering::Greater,
117-
_ => left.account.cmp(&right.account),
117+
_ => left.account.get_hash().cmp(&right.account.get_hash()),
118118
}
119119
}
120120

@@ -161,12 +161,13 @@ impl Verifier {
161161
_ => continue,
162162
};
163163

164-
// Convert account with e1 header to just hash
165-
// TODO: use StakeAddress, skipping first byte (e1) for now
166-
let account = RewardAccount::from(&account[1..]);
164+
let Ok(stake_address) = StakeAddress::from_binary(&account[1..]) else {
165+
error!("Bad stake address in {path} for address: {address} - skipping");
166+
continue;
167+
};
167168

168169
expected_rewards.entry(spo).or_default().push(RewardDetail {
169-
account,
170+
account: stake_address,
170171
rtype,
171172
amount,
172173
});
@@ -217,7 +218,7 @@ impl Verifier {
217218
error!(
218219
"Missing reward: SPO {} account {} {:?} {}",
219220
hex::encode(&expected_spo.0),
220-
hex::encode(&expected.account),
221+
hex::encode(&expected.account.get_hash()),
221222
expected.rtype,
222223
expected.amount
223224
);
@@ -227,7 +228,7 @@ impl Verifier {
227228
error!(
228229
"Extra reward: SPO {} account {} {:?} {}",
229230
hex::encode(&actual_spo.0),
230-
hex::encode(&actual.account),
231+
hex::encode(&actual.account.get_hash()),
231232
actual.rtype,
232233
actual.amount
233234
);
@@ -237,7 +238,7 @@ impl Verifier {
237238
if expected.amount != actual.amount {
238239
error!("Different reward: SPO {} account {} {:?} expected {}, actual {} ({})",
239240
hex::encode(&expected_spo.0),
240-
hex::encode(&expected.account),
241+
hex::encode(&expected.account.get_hash()),
241242
expected.rtype,
242243
expected.amount,
243244
actual.amount,
@@ -247,7 +248,7 @@ impl Verifier {
247248
debug!(
248249
"Reward match: SPO {} account {} {:?} {}",
249250
hex::encode(&expected_spo.0),
250-
hex::encode(&expected.account),
251+
hex::encode(&expected.account.get_hash()),
251252
expected.rtype,
252253
expected.amount
253254
);

0 commit comments

Comments
 (0)