Skip to content

Commit

Permalink
fix: Fp info query should return err for non existent Fp's (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 authored and SebastianElvis committed Sep 27, 2024
1 parent ce56239 commit 4dff495
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 13 additions & 2 deletions contracts/btc-staking/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ pub fn finality_provider_info(
deps: Deps,
btc_pk_hex: String,
height: Option<u64>,
) -> StdResult<FinalityProviderInfo> {
) -> Result<FinalityProviderInfo, ContractError> {
let fp_state = match height {
Some(h) => fps().may_load_at_height(deps.storage, &btc_pk_hex, h),
None => fps().may_load(deps.storage, &btc_pk_hex),
}?
.unwrap_or_default();
.ok_or_else(|| ContractError::FinalityProviderNotFound(btc_pk_hex.clone()))?;

Ok(FinalityProviderInfo {
btc_pk_hex,
Expand Down Expand Up @@ -710,6 +710,17 @@ mod tests {
power: 250,
}
);

// Query finality provider info for a non-existent FP
let non_existent_fp = "010203040506".to_string();
let result =
crate::queries::finality_provider_info(deps.as_ref(), non_existent_fp.clone(), None);

// Assert that the result is a FinalityProviderNotFound error
assert!(matches!(
result,
Err(ContractError::FinalityProviderNotFound(pk)) if pk == non_existent_fp
));
}

#[test]
Expand Down
8 changes: 5 additions & 3 deletions contracts/btc-staking/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,11 @@ pub(crate) mod tests {
.clone_from(&active_delegation.fp_btc_pk_list[0]);

// Check that the finality provider has no power yet
let res = queries::finality_provider_info(deps.as_ref(), new_fp.btc_pk_hex.clone(), None)
.unwrap();
assert_eq!(res.power, 0);
let res = queries::finality_provider_info(deps.as_ref(), new_fp.btc_pk_hex.clone(), None);
assert!(matches!(
res,
Err(ContractError::FinalityProviderNotFound(pk)) if pk == new_fp.btc_pk_hex
));

let msg = ExecuteMsg::BtcStaking {
new_fp: vec![new_fp.clone()],
Expand Down

0 comments on commit 4dff495

Please sign in to comment.