Skip to content

Commit

Permalink
Everything runs minus virtual-staking, some tests fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed May 9, 2024
1 parent 632ee1b commit 9b2be17
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions contracts/provider/vault/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ use cosmwasm_std::{coin, coins, to_json_binary, Addr, Decimal, Uint128, Validato
use cw_multi_test::{App as MtApp, StakingInfo};
use mesh_apis::ibc::AddValidator;
use mesh_external_staking::contract::sv::mt::ExternalStakingContractProxy;
use mesh_external_staking::contract::ExternalStakingContract;
use mesh_external_staking::msg::{AuthorizedEndpoint, ReceiveVirtualStake, StakeInfo};
use mesh_external_staking::state::SlashRatio;
use mesh_external_staking::state::Stake;
use mesh_native_staking::contract::sv::mt::NativeStakingContractProxy;
use mesh_native_staking::contract::NativeStakingContract;
use mesh_native_staking_proxy::contract::sv::mt::NativeStakingProxyContractProxy;
use mesh_native_staking_proxy::contract::NativeStakingProxyContract;
use mesh_sync::Tx::InFlightStaking;
use mesh_sync::{Tx, ValueRange};
use sylvia::multitest::App;
use sylvia::multitest::{App, Proxy};

use mesh_apis::vault_api::sv::mt::VaultApiProxy;
use mesh_external_staking::test_methods::sv::mt::TestMethodsProxy;

use crate::contract;
use crate::contract::sv::mt::VaultContractProxy;
use crate::contract::VaultContract;
use crate::error::ContractError;
use crate::msg::{
AccountResponse, AllAccountsResponseItem, AllActiveExternalStakingResponse, LienResponse,
Expand All @@ -29,7 +36,7 @@ const SLASHING_PERCENTAGE: u64 = 10;

/// App initialization
fn init_app(users: &[&str], amounts: &[u128]) -> App<MtApp> {
let mut app = App::new(|router, _api, storage| {
let app = App::custom(|router, _api, storage| {
for (&user, amount) in std::iter::zip(users, amounts) {
router
.bank
Expand All @@ -38,9 +45,9 @@ fn init_app(users: &[&str], amounts: &[u128]) -> App<MtApp> {
}
});

set_chain_native_denom(&mut app, OSMO);
set_chain_native_denom(&mut app.app_mut(), OSMO);

App::new(app)
app
}

fn add_local_validator(app: &mut App<MtApp>, validator: &str) {
Expand Down Expand Up @@ -78,13 +85,13 @@ fn set_chain_native_denom(app: &mut MtApp, denom: &str) {
/// Contracts setup
fn setup<'app>(
app: &'app App<MtApp>,
owner: &str,
owner: &'app str,
slash_percent: u64,
unbond_period: u64,
) -> (
VaultContractProxy<'app, MtApp>,
NativeStakingContractProxy<'app, MtApp>,
ExternalStakingContractProxy<'app, MtApp>,
Proxy<'app, MtApp, VaultContract<'app>>,
Proxy<'app, MtApp, NativeStakingContract<'app>>,
Proxy<'app, MtApp, ExternalStakingContract<'app>>,
) {
let (vault, native, external) = setup_inner(app, owner, slash_percent, unbond_period, true);
(vault, native.unwrap(), external)
Expand All @@ -93,27 +100,27 @@ fn setup<'app>(
/// Contracts setup
fn setup_without_local_staking<'app>(
app: &'app App<MtApp>,
owner: &str,
owner: &'app str,
slash_percent: u64,
unbond_period: u64,
) -> (
VaultContractProxy<'app, MtApp>,
ExternalStakingContractProxy<'app, MtApp>,
Proxy<'app, MtApp, VaultContract<'app>>,
Proxy<'app, MtApp, ExternalStakingContract<'app>>,
) {
let (vault, _, external) = setup_inner(app, owner, slash_percent, unbond_period, false);
(vault, external)
}

fn setup_inner<'app>(
app: &'app App<MtApp>,
owner: &str,
owner: &'app str,
slash_percent: u64,
unbond_period: u64,
local_staking: bool,
) -> (
VaultContractProxy<'app, MtApp>,
Option<NativeStakingContractProxy<'app, MtApp>>,
ExternalStakingContractProxy<'app, MtApp>,
Proxy<'app, MtApp, VaultContract<'app>>,
Option<Proxy<'app, MtApp, NativeStakingContract<'app>>>,
Proxy<'app, MtApp, ExternalStakingContract<'app>>,
) {
let vault_code = contract::sv::mt::CodeId::store_code(app);

Expand Down Expand Up @@ -146,19 +153,19 @@ fn setup_inner<'app>(
.unwrap();

let native_staking_addr = vault.config().unwrap().local_staking.map(Addr::unchecked);
let native_staking = native_staking_addr.map(|addr| NativeStakingContractProxy::new(addr, app));
let native_staking = native_staking_addr.map(|addr| Proxy::new(addr, app));

let cross_staking = setup_cross_stake(app, owner, &vault, slash_percent, unbond_period);
(vault, native_staking, cross_staking)
}

fn setup_cross_stake<'app>(
app: &'app App<MtApp>,
owner: &str,
vault: &VaultContractProxy<'app, MtApp>,
owner: &'app str,
vault: &Proxy<'app, MtApp, VaultContract<'app>>,
slash_percent: u64,
unbond_period: u64,
) -> ExternalStakingContractProxy<'app, MtApp> {
) -> Proxy<'app, MtApp, ExternalStakingContract<'app>> {
// FIXME: Code shouldn't be duplicated
let cross_staking_code = mesh_external_staking::contract::sv::mt::CodeId::store_code(app);
// FIXME: Connection endpoint should be unique
Expand All @@ -182,7 +189,7 @@ fn setup_cross_stake<'app>(

/// Set some active validators
fn set_active_validators(
cross_staking: &ExternalStakingContractProxy<MtApp>,
cross_staking: &Proxy<'_, MtApp, ExternalStakingContract<'_>>,
validators: &[&str],
) -> (u64, u64) {
let update_valset_height = 100;
Expand All @@ -191,7 +198,6 @@ fn set_active_validators(
for validator in validators {
let activate = AddValidator::mock(validator);
cross_staking
.test_methods_proxy()
.test_set_active_validator(activate.clone(), update_valset_height, update_valset_time)
.call("test")
.unwrap();
Expand All @@ -200,7 +206,7 @@ fn set_active_validators(
}

/// Bond some tokens
fn bond(vault: &VaultContractProxy<MtApp>, user: &str, amount: u128) {
fn bond(vault: &Proxy<'_, MtApp, VaultContract<'_>>, user: &str, amount: u128) {
vault
.bond()
.with_funds(&coins(amount, OSMO))
Expand All @@ -209,7 +215,7 @@ fn bond(vault: &VaultContractProxy<MtApp>, user: &str, amount: u128) {
}

fn stake_locally(
vault: &VaultContractProxy<MtApp>,
vault: &Proxy<'_, MtApp, VaultContract<'_>>,
user: &str,
stake: u128,
validator: &str,
Expand All @@ -224,8 +230,8 @@ fn stake_locally(
}

fn stake_remotely(
vault: &VaultContractProxy<MtApp>,
cross_staking: &ExternalStakingContractProxy<MtApp>,
vault: &Proxy<'_, MtApp, VaultContract<'_>>,
cross_staking: &Proxy<'_, MtApp, ExternalStakingContract<'_>>,
user: &str,
validators: &[&str],
amounts: &[u128],
Expand All @@ -248,23 +254,22 @@ fn stake_remotely(
let last_external_staking_tx =
get_last_external_staking_pending_tx_id(cross_staking).unwrap();
cross_staking
.test_methods_proxy()
.test_commit_stake(last_external_staking_tx)
.call("test")
.unwrap();
}
}

fn proxy_for_user<'a>(
local_staking: &NativeStakingContractProxy<MtApp>,
local_staking: &Proxy<'_, MtApp, NativeStakingContract<'_>>,
user: &str,
app: &'a App<MtApp>,
) -> NativeStakingProxyContractProxy<'a, MtApp> {
) -> Proxy<'a, MtApp, NativeStakingProxyContract<'a>> {
let proxy_addr = local_staking
.proxy_by_owner(user.to_string())
.unwrap()
.proxy;
NativeStakingProxyContractProxy::new(Addr::unchecked(proxy_addr), app)
Proxy::new(Addr::unchecked(proxy_addr), app)
}

fn process_staking_unbondings(app: &App<MtApp>) {
Expand All @@ -274,14 +279,14 @@ fn process_staking_unbondings(app: &App<MtApp>) {
}

#[track_caller]
fn get_last_vault_pending_tx_id(contract: &VaultContractProxy<MtApp>) -> Option<u64> {
fn get_last_vault_pending_tx_id(contract: &Proxy<'_, MtApp, VaultContract<'_>>) -> Option<u64> {
let txs = contract.all_pending_txs_desc(None, None).unwrap().txs;
txs.first().map(Tx::id)
}

#[track_caller]
fn get_last_external_staking_pending_tx_id(
contract: &ExternalStakingContractProxy<MtApp>,
contract: &Proxy<'_, MtApp, ExternalStakingContract<'_>>,
) -> Option<u64> {
let txs = contract.all_pending_txs_desc(None, None).unwrap().txs;
txs.first().map(Tx::id)
Expand Down Expand Up @@ -745,7 +750,6 @@ fn stake_cross() {
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&cross_staking).unwrap();
println!("last_external_staking_tx: {:?}", last_external_staking_tx);
cross_staking
.test_methods_proxy()
.test_commit_stake(last_external_staking_tx)
.call("test")
.unwrap();
Expand Down Expand Up @@ -810,7 +814,6 @@ fn stake_cross() {
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&cross_staking).unwrap();
println!("last_external_staking_tx: {:?}", last_external_staking_tx);
cross_staking
.test_methods_proxy()
.test_commit_stake(last_external_staking_tx)
.call("test")
.unwrap();
Expand Down Expand Up @@ -924,7 +927,6 @@ fn stake_cross() {
skip_time(&app, unbond_period);
let tx_id = get_last_external_staking_pending_tx_id(&cross_staking).unwrap();
cross_staking
.test_methods_proxy()
.test_commit_unstake(tx_id)
.call("test")
.unwrap();
Expand Down Expand Up @@ -992,7 +994,6 @@ fn stake_cross() {

let tx_id = get_last_external_staking_pending_tx_id(&cross_staking).unwrap();
cross_staking
.test_methods_proxy()
.test_commit_unstake(tx_id)
.call("test")
.unwrap();
Expand Down Expand Up @@ -1156,7 +1157,6 @@ fn stake_cross_txs() {
// Last tx commit_tx call
let last_tx = get_last_vault_pending_tx_id(&vault).unwrap();
vault
.vault_api_proxy()
.commit_tx(last_tx)
.call(cross_staking.contract_addr.as_str())
.unwrap();
Expand Down Expand Up @@ -1251,7 +1251,6 @@ fn stake_cross_txs() {

// Commit first tx
vault
.vault_api_proxy()
.commit_tx(first_tx)
.call(cross_staking.contract_addr.as_str())
.unwrap();
Expand Down Expand Up @@ -1332,7 +1331,6 @@ fn stake_cross_rollback_tx() {
// Rollback tx
let last_tx = get_last_vault_pending_tx_id(&vault).unwrap();
vault
.vault_api_proxy()
.rollback_tx(last_tx)
.call(cross_staking.contract_addr.as_str())
.unwrap();
Expand Down Expand Up @@ -1753,7 +1751,6 @@ fn cross_slash_scenario_1() {

// Validator 1 is slashed
cross_staking
.test_methods_proxy()
.test_handle_slashing(validator1.to_string(), Uint128::new(10))
.call("test")
.unwrap();
Expand Down Expand Up @@ -1865,7 +1862,6 @@ fn cross_slash_scenario_2() {

// Validator 1 is slashed
cross_staking
.test_methods_proxy()
.test_handle_slashing(validator1.to_string(), Uint128::new(20))
.call("test")
.unwrap();
Expand Down Expand Up @@ -1973,7 +1969,6 @@ fn cross_slash_scenario_3() {

// Validator 1 is slashed
cross_staking
.test_methods_proxy()
.test_handle_slashing(validator1.to_string(), Uint128::new(15))
.call("test")
.unwrap();
Expand Down Expand Up @@ -2106,7 +2101,6 @@ fn cross_slash_scenario_4() {

// Validator 1 is slashed
cross_staking_1
.test_methods_proxy()
.test_handle_slashing(validator1.to_string(), Uint128::new(14))
.call("test")
.unwrap();
Expand Down Expand Up @@ -2283,7 +2277,6 @@ fn cross_slash_scenario_5() {

// Validator 1 is slashed
cross_staking_1
.test_methods_proxy()
.test_handle_slashing(
validator1.to_string(),
Uint128::new(180) * Decimal::percent(slashing_percentage),
Expand Down Expand Up @@ -2426,7 +2419,6 @@ fn cross_slash_no_native_staking() {

// Validator 1 is slashed
cross_staking_1
.test_methods_proxy()
.test_handle_slashing(validator1.to_string(), Uint128::new(14))
.call("test")
.unwrap();
Expand Down Expand Up @@ -2545,7 +2537,6 @@ fn cross_slash_pending_unbonding() {
.call(user)
.unwrap();
cross_staking
.test_methods_proxy()
.test_commit_unstake(get_last_external_staking_pending_tx_id(&cross_staking).unwrap())
.call("test")
.unwrap();
Expand All @@ -2558,7 +2549,6 @@ fn cross_slash_pending_unbonding() {

// Validator 1 is slashed, over the current bond
cross_staking
.test_methods_proxy()
.test_handle_slashing(validator1.to_string(), Uint128::new(5))
.call("test")
.unwrap();
Expand Down Expand Up @@ -2609,6 +2599,11 @@ fn cross_slash_pending_unbonding() {

/// Scenario 7:
/// Same as scenario 1 but with native slashing, because of double-signing.
///
/// TODO: re-enable. It requires `test_handle_jailing` to be implemented, which is
/// also referenced in native-staking::local_staking_apis::jailing but doesn't seem to be
/// actually implemented anywhere.
#[cfg(feature = "todo")]
#[test]
fn native_slashing_tombstoning() {
let owner = "owner";
Expand Down Expand Up @@ -2733,6 +2728,11 @@ fn native_slashing_tombstoning() {

/// Scenario 8:
/// Same as scenario 1 but with native slashing, because of offline.
///
/// TODO: re-enable. It requires `test_handle_jailing` to be implemented, which is
/// also referenced in native-staking::local_staking_apis::jailing but doesn't seem to be
/// actually implemented anywhere.
#[cfg(feature = "todo")]
#[test]
fn native_slashing_jailing() {
let owner = "owner";
Expand Down

0 comments on commit 9b2be17

Please sign in to comment.