Skip to content

Commit

Permalink
test: refactor test data generation (#51)
Browse files Browse the repository at this point in the history
This PR refactors the tests, including

- making
[create_new_finality_provider](https://github.com/babylonlabs-io/babylon-contract/blob/c8f6ab64ac00a934bb1570d5188f2405da7a09dc/contracts/btc-staking/src/contract.rs#L401-L402)
generate valid finality providers
- making
[get_derived_btc_delegation](https://github.com/babylonlabs-io/babylon-contract/blob/3335ab7b6a3b82be241852eb8db6a523bf62b699/contracts/btc-staking/src/contract.rs#L351-L352)
generate valid BTC delegations
- removing the babylon-private dependency

This is the prerequisite for testing the full validation of finality
providers / BTC delegations.

---------

Co-authored-by: Mauro Lacy <[email protected]>
  • Loading branch information
SebastianElvis and Mauro Lacy authored Sep 3, 2024
1 parent 4c3bd45 commit a36f194
Show file tree
Hide file tree
Showing 48 changed files with 511 additions and 382 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
[submodule "packages/proto/babylon"]
path = packages/proto/babylon
url = [email protected]:babylonlabs-io/babylon.git
branch = dev
[submodule "packages/proto/babylon-private"]
path = packages/proto/babylon-private
url = [email protected]:babylonlabs-io/babylon-private.git
branch = base/consumer-chain-support
2 changes: 1 addition & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This document describes the process for `babylon-contract` releases.
* A release is initiated by creating a release branch `release-a.b` marking the major/minor release.
* Before beginning a release branch, check that dependencies have been pulled up to date:
* Protobuf references and generation
* `cd packages/proto/; cd babylon-private; git fetch; git pull --rebase; cd ../..; git add babylon-private; git commit -m "pull in latest babylon-private"`
* `cd packages/proto/; cd babylon; git fetch; git pull --rebase; cd ../..; git add babylon; git commit -m "pull in latest babylon"`
* `cd ../..; cargo run-script gen-proto; git add packages/proto; git commit -m "update gen proto"`
* Then build and run tests to ensure the new proto is working.
* `cargo build; cargo test`
Expand Down
105 changes: 45 additions & 60 deletions contracts/btc-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,32 +275,53 @@ fn handle_end_block(

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

use hex::ToHex;
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 babylon_apis::btc_staking_api::{
ActiveBtcDelegation, BtcUndelegationInfo, CovenantAdaptorSignatures,
FinalityProviderDescription, NewFinalityProvider, ProofOfPossessionBtc,
};
use babylon_apis::finality_api::PubRandCommit;

use test_utils::{get_btc_delegation_and_params, get_pub_rand_commit};
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";

/// Build an active BTC delegation from a BTC delegation
pub(crate) fn get_active_btc_delegation() -> ActiveBtcDelegation {
let (del, _) = get_btc_delegation_and_params();
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 {
Expand Down Expand Up @@ -347,37 +368,16 @@ pub(crate) mod tests {
}
}

/// Build an active BTC delegation from a BTC delegation
pub(crate) fn get_active_btc_delegation() -> ActiveBtcDelegation {
let del = get_btc_delegation(1, vec![1]);
new_active_btc_delegation(del)
}

// Build a derived active BTC delegation from the base (from testdata) BTC delegation
pub(crate) fn get_derived_btc_delegation(del_id: u8, fp_ids: &[u8]) -> ActiveBtcDelegation {
assert!(
0 < del_id && del_id < 10,
"Derived delegation id must be between 1 and 9"
);
fp_ids.iter().for_each(|&fp_id| {
assert!(
0 < fp_id && fp_id < 10,
"Derived FP ids must be between 1 and 9"
)
});
let mut del = get_active_btc_delegation();

// Change the BTC public key and the finality provider public key list based on the id
del.btc_pk_hex = format!("d{del_id}");
del.fp_btc_pk_list = fp_ids.iter().map(|fp_id| format!("f{fp_id}")).collect();

// Avoid repeated staking tx hash
let mut staking_tx = del.staking_tx.to_vec();
// FIXME: First byte can be 0xff, just by chance
staking_tx[0] += del_id - 1;
// FIXME: Binary breaks lexicographical order.
del.staking_tx = Binary::new(staking_tx);

// Avoid repeated slashing tx hash
let mut slashing_tx = del.slashing_tx.to_vec();
slashing_tx[0] += del_id - 1;
del.slashing_tx = Binary::new(slashing_tx);

del
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
Expand All @@ -397,23 +397,8 @@ pub(crate) mod tests {
}

pub(crate) fn create_new_finality_provider(id: i32) -> NewFinalityProvider {
NewFinalityProvider {
addr: format!("a{}", id),
description: Some(FinalityProviderDescription {
moniker: format!("fp{}", id),
identity: format!("Finality Provider {}", id),
website: format!("https:://fp{}.com", id),
security_contact: "security_contact".to_string(),
details: format!("details fp{}", id),
}),
commission: Decimal::percent(5),
btc_pk_hex: format!("f{}", id),
pop: Some(ProofOfPossessionBtc {
btc_sig_type: 0,
btc_sig: Binary::new(vec![]),
}),
consumer_id: format!("osmosis-{}", id),
}
let fp = get_finality_provider(id);
new_finality_provider(fp)
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions contracts/btc-staking/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ pub(crate) mod tests {
assert_eq!(res.height, 0);

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

let msg = ExecuteMsg::BtcStaking {
Expand Down Expand Up @@ -794,7 +794,7 @@ pub(crate) mod tests {
let _res = 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 = crate::contract::tests::get_derived_btc_delegation(1, &[]);
let mut del1 = crate::contract::tests::get_derived_btc_delegation(1, &[1]);
del1.fp_btc_pk_list = vec![pk_hex.clone()];

let msg = ExecuteMsg::BtcStaking {
Expand Down Expand Up @@ -950,7 +950,7 @@ pub(crate) mod tests {
let _res = 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 = crate::contract::tests::get_derived_btc_delegation(1, &[]);
let mut del1 = crate::contract::tests::get_derived_btc_delegation(1, &[1]);
del1.fp_btc_pk_list = vec![pk_hex.clone()];

let msg = ExecuteMsg::BtcStaking {
Expand Down
Loading

0 comments on commit a36f194

Please sign in to comment.