Skip to content

Commit da946c4

Browse files
authored
Merge pull request #305 from input-output-hk/whankinsiv/historical-accounts-withdrawal-processing
feat: historical accounts withdrawal processing and REST handler
2 parents ba489d9 + d9e652c commit da946c4

File tree

11 files changed

+201
-65
lines changed

11 files changed

+201
-65
lines changed

.github/workflows/run-tests-on-push-to-main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
--package acropolis_module_epochs_state \
4040
--package acropolis_module_genesis_bootstrapper \
4141
--package acropolis_module_governance_state \
42+
--package acropolis_module_historical_accounts_state \
4243
--package acropolis_module_mithril_snapshot_fetcher \
4344
--package acropolis_module_parameters_state \
4445
--package acropolis_module_snapshot_bootstrapper \

common/src/queries/accounts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum AccountsStateQuery {
1818
GetAccountRegistrationHistory { account: StakeAddress },
1919
GetAccountDelegationHistory { account: StakeAddress },
2020
GetAccountMIRHistory { account: StakeAddress },
21-
GetAccountWithdrawalHistory { stake_key: Vec<u8> },
21+
GetAccountWithdrawalHistory { account: StakeAddress },
2222
GetAccountAssociatedAddresses { stake_key: Vec<u8> },
2323
GetAccountAssets { stake_key: Vec<u8> },
2424
GetAccountAssetsTotals { stake_key: Vec<u8> },
@@ -52,7 +52,7 @@ pub enum AccountsStateQueryResponse {
5252
AccountRegistrationHistory(Vec<RegistrationUpdate>),
5353
AccountDelegationHistory(Vec<DelegationUpdate>),
5454
AccountMIRHistory(Vec<AccountWithdrawal>),
55-
AccountWithdrawalHistory(AccountWithdrawalHistory),
55+
AccountWithdrawalHistory(Vec<AccountWithdrawal>),
5656
AccountAssociatedAddresses(AccountAssociatedAddresses),
5757
AccountAssets(AccountAssets),
5858
AccountAssetsTotals(AccountAssetsTotals),

common/src/stake_addresses.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,8 @@ mod tests {
867867
}
868868

869869
mod withdrawal_tests {
870+
use crate::TxIdentifier;
871+
870872
use super::*;
871873

872874
#[test]
@@ -880,6 +882,7 @@ mod tests {
880882
let withdrawal = Withdrawal {
881883
address: stake_address.clone(),
882884
value: 40,
885+
tx_identifier: TxIdentifier::default(),
883886
};
884887
stake_addresses.process_withdrawal(&withdrawal);
885888

@@ -898,6 +901,7 @@ mod tests {
898901
let withdrawal = Withdrawal {
899902
address: stake_address.clone(),
900903
value: 24,
904+
tx_identifier: TxIdentifier::default(),
901905
};
902906
stake_addresses.process_withdrawal(&withdrawal);
903907
assert_eq!(stake_addresses.get(&stake_address).unwrap().rewards, 12);
@@ -906,6 +910,7 @@ mod tests {
906910
let withdrawal = Withdrawal {
907911
address: stake_address.clone(),
908912
value: 2,
913+
tx_identifier: TxIdentifier::default(),
909914
};
910915
stake_addresses.process_withdrawal(&withdrawal);
911916
assert_eq!(stake_addresses.get(&stake_address).unwrap().rewards, 10);
@@ -922,6 +927,7 @@ mod tests {
922927
let withdrawal = Withdrawal {
923928
address: stake_address.clone(),
924929
value: 0,
930+
tx_identifier: TxIdentifier::default(),
925931
};
926932

927933
stake_addresses.process_withdrawal(&withdrawal);
@@ -936,6 +942,7 @@ mod tests {
936942
let withdrawal = Withdrawal {
937943
address: stake_address.clone(),
938944
value: 10,
945+
tx_identifier: TxIdentifier::default(),
939946
};
940947

941948
// Should log error but not panic

common/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ pub struct Withdrawal {
610610

611611
/// Value to withdraw
612612
pub value: Lovelace,
613+
614+
// Identifier of withdrawal tx
615+
pub tx_identifier: TxIdentifier,
613616
}
614617

615618
/// Treasury pot account

modules/accounts_state/src/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ mod tests {
12821282
withdrawals: vec![Withdrawal {
12831283
address: stake_address.clone(),
12841284
value: 39,
1285+
tx_identifier: TxIdentifier::default(),
12851286
}],
12861287
};
12871288

modules/historical_accounts_state/src/historical_accounts_state.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl HistoricalAccountsState {
110110
if let Message::Cardano((ref block_info, CardanoMessage::ProtocolParams(params))) =
111111
params_msg.as_ref()
112112
{
113-
Self::check_sync(&current_block, &block_info);
113+
Self::check_sync(&current_block, block_info);
114114
let mut state = state_mutex.lock().await;
115115
state.volatile.start_new_epoch(block_info.number);
116116
if let Some(shelley) = &params.params.shelley {
@@ -124,7 +124,7 @@ impl HistoricalAccountsState {
124124
CardanoMessage::StakeRewardDeltas(rewards_msg),
125125
)) = rewards_msg.as_ref()
126126
{
127-
Self::check_sync(&current_block, &block_info);
127+
Self::check_sync(&current_block, block_info);
128128
let mut state = state_mutex.lock().await;
129129
state
130130
.handle_rewards(rewards_msg)
@@ -142,12 +142,9 @@ impl HistoricalAccountsState {
142142
);
143143
let _entered = span.enter();
144144

145-
Self::check_sync(&current_block, &block_info);
145+
Self::check_sync(&current_block, block_info);
146146
let mut state = state_mutex.lock().await;
147-
state
148-
.handle_tx_certificates(tx_certs_msg, block_info.epoch as u32)
149-
.inspect_err(|e| error!("TxCertificates handling error: {e:#}"))
150-
.ok();
147+
state.handle_tx_certificates(tx_certs_msg, block_info.epoch as u32);
151148
}
152149

153150
_ => error!("Unexpected message type: {certs_message:?}"),
@@ -163,12 +160,9 @@ impl HistoricalAccountsState {
163160
);
164161
let _entered = span.enter();
165162

166-
Self::check_sync(&current_block, &block_info);
163+
Self::check_sync(&current_block, block_info);
167164
let mut state = state_mutex.lock().await;
168-
state
169-
.handle_withdrawals(withdrawals_msg)
170-
.inspect_err(|e| error!("Withdrawals handling error: {e:#}"))
171-
.ok();
165+
state.handle_withdrawals(withdrawals_msg);
172166
}
173167

174168
_ => error!("Unexpected message type: {message:?}"),
@@ -184,7 +178,7 @@ impl HistoricalAccountsState {
184178
);
185179
let _entered = span.enter();
186180

187-
Self::check_sync(&current_block, &block_info);
181+
Self::check_sync(&current_block, block_info);
188182
{
189183
let mut state = state_mutex.lock().await;
190184
state
@@ -206,8 +200,7 @@ impl HistoricalAccountsState {
206200

207201
if should_prune {
208202
let (store, cfg) = {
209-
let mut state: tokio::sync::MutexGuard<'_, State> =
210-
state_mutex.lock().await;
203+
let mut state = state_mutex.lock().await;
211204
state.prune_volatile().await;
212205
(state.immutable.clone(), state.config.clone())
213206
};
@@ -320,7 +313,7 @@ impl HistoricalAccountsState {
320313

321314
let response = match query {
322315
AccountsStateQuery::GetAccountRegistrationHistory { account } => {
323-
match state.lock().await.get_registration_history(&account).await {
316+
match state.lock().await.get_registration_history(account).await {
324317
Ok(Some(registrations)) => {
325318
AccountsStateQueryResponse::AccountRegistrationHistory(
326319
registrations,
@@ -331,7 +324,7 @@ impl HistoricalAccountsState {
331324
}
332325
}
333326
AccountsStateQuery::GetAccountDelegationHistory { account } => {
334-
match state.lock().await.get_delegation_history(&account).await {
327+
match state.lock().await.get_delegation_history(account).await {
335328
Ok(Some(delegations)) => {
336329
AccountsStateQueryResponse::AccountDelegationHistory(delegations)
337330
}
@@ -340,13 +333,21 @@ impl HistoricalAccountsState {
340333
}
341334
}
342335
AccountsStateQuery::GetAccountMIRHistory { account } => {
343-
match state.lock().await.get_mir_history(&account).await {
336+
match state.lock().await.get_mir_history(account).await {
344337
Ok(Some(mirs)) => AccountsStateQueryResponse::AccountMIRHistory(mirs),
345338
Ok(None) => AccountsStateQueryResponse::NotFound,
346339
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
347340
}
348341
}
349-
342+
AccountsStateQuery::GetAccountWithdrawalHistory { account } => {
343+
match state.lock().await.get_withdrawal_history(account).await {
344+
Ok(Some(withdrawals)) => {
345+
AccountsStateQueryResponse::AccountWithdrawalHistory(withdrawals)
346+
}
347+
Ok(None) => AccountsStateQueryResponse::NotFound,
348+
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
349+
}
350+
}
350351
_ => AccountsStateQueryResponse::Error(format!(
351352
"Unimplemented query variant: {:?}",
352353
query

modules/historical_accounts_state/src/immutable_historical_account_store.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl ImmutableHistoricalAccountStore {
8787
if config.store_rewards_history {
8888
batch.insert(
8989
&self.rewards_history,
90-
&epoch_key,
90+
epoch_key,
9191
to_vec(&entry.reward_history)?,
9292
);
9393
}
@@ -96,44 +96,44 @@ impl ImmutableHistoricalAccountStore {
9696
if config.store_active_stake_history {
9797
batch.insert(
9898
&self.active_stake_history,
99-
&epoch_key,
99+
epoch_key,
100100
to_vec(&entry.active_stake_history)?,
101101
);
102102
}
103103

104104
// Persist account delegation updates
105105
if config.store_delegation_history {
106106
if let Some(updates) = &entry.delegation_history {
107-
batch.insert(&self.delegation_history, &epoch_key, to_vec(&updates)?);
107+
batch.insert(&self.delegation_history, epoch_key, to_vec(updates)?);
108108
}
109109
}
110110

111111
// Persist account registration updates
112112
if config.store_registration_history {
113113
if let Some(updates) = &entry.registration_history {
114-
batch.insert(&self.registration_history, &epoch_key, to_vec(&updates)?);
114+
batch.insert(&self.registration_history, epoch_key, to_vec(updates)?);
115115
}
116116
}
117117

118118
// Persist withdrawal updates
119119
if config.store_withdrawal_history {
120120
if let Some(updates) = &entry.withdrawal_history {
121-
batch.insert(&self.withdrawal_history, &epoch_key, to_vec(&updates)?);
121+
batch.insert(&self.withdrawal_history, epoch_key, to_vec(updates)?);
122122
}
123123
}
124124

125125
// Persist MIR updates
126126
if config.store_mir_history {
127127
if let Some(updates) = &entry.mir_history {
128-
batch.insert(&self.mir_history, &epoch_key, to_vec(&updates)?);
128+
batch.insert(&self.mir_history, epoch_key, to_vec(updates)?);
129129
}
130130
}
131131

132132
// Persist address updates
133133
// TODO: Deduplicate addresses across epochs
134134
if config.store_addresses {
135135
if let Some(updates) = &entry.addresses {
136-
batch.insert(&self.addresses, &epoch_key, to_vec(&updates)?);
136+
batch.insert(&self.addresses, epoch_key, to_vec(updates)?);
137137
}
138138
}
139139
}
@@ -157,7 +157,7 @@ impl ImmutableHistoricalAccountStore {
157157
account: &StakeAddress,
158158
) -> Result<Option<Vec<RewardHistory>>> {
159159
let mut immutable_rewards =
160-
self.collect_partition::<RewardHistory>(&self.rewards_history, &account.get_hash())?;
160+
self.collect_partition::<RewardHistory>(&self.rewards_history, account.get_hash())?;
161161

162162
self.merge_pending(
163163
account,
@@ -175,7 +175,7 @@ impl ImmutableHistoricalAccountStore {
175175
) -> Result<Option<Vec<ActiveStakeHistory>>> {
176176
let mut immutable_active_stake = self.collect_partition::<ActiveStakeHistory>(
177177
&self.active_stake_history,
178-
&account.get_hash(),
178+
account.get_hash(),
179179
)?;
180180

181181
self.merge_pending(
@@ -194,7 +194,7 @@ impl ImmutableHistoricalAccountStore {
194194
) -> Result<Option<Vec<RegistrationUpdate>>> {
195195
let mut immutable_registrations = self.collect_partition::<RegistrationUpdate>(
196196
&self.registration_history,
197-
&account.get_hash(),
197+
account.get_hash(),
198198
)?;
199199

200200
self.merge_pending(
@@ -212,7 +212,7 @@ impl ImmutableHistoricalAccountStore {
212212
account: &StakeAddress,
213213
) -> Result<Option<Vec<DelegationUpdate>>> {
214214
let mut immutable_delegations = self
215-
.collect_partition::<DelegationUpdate>(&self.delegation_history, &account.get_hash())?;
215+
.collect_partition::<DelegationUpdate>(&self.delegation_history, account.get_hash())?;
216216

217217
self.merge_pending(
218218
account,
@@ -229,21 +229,19 @@ impl ImmutableHistoricalAccountStore {
229229
account: &StakeAddress,
230230
) -> Result<Option<Vec<AccountWithdrawal>>> {
231231
let mut immutable_mirs =
232-
self.collect_partition::<AccountWithdrawal>(&self.mir_history, &account.get_hash())?;
232+
self.collect_partition::<AccountWithdrawal>(&self.mir_history, account.get_hash())?;
233233

234234
self.merge_pending(account, |e| e.mir_history.as_ref(), &mut immutable_mirs).await;
235235

236236
Ok((!immutable_mirs.is_empty()).then_some(immutable_mirs))
237237
}
238238

239-
pub async fn _get_withdrawal_history(
239+
pub async fn get_withdrawal_history(
240240
&self,
241241
account: &StakeAddress,
242242
) -> Result<Option<Vec<AccountWithdrawal>>> {
243-
let mut immutable_withdrawals = self.collect_partition::<AccountWithdrawal>(
244-
&self.withdrawal_history,
245-
&account.get_hash(),
246-
)?;
243+
let mut immutable_withdrawals = self
244+
.collect_partition::<AccountWithdrawal>(&self.withdrawal_history, account.get_hash())?;
247245

248246
self.merge_pending(
249247
account,
@@ -260,7 +258,7 @@ impl ImmutableHistoricalAccountStore {
260258
account: &StakeAddress,
261259
) -> Result<Option<Vec<ShelleyAddress>>> {
262260
let mut immutable_addresses =
263-
self.collect_partition::<ShelleyAddress>(&self.addresses, &account.get_hash())?;
261+
self.collect_partition::<ShelleyAddress>(&self.addresses, account.get_hash())?;
264262

265263
self.merge_pending(account, |e| e.addresses.as_ref(), &mut immutable_addresses).await;
266264

0 commit comments

Comments
 (0)