Skip to content

Commit

Permalink
R/test utils (#70)
Browse files Browse the repository at this point in the history
Refactor test utilities so that they are common to the contracts.
Also fixes the finality tests under the `full-validation` feature
  • Loading branch information
maurolacy authored Sep 25, 2024
1 parent 70b6af6 commit 9b0bd97
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 323 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/local-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
steps:
- uses: actions/checkout@v4.1.0
- name: Build contracts, check formats, and run unit tests (full validation)
run: cargo test --package btc-staking --lib --features full-validation
run: cargo test --lib --features full-validation
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions contracts/babylon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ default = []
cranelift = ["cosmwasm-vm/cranelift"]
# for quicker tests, cargo test --lib
library = []
# feature for enabling the full validation
full-validation = [ "btc-staking/full-validation" ]

[dependencies]
babylon-apis = { path = "../../packages/apis" }
Expand Down
2 changes: 2 additions & 0 deletions contracts/btc-finality/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ default = []
cranelift = ["cosmwasm-vm/cranelift"]
# for quicker tests, cargo test --lib
library = []
# feature for enabling the full validation
full-validation = [ "btc-staking/full-validation" ]

[dependencies]
babylon-apis = { path = "../../packages/apis" }
Expand Down
108 changes: 0 additions & 108 deletions contracts/btc-finality/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,126 +241,18 @@ pub(crate) fn encode_smart_query<Q: CustomQuery>(

#[cfg(test)]
pub(crate) mod tests {
use std::str::FromStr;

use super::*;

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};
use cosmwasm_std::{
from_json,
testing::{message_info, mock_dependencies, mock_env},
Binary, Decimal,
};
use cw_controllers::AdminResponse;
use hex::ToHex;
use test_utils::{get_btc_delegation, get_finality_provider, get_pub_rand_commit};

pub(crate) const CREATOR: &str = "creator";
pub(crate) const INIT_ADMIN: &str = "initial_admin";
const NEW_ADMIN: &str = "new_admin";

fn new_finality_provider(fp: FinalityProvider) -> NewFinalityProvider {
NewFinalityProvider {
addr: fp.addr,
description: fp.description.map(|desc| FinalityProviderDescription {
moniker: desc.moniker,
identity: desc.identity,
website: desc.website,
security_contact: desc.security_contact,
details: desc.details,
}),
commission: Decimal::from_str(&fp.commission).unwrap(),
btc_pk_hex: fp.btc_pk.encode_hex(),
pop: match fp.pop {
Some(pop) => Some(ProofOfPossessionBtc {
btc_sig_type: pop.btc_sig_type,
btc_sig: Binary::new(pop.btc_sig.to_vec()),
}),
None => None,
},
consumer_id: fp.consumer_id,
}
}

fn new_active_btc_delegation(del: BtcDelegation) -> ActiveBtcDelegation {
let btc_undelegation = del.btc_undelegation.unwrap();

ActiveBtcDelegation {
staker_addr: del.staker_addr,
btc_pk_hex: del.btc_pk.encode_hex(),
fp_btc_pk_list: del
.fp_btc_pk_list
.iter()
.map(|fp_btc_pk| fp_btc_pk.encode_hex())
.collect(),
start_height: del.start_height,
end_height: del.end_height,
total_sat: del.total_sat,
staking_tx: Binary::new(del.staking_tx.to_vec()),
slashing_tx: Binary::new(del.slashing_tx.to_vec()),
delegator_slashing_sig: Binary::new(del.delegator_sig.to_vec()),
covenant_sigs: del
.covenant_sigs
.iter()
.map(|cov_sig| CovenantAdaptorSignatures {
cov_pk: Binary::new(cov_sig.cov_pk.to_vec()),
adaptor_sigs: cov_sig
.adaptor_sigs
.iter()
.map(|adaptor_sig| Binary::new(adaptor_sig.to_vec()))
.collect(),
})
.collect(),
staking_output_idx: del.staking_output_idx,
unbonding_time: del.unbonding_time,
undelegation_info: BtcUndelegationInfo {
unbonding_tx: Binary::new(btc_undelegation.unbonding_tx.to_vec()),
slashing_tx: Binary::new(btc_undelegation.slashing_tx.to_vec()),
delegator_unbonding_sig: Binary::new(
btc_undelegation.delegator_unbonding_sig.to_vec(),
),
delegator_slashing_sig: Binary::new(
btc_undelegation.delegator_slashing_sig.to_vec(),
),
covenant_unbonding_sig_list: vec![],
covenant_slashing_sigs: vec![],
},
params_version: del.params_version,
}
}

/// Build a derived active BTC delegation from the base (from testdata) BTC delegation
pub(crate) fn get_derived_btc_delegation(del_id: i32, fp_ids: &[i32]) -> ActiveBtcDelegation {
let del = get_btc_delegation(del_id, fp_ids.to_vec());
new_active_btc_delegation(del)
}

/// Get public randomness public key, commitment, and signature information
///
/// Signature is a Schnorr signature over the commitment
pub(crate) fn get_public_randomness_commitment() -> (String, PubRandCommit, Vec<u8>) {
let pub_rand_commitment_msg = get_pub_rand_commit();
(
pub_rand_commitment_msg.fp_btc_pk.encode_hex(),
PubRandCommit {
start_height: pub_rand_commitment_msg.start_height,
num_pub_rand: pub_rand_commitment_msg.num_pub_rand,
commitment: pub_rand_commitment_msg.commitment.to_vec(),
},
pub_rand_commitment_msg.sig.to_vec(),
)
}

pub(crate) fn create_new_finality_provider(id: i32) -> NewFinalityProvider {
let fp = get_finality_provider(id);
new_finality_provider(fp)
}

#[test]
fn instantiate_without_admin() {
let mut deps = mock_dependencies();
Expand Down
18 changes: 10 additions & 8 deletions contracts/btc-finality/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ mod instantiation {
mod finality {
use super::*;

use crate::contract::tests::{
create_new_finality_provider, get_derived_btc_delegation, get_public_randomness_commitment,
};
use crate::msg::FinalitySignatureResponse;
use babylon_apis::finality_api::IndexedBlock;
use test_utils::get_public_randomness_commitment;

use cosmwasm_std::Event;
use test_utils::{get_add_finality_sig, get_pub_rand_value};
use test_utils::{
create_new_finality_provider, get_add_finality_sig, get_derived_btc_delegation,
get_pub_rand_value,
};

#[test]
fn commit_public_randomness_works() {
Expand Down Expand Up @@ -259,12 +260,13 @@ mod finality {

mod slashing {
use babylon_apis::finality_api::IndexedBlock;
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_public_randomness_commitment,
use test_utils::{
create_new_finality_provider, get_add_finality_sig, get_add_finality_sig_2,
get_derived_btc_delegation, get_pub_rand_value,
};

use crate::multitest::suite::SuiteBuilder;
use test_utils::get_public_randomness_commitment;

#[test]
fn slashing_works() {
Expand Down
12 changes: 10 additions & 2 deletions contracts/btc-finality/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result as AnyResult;
use derivative::Derivative;
use hex::ToHex;

use cosmwasm_std::Addr;
use cosmwasm_std::{to_json_binary, Addr};

use cw_multi_test::{AppResponse, Contract, ContractWrapper, Executor};

Expand All @@ -12,6 +12,7 @@ use babylon_apis::{btc_staking_api, finality_api};
use babylon_bindings::BabylonMsg;
use babylon_bindings_test::BabylonApp;
use babylon_bitcoin::chain_params::Network;

use btc_staking::msg::{ActivatedHeightResponse, FinalityProviderInfo};

use crate::msg::{EvidenceResponse, FinalitySignatureResponse};
Expand Down Expand Up @@ -75,6 +76,7 @@ impl SuiteBuilder {
let btc_finality_code_id =
app.store_code_with_creator(owner.clone(), contract_btc_finality());
let contract_code_id = app.store_code_with_creator(owner.clone(), contract_babylon());
let staking_params = btc_staking::test_utils::staking_params();
let contract = app
.instantiate_contract(
contract_code_id,
Expand All @@ -86,7 +88,13 @@ impl SuiteBuilder {
checkpoint_finalization_timeout: 10,
notify_cosmos_zone: false,
btc_staking_code_id: Some(btc_staking_code_id),
btc_staking_msg: None,
btc_staking_msg: Some(
to_json_binary(&btc_staking::msg::InstantiateMsg {
params: Some(staking_params),
admin: None,
})
.unwrap(),
),
btc_finality_code_id: Some(btc_finality_code_id),
btc_finality_msg: None,
admin: Some(owner.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion contracts/btc-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ babylon-proto = { path = "../../packages/proto" }
babylon-btcstaking = { path = "../../packages/btcstaking" }
babylon-bitcoin = { path = "../../packages/bitcoin" }
eots = { path = "../../packages/eots" }
test-utils = { path = "../../packages/test-utils" }

bitcoin = { workspace = true }
cosmwasm-schema = { workspace = true }
Expand All @@ -60,7 +61,6 @@ thiserror = { workspace = true }
cw-controllers = { workspace = true }

[dev-dependencies]
test-utils = { path = "../../packages/test-utils" }
babylon-proto = { path = "../../packages/proto" }
cosmwasm-vm = { workspace = true }
prost = { workspace = true }
Loading

0 comments on commit 9b0bd97

Please sign in to comment.