Skip to content

Commit c68fba0

Browse files
committed
feat: implement reward history getter and query handler
Signed-off-by: William Hankins <[email protected]>
1 parent 0d58690 commit c68fba0

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

common/src/queries/accounts.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub const DEFAULT_HISTORICAL_ACCOUNTS_QUERY_TOPIC: (&str, &str) = (
1313
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
1414
pub enum AccountsStateQuery {
1515
GetAccountInfo { stake_address: StakeAddress },
16-
GetAccountRewardHistory { stake_key: Vec<u8> },
16+
GetAccountRewardHistory { account: StakeAddress },
1717
GetAccountHistory { stake_key: Vec<u8> },
1818
GetAccountRegistrationHistory { account: StakeAddress },
1919
GetAccountDelegationHistory { account: StakeAddress },
@@ -47,7 +47,7 @@ pub enum AccountsStateQuery {
4747
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4848
pub enum AccountsStateQueryResponse {
4949
AccountInfo(AccountInfo),
50-
AccountRewardHistory(AccountRewardHistory),
50+
AccountRewardHistory(Vec<RewardHistory>),
5151
AccountHistory(AccountHistory),
5252
AccountRegistrationHistory(Vec<RegistrationUpdate>),
5353
AccountDelegationHistory(Vec<DelegationUpdate>),
@@ -91,9 +91,6 @@ pub struct AccountInfo {
9191
pub delegated_drep: Option<DRepChoice>,
9292
}
9393

94-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
95-
pub struct AccountRewardHistory {}
96-
9794
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9895
pub struct AccountHistory {}
9996

@@ -150,6 +147,20 @@ pub struct AccountWithdrawal {
150147
pub amount: u64,
151148
}
152149

150+
#[derive(
151+
Debug, Clone, minicbor::Decode, minicbor::Encode, serde::Serialize, serde::Deserialize,
152+
)]
153+
pub struct RewardHistory {
154+
#[n(0)]
155+
pub epoch: u32,
156+
#[n(1)]
157+
pub amount: u64,
158+
#[n(2)]
159+
pub pool: PoolId,
160+
#[n(3)]
161+
pub is_owner: bool,
162+
}
163+
153164
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
154165
pub struct AccountWithdrawalHistory {}
155166

modules/historical_accounts_state/src/historical_accounts_state.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ impl HistoricalAccountsState {
348348
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
349349
}
350350
}
351+
AccountsStateQuery::GetAccountRewardHistory { account } => {
352+
match state.lock().await.get_reward_history(account).await {
353+
Ok(Some(rewards)) => {
354+
AccountsStateQueryResponse::AccountRewardHistory(rewards)
355+
}
356+
Ok(None) => AccountsStateQueryResponse::NotFound,
357+
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
358+
}
359+
}
351360
_ => AccountsStateQueryResponse::Error(format!(
352361
"Unimplemented query variant: {:?}",
353362
query

modules/historical_accounts_state/src/immutable_historical_account_store.rs

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

33
use acropolis_common::{
4-
queries::accounts::{AccountWithdrawal, DelegationUpdate, RegistrationUpdate},
4+
queries::accounts::{AccountWithdrawal, DelegationUpdate, RegistrationUpdate, RewardHistory},
55
ShelleyAddress, StakeAddress,
66
};
77
use anyhow::Result;
@@ -11,7 +11,7 @@ use rayon::iter::{IntoParallelIterator, ParallelIterator};
1111
use tokio::sync::Mutex;
1212
use tracing::{debug, error};
1313

14-
use crate::state::{AccountEntry, ActiveStakeHistory, HistoricalAccountsConfig, RewardHistory};
14+
use crate::state::{AccountEntry, ActiveStakeHistory, HistoricalAccountsConfig};
1515

1616
pub struct ImmutableHistoricalAccountStore {
1717
rewards_history: Partition,
@@ -152,7 +152,7 @@ impl ImmutableHistoricalAccountStore {
152152
pending.extend(drained);
153153
}
154154

155-
pub async fn _get_rewards_history(
155+
pub async fn get_reward_history(
156156
&self,
157157
account: &StakeAddress,
158158
) -> Result<Option<Vec<RewardHistory>>> {

modules/historical_accounts_state/src/state.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use acropolis_common::{
88
AddressDeltasMessage, StakeRewardDeltasMessage, TxCertificatesMessage, WithdrawalsMessage,
99
},
1010
queries::accounts::{
11-
AccountWithdrawal, DelegationUpdate, RegistrationStatus, RegistrationUpdate,
11+
AccountWithdrawal, DelegationUpdate, RegistrationStatus, RegistrationUpdate, RewardHistory,
1212
},
13-
BlockInfo, InstantaneousRewardTarget, PoolId, ShelleyAddress, StakeAddress, StakeCredential,
14-
TxCertificate, TxIdentifier,
13+
BlockInfo, InstantaneousRewardTarget, PoolId, ShelleyAddress, StakeAddress, TxCertificate,
14+
TxIdentifier,
1515
};
1616
use tracing::warn;
1717

@@ -33,18 +33,6 @@ pub struct AccountEntry {
3333
pub addresses: Option<Vec<ShelleyAddress>>,
3434
}
3535

36-
#[derive(Debug, Clone, minicbor::Decode, minicbor::Encode)]
37-
pub struct RewardHistory {
38-
#[n(0)]
39-
pub epoch: u32,
40-
#[n(1)]
41-
pub amount: u64,
42-
#[n(2)]
43-
pub pool: PoolId,
44-
#[n(3)]
45-
pub is_owner: bool,
46-
}
47-
4836
#[derive(Debug, Clone, minicbor::Decode, minicbor::Encode)]
4937
pub struct ActiveStakeHistory {
5038
#[n(0)]
@@ -239,11 +227,23 @@ impl State {
239227
}
240228
}
241229

242-
pub async fn _get_reward_history(
230+
pub async fn get_reward_history(
243231
&self,
244-
_account: &StakeCredential,
245-
) -> Result<Vec<RewardHistory>> {
246-
Ok(Vec::new())
232+
account: &StakeAddress,
233+
) -> Result<Option<Vec<RewardHistory>>> {
234+
let immutable = self.immutable.get_reward_history(account).await?;
235+
236+
let mut volatile = Vec::new();
237+
self.merge_volatile_history(account, |e| e.reward_history.as_ref(), &mut volatile);
238+
239+
match immutable {
240+
Some(mut rewards) => {
241+
rewards.extend(volatile);
242+
Ok(Some(rewards))
243+
}
244+
None if volatile.is_empty() => Ok(None),
245+
None => Ok(Some(volatile)),
246+
}
247247
}
248248

249249
pub async fn _get_active_stake_history(

0 commit comments

Comments
 (0)