Skip to content

Commit

Permalink
imp: get access to bank and expose types from modules other than ibc
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani committed Nov 30, 2023
1 parent 2575a5b commit 7e68779
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 128 deletions.
186 changes: 126 additions & 60 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/app/src/abci/v0_37/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use tendermint_proto::v0_37::abci::ResponseQuery;
use tendermint::merkle::proof::ProofOp;
use tendermint::merkle::proof::ProofOps;

use crate::modules::auth::account::ACCOUNT_PREFIX;
use crate::modules::auth::ACCOUNT_PREFIX;
use crate::modules::types::IdentifiedModule;
use crate::utils::macros::ResponseFromErrorExt;

Expand Down
41 changes: 33 additions & 8 deletions crates/app/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::modules::auth::AuthAccountKeeper;
use crate::modules::auth::AuthAccountReader;
use crate::modules::bank::Bank;
use crate::modules::context::prefix;
use crate::modules::context::Identifiable;
use crate::modules::context::Module;
use crate::modules::ibc::impls::Ibc;
use crate::modules::ibc::Ibc;
use crate::modules::types::IdentifiedModule;
use crate::modules::types::ModuleList;
use crate::modules::types::ModuleStore;
Expand Down Expand Up @@ -119,19 +122,41 @@ impl<S: Default + Debug + ProvableStore> BaseCoinApp<S> {
pub fn ibc(&self) -> Ibc<RevertibleStore<S>> {
let modules = self.modules.read_access();

let ibc_module = modules
modules
.iter()
.find(|m| m.id == prefix::Ibc {}.identifier())
.and_then(|m| {
let a = m
.module
m.module
.as_any()
.downcast_ref::<Ibc<RevertibleStore<S>>>()
.cloned();
a
.cloned()
})
.expect("IBC module not found");
.expect("IBC module not found")
}

ibc_module
/// Gives access to the Bank module.
pub fn bank(
&self,
) -> Bank<
RevertibleStore<S>,
AuthAccountReader<RevertibleStore<S>>,
AuthAccountKeeper<RevertibleStore<S>>,
> {
let modules = self.modules.read_access();

modules
.iter()
.find(|m| m.id == prefix::Bank {}.identifier())
.and_then(|m| {
m.module
.as_any()
.downcast_ref::<Bank<
RevertibleStore<S>,
AuthAccountReader<RevertibleStore<S>>,
AuthAccountKeeper<RevertibleStore<S>>,
>>()
.cloned()
})
.expect("Bank module not found")
}
}
2 changes: 1 addition & 1 deletion crates/app/src/modules/auth/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::modules::auth::account::AccountsPath;
use crate::modules::auth::account::AuthAccount;
use crate::modules::auth::context::{Account, AccountKeeper, AccountReader};
use crate::modules::auth::service::AuthService;
use crate::modules::bank::util::Denom;
use crate::modules::bank::Denom;
use crate::{modules::context::Module, types::error::Error as AppError};

use basecoin_store::context::{ProvableStore, Store};
Expand Down
19 changes: 14 additions & 5 deletions crates/app/src/modules/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
pub(crate) mod account;
pub(crate) mod context;
pub(crate) mod impls;
pub(crate) mod service;
mod account;
mod context;
mod impls;
mod service;

pub use impls::{Auth, AuthAccountKeeper, AuthAccountReader};
pub use account::*;
pub use context::*;
pub use impls::*;
pub use service::*;

/// Re-exports `auth` module proto types for convenience.
pub mod proto {
pub use cosmrs::AccountId;
pub use ibc_proto::cosmos::auth::*;
}
34 changes: 24 additions & 10 deletions crates/app/src/modules/bank/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use super::context::{BankKeeper, BankReader};
use super::error::Error;
use super::service::BankService;
use super::util::{Balances, BalancesPath, Coin, Denom};
use crate::modules::auth::account::{AuthAccount, ACCOUNT_PREFIX};
use crate::modules::auth::context::{AccountKeeper, AccountReader};
use crate::modules::auth::AccountKeeper;
use crate::modules::auth::AccountReader;
use crate::modules::auth::{AuthAccount, ACCOUNT_PREFIX};
use crate::modules::context::Module;
use crate::types::error::Error as AppError;
use crate::types::QueryResult;

use basecoin_store::context::{ProvableStore, Store};
use basecoin_store::impls::SharedStore;
use basecoin_store::types::{Height, JsonStore, Path, TypedStore};
use basecoin_store::utils::{Codec, JsonCodec};
use basecoin_store::utils::{Async, Codec, JsonCodec};

