Skip to content

Commit 769be78

Browse files
authored
feat: op consumer chain slashing (2/2) (#98)
## Summary This is a follow-on PR from babylonlabs-io/cosmos-bsn-contracts#92 (implement slashing for op stack consumer chains) It updates the `EquivocationEvidence` msg to match the latest Babylon update ## Test plan ```bash # build the contract cargo build cargo optimize # test it cargo test cargo integration ```
1 parent 976b919 commit 769be78

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

contracts/op-finality-gadget/src/contract.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ pub fn execute(
8282
} => handle_finality_signature(
8383
deps,
8484
env,
85+
info,
8586
&fp_pubkey_hex,
8687
height,
8788
&pub_rand,
8889
&proof,
8990
&block_hash,
9091
&signature,
9192
),
92-
ExecuteMsg::Slashing { evidence } => handle_slashing(&evidence),
93+
ExecuteMsg::Slashing { sender, evidence } => handle_slashing(&sender, &evidence),
9394
ExecuteMsg::SetEnabled { enabled } => set_enabled(deps, info, enabled),
9495
ExecuteMsg::UpdateAdmin { admin } => ADMIN
9596
.execute_update_admin(deps, info, Some(api.addr_validate(&admin)?))

contracts/op-finality-gadget/src/exec/finality.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use babylon_bindings::BabylonMsg;
1313

1414
use babylon_apis::finality_api::{Evidence, PubRandCommit};
1515
use babylon_merkle::Proof;
16-
use cosmwasm_std::{to_json_binary, Deps, DepsMut, Env, Event, Response, WasmMsg};
16+
use cosmwasm_std::{
17+
to_json_binary, Addr, Deps, DepsMut, Env, Event, MessageInfo, Response, WasmMsg,
18+
};
1719
use k256::ecdsa::signature::Verifier;
1820
use k256::schnorr::{Signature, VerifyingKey};
1921
use k256::sha2::{Digest, Sha256};
@@ -114,6 +116,7 @@ pub(crate) fn verify_commitment_signature(
114116
pub fn handle_finality_signature(
115117
deps: DepsMut,
116118
env: Env,
119+
info: MessageInfo,
117120
fp_btc_pk_hex: &str,
118121
height: u64,
119122
pub_rand: &[u8],
@@ -211,7 +214,7 @@ pub fn handle_finality_signature(
211214

212215
// slash this finality provider, including setting its voting power to
213216
// zero, extracting its BTC SK, and emit an event
214-
let (msg, ev) = slash_finality_provider(&env, fp_btc_pk_hex, &evidence)?;
217+
let (msg, ev) = slash_finality_provider(&env, &info, fp_btc_pk_hex, &evidence)?;
215218
res = res.add_message(msg);
216219
res = res.add_event(ev);
217220
}
@@ -311,6 +314,7 @@ fn check_fp_exist(deps: Deps, fp_pubkey_hex: &str) -> Result<(), ContractError>
311314
/// its voting power to zero, extracting its BTC SK, and emitting an event
312315
fn slash_finality_provider(
313316
env: &Env,
317+
info: &MessageInfo,
314318
fp_btc_pk_hex: &str,
315319
evidence: &Evidence,
316320
) -> Result<(WasmMsg, Event), ContractError> {
@@ -328,6 +332,7 @@ fn slash_finality_provider(
328332
// Emit slashing event.
329333
// Raises slashing event to babylon over IBC.
330334
let msg = ExecuteMsg::Slashing {
335+
sender: info.sender.clone(),
331336
evidence: evidence.clone(),
332337
};
333338
let wasm_msg: WasmMsg = WasmMsg::Execute {
@@ -357,12 +362,22 @@ fn slash_finality_provider(
357362
Ok((wasm_msg, ev))
358363
}
359364

360-
pub(crate) fn handle_slashing(evidence: &Evidence) -> Result<Response<BabylonMsg>, ContractError> {
365+
pub(crate) fn handle_slashing(
366+
sender: &Addr,
367+
evidence: &Evidence,
368+
) -> Result<Response<BabylonMsg>, ContractError> {
361369
let mut res = Response::new();
362370
// Send msg to Babylon
363371

364372
let msg = BabylonMsg::EquivocationEvidence {
365-
evidence: Some(evidence.clone()),
373+
signer: sender.to_string(),
374+
fp_btc_pk: evidence.fp_btc_pk.clone(),
375+
block_height: evidence.block_height,
376+
pub_rand: evidence.pub_rand.clone(),
377+
canonical_app_hash: evidence.canonical_app_hash.clone(),
378+
fork_app_hash: evidence.fork_app_hash.clone(),
379+
canonical_finality_sig: evidence.canonical_finality_sig.clone(),
380+
fork_finality_sig: evidence.fork_finality_sig.clone(),
366381
};
367382

368383
// Convert to CosmosMsg
@@ -376,8 +391,9 @@ pub(crate) fn handle_slashing(evidence: &Evidence) -> Result<Response<BabylonMsg
376391
#[cfg(test)]
377392
pub(crate) mod tests {
378393
use super::*;
379-
use cosmwasm_std::from_json;
380394
use cosmwasm_std::testing::mock_env;
395+
use cosmwasm_std::Addr;
396+
use cosmwasm_std::{from_json, testing::message_info};
381397
use std::collections::HashMap;
382398

383399
use babylon_apis::finality_api::PubRandCommit;
@@ -471,9 +487,9 @@ pub(crate) mod tests {
471487

472488
// Create mock environment
473489
let env = mock_env(); // You'll need to add this mock helper
474-
490+
let info = message_info(&Addr::unchecked("test"), &[]);
475491
// Test slash_finality_provider
476-
let (wasm_msg, event) = slash_finality_provider(&env, &pk_hex, &evidence).unwrap();
492+
let (wasm_msg, event) = slash_finality_provider(&env, &info, &pk_hex, &evidence).unwrap();
477493

478494
// Verify the WasmMsg is correctly constructed
479495
match wasm_msg {
@@ -487,6 +503,7 @@ pub(crate) mod tests {
487503
let msg_evidence = from_json::<ExecuteMsg>(&msg).unwrap();
488504
match msg_evidence {
489505
ExecuteMsg::Slashing {
506+
sender: _,
490507
evidence: msg_evidence,
491508
} => {
492509
assert_eq!(evidence, msg_evidence);

contracts/op-finality-gadget/src/msg.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {
66

77
use babylon_apis::finality_api::Evidence;
88
use cosmwasm_schema::{cw_serde, QueryResponses};
9-
use cosmwasm_std::Binary;
9+
use cosmwasm_std::{Addr, Binary};
1010

1111
use babylon_merkle::Proof;
1212

@@ -80,6 +80,7 @@ pub enum ExecuteMsg {
8080
///
8181
/// This message can be called by the admin only.
8282
Slashing {
83+
sender: Addr,
8384
evidence: Evidence,
8485
},
8586
/// Enable or disable finality gadget.

0 commit comments

Comments
 (0)