Skip to content

Commit

Permalink
Added initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Oct 19, 2023
1 parent 31981c4 commit fd03c5b
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 39 deletions.
74 changes: 52 additions & 22 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::gov::Gov;
use crate::ibc::Ibc;
use crate::module::{FailingModule, Module};
use crate::staking::{Distribution, DistributionKeeper, StakeKeeper, Staking, StakingSudo};
use crate::stargate::{FailingStargate, Stargate, StargateMsg, StargateQuery};
use crate::transactions::transactional;
use crate::wasm::{ContractData, Wasm, WasmKeeper, WasmSudo};
use crate::AppBuilder;
Expand Down Expand Up @@ -35,6 +36,8 @@ pub type BasicApp<ExecC = Empty, QueryC = Empty> = App<
StakeKeeper,
DistributionKeeper,
FailingModule<IbcMsg, IbcQuery, Empty>,
FailingModule<GovMsg, Empty, Empty>,
FailingStargate,
>;

/// Router is a persisted state. You can query this.
Expand All @@ -51,15 +54,16 @@ pub struct App<
Distr = DistributionKeeper,
Ibc = FailingModule<IbcMsg, IbcQuery, Empty>,
Gov = FailingModule<GovMsg, Empty, Empty>,
Stargate = FailingStargate,
> {
pub(crate) router: Router<Bank, Custom, Wasm, Staking, Distr, Ibc, Gov>,
pub(crate) router: Router<Bank, Custom, Wasm, Staking, Distr, Ibc, Gov, Stargate>,
pub(crate) api: Api,
pub(crate) storage: Storage,
pub(crate) block: BlockInfo,
}

pub fn no_init<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>(
_: &mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>,
pub fn no_init<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>(
_: &mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
_: &dyn Api,
_: &mut dyn Storage,
) {
Expand All @@ -84,12 +88,14 @@ impl BasicApp {
DistributionKeeper,
FailingModule<IbcMsg, IbcQuery, Empty>,
FailingModule<GovMsg, Empty, Empty>,
FailingStargate,
>,
&dyn Api,
&mut dyn Storage,
),
{
AppBuilder::new().build(init_fn)
let a: BasicApp = AppBuilder::new().build(init_fn);
a
}
}

Expand All @@ -108,6 +114,7 @@ where
DistributionKeeper,
FailingModule<IbcMsg, IbcQuery, Empty>,
FailingModule<GovMsg, Empty, Empty>,
FailingStargate,
>,
&dyn Api,
&mut dyn Storage,
Expand All @@ -116,8 +123,8 @@ where
AppBuilder::new_custom().build(init_fn)
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT> Querier
for App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT> Querier
for App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
CustomT::ExecT: Clone + Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
Expand All @@ -130,6 +137,7 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
self.router
Expand All @@ -138,8 +146,9 @@ where
}
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT> Executor<CustomT::ExecT>
for App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
Executor<CustomT::ExecT>
for App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
CustomT::ExecT: Clone + Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
Expand All @@ -152,6 +161,7 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
fn execute(&mut self, sender: Addr, msg: CosmosMsg<CustomT::ExecT>) -> AnyResult<AppResponse> {
let mut all = self.execute_multi(sender, vec![msg])?;
Expand All @@ -160,8 +170,8 @@ where
}
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
WasmT: Wasm<CustomT::ExecT, CustomT::QueryT>,
BankT: Bank,
Expand All @@ -172,9 +182,12 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
/// Returns a shared reference to application's router.
pub fn router(&self) -> &Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT> {
pub fn router(
&self,
) -> &Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT> {
&self.router
}

Expand All @@ -196,7 +209,7 @@ where
pub fn init_modules<F, T>(&mut self, init_fn: F) -> T
where
F: FnOnce(
&mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>,
&mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
&dyn Api,
&mut dyn Storage,
) -> T,
Expand All @@ -207,7 +220,7 @@ where
pub fn read_module<F, T>(&self, query_fn: F) -> T
where
F: FnOnce(
&Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>,
&Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
&dyn Api,
&dyn Storage,
) -> T,
Expand All @@ -218,8 +231,8 @@ where

// Helper functions to call some custom WasmKeeper logic.
// They show how we can easily add such calls to other custom keepers (CustomT, StakingT, etc)
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
BankT: Bank,
ApiT: Api,
Expand All @@ -230,6 +243,7 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
CustomT::ExecT: Clone + Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
{
Expand Down Expand Up @@ -317,8 +331,8 @@ where
}
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
CustomT::ExecT: Debug + PartialEq + Clone + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
Expand All @@ -331,6 +345,7 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
pub fn set_block(&mut self, block: BlockInfo) {
self.block = block;
Expand Down Expand Up @@ -423,7 +438,7 @@ where
}

#[derive(Clone)]
pub struct Router<Bank, Custom, Wasm, Staking, Distr, Ibc, Gov> {
pub struct Router<Bank, Custom, Wasm, Staking, Distr, Ibc, Gov, Stargate> {
// 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,
Expand All @@ -434,10 +449,11 @@ pub struct Router<Bank, Custom, Wasm, Staking, Distr, Ibc, Gov> {
pub distribution: Distr,
pub ibc: Ibc,
pub gov: Gov,
pub stargate: Stargate,
}

impl<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
CustomT::ExecT: Clone + Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
Expand All @@ -448,6 +464,7 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
pub fn querier<'a>(
&'a self,
Expand Down Expand Up @@ -521,8 +538,8 @@ pub trait CosmosRouter {
) -> AnyResult<AppResponse>;
}

impl<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT> CosmosRouter
for Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT>
impl<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT> CosmosRouter
for Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>
where
CustomT::ExecT: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
Expand All @@ -533,6 +550,7 @@ where
DistrT: Distribution,
IbcT: Ibc,
GovT: Gov,
StargateT: Stargate,
{
type ExecC = CustomT::ExecT;
type QueryC = CustomT::QueryT;
Expand All @@ -555,6 +573,14 @@ where
.execute(api, storage, self, block, sender, msg),
CosmosMsg::Ibc(msg) => self.ibc.execute(api, storage, self, block, sender, msg),
CosmosMsg::Gov(msg) => self.gov.execute(api, storage, self, block, sender, msg),
CosmosMsg::Stargate { type_url, value } => self.stargate.execute(
api,
storage,
self,
block,
sender,
StargateMsg { type_url, value },
),
_ => bail!("Cannot execute {:?}", msg),
}
}
Expand All @@ -576,6 +602,10 @@ where
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),
QueryRequest::Stargate { path, data } => {
self.stargate
.query(api, storage, &querier, block, StargateQuery { path, data })
}
_ => unimplemented!(),
}
}
Expand Down
Loading

0 comments on commit fd03c5b

Please sign in to comment.