From 0981555346f0c691a06bd96390071487880bf301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kuras?= Date: Thu, 17 Nov 2022 20:46:11 +0100 Subject: [PATCH] Stargate messages --- Cargo.toml | 2 +- src/app.rs | 195 ++++++++++++++++++++++++++++++++++++++++++------- src/gov.rs | 7 ++ src/ibc.rs | 7 ++ src/lib.rs | 2 + src/staking.rs | 6 +- src/wasm.rs | 9 ++- 7 files changed, 197 insertions(+), 31 deletions(-) create mode 100644 src/gov.rs create mode 100644 src/ibc.rs diff --git a/Cargo.toml b/Cargo.toml index 723cf9a4..3bb5317b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ backtrace = ["anyhow/backtrace"] [dependencies] cw-utils = "0.16" cw-storage-plus = "0.16" -cosmwasm-std = { version = "1.0", features = ["staking"] } +cosmwasm-std = { version = "1.0", features = ["staking", "stargate"] } itertools = "0.10.1" schemars = "0.8.1" serde = { version = "1.0", default-features = false, features = ["derive"] } diff --git a/src/app.rs b/src/app.rs index 9087cebf..805c32a7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,8 +6,8 @@ use anyhow::Result as AnyResult; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; use cosmwasm_std::{ from_slice, to_binary, Addr, Api, Binary, BlockInfo, ContractResult, CosmosMsg, CustomQuery, - Empty, Querier, QuerierResult, QuerierWrapper, QueryRequest, Record, Storage, SystemError, - SystemResult, + Empty, GovMsg, IbcMsg, IbcQuery, Querier, QuerierResult, QuerierWrapper, QueryRequest, Record, + Storage, SystemError, SystemResult, }; use schemars::JsonSchema; use serde::de::DeserializeOwned; @@ -16,6 +16,8 @@ use serde::Serialize; use crate::bank::{Bank, BankKeeper, BankSudo}; use crate::contracts::Contract; use crate::executor::{AppResponse, Executor}; +use crate::gov::Gov; +use crate::ibc::Ibc; use crate::module::{FailingModule, Module}; use crate::staking::{Distribution, DistributionKeeper, StakeKeeper, Staking, StakingSudo}; use crate::transactions::transactional; @@ -35,6 +37,7 @@ pub type BasicApp = App< WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, >; /// Router is a persisted state. You can query this. @@ -48,15 +51,17 @@ pub struct App< Wasm = WasmKeeper, Staking = StakeKeeper, Distr = DistributionKeeper, + Ibc = FailingModule, + Gov = FailingModule, > { - router: Router, + router: Router, api: Api, storage: Storage, block: BlockInfo, } -fn no_init( - _: &mut Router, +fn no_init( + _: &mut Router, _: &dyn Api, _: &mut dyn Storage, ) { @@ -79,6 +84,8 @@ impl BasicApp { WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, >, &dyn Api, &mut dyn Storage, @@ -101,6 +108,8 @@ where WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, >, &dyn Api, &mut dyn Storage, @@ -163,10 +172,12 @@ pub type BasicAppBuilder = AppBuilder< WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, >; /// Utility to build App in stages. If particular items wont be set, defaults would be used -pub struct AppBuilder { +pub struct AppBuilder { api: Api, block: BlockInfo, storage: Storage, @@ -175,6 +186,8 @@ pub struct AppBuilder { custom: Custom, staking: Staking, distribution: Distr, + ibc: Ibc, + gov: Gov, } impl Default @@ -186,6 +199,8 @@ impl Default WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, > { fn default() -> Self { @@ -202,6 +217,8 @@ impl WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, > { /// Creates builder with default components working with empty exec and query messages. @@ -215,6 +232,8 @@ impl custom: FailingModule::new(), staking: StakeKeeper::new(), distribution: DistributionKeeper::new(), + ibc: FailingModule::new(), + gov: FailingModule::new(), } } } @@ -228,6 +247,8 @@ impl WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, > where ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static, @@ -245,12 +266,14 @@ where custom: FailingModule::new(), staking: StakeKeeper::new(), distribution: DistributionKeeper::new(), + ibc: FailingModule::new(), + gov: FailingModule::new(), } } } -impl - AppBuilder +impl + AppBuilder { /// Overwrites default wasm executor. /// @@ -264,7 +287,7 @@ impl pub fn with_wasm>( self, wasm: NewWasm, - ) -> AppBuilder { + ) -> AppBuilder { let AppBuilder { bank, api, @@ -273,6 +296,8 @@ impl block, staking, distribution, + ibc, + gov, .. } = self; @@ -285,6 +310,8 @@ impl custom, staking, distribution, + ibc, + gov, } } @@ -292,7 +319,7 @@ impl pub fn with_bank( self, bank: NewBank, - ) -> AppBuilder { + ) -> AppBuilder { let AppBuilder { wasm, api, @@ -301,6 +328,8 @@ impl block, staking, distribution, + ibc, + gov, .. } = self; @@ -313,6 +342,8 @@ impl custom, staking, distribution, + ibc, + gov, } } @@ -320,7 +351,7 @@ impl pub fn with_api( self, api: NewApi, - ) -> AppBuilder { + ) -> AppBuilder { let AppBuilder { wasm, bank, @@ -329,6 +360,8 @@ impl block, staking, distribution, + ibc, + gov, .. } = self; @@ -341,6 +374,8 @@ impl custom, staking, distribution, + ibc, + gov, } } @@ -348,7 +383,7 @@ impl pub fn with_storage( self, storage: NewStorage, - ) -> AppBuilder { + ) -> AppBuilder { let AppBuilder { wasm, api, @@ -357,6 +392,8 @@ impl block, staking, distribution, + ibc, + gov, .. } = self; @@ -369,6 +406,8 @@ impl custom, staking, distribution, + ibc, + gov, } } @@ -384,7 +423,7 @@ impl pub fn with_custom( self, custom: NewCustom, - ) -> AppBuilder { + ) -> AppBuilder { let AppBuilder { wasm, bank, @@ -393,6 +432,8 @@ impl block, staking, distribution, + ibc, + gov, .. } = self; @@ -405,6 +446,8 @@ impl custom, staking, distribution, + ibc, + gov, } } @@ -412,7 +455,7 @@ impl pub fn with_staking( self, staking: NewStaking, - ) -> AppBuilder { + ) -> AppBuilder { let AppBuilder { wasm, api, @@ -421,6 +464,8 @@ impl block, bank, distribution, + ibc, + gov, .. } = self; @@ -433,14 +478,81 @@ impl custom, staking, distribution, + ibc, + gov, } } - /// Overwrites default bank interface + /// Overwrites default distribution interface pub fn with_distribution( self, distribution: NewDistribution, - ) -> AppBuilder { + ) -> AppBuilder + { + let AppBuilder { + wasm, + api, + storage, + custom, + block, + staking, + bank, + ibc, + gov, + .. + } = self; + + AppBuilder { + api, + block, + storage, + bank, + wasm, + custom, + staking, + distribution, + ibc, + gov, + } + } + + /// Overwrites default ibc interface + pub fn with_ibc( + self, + ibc: NewIbc, + ) -> AppBuilder { + let AppBuilder { + wasm, + api, + storage, + custom, + block, + staking, + bank, + distribution, + gov, + .. + } = self; + + AppBuilder { + api, + block, + storage, + bank, + wasm, + custom, + staking, + distribution, + ibc, + gov, + } + } + + /// Overwrites default gov interface + pub fn with_gov( + self, + gov: NewGov, + ) -> AppBuilder { let AppBuilder { wasm, api, @@ -449,6 +561,8 @@ impl block, staking, bank, + distribution, + ibc, .. } = self; @@ -461,6 +575,8 @@ impl custom, staking, distribution, + ibc, + gov, } } @@ -476,7 +592,7 @@ impl pub fn build( self, init_fn: F, - ) -> App + ) -> App where BankT: Bank, ApiT: Api, @@ -485,7 +601,13 @@ impl WasmT: Wasm, StakingT: Staking, DistrT: Distribution, - F: FnOnce(&mut Router, &dyn Api, &mut dyn Storage), + IbcT: Ibc, + GovT: Gov, + F: FnOnce( + &mut Router, + &dyn Api, + &mut dyn Storage, + ), { let router = Router { wasm: self.wasm, @@ -493,6 +615,8 @@ impl custom: self.custom, staking: self.staking, distribution: self.distribution, + ibc: self.ibc, + gov: self.gov, }; let mut app = App { @@ -506,8 +630,8 @@ impl } } -impl - App +impl + App where WasmT: Wasm, BankT: Bank, @@ -516,11 +640,13 @@ where CustomT: Module, StakingT: Staking, DistrT: Distribution, + IbcT: Ibc, + GovT: Gov, { pub fn init_modules(&mut self, init_fn: F) -> T where F: FnOnce( - &mut Router, + &mut Router, &dyn Api, &mut dyn Storage, ) -> T, @@ -530,7 +656,11 @@ where pub fn read_module(&self, query_fn: F) -> T where - F: FnOnce(&Router, &dyn Api, &dyn Storage) -> T, + F: FnOnce( + &Router, + &dyn Api, + &dyn Storage, + ) -> T, { query_fn(&self.router, &self.api, &self.storage) } @@ -678,7 +808,7 @@ where } } -pub struct Router { +pub struct Router { // this can remain crate-only as all special functions are wired up to app currently // we need to figure out another format for wasm, as some like sudo need to be called after init pub(crate) wasm: Wasm, @@ -687,9 +817,12 @@ pub struct Router { pub custom: Custom, pub staking: Staking, pub distribution: Distr, + pub ibc: Ibc, + pub gov: Gov, } -impl Router +impl + Router where CustomT::ExecT: Clone + fmt::Debug + PartialEq + JsonSchema + DeserializeOwned + 'static, CustomT::QueryT: CustomQuery + DeserializeOwned + 'static, @@ -698,6 +831,8 @@ where BankT: Bank, StakingT: Staking, DistrT: Distribution, + IbcT: Ibc, + GovT: Gov, { pub fn querier<'a>( &'a self, @@ -771,8 +906,8 @@ pub trait CosmosRouter { ) -> AnyResult; } -impl CosmosRouter - for Router +impl CosmosRouter + for Router where CustomT::ExecT: std::fmt::Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static, CustomT::QueryT: CustomQuery + DeserializeOwned + 'static, @@ -781,6 +916,8 @@ where BankT: Bank, StakingT: Staking, DistrT: Distribution, + IbcT: Ibc, + GovT: Gov, { type ExecC = CustomT::ExecT; type QueryC = CustomT::QueryT; @@ -801,6 +938,7 @@ where CosmosMsg::Distribution(msg) => self .distribution .execute(api, storage, self, block, sender, msg), + CosmosMsg::Ibc(msg) => self.ibc.execute(api, storage, self, block, sender, msg), _ => bail!("Cannot execute {:?}", msg), } } @@ -821,6 +959,7 @@ where QueryRequest::Bank(req) => self.bank.query(api, storage, &querier, block, req), QueryRequest::Custom(req) => self.custom.query(api, storage, &querier, block, req), QueryRequest::Staking(req) => self.staking.query(api, storage, &querier, block, req), + QueryRequest::Ibc(req) => self.ibc.query(api, storage, &querier, block, req), _ => unimplemented!(), } } @@ -1601,8 +1740,8 @@ mod test { // TODO: check error? } - fn query_router( - router: &Router, + fn query_router( + router: &Router, api: &dyn Api, storage: &dyn Storage, rcpt: &Addr, diff --git a/src/gov.rs b/src/gov.rs new file mode 100644 index 00000000..0569cb41 --- /dev/null +++ b/src/gov.rs @@ -0,0 +1,7 @@ +use cosmwasm_std::{Empty, GovMsg}; + +use crate::{FailingModule, Module}; + +pub trait Gov: Module {} + +impl Gov for FailingModule {} diff --git a/src/ibc.rs b/src/ibc.rs new file mode 100644 index 00000000..65c552da --- /dev/null +++ b/src/ibc.rs @@ -0,0 +1,7 @@ +use cosmwasm_std::{Empty, IbcMsg, IbcQuery}; + +use crate::{FailingModule, Module}; + +pub trait Ibc: Module {} + +impl Ibc for FailingModule {} diff --git a/src/lib.rs b/src/lib.rs index 7d9ae4ff..e629e858 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,8 @@ mod contracts; pub mod custom_handler; pub mod error; mod executor; +mod gov; +mod ibc; mod module; mod prefixed_storage; mod staking; diff --git a/src/staking.rs b/src/staking.rs index fa1dc361..b4880182 100644 --- a/src/staking.rs +++ b/src/staking.rs @@ -871,7 +871,7 @@ mod test { use cosmwasm_std::{ from_slice, testing::{mock_env, MockApi, MockStorage}, - BalanceResponse, BankQuery, + BalanceResponse, BankQuery, GovMsg, IbcMsg, IbcQuery, }; /// Type alias for default build `Router` to make its reference in typical scenario @@ -881,6 +881,8 @@ mod test { WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, >; fn mock_router() -> BasicRouter { @@ -890,6 +892,8 @@ mod test { custom: FailingModule::new(), staking: StakeKeeper::new(), distribution: DistributionKeeper::new(), + ibc: FailingModule::new(), + gov: FailingModule::new(), } } diff --git a/src/wasm.rs b/src/wasm.rs index bb049033..4afb58af 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -936,7 +936,10 @@ fn execute_response(data: Option) -> Option { #[cfg(test)] mod test { use cosmwasm_std::testing::{mock_env, mock_info, MockApi, MockQuerier, MockStorage}; - use cosmwasm_std::{coin, from_slice, to_vec, BankMsg, Coin, CosmosMsg, Empty, StdError}; + use cosmwasm_std::{ + coin, from_slice, to_vec, BankMsg, Coin, CosmosMsg, Empty, GovMsg, IbcMsg, IbcQuery, + StdError, + }; use crate::app::Router; use crate::bank::BankKeeper; @@ -955,6 +958,8 @@ mod test { WasmKeeper, StakeKeeper, DistributionKeeper, + FailingModule, + FailingModule, >; fn mock_router() -> BasicRouter { @@ -964,6 +969,8 @@ mod test { custom: FailingModule::new(), staking: StakeKeeper::new(), distribution: DistributionKeeper::new(), + ibc: FailingModule::new(), + gov: FailingModule::new(), } }