Skip to content

Commit

Permalink
Fix: proper routing / auth of slashing message
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Lacy committed Sep 17, 2024
1 parent 1e479bd commit 1a6708e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
35 changes: 25 additions & 10 deletions contracts/babylon/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use crate::error::ContractError;
use cosmwasm_std::{
to_json_binary, Addr, Binary, Deps, DepsMut, Empty, Env, MessageInfo, QueryResponse, Reply,
Response, SubMsg, SubMsgResponse, WasmMsg,
};
use cw2::set_contract_version;
use cw_utils::ParseReplyError;

use babylon_apis::btc_staking_api;
use babylon_bindings::BabylonMsg;

use crate::error::ContractError;
use crate::ibc::{ibc_packet, IBC_CHANNEL};
use crate::msg::contract::{ContractMsg, ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::queries;
use crate::state::btc_light_client;
use crate::state::config::{Config, CONFIG};
use babylon_bindings::BabylonMsg;

pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -202,20 +204,33 @@ pub fn execute(
Ok(Response::new())
}
ExecuteMsg::Slashing { evidence } => {
// This is an internal routing message from the `btc-staking` contract
// This is an internal routing message from the `btc_finality` contract
let cfg = CONFIG.load(deps.storage)?;
// Check sender
let btc_staking = CONFIG
.load(deps.storage)?
.btc_staking
.ok_or(ContractError::BtcStakingNotSet {})?;
if info.sender != btc_staking {
let btc_finality = cfg
.btc_finality
.ok_or(ContractError::BtcFinalityNotSet {})?;
if info.sender != btc_finality {
return Err(ContractError::Unauthorized {});
}
// Send to the staking contract for processing
let btc_staking = cfg.btc_staking.ok_or(ContractError::BtcStakingNotSet {})?;
// Slashes this finality provider, i.e., sets its slashing height to the block height
// and its power to zero
let msg = btc_staking_api::ExecuteMsg::Slash {
fp_btc_pk_hex: hex::encode(evidence.fp_btc_pk.clone()),
};
let wasm_msg = WasmMsg::Execute {
contract_addr: btc_staking.to_string(),
msg: to_json_binary(&msg)?,
funds: vec![],
};

// Send over IBC to the Provider (Babylon)
let channel = IBC_CHANNEL.load(deps.storage)?;
let msg = ibc_packet::slashing_msg(&env, &channel, &evidence)?;
let ibc_msg = ibc_packet::slashing_msg(&env, &channel, &evidence)?;
// TODO: Add events
Ok(Response::new().add_message(msg))
Ok(Response::new().add_message(wasm_msg).add_message(ibc_msg))
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/babylon/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum ContractError {
Unauthorized {},
#[error("The BTC staking contract is not set")]
BtcStakingNotSet {},
#[error("The BTC finality contract is not set")]
BtcFinalityNotSet {},
#[error("Invalid configuration: {msg}")]
InvalidConfig { msg: String },
}
Expand Down
6 changes: 3 additions & 3 deletions packages/apis/src/btc_staking_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub enum ExecuteMsg {
unbonded_del: Vec<UnbondedBtcDelegation>,
},
/// Slash finality provider staking power.
/// Used by the finality contract only.
/// The finality contract will call this message to slash the finality provider's staking power
/// when the finality provider is found to be malicious
/// Used by the babylon-contract only.
/// The Babylon contract will call this message to set the finality provider's staking power to
/// zero when the finality provider is found to be malicious by the finality contract.
Slash { fp_btc_pk_hex: String },
}

Expand Down

0 comments on commit 1a6708e

Please sign in to comment.