Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fp info query should return err for non existent Fp's #71

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions contracts/btc-staking/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,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 @@ -671,6 +671,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 @@ -533,9 +533,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