Skip to content

Commit

Permalink
Merge branch 'main' into verify-covenant-sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Sep 16, 2024
2 parents 7017177 + d53c5f6 commit 8d333e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions contracts/btc-staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub enum ContractError {
FinalityProviderNotFound(String),
#[error("Staking tx hash already exists: {0}")]
DelegationAlreadyExists(String),
#[error("BTC delegation is not active")]
DelegationIsNotActive,
#[error("BTC delegation is not active: {0}")]
DelegationIsNotActive(String),
#[error("Invalid covenant signature: {0}")]
InvalidCovenantSig(String),
#[error("Invalid Btc tx: {0}")]
Expand Down
11 changes: 8 additions & 3 deletions contracts/btc-staking/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,13 @@ fn handle_undelegation(

// Ensure the BTC delegation is active
if !btc_del.is_active() {
return Err(ContractError::DelegationIsNotActive);
return Err(ContractError::DelegationIsNotActive(
staking_tx_hash.to_string(),
));
}

// verify the early unbonded delegation (full or lite)
let params = PARAMS.load(storage)?;
verify_undelegation(&params, &btc_del, &undelegation.unbonding_tx_sig)?;

// Add the signature to the BTC delegation's undelegation and set back
Expand Down Expand Up @@ -278,12 +281,14 @@ fn handle_slashed_delegation(

// Ensure the BTC delegation is active
if !btc_del.is_active() {
return Err(ContractError::DelegationIsNotActive);
return Err(ContractError::DelegationIsNotActive(
staking_tx_hash.to_string(),
));
}

// verify the slashed delegation (full or lite)
let recovered_fp_sk_hex = delegation.recovered_fp_btc_sk.clone();
verify_slashed_delegation(&btc_del, recovered_fp_sk_hex)?;
verify_slashed_delegation(&btc_del, &recovered_fp_sk_hex)?;

// Discount the voting power from the affected finality providers
let affected_fps = DELEGATION_FPS.load(storage, staking_tx_hash.as_ref())?;
Expand Down
30 changes: 16 additions & 14 deletions contracts/btc-staking/src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ fn verify_pop(
}

#[cfg(feature = "full-validation")]
fn get_pks(
staker_pk_hex: String,
fp_pk_hex_list: Vec<String>,
cov_pk_hex_list: Vec<String>,
fn decode_pks(
staker_pk_hex: &str,
fp_pk_hex_list: &[String],
cov_pk_hex_list: &[String],
) -> Result<(VerifyingKey, Vec<VerifyingKey>, Vec<VerifyingKey>), ContractError> {
// get staker's public key
let staker_pk_bytes =
Expand Down Expand Up @@ -139,10 +139,10 @@ pub fn verify_active_delegation(
// TODO: fix contract size when full-validation is enabled
#[cfg(feature = "full-validation")]
{
let (staker_pk, fp_pks, cov_pks) = get_pks(
active_delegation.btc_pk_hex.clone(),
active_delegation.fp_btc_pk_list.clone(),
params.covenant_pks.clone(),
let (staker_pk, fp_pks, cov_pks) = decode_pks(
&active_delegation.btc_pk_hex,
&active_delegation.fp_btc_pk_list,
&params.covenant_pks,
)?;

// Check if data provided in request, matches data to which staking tx is
Expand Down Expand Up @@ -434,10 +434,10 @@ pub fn verify_undelegation(
*/

// get keys
let (staker_pk, fp_pks, cov_pks) = get_pks(
btc_del.btc_pk_hex.clone(),
btc_del.fp_btc_pk_list.clone(),
params.covenant_pks.clone(),
let (staker_pk, fp_pks, cov_pks) = decode_pks(
&btc_del.btc_pk_hex,
&btc_del.fp_btc_pk_list,
&params.covenant_pks,
)?;

// get the unbonding path script
Expand Down Expand Up @@ -484,7 +484,7 @@ pub fn verify_undelegation(

pub fn verify_slashed_delegation(
active_delegation: &BtcDelegation,
slashed_fp_sk_hex: String,
slashed_fp_sk_hex: &str,
) -> Result<(), ContractError> {
// The following code is marked with `#[cfg(feature = "full-validation")]`
// so that it is included in the build if the `full-validation` feature is
Expand All @@ -511,7 +511,9 @@ pub fn verify_slashed_delegation(
.fp_btc_pk_list
.contains(&slashed_fp_pk_hex)
{
return Err(ContractError::FinalityProviderNotRegistered);
return Err(ContractError::FinalityProviderNotFound(
slashed_fp_pk_hex.to_string(),
));
}
}

Expand Down

0 comments on commit 8d333e1

Please sign in to comment.