From f4f4c46125972f8ab8d0fd512092b4d9782eaf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Wed, 23 Aug 2023 17:11:32 +0200 Subject: [PATCH] tests: Remove unused `packages/mocks` --- Cargo.lock | 14 -- Cargo.toml | 1 - packages/mocks/Cargo.toml | 17 -- packages/mocks/src/cross_staking.rs | 134 --------------- packages/mocks/src/lib.rs | 3 - packages/mocks/src/local_staking.rs | 127 -------------- packages/mocks/src/vault.rs | 248 ---------------------------- 7 files changed, 544 deletions(-) delete mode 100644 packages/mocks/Cargo.toml delete mode 100644 packages/mocks/src/cross_staking.rs delete mode 100644 packages/mocks/src/lib.rs delete mode 100644 packages/mocks/src/local_staking.rs delete mode 100644 packages/mocks/src/vault.rs diff --git a/Cargo.lock b/Cargo.lock index af266cb7..e9740778 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -594,20 +594,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "mesh-mocks" -version = "0.5.0-alpha.1" -dependencies = [ - "cosmwasm-schema", - "cosmwasm-std", - "cw-storage-plus", - "cw-utils", - "mesh-apis", - "serde", - "sylvia", - "thiserror", -] - [[package]] name = "mesh-native-staking" version = "0.5.0-alpha.1" diff --git a/Cargo.toml b/Cargo.toml index f88f45c6..bcf56fec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ repository = "https://github.com/osmosis-labs/mesh-security" [workspace.dependencies] mesh-apis = { path = "./packages/apis" } mesh-bindings = { path = "./packages/bindings" } -mesh-mocks = { path = "./packages/mocks" } mesh-sync = { path = "./packages/sync" } mesh-vault = { path = "./contracts/provider/vault" } diff --git a/packages/mocks/Cargo.toml b/packages/mocks/Cargo.toml deleted file mode 100644 index 77ab4a13..00000000 --- a/packages/mocks/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "mesh-mocks" -version = { workspace = true } -edition = { workspace = true } -license = { workspace = true } - -[dependencies] -sylvia = { workspace = true } -cosmwasm-std = { workspace = true } -cosmwasm-schema = { workspace = true } -cw-utils = { workspace = true } -cw-storage-plus = { workspace = true } -mesh-apis = { workspace = true } - -thiserror = { workspace = true } -serde = { workspace = true } - diff --git a/packages/mocks/src/cross_staking.rs b/packages/mocks/src/cross_staking.rs deleted file mode 100644 index 2053d6a8..00000000 --- a/packages/mocks/src/cross_staking.rs +++ /dev/null @@ -1,134 +0,0 @@ -use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; -use sylvia::{contract, schemars}; -use thiserror::Error; - -use cosmwasm_schema::cw_serde; -use cosmwasm_std::{ensure_eq, Binary, Coin, Decimal, Response, StdError}; -use cw_storage_plus::Item; -use cw_utils::{nonpayable, PaymentError}; - -use mesh_apis::cross_staking_api::{self, CrossStakingApi, MaxSlashResponse}; -use mesh_apis::vault_api::VaultApiHelper; - -#[derive(Error, Debug)] -pub enum StakingError { - #[error("{0}")] - Std(#[from] StdError), - - #[error("{0}")] - Payment(#[from] PaymentError), - - #[error("Unauthorized")] - Unauthorized {}, - - #[error("Staking must be in this denom: {0}")] - WrongDenom(String), -} - -#[cw_serde] -pub struct Config { - /// The address of the vault contract (where we get and return stake) - pub vault: VaultApiHelper, - - pub max_slash: Decimal, - - pub stake_denom: String, -} - -pub struct MockCrossStakingContract<'a> { - config: Item<'a, Config>, -} - -#[contract] -#[messages(cross_staking_api as CrossStakingApi)] -#[error(StakingError)] -impl MockCrossStakingContract<'_> { - pub const fn new() -> Self { - Self { - config: Item::new("config"), - } - } - - /// Anyone can create a cross-staking contract. It must know who the vault is - #[msg(instantiate)] - pub fn instantiate( - &self, - ctx: InstantiateCtx, - max_slash: Decimal, - vault: String, - stake_denom: String, - ) -> Result { - let config = Config { - vault: VaultApiHelper(ctx.deps.api.addr_validate(&vault)?), - max_slash, - stake_denom, - }; - self.config.save(ctx.deps.storage, &config)?; - Ok(Response::new()) - } - - #[msg(exec)] - fn release_stake(&self, ctx: ExecCtx, amount: Coin) -> Result { - nonpayable(&ctx.info)?; - - // assert proper denom - let cfg = self.config.load(ctx.deps.storage)?; - ensure_eq!( - cfg.stake_denom, - amount.denom, - StakingError::WrongDenom(cfg.stake_denom) - ); - - // blindly reduce lien on vault - let wasm = cfg - .vault - .release_cross_stake(ctx.info.sender.into_string(), amount, vec![])?; - Ok(Response::new().add_message(wasm)) - } -} - -#[contract] -#[messages(cross_staking_api as CrossStakingApi)] -impl CrossStakingApi for MockCrossStakingContract<'_> { - type Error = StakingError; - - /// Receives stake (info.funds) from vault contract on behalf of owner and performs the action - /// specified in msg with it. - /// Msg is custom to each implementation of the staking contract and opaque to the vault - #[msg(exec)] - fn receive_virtual_stake( - &self, - ctx: ExecCtx, - owner: String, - amount: Coin, - tx_id: u64, - msg: Binary, - ) -> Result { - nonpayable(&ctx.info)?; - - // only can be called by the vault - let cfg = self.config.load(ctx.deps.storage)?; - ensure_eq!( - cfg.vault.addr(), - &ctx.info.sender, - StakingError::Unauthorized {} - ); - // assert proper denom - ensure_eq!( - cfg.stake_denom, - amount.denom, - StakingError::WrongDenom(cfg.stake_denom) - ); - - // ignore args - let _ = (owner, msg, amount, tx_id); - Ok(Response::new()) - } - - /// Returns the maximum percentage that can be slashed (hardcoded on instantiate) - #[msg(query)] - fn max_slash(&self, ctx: QueryCtx) -> Result { - let Config { max_slash, .. } = self.config.load(ctx.deps.storage)?; - Ok(MaxSlashResponse { max_slash }) - } -} diff --git a/packages/mocks/src/lib.rs b/packages/mocks/src/lib.rs deleted file mode 100644 index a4d28725..00000000 --- a/packages/mocks/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod cross_staking; -pub mod local_staking; -pub mod vault; diff --git a/packages/mocks/src/local_staking.rs b/packages/mocks/src/local_staking.rs deleted file mode 100644 index 91e57f42..00000000 --- a/packages/mocks/src/local_staking.rs +++ /dev/null @@ -1,127 +0,0 @@ -use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; -use sylvia::{contract, schemars}; -use thiserror::Error; - -use cosmwasm_schema::cw_serde; -use cosmwasm_std::{ensure_eq, Binary, Coin, Decimal, Response, StdError}; -use cw_storage_plus::Item; -use cw_utils::{must_pay, nonpayable, PaymentError}; - -use mesh_apis::local_staking_api::{self, LocalStakingApi, MaxSlashResponse}; -use mesh_apis::vault_api::VaultApiHelper; - -#[derive(Error, Debug)] -pub enum StakingError { - #[error("{0}")] - Std(#[from] StdError), - - #[error("{0}")] - Payment(#[from] PaymentError), - - #[error("Unauthorized")] - Unauthorized {}, - - #[error("Staking must be in this denom: {0}")] - WrongDenom(String), -} - -#[cw_serde] -pub struct Config { - /// The denom we accept for staking - pub stake_denom: String, - - /// The address of the vault contract (where we get and return stake) - pub vault: VaultApiHelper, - - pub max_slash: Decimal, -} - -pub struct MockLocalStakingContract<'a> { - config: Item<'a, Config>, -} - -#[contract] -#[messages(local_staking_api as LocalStakingApi)] -#[error(StakingError)] -impl MockLocalStakingContract<'_> { - pub const fn new() -> Self { - Self { - config: Item::new("config"), - } - } - - /// The caller of the instantiation will be the vault contract - #[msg(instantiate)] - pub fn instantiate( - &self, - ctx: InstantiateCtx, - stake_denom: String, - max_slash: Decimal, - ) -> Result { - let config = Config { - stake_denom, - vault: VaultApiHelper(ctx.info.sender), - max_slash, - }; - self.config.save(ctx.deps.storage, &config)?; - Ok(Response::new()) - } - - #[msg(exec)] - fn release_stake(&self, ctx: ExecCtx, amount: Coin) -> Result { - nonpayable(&ctx.info)?; - - // assert proper denom - let cfg = self.config.load(ctx.deps.storage)?; - ensure_eq!( - cfg.stake_denom, - amount.denom, - StakingError::WrongDenom(cfg.stake_denom) - ); - - // blindly send money back to vault - let wasm = cfg - .vault - .release_local_stake(ctx.info.sender.into_string(), vec![amount])?; - Ok(Response::new().add_message(wasm)) - } -} - -#[contract] -#[messages(local_staking_api as LocalStakingApi)] -impl LocalStakingApi for MockLocalStakingContract<'_> { - type Error = StakingError; - - /// Receives stake (info.funds) from vault contract on behalf of owner and performs the action - /// specified in msg with it. - /// Msg is custom to each implementation of the staking contract and opaque to the vault - #[msg(exec)] - fn receive_stake( - &self, - ctx: ExecCtx, - owner: String, - msg: Binary, - ) -> Result { - // only can be called by the vault - let cfg = self.config.load(ctx.deps.storage)?; - ensure_eq!( - cfg.vault.addr(), - &ctx.info.sender, - StakingError::Unauthorized {} - ); - - // assert funds passed in - let _paid = must_pay(&ctx.info, &cfg.stake_denom)?; - - // ignore args - let _ = (owner, msg); - Ok(Response::new()) - } - - /// Returns the maximum percentage that can be slashed (hardcoded on instantiate) - #[msg(query)] - fn max_slash(&self, ctx: QueryCtx) -> Result { - let Config { max_slash, .. } = self.config.load(ctx.deps.storage)?; - Ok(MaxSlashResponse { max_slash }) - } -} diff --git a/packages/mocks/src/vault.rs b/packages/mocks/src/vault.rs deleted file mode 100644 index 7e207b84..00000000 --- a/packages/mocks/src/vault.rs +++ /dev/null @@ -1,248 +0,0 @@ -use cosmwasm_schema::cw_serde; -use cosmwasm_std::{ - ensure_eq, Addr, BankMsg, Binary, Coin, DepsMut, Env, Reply, Response, StdError, SubMsg, - SubMsgResponse, WasmMsg, -}; -use cw_utils::{must_pay, nonpayable, ParseReplyError, PaymentError}; -use thiserror::Error; - -use cw_storage_plus::Item; -use cw_utils::parse_instantiate_response_data; - -use mesh_apis::cross_staking_api::CrossStakingApiHelper; -use mesh_apis::local_staking_api::LocalStakingApiHelper; -use mesh_apis::vault_api::{self, VaultApi}; -use sylvia::types::{ExecCtx, InstantiateCtx}; -use sylvia::{contract, schemars}; - -pub const REPLY_ID_INSTANTIATE: u64 = 1; - -#[derive(Error, Debug)] -pub enum VaultError { - #[error("{0}")] - Std(#[from] StdError), - - #[error("{0}")] - Payment(#[from] PaymentError), - - #[error("{0}")] - ParseReply(#[from] ParseReplyError), - - #[error("Invalid reply id: {0}")] - InvalidReplyId(u64), - - #[error("Staking must be in this denom: {0}")] - WrongDenom(String), -} - -#[cw_serde] -pub struct Config { - /// The denom we accept for staking (only native tokens) - pub denom: String, - - /// The address of the local staking contract (where actual tokens go) - pub local_staking: LocalStakingApiHelper, -} - -/// This is the info used to construct the native staking contract -#[cw_serde] -pub struct StakingInitInfo { - /// Admin for the local staking contract. If empty, it is immutable - pub admin: Option, - /// Code id used to instantiate the local staking contract - pub code_id: u64, - /// JSON-encoded local staking `InstantiateMsg` struct (as raw `Binary`) - pub msg: Binary, - /// A human-readable label for the local staking contract (will use a default if not provided) - pub label: Option, -} - -pub struct MockVaultContract<'a> { - config: Item<'a, Config>, -} - -#[contract] -#[messages(vault_api as VaultApi)] -#[error(VaultError)] -impl MockVaultContract<'_> { - pub const fn new() -> Self { - Self { - config: Item::new("config"), - } - } - - #[msg(instantiate)] - pub fn instantiate( - &self, - ctx: InstantiateCtx, - denom: String, - local_staking: StakingInitInfo, - ) -> Result { - let config = Config { - denom, - // We set this in reply, so proper once the reply message completes successfully - local_staking: LocalStakingApiHelper(Addr::unchecked("")), - }; - self.config.save(ctx.deps.storage, &config)?; - - // instantiate local_staking and handle reply - let msg = WasmMsg::Instantiate { - admin: local_staking.admin, - code_id: local_staking.code_id, - msg: local_staking.msg, - funds: vec![], - label: local_staking - .label - .unwrap_or_else(|| "Mesh Security Local Staking".to_string()), - }; - let sub_msg = SubMsg::reply_on_success(msg, REPLY_ID_INSTANTIATE); - Ok(Response::new().add_submessage(sub_msg)) - } - - // mock so no tracking, just ensure proper denom - #[msg(exec)] - fn bond(&self, ctx: ExecCtx) -> Result { - let cfg = self.config.load(ctx.deps.storage)?; - let _ = must_pay(&ctx.info, &cfg.denom)?; - Ok(Response::new()) - } - - // mock so no checks - #[msg(exec)] - fn unbond(&self, ctx: ExecCtx, amount: Coin) -> Result { - nonpayable(&ctx.info)?; - let Config { denom, .. } = self.config.load(ctx.deps.storage)?; - ensure_eq!(amount.denom, denom, VaultError::WrongDenom(denom)); - let msg = BankMsg::Send { - to_address: ctx.info.sender.to_string(), - amount: vec![amount], - }; - Ok(Response::new().add_message(msg)) - } - - /// This assigns a claim of amount tokens to the remote contract, which can take some action with it - #[msg(exec)] - fn stake_remote( - &self, - ctx: ExecCtx, - // address of the contract to virtually stake on - contract: String, - // amount to stake on that contract - amount: Coin, - // associated transaction id - tx_id: u64, - // action to take with that stake - msg: Binary, - ) -> Result { - nonpayable(&ctx.info)?; - - // embed user-message in the actual message we want - let cross_staking = CrossStakingApiHelper(ctx.deps.api.addr_validate(&contract)?); - let wasm_msg = cross_staking.receive_virtual_stake( - ctx.info.sender.into_string(), - amount, - tx_id, - msg, - vec![], - )?; - Ok(Response::new().add_message(wasm_msg)) - } - - /// This sends actual tokens to the local staking contract - #[msg(exec)] - fn stake_local( - &self, - ctx: ExecCtx, - // amount to stake on that contract - amount: Coin, - // action to take with that stake - msg: Binary, - ) -> Result { - nonpayable(&ctx.info)?; - let Config { - denom, - local_staking, - } = self.config.load(ctx.deps.storage)?; - - ensure_eq!(amount.denom, denom, VaultError::WrongDenom(denom)); - let funds = vec![amount]; - let wasm_msg = local_staking.receive_stake(ctx.info.sender.into_string(), msg, funds)?; - - Ok(Response::new().add_message(wasm_msg)) - } - - fn reply_init_callback( - &self, - deps: DepsMut, - reply: SubMsgResponse, - ) -> Result { - let init_data = parse_instantiate_response_data(&reply.data.unwrap())?; - let local_staking = Addr::unchecked(init_data.contract_address); - - let mut cfg = self.config.load(deps.storage)?; - cfg.local_staking = LocalStakingApiHelper(local_staking); - self.config.save(deps.storage, &cfg)?; - - Ok(Response::new()) - } -} - -pub fn reply(deps: DepsMut, _env: Env, reply: Reply) -> Result { - match reply.id { - REPLY_ID_INSTANTIATE => { - MockVaultContract::new().reply_init_callback(deps, reply.result.unwrap()) - } - _ => Err(VaultError::InvalidReplyId(reply.id)), - } -} - -#[contract] -#[messages(vault_api as VaultApi)] -impl VaultApi for MockVaultContract<'_> { - type Error = VaultError; - - /// This must be called by the remote staking contract to release this claim - #[msg(exec)] - fn release_cross_stake( - &self, - _ctx: ExecCtx, - // address of the user who originally called stake_remote - owner: String, - // amount to unstake on that contract - amount: Coin, - ) -> Result { - let _ = (owner, amount); - // we don't track liens so no-op - Ok(Response::new()) - } - - /// This must be called by the local staking contract to release this claim - /// Amount of tokens unstaked are those included in ctx.info.funds - #[msg(exec)] - fn release_local_stake( - &self, - _ctx: ExecCtx, - // address of the user who originally called stake_remote - owner: String, - ) -> Result { - let _ = owner; - // we don't track liens so no-op - Ok(Response::new()) - } - - /// This must be called by the remote staking contract to commit the remote staking call on success. - /// Transaction ID is used to identify the original (vault contract originated) transaction. - #[msg(exec)] - fn commit_tx(&self, _ctx: ExecCtx, tx_id: u64) -> Result { - let _ = tx_id; - Ok(Response::new()) - } - - /// This must be called by the remote staking contract to rollback the remote staking call on failure. - /// Transaction ID is used to identify the original (vault contract originated) transaction. - #[msg(exec)] - fn rollback_tx(&self, _ctx: ExecCtx, tx_id: u64) -> Result { - let _ = tx_id; - Ok(Response::new()) - } -}