Skip to content

Commit

Permalink
Remove original finality (no mt) tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Lacy committed Sep 24, 2024
1 parent bca20d1 commit 543e639
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 278 deletions.
17 changes: 1 addition & 16 deletions contracts/btc-finality/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,12 @@ pub(crate) mod tests {

use super::*;

use crate::state::config::Params;
use babylon_apis::btc_staking_api::{
ActiveBtcDelegation, BtcUndelegationInfo, CovenantAdaptorSignatures,
FinalityProviderDescription, NewFinalityProvider, ProofOfPossessionBtc,
};
use babylon_apis::finality_api::PubRandCommit;
use babylon_proto::babylon::btcstaking::v1::{
BtcDelegation, FinalityProvider, Params as ProtoParams,
};
use babylon_proto::babylon::btcstaking::v1::{BtcDelegation, FinalityProvider};
use cosmwasm_std::{
from_json,
testing::{message_info, mock_dependencies, mock_env},
Expand All @@ -269,18 +266,6 @@ pub(crate) mod tests {
pub(crate) const INIT_ADMIN: &str = "initial_admin";
const NEW_ADMIN: &str = "new_admin";

fn new_params(params: ProtoParams) -> Params {
Params {
max_active_finality_providers: params.max_active_finality_providers,
min_pub_rand: 10, // TODO: fix this
}
}

pub(crate) fn get_params() -> Params {
let proto_params = test_utils::get_params();
new_params(proto_params)
}

fn new_finality_provider(fp: FinalityProvider) -> NewFinalityProvider {
NewFinalityProvider {
addr: fp.addr,
Expand Down
262 changes: 0 additions & 262 deletions contracts/btc-finality/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,265 +581,3 @@ pub fn list_fps_by_power(
let res: FinalityProvidersByPowerResponse = querier.query(&query)?;
Ok(res.fps)
}

#[cfg(test)]
mod tests {
use hex::ToHex;

use cosmwasm_std::testing::{
message_info, mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage,
};
use cosmwasm_std::{to_json_binary, Binary, Env, OwnedDeps, Response, SubMsg, WasmMsg};

use babylon_apis::btc_staking_api;
use babylon_apis::finality_api::{ExecuteMsg, IndexedBlock, SudoMsg};
use babylon_bindings::BabylonMsg;
use test_utils::{get_add_finality_sig, get_add_finality_sig_2, get_pub_rand_value};

use crate::contract::tests::{
create_new_finality_provider, get_derived_btc_delegation, get_params,
get_public_randomness_commitment,
};
use crate::contract::{execute, instantiate};
use crate::error::ContractError;
use crate::msg::InstantiateMsg;
use crate::queries::{block, evidence};

const CREATOR: &str = "creator";

fn mock_env_height(height: u64) -> Env {
let mut env = mock_env();
env.block.height = height;

env
}

#[track_caller]
pub fn call_begin_block(
deps: &mut OwnedDeps<MockStorage, MockApi, MockQuerier>,
app_hash: &[u8],
height: u64,
) -> Result<Response<BabylonMsg>, ContractError> {
let env = mock_env_height(height);
// Hash is not used in the begin-block handler
let hash_hex = "deadbeef".to_string();
let app_hash_hex = app_hash.encode_hex();

crate::contract::sudo(
deps.as_mut(),
env.clone(),
SudoMsg::BeginBlock {
hash_hex,
app_hash_hex,
},
)
}

#[track_caller]
pub fn call_end_block(
deps: &mut OwnedDeps<MockStorage, MockApi, MockQuerier>,
app_hash: &[u8],
height: u64,
) -> Result<Response<BabylonMsg>, ContractError> {
let env = mock_env_height(height);
// Hash is not used in the end-block handler
let hash_hex = "deadbeef".to_string();
let app_hash_hex = app_hash.encode_hex();

crate::contract::sudo(
deps.as_mut(),
env.clone(),
SudoMsg::EndBlock {
hash_hex,
app_hash_hex,
},
)
}

#[test]
#[ignore]
fn slashing_works() {
let mut deps = mock_dependencies();
let info = message_info(&deps.api.addr_make(CREATOR), &[]);

// Read public randomness commitment test data
let (pk_hex, pub_rand, pubrand_signature) = get_public_randomness_commitment();
let pub_rand_one = get_pub_rand_value();
// Read equivalent / consistent add finality signature test data
let add_finality_signature = get_add_finality_sig();
let proof = add_finality_signature.proof.unwrap();

let initial_height = pub_rand.start_height;
let initial_env = mock_env_height(initial_height);

instantiate(
deps.as_mut(),
initial_env.clone(),
info.clone(),
InstantiateMsg {
params: Some(get_params()),
admin: None,
},
)
.unwrap();

// Register one FP
// NOTE: the test data ensures that pub rand commit / finality sig are
// signed by the 1st FP
let new_fp = create_new_finality_provider(1);
assert_eq!(new_fp.btc_pk_hex, pk_hex);

let msg = btc_staking_api::ExecuteMsg::BtcStaking {
new_fp: vec![new_fp.clone()],
active_del: vec![],
slashed_del: vec![],
unbonded_del: vec![],
};

let _res =
btc_staking::contract::execute(deps.as_mut(), initial_env.clone(), info.clone(), msg)
.unwrap();

// Add a delegation, so that the finality provider has some power
let mut del1 = get_derived_btc_delegation(1, &[1]);
del1.fp_btc_pk_list = vec![pk_hex.clone()];

let msg = btc_staking_api::ExecuteMsg::BtcStaking {
new_fp: vec![],
active_del: vec![del1.clone()],
slashed_del: vec![],
unbonded_del: vec![],
};

btc_staking::contract::execute(deps.as_mut(), initial_env.clone(), info.clone(), msg)
.unwrap();

// Check that the finality provider power has been updated
let fp_info = btc_staking::queries::finality_provider_info(
deps.as_ref(),
new_fp.btc_pk_hex.clone(),
None,
)
.unwrap();
assert_eq!(fp_info.power, del1.total_sat);

// Submit public randomness commitment for the FP and the involved heights
let msg = ExecuteMsg::CommitPublicRandomness {
fp_pubkey_hex: pk_hex.clone(),
start_height: pub_rand.start_height,
num_pub_rand: pub_rand.num_pub_rand,
commitment: pub_rand.commitment.into(),
signature: pubrand_signature.into(),
};

execute(deps.as_mut(), initial_env, info.clone(), msg).unwrap();

// Call the begin-block sudo handler at the next height, for completeness
let next_height = initial_height + 1;
call_begin_block(
&mut deps,
&add_finality_signature.block_app_hash,
next_height,
)
.unwrap();

// Call the end-block sudo handler, so that the block is indexed in the store
call_end_block(
&mut deps,
&add_finality_signature.block_app_hash,
next_height,
)
.unwrap();

// Submit a finality signature from that finality provider at next height (initial_height + 1)
let submit_height = next_height;
// Increase block height
let next_height = next_height + 1;
let next_env = mock_env_height(next_height);
// Call the begin-block sudo handler at the next height, for completeness
call_begin_block(&mut deps, "deadbeef01".as_bytes(), next_height).unwrap();

let finality_signature = add_finality_signature.finality_sig.to_vec();
let msg = ExecuteMsg::SubmitFinalitySignature {
fp_pubkey_hex: pk_hex.clone(),
height: submit_height,
pub_rand: pub_rand_one.clone().into(),
proof: proof.clone().into(),
block_hash: add_finality_signature.block_app_hash.to_vec().into(),
signature: Binary::new(finality_signature.clone()),
};

let res = execute(deps.as_mut(), next_env.clone(), info.clone(), msg.clone()).unwrap();
assert_eq!(0, res.messages.len());
assert_eq!(0, res.events.len());

// Submitting the same signature twice is tolerated
let res = execute(deps.as_mut(), next_env.clone(), info.clone(), msg).unwrap();
assert_eq!(0, res.messages.len());
assert_eq!(0, res.events.len());

// Submit another (different and valid) finality signature, from the same finality provider
// at the same height
let add_finality_signature_2 = get_add_finality_sig_2();
let msg = ExecuteMsg::SubmitFinalitySignature {
fp_pubkey_hex: pk_hex.clone(),
height: submit_height,
pub_rand: pub_rand_one.clone().into(),
proof: proof.into(),
block_hash: add_finality_signature_2.block_app_hash.to_vec().into(),
signature: Binary::new(add_finality_signature_2.finality_sig.to_vec()),
};
let res = execute(deps.as_mut(), next_env.clone(), info.clone(), msg).unwrap();

// Assert the double signing evidence is proper
let btc_pk = hex::decode(pk_hex.clone()).unwrap();
let evidence = evidence(deps.as_ref(), pk_hex.clone(), submit_height)
.unwrap()
.evidence
.unwrap();
assert_eq!(evidence.block_height, submit_height);
assert_eq!(evidence.fp_btc_pk, btc_pk);

// Assert the slashing propagation msg is there
assert_eq!(1, res.messages.len());
// Assert the slashing propagation msg is proper
let babylon_addr = btc_staking::queries::config(deps.as_ref()).unwrap().babylon;
// Assert the slashing event is there
assert_eq!(1, res.events.len());
// Assert the slashing event is proper
assert_eq!(res.events[0].ty, "slashed_finality_provider".to_string());
assert_eq!(
res.messages[0],
SubMsg::new(WasmMsg::Execute {
contract_addr: babylon_addr.to_string(),
msg: to_json_binary(&babylon_contract::ExecuteMsg::Slashing { evidence }).unwrap(),
funds: vec![]
})
);

// Call the end-block sudo handler for completeness / realism
call_end_block(&mut deps, "deadbeef01".as_bytes(), next_height).unwrap();

// Call the next (final) block begin blocker, to compute the active FP set
let final_height = next_height + 1;
call_begin_block(&mut deps, "deadbeef02".as_bytes(), final_height).unwrap();

// Call the next (final) block end blocker, to process the finality signatures
call_end_block(&mut deps, "deadbeef02".as_bytes(), final_height).unwrap();

// Assert the canonical block has been indexed (and finalised)
let indexed_block = block(deps.as_ref(), submit_height).unwrap();
assert_eq!(
indexed_block,
IndexedBlock {
height: submit_height,
app_hash: add_finality_signature.block_app_hash.to_vec(),
finalized: true,
}
);

// Assert the finality provider has been slashed
let fp = btc_staking::queries::finality_provider(deps.as_ref(), pk_hex).unwrap();
assert_eq!(fp.slashed_height, submit_height);
}
}

0 comments on commit 543e639

Please sign in to comment.