Skip to content

Commit 779c262

Browse files
authored
Merge pull request #292 from input-output-hk/whankinsiv/reg-deleg-and-mir-rest-handlers
feat: registration, delegation, and MIR history account endpoints
2 parents 465b906 + 5eb101a commit 779c262

File tree

8 files changed

+467
-72
lines changed

8 files changed

+467
-72
lines changed

common/src/queries/accounts.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ pub enum RegistrationStatus {
131131
Deregistered,
132132
}
133133

134+
impl std::fmt::Display for RegistrationStatus {
135+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136+
match self {
137+
RegistrationStatus::Registered => write!(f, "registered"),
138+
RegistrationStatus::Deregistered => write!(f, "deregistered"),
139+
}
140+
}
141+
}
142+
134143
#[derive(
135144
Debug, Clone, serde::Serialize, serde::Deserialize, minicbor::Decode, minicbor::Encode,
136145
)]

modules/historical_accounts_state/src/historical_accounts_state.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,25 +321,28 @@ impl HistoricalAccountsState {
321321
let response = match query {
322322
AccountsStateQuery::GetAccountRegistrationHistory { account } => {
323323
match state.lock().await.get_registration_history(&account).await {
324-
Ok(registrations) => {
324+
Ok(Some(registrations)) => {
325325
AccountsStateQueryResponse::AccountRegistrationHistory(
326326
registrations,
327327
)
328328
}
329+
Ok(None) => AccountsStateQueryResponse::NotFound,
329330
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
330331
}
331332
}
332333
AccountsStateQuery::GetAccountDelegationHistory { account } => {
333334
match state.lock().await.get_delegation_history(&account).await {
334-
Ok(delegations) => {
335+
Ok(Some(delegations)) => {
335336
AccountsStateQueryResponse::AccountDelegationHistory(delegations)
336337
}
338+
Ok(None) => AccountsStateQueryResponse::NotFound,
337339
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
338340
}
339341
}
340342
AccountsStateQuery::GetAccountMIRHistory { account } => {
341343
match state.lock().await.get_mir_history(&account).await {
342-
Ok(mirs) => AccountsStateQueryResponse::AccountMIRHistory(mirs),
344+
Ok(Some(mirs)) => AccountsStateQueryResponse::AccountMIRHistory(mirs),
345+
Ok(None) => AccountsStateQueryResponse::NotFound,
343346
Err(e) => AccountsStateQueryResponse::Error(e.to_string()),
344347
}
345348
}

modules/historical_accounts_state/src/immutable_historical_account_store.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use fjall::{Keyspace, Partition, PartitionCreateOptions};
99
use minicbor::{decode, to_vec};
1010
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1111
use tokio::sync::Mutex;
12-
use tracing::{debug, error, info};
12+
use tracing::{debug, error};
1313

1414
use crate::state::{AccountEntry, ActiveStakeHistory, HistoricalAccountsConfig, RewardHistory};
1515

@@ -61,17 +61,21 @@ impl ImmutableHistoricalAccountStore {
6161
/// and addresses into their respective Fjall partitions for an entire epoch.
6262
/// Skips any partitions that have already stored the given epoch.
6363
/// All writes are batched and committed atomically, preventing on-disk corruption in case of failure.
64-
pub async fn persist_epoch(&self, epoch: u32, config: &HistoricalAccountsConfig) -> Result<()> {
65-
if !config.any_enabled() {
66-
debug!("no persistence needed for epoch {epoch} (disabled)",);
67-
return Ok(());
68-
}
69-
64+
pub async fn persist_epoch(
65+
&self,
66+
epoch: u32,
67+
config: &HistoricalAccountsConfig,
68+
) -> Result<u64> {
7069
let drained_blocks = {
7170
let mut pending = self.pending.lock().await;
7271
std::mem::take(&mut *pending)
7372
};
7473

74+
if !config.any_enabled() {
75+
debug!("no persistence needed for epoch {epoch} (disabled)",);
76+
return Ok(0);
77+
}
78+
7579
let mut batch = self.keyspace.batch();
7680
let mut change_count = 0;
7781

@@ -135,10 +139,7 @@ impl ImmutableHistoricalAccountStore {
135139
}
136140

137141
match batch.commit() {
138-
Ok(_) => {
139-
info!("committed {change_count} account changes for epoch {epoch}");
140-
Ok(())
141-
}
142+
Ok(_) => Ok(change_count),
142143
Err(e) => {
143144
error!("batch commit failed for epoch {epoch}: {e}");
144145
Err(e.into())

modules/historical_accounts_state/src/state.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -249,41 +249,58 @@ impl State {
249249
pub async fn get_registration_history(
250250
&self,
251251
account: &StakeAddress,
252-
) -> Result<Vec<RegistrationUpdate>> {
253-
let mut registrations =
254-
self.immutable.get_registration_history(&account).await?.unwrap_or_default();
252+
) -> Result<Option<Vec<RegistrationUpdate>>> {
253+
let immutable = self.immutable.get_registration_history(&account).await?;
255254

256-
self.merge_volatile_history(
257-
&account,
258-
|e| e.registration_history.as_ref(),
259-
&mut registrations,
260-
);
255+
let mut volatile = Vec::new();
256+
self.merge_volatile_history(&account, |e| e.registration_history.as_ref(), &mut volatile);
261257

262-
Ok(registrations)
258+
match immutable {
259+
Some(mut registrations) => {
260+
registrations.extend(volatile);
261+
Ok(Some(registrations))
262+
}
263+
None if volatile.is_empty() => Ok(None),
264+
None => Ok(Some(volatile)),
265+
}
263266
}
264267

265268
pub async fn get_delegation_history(
266269
&self,
267270
account: &StakeAddress,
268-
) -> Result<Vec<DelegationUpdate>> {
269-
let mut delegations =
270-
self.immutable.get_delegation_history(&account).await?.unwrap_or_default();
271+
) -> Result<Option<Vec<DelegationUpdate>>> {
272+
let immutable = self.immutable.get_delegation_history(&account).await?;
271273

272-
self.merge_volatile_history(
273-
&account,
274-
|e| e.delegation_history.as_ref(),
275-
&mut delegations,
276-
);
274+
let mut volatile = Vec::new();
275+
self.merge_volatile_history(&account, |e| e.delegation_history.as_ref(), &mut volatile);
277276

278-
Ok(delegations)
277+
match immutable {
278+
Some(mut delegations) => {
279+
delegations.extend(volatile);
280+
Ok(Some(delegations))
281+
}
282+
None if volatile.is_empty() => Ok(None),
283+
None => Ok(Some(volatile)),
284+
}
279285
}
280286

281-
pub async fn get_mir_history(&self, account: &StakeAddress) -> Result<Vec<AccountWithdrawal>> {
282-
let mut mirs = self.immutable.get_mir_history(&account).await?.unwrap_or_default();
287+
pub async fn get_mir_history(
288+
&self,
289+
account: &StakeAddress,
290+
) -> Result<Option<Vec<AccountWithdrawal>>> {
291+
let immutable = self.immutable.get_mir_history(&account).await?;
283292

284-
self.merge_volatile_history(&account, |e| e.mir_history.as_ref(), &mut mirs);
293+
let mut volatile = Vec::new();
294+
self.merge_volatile_history(&account, |e| e.mir_history.as_ref(), &mut volatile);
285295

286-
Ok(mirs)
296+
match immutable {
297+
Some(mut mirs) => {
298+
mirs.extend(volatile);
299+
Ok(Some(mirs))
300+
}
301+
None if volatile.is_empty() => Ok(None),
302+
None => Ok(Some(volatile)),
303+
}
287304
}
288305

289306
pub async fn _get_withdrawal_history(

0 commit comments

Comments
 (0)