use cosmrs::{bank::MsgSend, proto, AccountId};
use ibc_proto::{cosmos::bank::v1beta1::query_server::QueryServer, google::protobuf::Any};
Expand Down Expand Up @@ -199,7 +200,12 @@ pub struct Bank<S, AR, AK> {
account_keeper: AK,
}

impl<S: ProvableStore + Default, AR: AccountReader, AK: AccountKeeper> Bank<S, AR, AK> {
impl<S, AR, AK> Bank<S, AR, AK>
where
S: ProvableStore + Default,
AR: AccountReader,
AK: AccountKeeper,
{
pub fn new(store: SharedStore<S>, account_reader: AR, account_keeper: AK) -> Self {
Self {
store: store.clone(),
Expand All @@ -220,12 +226,21 @@ impl<S: ProvableStore + Default, AR: AccountReader, AK: AccountKeeper> Bank<S, A
})
}

pub fn balance_reader(&self) -> &BankBalanceReader<S> {
&self.balance_reader
}

pub fn bank_keeper(&self) -> &BankBalanceKeeper<S> {
&self.balance_keeper
}
}

impl<S: Store, AR: AccountReader, AK: AccountKeeper> Bank<S, AR, AK> {
impl<S, AR, AK> Bank<S, AR, AK>
where
S: ProvableStore,
AR: AccountReader,
AK: AccountKeeper,
{
fn decode<T: Message + Default>(message: Any) -> Result<T, AppError> {
if message.type_url != "/cosmos.bank.v1beta1.MsgSend" {
return Err(AppError::NotHandled);
Expand All @@ -234,12 +249,11 @@ impl<S: Store, AR: AccountReader, AK: AccountKeeper> Bank<S, AR, AK> {
}
}

impl<
S: ProvableStore,
AR: AccountReader + Send + Sync + 'static,
AK: AccountKeeper + Send + Sync + 'static,
> Module for Bank<S, AR, AK>
impl<S, AR, AK> Module for Bank<S, AR, AK>
where
S: ProvableStore,
AR: AccountReader + Async,
AK: AccountKeeper + Async,
<AR as AccountReader>::Address: From<AccountId>,
<AK as AccountKeeper>::Account: From<AuthAccount>,
{
Expand Down
22 changes: 16 additions & 6 deletions crates/app/src/modules/bank/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
pub(crate) mod context;
pub(crate) mod error;
pub(crate) mod impls;
pub(crate) mod service;
pub(crate) mod util;
mod context;
mod error;
mod impls;
mod service;
mod util;

pub use impls::Bank;
pub use context::*;
pub use error::*;
pub use impls::*;
pub use service::*;
pub use util::*;

/// Re-exports `bank` module proto types for convenience.
pub mod proto {
pub use cosmrs::Coin;
pub use ibc_proto::cosmos::bank::*;
}
14 changes: 11 additions & 3 deletions crates/app/src/modules/gov/impls.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::path::ProposalPath;
use super::proposal::Proposal;
use super::service::GovernanceService;
use crate::modules::context::Module;
use crate::modules::context::{Identifiable, Module};
use crate::modules::gov::msg::MsgSubmitProposal;
use crate::modules::upgrade::Upgrade;
use crate::types::error::Error as AppError;
use crate::types::QueryResult;

use basecoin_store::context::Store;
use basecoin_store::context::{ProvableStore, Store};
use basecoin_store::impls::SharedStore;
use basecoin_store::types::{Height, Path, ProtobufStore, TypedStore};
use basecoin_store::types::{Height, Identifier, Path, ProtobufStore, TypedStore};
use basecoin_store::utils::{SharedRw, SharedRwExt};

use ibc::cosmos_host::upgrade_proposal::upgrade_client_proposal_handler;
Expand Down Expand Up @@ -58,6 +58,14 @@ where
}
}

impl<S: ProvableStore + Debug> Identifiable for Upgrade<S> {
type Identifier = Identifier;

fn identifier(&self) -> Self::Identifier {
"upgrade".to_owned().try_into().unwrap()
}
}

impl<S> Module for Governance<S>
where
S: Store + Debug,
Expand Down
24 changes: 17 additions & 7 deletions crates/app/src/modules/gov/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
pub(crate) mod error;
pub(crate) mod impls;
pub(crate) mod msg;
pub(crate) mod path;
pub(crate) mod proposal;
pub(crate) mod service;
mod error;
mod impls;
mod msg;
mod path;
mod proposal;
mod service;

pub use impls::Governance;
pub use error::*;
pub use impls::*;
pub use msg::*;
pub use path::*;
pub use proposal::*;
pub use service::*;

/// Re-exports `gov` module proto types for convenience.
pub mod proto {
pub use ibc_proto::cosmos::gov::*;
}
2 changes: 1 addition & 1 deletion crates/app/src/modules/gov/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ibc_proto::cosmos::gov::v1beta1::ProposalStatus;
use ibc_proto::google::protobuf::Any;
use ibc_proto::Protobuf;

use crate::modules::bank::util::Coin;
use crate::modules::bank::Coin;
use crate::types::error::Error;

use super::proposal::Proposal;
Expand Down
2 changes: 1 addition & 1 deletion crates/app/src/modules/gov/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ibc_proto::google::protobuf::{Any, Timestamp};
use ibc_proto::Protobuf;

use super::error::Error;
use crate::modules::bank::util::Coin;
use crate::modules::bank::Coin;

pub(crate) const TYPE_URL: &str = "/cosmos.gov.v1beta1.Proposal";

Expand Down
11 changes: 10 additions & 1 deletion crates/app/src/modules/ibc/impls.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::CHAIN_REVISION_NUMBER;
use crate::{
modules::{
bank::impls::BankBalanceKeeper,
bank::BankBalanceKeeper,
context::{Identifiable, Module},
ibc::{router::IbcRouter, transfer::IbcTransferModule},
upgrade::Upgrade,
},
types::{error::Error as AppError, QueryResult},
};
use basecoin_store::types::Identifier;
use basecoin_store::{
context::{ProvableStore, Store},
impls::SharedStore,
Expand Down Expand Up @@ -197,6 +198,14 @@ where
}
}

impl<S: ProvableStore + Debug> Identifiable for Ibc<S> {
type Identifier = Identifier;

fn identifier(&self) -> Self::Identifier {
"ibc".to_owned().try_into().unwrap()
}
}

impl<S> Module for Ibc<S>
where
S: ProvableStore + Debug,
Expand Down
17 changes: 9 additions & 8 deletions crates/app/src/modules/ibc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
pub mod client_contexts;
pub mod error;
pub mod impls;
mod client_contexts;
mod error;
mod impls;
mod router;
pub mod transfer;
mod transfer;

pub use impls::AnyConsensusState;
pub use impls::Ibc;
pub use impls::IbcContext;
pub use transfer::IbcTransferModule;
pub use client_contexts::*;
pub use error::*;
pub use impls::*;
pub use router::*;
pub use transfer::*;
2 changes: 1 addition & 1 deletion crates/app/src/modules/ibc/router.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::modules::bank::impls::BankBalanceKeeper;
use crate::modules::bank::BankBalanceKeeper;
use crate::modules::ibc::transfer::IbcTransferModule;

use basecoin_store::context::Store;
Expand Down
6 changes: 3 additions & 3 deletions crates/app/src/modules/ibc/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::modules::auth::account::ACCOUNT_PREFIX;
use crate::modules::bank::context::BankKeeper;
use crate::modules::bank::util::{Coin, Denom};
use crate::modules::auth::ACCOUNT_PREFIX;
use crate::modules::bank::BankKeeper;
use crate::modules::bank::{Coin, Denom};

use ibc::apps::transfer::context::TokenTransferExecutionContext;
use ibc::apps::transfer::context::TokenTransferValidationContext;
Expand Down
12 changes: 9 additions & 3 deletions crates/app/src/modules/staking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pub(crate) mod impls;
pub(crate) mod service;
mod impls;
mod service;

pub use impls::Staking;
pub use impls::*;
pub use service::*;

/// Re-exports `staking` module proto types for convenience.
pub mod proto {
pub use ibc_proto::cosmos::staking::*;
}
10 changes: 9 additions & 1 deletion crates/app/src/modules/upgrade/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use super::path::UpgradePlanPath;
use super::query::UPGRADE_PLAN_QUERY_PATH;
use super::service::UpgradeService;
use crate::modules::context::Module;
use crate::modules::ibc::impls::{AnyConsensusState, IbcContext};
use crate::modules::ibc::{AnyConsensusState, IbcContext};
use crate::types::error::Error as AppError;
use crate::types::query::QueryResult;

Expand Down Expand Up @@ -71,6 +71,14 @@ where
}
}

// impl<S: ProvableStore + Debug> Identifiable for Upgrade<S> {
// type Identifier = Identifier;

// fn identifier(&self) -> Self::Identifier {
// "upgrade".to_owned().try_into().unwrap()
// }
// }

impl<S> Module for Upgrade<S>
where
S: ProvableStore + Debug,
Expand Down
Loading

0 comments on commit 7e68779

Please sign in to comment.