Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R/test utils #70

Merged
merged 60 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
21760a8
Move active fps calca to finality mod
Sep 11, 2024
7892932
Fix: Non-zero fp voting power check
Sep 11, 2024
ab5e5ea
Move FP_SET to finality state where it belongs
Sep 12, 2024
12f826d
Split staking and finality execution handlers
Sep 12, 2024
5ee7987
Move finality stuff out of btc-staking and into babylon-finality
Sep 12, 2024
c56df77
Add babylon-finality contract (WIP)
Sep 13, 2024
806409e
Fix rebase errors
Sep 15, 2024
44f1919
Remove sudo handlers from the staking contract
Sep 15, 2024
3177ada
Add staking slash handler / msg
Sep 15, 2024
065e1ef
Refactor: use staking queries, and batched active FPs processing
Sep 16, 2024
b09b542
cargo fmt
Sep 16, 2024
262b4a4
Add babylon contract version to store in passing
Sep 17, 2024
63cdd1f
Add finality contract instantiation
Sep 17, 2024
4b6bd78
Remove finality dependendency
Sep 17, 2024
feed346
Rename to btc-finality for consistency
Sep 17, 2024
246ea4b
Refactor: route slashing through babylon only
Sep 17, 2024
a145f7e
Fix: proper routing / auth of slashing message
Sep 17, 2024
7d4c030
Update / Set staking address during finality contract instantiation
Sep 17, 2024
16e94f5
Fix / adapt benches
Sep 18, 2024
066ea79
Improve / adjust size checks
Sep 18, 2024
49b6ec8
Fix / adjust staking tests
Sep 18, 2024
3562f8b
Remove unused structs
Sep 18, 2024
95ff295
Make test helpers public
Sep 18, 2024
4e43626
Update schema
Sep 18, 2024
0938d3b
Fix: tests compilation / imports
Sep 18, 2024
00efde3
Ignore broken (no multi-test) tests
Sep 18, 2024
d3aece7
Fix / unify schema bin names
Sep 18, 2024
6124b93
Update btc-finality schemas
Sep 18, 2024
9af350c
Add btc-finality to CI releases
Sep 18, 2024
a7a03ad
Add btc-finality integration tests
Sep 18, 2024
21a72d9
Add btc finality to init tests
Sep 20, 2024
4646f1e
Add multitest to btc-finality
Sep 22, 2024
7a4c1e9
Add convenience contracts fields to suite
Sep 23, 2024
2f6583e
Remove unused funds
Sep 23, 2024
dfc0589
Add public randomness commitment mt
Sep 23, 2024
c6935e0
Fix: FP smart query results processing
Sep 23, 2024
8d7f357
Remove original test
Sep 23, 2024
6c34f64
Add TODO
Sep 23, 2024
14cdb0d
Move sudo msg from staking to finality
Sep 23, 2024
3380a9d
Add new at height app ctor
Sep 23, 2024
1f9f05c
Add finality sig happy path mt
Sep 23, 2024
af7c705
Add test modules for clarity
Sep 23, 2024
5c339e3
Add finality round works mt
Sep 23, 2024
bca20d1
Add slashing works mt
Sep 24, 2024
543e639
Remove original finality (no mt) tests
Sep 24, 2024
4bdc93a
Fix: slashing test
Sep 24, 2024
5380765
Feature gate / disable IBC messages for (multi-)tests
Sep 24, 2024
8b1ccf5
Fix: Avoid full-validation failure / bug in CI
Sep 24, 2024
be727ae
Remove (unused) denom from config
Sep 24, 2024
e0af2d9
Remove (unused / redundant) params from staking params
Sep 24, 2024
05438bb
Update schema
Sep 24, 2024
29a1b16
Move staking test helpers to the test-utils package
Sep 24, 2024
d67f7d8
Move finality test helpers to the test-utils package
Sep 24, 2024
01f98cc
Improve Params setting / passing
Sep 25, 2024
6434ca8
Enable / add full validation feature for finality
Sep 25, 2024
84a63b1
Add test_utils pub mod
Sep 25, 2024
91a2c36
Fix: tests under full validation
Sep 25, 2024
16a8a4c
Enable full validation tests for all contracts
Sep 25, 2024
365d5c4
Full validation also for babylon contract
Sep 25, 2024
5f42c3b
Merge branch 'main' into r/test-utils
Sep 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" ]
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the finality contract need the option for full validation? I thought the full validation is only relevant for Babylon <-> contract interaction, and only BTC staking contracts needs to have that. In theory finality contract can work with BTC staking contract with or without full validation.

For testing finality contract, we can always use BTC staking contract without full validation imo

Copy link
Collaborator Author

@maurolacy maurolacy Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this is a stronger setting, in which we confirm the full-validation feature works also in / for the multi-contract tests.
In fact all this comes from the fact that CI was running workspace tests with the full validation feature enabled, and they were failing for the finality contract.

I've now enabled that back, and tests are passing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Is there a way to make this feature only for developments/tests? np with me if we cannot do it though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already like that. Because the code under the library feature, used when importing other contracts, is only used / invoked from test code. These high level full-validation features in contracts other than the staking contract are just wrappers to the staking one; so that the feature can be enabled on purpose. Which, only affects tests.


[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" }
maurolacy marked this conversation as resolved.
Show resolved Hide resolved

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