Skip to content

Commit

Permalink
feat: hotfix slice indexing panic (#393)
Browse files Browse the repository at this point in the history
* fix(rm slice indexing): avoid panic catastrophe

* fix(rm slice indexing): avoid panic catastrophe
  • Loading branch information
cong-or authored Apr 9, 2024
1 parent b582ef2 commit f4cad89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
21 changes: 11 additions & 10 deletions catalyst-gateway/bin/src/follower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,17 @@ async fn init_follower(
// Block processing for Eras before staking are ignored.
if valid_era(block.era()) {
// index catalyst registrations
// match db.index_registration_data(block.txs(), slot,
// network).await { Ok(()) => (),
// Err(err) => {
// error!(
// "Unable to index registration data for block {:?} -
// skip..", err
// );
// continue;
// },
// }
match db.index_registration_data(block.txs(), slot, network).await {
Ok(()) => (),
Err(err) => {
error!(
"Unable to index registration data for block {:?} -
skip..",
err
);
continue;
},
}

// Rewards
}
Expand Down
16 changes: 9 additions & 7 deletions catalyst-gateway/bin/src/registration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,25 +338,27 @@ pub fn inspect_rewards_addr(
Ok(rewards_address)
}

#[allow(clippy::indexing_slicing)]
/// Extract Nonce
pub fn inspect_nonce(metamap: &[(Value, Value)]) -> Result<Nonce, Box<dyn Error>> {
let nonce = match metamap[NONCE] {
(Value::Integer(_four), Value::Integer(nonce)) => Nonce(nonce.try_into()?),
let nonce: i128 = match metamap.get(NONCE).ok_or("Issue with nonce parsing")? {
(Value::Integer(_four), Value::Integer(nonce)) => i128::from(*nonce),
_ => return Err("Invalid nonce".to_string().into()),
};
Ok(nonce)

Ok(Nonce(nonce.try_into()?))
}

#[allow(clippy::indexing_slicing)]
/// Extract optional voting purpose
pub fn inspect_voting_purpose(
metamap: &[(Value, Value)],
) -> Result<Option<VotingPurpose>, Box<dyn Error>> {
if metamap.len() == 5 {
match metamap[VOTE_PURPOSE] {
match metamap
.get(VOTE_PURPOSE)
.ok_or("Issue with voting purpose parsing")?
{
(Value::Integer(_five), Value::Integer(purpose)) => {
Ok(Some(VotingPurpose(purpose.try_into()?)))
Ok(Some(VotingPurpose(i128::from(*purpose).try_into()?)))
},
_ => Ok(None),
}
Expand Down

0 comments on commit f4cad89

Please sign in to comment.