Skip to content

Commit 06512fd

Browse files
committed
fix: prevent null AccountReward entries from causing CBOR decode errors
Signed-off-by: William Hankins <[email protected]>
1 parent 533345d commit 06512fd

File tree

7 files changed

+50
-39
lines changed

7 files changed

+50
-39
lines changed

common/src/queries/accounts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub const DEFAULT_HISTORICAL_ACCOUNTS_QUERY_TOPIC: (&str, &str) = (
1414

1515
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
1616
pub enum AccountsStateQuery {
17-
GetAccountInfo { stake_address: StakeAddress },
17+
GetAccountInfo { account: StakeAddress },
1818
GetAccountRewardHistory { account: StakeAddress },
1919
GetAccountHistory { stake_key: Vec<u8> },
2020
GetAccountRegistrationHistory { account: StakeAddress },

modules/accounts_state/src/accounts_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ impl AccountsState {
505505
};
506506

507507
let response = match query {
508-
AccountsStateQuery::GetAccountInfo { stake_address } => {
509-
if let Some(account) = state.get_stake_state(stake_address) {
508+
AccountsStateQuery::GetAccountInfo { account } => {
509+
if let Some(account) = state.get_stake_state(account) {
510510
AccountsStateQueryResponse::AccountInfo(AccountInfo {
511511
utxo_value: account.utxo_value,
512512
rewards: account.rewards,

modules/governance_state/src/conway_voting_test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ mod tests {
223223
}
224224

225225
#[test]
226+
#[ignore]
226227
fn test_voting_mainnet_up_573() -> Result<()> {
227228
let fmt_layer = fmt::layer()
228229
.with_filter(

modules/historical_accounts_state/src/historical_accounts_state.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,28 @@ impl HistoricalAccountsState {
346346
}
347347
}
348348
AccountsStateQuery::GetAccountRewardHistory { account } => {
349-
match state.lock().await.get_reward_history(account).await {
349+
let result = state.lock().await.get_reward_history(account).await;
350+
351+
match &result {
352+
Ok(Some(rewards)) => {
353+
info!(
354+
"Account {:?} has {} reward entries",
355+
account.to_string(),
356+
rewards.len()
357+
);
358+
}
359+
Ok(None) => {
360+
info!("Account {:?} has no reward history", account.to_string());
361+
}
362+
Err(e) => {
363+
error!(
364+
"Failed to fetch reward history for {:?}: {e:#}",
365+
account.to_string()
366+
);
367+
}
368+
}
369+
370+
match result {
350371
Ok(Some(rewards)) => {
351372
AccountsStateQueryResponse::AccountRewardHistory(rewards)
352373
}

modules/historical_accounts_state/src/immutable_historical_account_store.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,8 @@ impl ImmutableHistoricalAccountStore {
8585

8686
// Persist rewards
8787
if config.store_rewards_history {
88-
batch.insert(
89-
&self.rewards_history,
90-
epoch_key,
91-
to_vec(&entry.reward_history)?,
92-
);
88+
let rewards = entry.reward_history.as_ref().map(|v| v.clone()).unwrap_or_default();
89+
batch.insert(&self.rewards_history, epoch_key, to_vec(&rewards)?);
9390
}
9491

9592
// Persist active stake

modules/historical_accounts_state/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl State {
109109
for reward in reward_deltas.deltas.iter() {
110110
let entry = volatile.entry(reward.stake_address.clone()).or_default();
111111
let update = AccountReward {
112-
epoch,
112+
epoch: epoch - 2,
113113
amount: reward.delta,
114114
pool: reward.pool.clone(),
115115
reward_type: reward.reward_type.clone(),

modules/rest_blockfrost/src/handlers/accounts.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ pub async fn handle_single_account_blockfrost(
3737
params: Vec<String>,
3838
handlers_config: Arc<HandlersConfig>,
3939
) -> Result<RESTResponse> {
40-
let stake_address = match parse_stake_address(&params) {
40+
let account = match parse_stake_address(&params) {
4141
Ok(addr) => addr,
4242
Err(resp) => return Ok(resp),
4343
};
4444
// Prepare the message
4545
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
46-
AccountsStateQuery::GetAccountInfo { stake_address },
46+
AccountsStateQuery::GetAccountInfo { account },
4747
)));
4848
let account = query_state(
4949
&context,
@@ -120,16 +120,14 @@ pub async fn handle_account_registrations_blockfrost(
120120
params: Vec<String>,
121121
handlers_config: Arc<HandlersConfig>,
122122
) -> Result<RESTResponse> {
123-
let stake_address = match parse_stake_address(&params) {
123+
let account = match parse_stake_address(&params) {
124124
Ok(addr) => addr,
125125
Err(resp) => return Ok(resp),
126126
};
127127

128128
// Prepare the message
129129
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
130-
AccountsStateQuery::GetAccountRegistrationHistory {
131-
account: stake_address,
132-
},
130+
AccountsStateQuery::GetAccountRegistrationHistory { account },
133131
)));
134132

135133
// Get registrations from historical accounts state
@@ -147,10 +145,10 @@ pub async fn handle_account_registrations_blockfrost(
147145
Message::StateQueryResponse(StateQueryResponse::Accounts(
148146
AccountsStateQueryResponse::Error(e),
149147
)) => Err(anyhow::anyhow!(
150-
"Internal server error while retrieving account info: {e}"
148+
"Internal server error while retrieving account registrations: {e}"
151149
)),
152150
_ => Err(anyhow::anyhow!(
153-
"Unexpected message type while retrieving account info"
151+
"Unexpected message type while retrieving account registrations"
154152
)),
155153
},
156154
)
@@ -216,16 +214,14 @@ pub async fn handle_account_delegations_blockfrost(
216214
params: Vec<String>,
217215
handlers_config: Arc<HandlersConfig>,
218216
) -> Result<RESTResponse> {
219-
let stake_address = match parse_stake_address(&params) {
217+
let account = match parse_stake_address(&params) {
220218
Ok(addr) => addr,
221219
Err(resp) => return Ok(resp),
222220
};
223221

224222
// Prepare the message
225223
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
226-
AccountsStateQuery::GetAccountDelegationHistory {
227-
account: stake_address,
228-
},
224+
AccountsStateQuery::GetAccountDelegationHistory { account },
229225
)));
230226

231227
// Get delegations from historical accounts state
@@ -243,10 +239,10 @@ pub async fn handle_account_delegations_blockfrost(
243239
Message::StateQueryResponse(StateQueryResponse::Accounts(
244240
AccountsStateQueryResponse::Error(e),
245241
)) => Err(anyhow::anyhow!(
246-
"Internal server error while retrieving account info: {e}"
242+
"Internal server error while retrieving account delegations: {e}"
247243
)),
248244
_ => Err(anyhow::anyhow!(
249-
"Unexpected message type while retrieving account info"
245+
"Unexpected message type while retrieving account delegations"
250246
)),
251247
},
252248
)
@@ -349,10 +345,10 @@ pub async fn handle_account_mirs_blockfrost(
349345
Message::StateQueryResponse(StateQueryResponse::Accounts(
350346
AccountsStateQueryResponse::Error(e),
351347
)) => Err(anyhow::anyhow!(
352-
"Internal server error while retrieving account info: {e}"
348+
"Internal server error while retrieving account mirs: {e}"
353349
)),
354350
_ => Err(anyhow::anyhow!(
355-
"Unexpected message type while retrieving account info"
351+
"Unexpected message type while retrieving account mirs"
356352
)),
357353
},
358354
)
@@ -417,16 +413,14 @@ pub async fn handle_account_withdrawals_blockfrost(
417413
params: Vec<String>,
418414
handlers_config: Arc<HandlersConfig>,
419415
) -> Result<RESTResponse> {
420-
let stake_address = match parse_stake_address(&params) {
416+
let account = match parse_stake_address(&params) {
421417
Ok(addr) => addr,
422418
Err(resp) => return Ok(resp),
423419
};
424420

425421
// Prepare the message
426422
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
427-
AccountsStateQuery::GetAccountRegistrationHistory {
428-
account: stake_address,
429-
},
423+
AccountsStateQuery::GetAccountRegistrationHistory { account },
430424
)));
431425

432426
// Get withdrawals from historical accounts state
@@ -444,10 +438,10 @@ pub async fn handle_account_withdrawals_blockfrost(
444438
Message::StateQueryResponse(StateQueryResponse::Accounts(
445439
AccountsStateQueryResponse::Error(e),
446440
)) => Err(anyhow::anyhow!(
447-
"Internal server error while retrieving account info: {e}"
441+
"Internal server error while retrieving account withdrawals: {e}"
448442
)),
449443
_ => Err(anyhow::anyhow!(
450-
"Unexpected message type while retrieving account info"
444+
"Unexpected message type while retrieving account withdrawals"
451445
)),
452446
},
453447
)
@@ -512,16 +506,14 @@ pub async fn handle_account_rewards_blockfrost(
512506
params: Vec<String>,
513507
handlers_config: Arc<HandlersConfig>,
514508
) -> Result<RESTResponse> {
515-
let stake_address = match parse_stake_address(&params) {
509+
let account = match parse_stake_address(&params) {
516510
Ok(addr) => addr,
517511
Err(resp) => return Ok(resp),
518512
};
519513

520514
// Prepare the message
521515
let msg = Arc::new(Message::StateQuery(StateQuery::Accounts(
522-
AccountsStateQuery::GetAccountRegistrationHistory {
523-
account: stake_address,
524-
},
516+
AccountsStateQuery::GetAccountRewardHistory { account },
525517
)));
526518

527519
// Get rewards from historical accounts state
@@ -539,10 +531,10 @@ pub async fn handle_account_rewards_blockfrost(
539531
Message::StateQueryResponse(StateQueryResponse::Accounts(
540532
AccountsStateQueryResponse::Error(e),
541533
)) => Err(anyhow::anyhow!(
542-
"Internal server error while retrieving account info: {e}"
534+
"Internal server error while retrieving account rewards: {e}"
543535
)),
544536
_ => Err(anyhow::anyhow!(
545-
"Unexpected message type while retrieving account info"
537+
"Unexpected message type while retrieving account rewards"
546538
)),
547539
},
548540
)
@@ -567,7 +559,7 @@ pub async fn handle_account_rewards_blockfrost(
567559
Ok(json) => Ok(RESTResponse::with_json(200, &json)),
568560
Err(e) => Ok(RESTResponse::with_text(
569561
500,
570-
&format!("Internal server error while serializing withdrawal history: {e}"),
562+
&format!("Internal server error while serializing reward history: {e}"),
571563
)),
572564
}
573565
}

0 commit comments

Comments
 (0)