Skip to content

Commit dc556bf

Browse files
authored
Merge pull request #322 from input-output-hk/whankinsiv/historical-accounts-reward-processing
feat: historical accounts reward processing
2 parents 377afd4 + ffd706a commit dc556bf

File tree

14 files changed

+321
-181
lines changed

14 files changed

+321
-181
lines changed

common/src/queries/accounts.rs

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

3-
use crate::{DRepChoice, KeyHash, PoolId, PoolLiveStakeInfo, StakeAddress, TxIdentifier};
3+
use crate::{
4+
DRepChoice, KeyHash, PoolId, PoolLiveStakeInfo, RewardType, StakeAddress, TxIdentifier,
5+
};
46

57
pub const DEFAULT_ACCOUNTS_QUERY_TOPIC: (&str, &str) =
68
("accounts-state-query-topic", "cardano.query.accounts");
@@ -12,8 +14,8 @@ pub const DEFAULT_HISTORICAL_ACCOUNTS_QUERY_TOPIC: (&str, &str) = (
1214

1315
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
1416
pub enum AccountsStateQuery {
15-
GetAccountInfo { stake_address: StakeAddress },
16-
GetAccountRewardHistory { stake_key: Vec<u8> },
17+
GetAccountInfo { account: StakeAddress },
18+
GetAccountRewardHistory { account: StakeAddress },
1719
GetAccountHistory { stake_key: Vec<u8> },
1820
GetAccountRegistrationHistory { account: StakeAddress },
1921
GetAccountDelegationHistory { account: StakeAddress },
@@ -47,7 +49,7 @@ pub enum AccountsStateQuery {
4749
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4850
pub enum AccountsStateQueryResponse {
4951
AccountInfo(AccountInfo),
50-
AccountRewardHistory(AccountRewardHistory),
52+
AccountRewardHistory(Vec<AccountReward>),
5153
AccountHistory(AccountHistory),
5254
AccountRegistrationHistory(Vec<RegistrationUpdate>),
5355
AccountDelegationHistory(Vec<DelegationUpdate>),
@@ -91,9 +93,6 @@ pub struct AccountInfo {
9193
pub delegated_drep: Option<DRepChoice>,
9294
}
9395

94-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
95-
pub struct AccountRewardHistory {}
96-
9796
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9897
pub struct AccountHistory {}
9998

@@ -150,6 +149,20 @@ pub struct AccountWithdrawal {
150149
pub amount: u64,
151150
}
152151

152+
#[derive(
153+
Debug, Clone, minicbor::Decode, minicbor::Encode, serde::Serialize, serde::Deserialize,
154+
)]
155+
pub struct AccountReward {
156+
#[n(0)]
157+
pub epoch: u32,
158+
#[n(1)]
159+
pub amount: u64,
160+
#[n(2)]
161+
pub pool: PoolId,
162+
#[n(3)]
163+
pub reward_type: RewardType,
164+
}
165+
153166
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
154167
pub struct AccountWithdrawalHistory {}
155168

common/src/stake_addresses.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,13 @@ impl StakeAddressMap {
535535
update_value_with_delta(&mut sas.rewards, delta)
536536
}
537537

538+
pub fn pay_reward(&mut self, stake_address: &StakeAddress, delta: u64) -> Result<()> {
539+
let sas = self.entry(stake_address.clone()).or_default();
540+
sas.rewards =
541+
sas.rewards.checked_add(delta).ok_or_else(|| anyhow::anyhow!("reward overflow"))?;
542+
Ok(())
543+
}
544+
538545
/// Update utxo value with delta
539546
pub fn update_utxo_value(&mut self, stake_address: &StakeAddress, delta: i64) -> Result<()> {
540547
let sas = self.entry(stake_address.clone()).or_default();

common/src/types.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,38 @@ pub struct StakeAddressDelta {
212212
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
213213
pub struct StakeRewardDelta {
214214
pub stake_address: StakeAddress,
215-
pub delta: i64,
215+
pub delta: u64,
216+
pub reward_type: RewardType,
217+
pub pool: PoolId,
218+
}
219+
220+
/// Type of reward being given
221+
#[derive(
222+
Debug,
223+
Clone,
224+
PartialEq,
225+
minicbor::Encode,
226+
minicbor::Decode,
227+
serde::Serialize,
228+
serde::Deserialize,
229+
)]
230+
pub enum RewardType {
231+
#[n(0)]
232+
Leader,
233+
#[n(1)]
234+
Member,
235+
#[n(2)]
236+
PoolRefund,
237+
}
238+
239+
impl fmt::Display for RewardType {
240+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241+
match self {
242+
RewardType::Leader => write!(f, "leader"),
243+
RewardType::Member => write!(f, "member"),
244+
RewardType::PoolRefund => write!(f, "pool_deposit_refund"),
245+
}
246+
}
216247
}
217248

218249
pub type PolicyId = [u8; 28];

modules/accounts_state/src/accounts_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ impl AccountsState {
505505
};
506506

507507
let response = match query {
508-
AccountsStateQuery::GetAccountInfo { stake_address } => {
509-
if let Some(account) = state.get_stake_state(stake_address) {
508+
AccountsStateQuery::GetAccountInfo { account } => {
509+
if let Some(account) = state.get_stake_state(account) {
510510
AccountsStateQueryResponse::AccountInfo(AccountInfo {
511511
utxo_value: account.utxo_value,
512512
rewards: account.rewards,

modules/accounts_state/src/rewards.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use acropolis_common::{
55
protocol_params::ShelleyParams, rational_number::RationalNumber, KeyHash, Lovelace, SPORewards,
66
StakeAddress,
77
};
8+
use acropolis_common::{PoolId, RewardType};
89
use anyhow::{bail, Result};
910
use bigdecimal::{BigDecimal, One, ToPrimitive, Zero};
1011
use std::cmp::min;
@@ -13,13 +14,6 @@ use std::collections::{BTreeMap, HashSet};
1314
use std::sync::Arc;
1415
use tracing::{debug, info, warn};
1516

16-
/// Type of reward being given
17-
#[derive(Debug, Clone, PartialEq)]
18-
pub enum RewardType {
19-
Leader,
20-
Member,
21-
}
22-
2317
/// Reward Detail
2418
#[derive(Debug, Clone)]
2519
pub struct RewardDetail {
@@ -31,6 +25,9 @@ pub struct RewardDetail {
3125

3226
/// Reward amount
3327
pub amount: Lovelace,
28+
29+
// Pool that reward came from
30+
pub pool: PoolId,
3431
}
3532

3633
/// Result of a rewards calculation
@@ -211,6 +208,7 @@ pub fn calculate_rewards(
211208
num_delegators_paid += 1;
212209
total_paid_to_delegators += reward.amount;
213210
}
211+
RewardType::PoolRefund => {}
214212
}
215213
spo_rewards.total_rewards += reward.amount;
216214
result.total_paid += reward.amount;
@@ -391,6 +389,7 @@ fn calculate_spo_rewards(
391389
account: delegator_stake_address.clone(),
392390
rtype: RewardType::Member,
393391
amount: to_pay,
392+
pool: operator_id.to_vec(),
394393
});
395394
total_paid += to_pay;
396395
delegators_paid += 1;
@@ -408,6 +407,7 @@ fn calculate_spo_rewards(
408407
account: spo.reward_account.clone(),
409408
rtype: RewardType::Leader,
410409
amount: spo_benefit,
410+
pool: operator_id.to_vec(),
411411
});
412412
} else {
413413
info!(

0 commit comments

Comments
 (0)