Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions common/src/queries/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;

use crate::{DRepChoice, KeyHash, PoolId, PoolLiveStakeInfo, StakeAddress, TxIdentifier};
use crate::{
DRepChoice, KeyHash, PoolId, PoolLiveStakeInfo, RewardType, StakeAddress, TxIdentifier,
};

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

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum AccountsStateQuery {
GetAccountInfo { stake_address: StakeAddress },
GetAccountRewardHistory { stake_key: Vec<u8> },
GetAccountInfo { account: StakeAddress },
GetAccountRewardHistory { account: StakeAddress },
GetAccountHistory { stake_key: Vec<u8> },
GetAccountRegistrationHistory { account: StakeAddress },
GetAccountDelegationHistory { account: StakeAddress },
Expand Down Expand Up @@ -47,7 +49,7 @@ pub enum AccountsStateQuery {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum AccountsStateQueryResponse {
AccountInfo(AccountInfo),
AccountRewardHistory(AccountRewardHistory),
AccountRewardHistory(Vec<AccountReward>),
AccountHistory(AccountHistory),
AccountRegistrationHistory(Vec<RegistrationUpdate>),
AccountDelegationHistory(Vec<DelegationUpdate>),
Expand Down Expand Up @@ -91,9 +93,6 @@ pub struct AccountInfo {
pub delegated_drep: Option<DRepChoice>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AccountRewardHistory {}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AccountHistory {}

Expand Down Expand Up @@ -150,6 +149,20 @@ pub struct AccountWithdrawal {
pub amount: u64,
}

#[derive(
Debug, Clone, minicbor::Decode, minicbor::Encode, serde::Serialize, serde::Deserialize,
)]
pub struct AccountReward {
#[n(0)]
pub epoch: u32,
#[n(1)]
pub amount: u64,
#[n(2)]
pub pool: PoolId,
#[n(3)]
pub reward_type: RewardType,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AccountWithdrawalHistory {}

Expand Down
7 changes: 7 additions & 0 deletions common/src/stake_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,13 @@ impl StakeAddressMap {
update_value_with_delta(&mut sas.rewards, delta)
}

pub fn pay_reward(&mut self, stake_address: &StakeAddress, delta: u64) -> Result<()> {
let sas = self.entry(stake_address.clone()).or_default();
sas.rewards =
sas.rewards.checked_add(delta).ok_or_else(|| anyhow::anyhow!("reward overflow"))?;
Ok(())
}

/// Update utxo value with delta
pub fn update_utxo_value(&mut self, stake_address: &StakeAddress, delta: i64) -> Result<()> {
let sas = self.entry(stake_address.clone()).or_default();
Expand Down
33 changes: 32 additions & 1 deletion common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,38 @@ pub struct StakeAddressDelta {
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct StakeRewardDelta {
pub stake_address: StakeAddress,
pub delta: i64,
pub delta: u64,
pub reward_type: RewardType,
pub pool: PoolId,
}

/// Type of reward being given
#[derive(
Debug,
Clone,
PartialEq,
minicbor::Encode,
minicbor::Decode,
serde::Serialize,
serde::Deserialize,
)]
pub enum RewardType {
#[n(0)]
Leader,
#[n(1)]
Member,
#[n(2)]
PoolRefund,
}

impl fmt::Display for RewardType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RewardType::Leader => write!(f, "leader"),
RewardType::Member => write!(f, "member"),
RewardType::PoolRefund => write!(f, "pool_deposit_refund"),
}
}
}

pub type PolicyId = [u8; 28];
Expand Down
4 changes: 2 additions & 2 deletions modules/accounts_state/src/accounts_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ impl AccountsState {
};

let response = match query {
AccountsStateQuery::GetAccountInfo { stake_address } => {
if let Some(account) = state.get_stake_state(stake_address) {
AccountsStateQuery::GetAccountInfo { account } => {
if let Some(account) = state.get_stake_state(account) {
AccountsStateQueryResponse::AccountInfo(AccountInfo {
utxo_value: account.utxo_value,
rewards: account.rewards,
Expand Down
14 changes: 7 additions & 7 deletions modules/accounts_state/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use acropolis_common::{
protocol_params::ShelleyParams, rational_number::RationalNumber, KeyHash, Lovelace, SPORewards,
StakeAddress,
};
use acropolis_common::{PoolId, RewardType};
use anyhow::{bail, Result};
use bigdecimal::{BigDecimal, One, ToPrimitive, Zero};
use std::cmp::min;
Expand All @@ -13,13 +14,6 @@ use std::collections::{BTreeMap, HashSet};
use std::sync::Arc;
use tracing::{debug, info, warn};

/// Type of reward being given
#[derive(Debug, Clone, PartialEq)]
pub enum RewardType {
Leader,
Member,
}

/// Reward Detail
#[derive(Debug, Clone)]
pub struct RewardDetail {
Expand All @@ -31,6 +25,9 @@ pub struct RewardDetail {

/// Reward amount
pub amount: Lovelace,

// Pool that reward came from
pub pool: PoolId,
}

/// Result of a rewards calculation
Expand Down Expand Up @@ -211,6 +208,7 @@ pub fn calculate_rewards(
num_delegators_paid += 1;
total_paid_to_delegators += reward.amount;
}
RewardType::PoolRefund => {}
}
spo_rewards.total_rewards += reward.amount;
result.total_paid += reward.amount;
Expand Down Expand Up @@ -391,6 +389,7 @@ fn calculate_spo_rewards(
account: delegator_stake_address.clone(),
rtype: RewardType::Member,
amount: to_pay,
pool: operator_id.to_vec(),
});
total_paid += to_pay;
delegators_paid += 1;
Expand All @@ -408,6 +407,7 @@ fn calculate_spo_rewards(
account: spo.reward_account.clone(),
rtype: RewardType::Leader,
amount: spo_benefit,
pool: operator_id.to_vec(),
});
} else {
info!(
Expand Down
Loading