Skip to content

Commit

Permalink
Merge pull request #135 from CosmWasm/try-to-set-cache
Browse files Browse the repository at this point in the history
Properly cache wasm code
  • Loading branch information
ethanfrey committed Nov 6, 2020
2 parents a2b075c + 610ca9c commit 7f2292d
Show file tree
Hide file tree
Showing 6 changed files with 508 additions and 337 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ jobs:
- run:
name: Run unit tests
command: cargo test --locked
- run:
name: Run unit tests (with iterator)
command: cargo test --locked --features iterator
- save_cache:
paths:
- /usr/local/cargo/registry
Expand Down
63 changes: 63 additions & 0 deletions packages/multi-test/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cosmwasm_std::{

//*** TODO: remove this and import cw0::balance when we are both on 0.12 ***/
use crate::balance::NativeBalance;
use crate::transactions::{RepLog, StorageTransaction};

/// Bank is a minimal contract-like interface that implements a bank module
/// It is initialized outside of the trait
Expand All @@ -29,6 +30,68 @@ pub trait Bank {
fn clone(&self) -> Box<dyn Bank>;
}

pub struct BankRouter {
bank: Box<dyn Bank>,
storage: Box<dyn Storage>,
}

impl BankRouter {
pub fn new<B: Bank + 'static>(bank: B, storage: Box<dyn Storage>) -> Self {
BankRouter {
bank: Box::new(bank),
storage,
}
}

// this is an "admin" function to let us adjust bank accounts
pub fn set_balance(&mut self, account: HumanAddr, amount: Vec<Coin>) -> Result<(), String> {
self.bank
.set_balance(self.storage.as_mut(), account, amount)
}

pub fn cache(&'_ self) -> BankCache<'_> {
BankCache::new(self)
}

pub fn query(&self, request: BankQuery) -> Result<Binary, String> {
self.bank.query(self.storage.as_ref(), request)
}
}

pub struct BankCache<'a> {
// and this into one with reference
router: &'a BankRouter,
state: StorageTransaction<'a>,
}

pub struct BankOps(RepLog);

impl BankOps {
pub fn commit(self, router: &mut BankRouter) {
self.0.commit(router.storage.as_mut())
}
}

impl<'a> BankCache<'a> {
fn new(router: &'a BankRouter) -> Self {
BankCache {
router,
state: StorageTransaction::new(router.storage.as_ref()),
}
}

/// When we want to commit the BankCache, we need a 2 step process to satisfy Rust reference counting:
/// 1. prepare() consumes BankCache, releasing &BankRouter, and creating a self-owned update info.
/// 2. BankOps::commit() can now take &mut BankRouter and updates the underlying state
pub fn prepare(self) -> BankOps {
BankOps(self.state.prepare())
}

pub fn execute(&mut self, sender: HumanAddr, msg: BankMsg) -> Result<(), String> {
self.router.bank.handle(&mut self.state, sender, msg)
}
}

#[derive(Default)]
pub struct SimpleBank {}

Expand Down
Loading

0 comments on commit 7f2292d

Please sign in to comment.