Skip to content

Commit 262a5e3

Browse files
committed
feat: reward delta processing in historical accounts state
Signed-off-by: William Hankins <[email protected]>
1 parent c68fba0 commit 262a5e3

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

common/src/queries/accounts.rs

Lines changed: 4 additions & 2 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");
@@ -158,7 +160,7 @@ pub struct RewardHistory {
158160
#[n(2)]
159161
pub pool: PoolId,
160162
#[n(3)]
161-
pub is_owner: bool,
163+
pub reward_type: RewardType,
162164
}
163165

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

common/src/types.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,15 @@ pub struct StakeRewardDelta {
217217
}
218218

219219
/// Type of reward being given
220-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
220+
#[derive(
221+
Debug, Clone, minicbor::Encode, minicbor::Decode, serde::Serialize, serde::Deserialize,
222+
)]
221223
pub enum RewardType {
224+
#[n(0)]
222225
Leader,
226+
#[n(1)]
223227
Member,
228+
#[n(2)]
224229
PoolRefund,
225230
}
226231

modules/historical_accounts_state/src/historical_accounts_state.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ impl HistoricalAccountsState {
126126
{
127127
Self::check_sync(&current_block, block_info);
128128
let mut state = state_mutex.lock().await;
129-
state
130-
.handle_rewards(rewards_msg)
131-
.inspect_err(|e| error!("Reward deltas handling error: {e:#}"))
132-
.ok();
129+
state.handle_rewards(rewards_msg, block_info.epoch as u32);
133130
}
134131
}
135132

modules/historical_accounts_state/src/state.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,18 @@ impl State {
104104
&& block_info.number > self.volatile.epoch_start_block + self.volatile.security_param_k
105105
}
106106

107-
pub fn handle_rewards(&mut self, _reward_deltas: &StakeRewardDeltasMessage) -> Result<()> {
108-
Ok(())
107+
pub fn handle_rewards(&mut self, reward_deltas: &StakeRewardDeltasMessage, epoch: u32) {
108+
let volatile = self.volatile.window.back_mut().expect("window should never be empty");
109+
for reward in reward_deltas.deltas.iter() {
110+
let entry = volatile.entry(reward.stake_address.clone()).or_default();
111+
let update = RewardHistory {
112+
epoch,
113+
amount: reward.delta,
114+
pool: Vec::new(),
115+
reward_type: reward.reward_type.clone(),
116+
};
117+
entry.reward_history.get_or_insert_with(Vec::new).push(update);
118+
}
109119
}
110120

111121
pub fn handle_tx_certificates(&mut self, tx_certs: &TxCertificatesMessage, epoch: u32) {

0 commit comments

Comments
 (0)