Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitest module query #437

Merged
merged 4 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 75 additions & 21 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct App<
}

fn no_init<BankT, CustomT, WasmT, StakingT, DistrT>(
_: &Router<BankT, CustomT, WasmT, StakingT, DistrT>,
_: &mut Router<BankT, CustomT, WasmT, StakingT, DistrT>,
_: &dyn Api,
_: &mut dyn Storage,
) {
Expand All @@ -65,7 +65,7 @@ impl BasicApp {
pub fn new<F>(init_fn: F) -> Self
where
F: FnOnce(
&Router<
&mut Router<
BankKeeper,
FailingModule<Empty, Empty>,
WasmKeeper<Empty, Empty>,
Expand All @@ -87,7 +87,7 @@ where
ExecC: Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static,
QueryC: Debug + CustomQuery + DeserializeOwned + 'static,
F: FnOnce(
&Router<
&mut Router<
BankKeeper,
FailingModule<ExecC, QueryC>,
WasmKeeper<ExecC, QueryC>,
Expand Down Expand Up @@ -463,7 +463,9 @@ impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT>
StorageT: Storage,
CustomT: Module,
WasmT: Wasm<CustomT::ExecT, CustomT::QueryT>,
F: FnOnce(&Router<BankT, CustomT, WasmT, StakingT, DistrT>, &dyn Api, &mut dyn Storage),
StakingT: Staking,
DistrT: Distribution,
F: FnOnce(&mut Router<BankT, CustomT, WasmT, StakingT, DistrT>, &dyn Api, &mut dyn Storage),
{
let router = Router {
wasm: self.wasm,
Expand All @@ -473,15 +475,78 @@ impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT>
distribution: self.distribution,
};

let mut storage = self.storage;
init_fn(&router, &self.api, &mut storage);

App {
let mut app = App {
router,
api: self.api,
block: self.block,
storage,
}
storage: self.storage,
};
app.init_modules(init_fn);
app
}
}

impl<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT>
App<BankT, ApiT, StorageT, CustomT, WasmT, StakingT, DistrT>
where
WasmT: Wasm<CustomT::ExecT, CustomT::QueryT>,
BankT: Bank,
ApiT: Api,
StorageT: Storage,
CustomT: Module,
StakingT: Staking,
DistrT: Distribution,
{
pub fn init_modules<F, T>(&mut self, init_fn: F) -> T
where
F: FnOnce(
&mut Router<BankT, CustomT, WasmT, StakingT, DistrT>,
&dyn Api,
&mut dyn Storage,
) -> T,
{
init_fn(&mut self.router, &self.api, &mut self.storage)
}

pub fn read_module<F, T>(&self, query_fn: F) -> T
where
F: FnOnce(&Router<BankT, CustomT, WasmT, StakingT, DistrT>, &dyn Api, &dyn Storage) -> T,
{
query_fn(&self.router, &self.api, &self.storage)
}
}

// 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, StakingT, DistrT>
App<
BankT,
ApiT,
StorageT,
CustomT,
WasmKeeper<CustomT::ExecT, CustomT::QueryT>,
StakingT,
DistrT,
>
where
BankT: Bank,
ApiT: Api,
StorageT: Storage,
CustomT: Module,
StakingT: Staking,
DistrT: Distribution,
CustomT::ExecT: Clone + fmt::Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
{
/// This registers contract code (like uploading wasm bytecode on a chain),
/// so it can later be used to instantiate a contract.
pub fn store_code(&mut self, code: Box<dyn Contract<CustomT::ExecT>>) -> u64 {
self.init_modules(|router, _, _| router.wasm.store_code(code) as u64)
}

/// This allows to get `ContractData` for specific contract
pub fn contract_data(&self, address: &Addr) -> AnyResult<ContractData> {
self.read_module(|router, _, storage| router.wasm.load_contract(storage, address))
}
}

Expand Down Expand Up @@ -544,17 +609,6 @@ where
})
}

/// This registers contract code (like uploading wasm bytecode on a chain),
/// so it can later be used to instantiate a contract.
pub fn store_code(&mut self, code: Box<dyn Contract<CustomT::ExecT>>) -> u64 {
self.router.wasm.store_code(code) as u64
}

/// This allows to get `ContractData` for specific contract
pub fn contract_data(&self, address: &Addr) -> AnyResult<ContractData> {
self.router.wasm.contract_data(&self.storage, address)
}

/// Runs arbitrary CosmosMsg in "sudo" mode.
/// This will create a cache before the execution, so no state changes are persisted if this
/// returns an error, but all are persisted on success.
Expand Down
36 changes: 14 additions & 22 deletions packages/multi-test/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ pub trait Wasm<ExecC, QueryC> {
msg: WasmMsg,
) -> AnyResult<AppResponse>;

// Add a new contract. Must be done on the base object, when no contracts running
fn store_code(&mut self, code: Box<dyn Contract<ExecC>>) -> usize;

// Helper for querying for specific contract data
fn contract_data(&self, storage: &dyn Storage, address: &Addr) -> AnyResult<ContractData>;

/// Admin interface, cannot be called via CosmosMsg
fn sudo(
&self,
Expand Down Expand Up @@ -145,16 +139,6 @@ where
self.process_response(api, router, storage, block, resender, res, msgs)
}

fn store_code(&mut self, code: Box<dyn Contract<ExecC>>) -> usize {
let idx = self.codes.len() + 1;
self.codes.insert(idx, code);
idx
}

fn contract_data(&self, storage: &dyn Storage, address: &Addr) -> AnyResult<ContractData> {
self.load_contract(storage, address)
}

fn sudo(
&self,
api: &dyn Api,
Expand All @@ -172,6 +156,20 @@ where
}
}

impl<ExecC, QueryC> WasmKeeper<ExecC, QueryC> {
pub fn store_code(&mut self, code: Box<dyn Contract<ExecC>>) -> usize {
let idx = self.codes.len() + 1;
self.codes.insert(idx, code);
idx
}

pub fn load_contract(&self, storage: &dyn Storage, address: &Addr) -> AnyResult<ContractData> {
CONTRACTS
.load(&prefixed_read(storage, NAMESPACE_WASM), address)
.map_err(Into::into)
}
}

impl<ExecC, QueryC> WasmKeeper<ExecC, QueryC>
where
ExecC: Clone + fmt::Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
Expand Down Expand Up @@ -719,12 +717,6 @@ where
})
}

pub fn load_contract(&self, storage: &dyn Storage, address: &Addr) -> AnyResult<ContractData> {
CONTRACTS
.load(&prefixed_read(storage, NAMESPACE_WASM), address)
.map_err(Into::into)
}

pub fn save_contract(
&self,
storage: &mut dyn Storage,
Expand Down