Skip to content

Commit

Permalink
wip better state reloading mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
abright committed Sep 13, 2022
1 parent b74df73 commit 965e280
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
7 changes: 7 additions & 0 deletions fil_fungible_token/src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ where
self.state = state;
}

/// Loads a fresh copy of the state from a blockstore from a given cid, replacing existing state
/// The old state is returned to enable comparisons and the like but can be safely dropped otherwise
pub fn load_replace(&mut self, cid: &Cid) -> Result<TokenState> {
let new_state = TokenState::load(&self.bs, cid)?;
Ok(std::mem::replace(self.state, new_state))
}

/// Opens an atomic transaction on TokenState which allows a closure to make multiple
/// modifications to the state tree.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ repository = "https://github.com/helix-collective/filecoin"
edition = "2021"

[dependencies]
cid = { version = "0.8.5", default-features = false }
fvm_ipld_blockstore = { version = "0.1.1" }
fvm_ipld_encoding = { version = "0.2.2" }
fvm_sdk = { version = "2.0.0-alpha.2" }
Expand Down
36 changes: 13 additions & 23 deletions testing/fil_token_integration/actors/basic_token_actor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod util;

use cid::Cid;
use fil_fungible_token::runtime::blockstore::Blockstore;
use fil_fungible_token::runtime::messaging::FvmMessenger;
use fil_fungible_token::token::types::{
Expand Down Expand Up @@ -146,7 +145,8 @@ pub struct MintParams {
impl Cbor for MintParams {}

impl BasicToken<'_> {
fn mint(&mut self, params: MintParams) -> Result<(Cid, MintReturn), RuntimeError> {
fn mint(&mut self, params: MintParams) -> Result<MintReturn, RuntimeError> {
let owner = params.initial_owner;
let (mut hook, ret) = self.util.mint(
&caller_address(),
&params.initial_owner,
Expand All @@ -160,7 +160,15 @@ impl BasicToken<'_> {

hook.call(self.util.msg())?;

Ok((cid, ret))
let new_cid = sdk::sself::root().unwrap();
let ret = if cid == new_cid {
ret
} else {
self.util.load_replace(&new_cid).unwrap();
MintReturn { balance: self.balance_of(owner).unwrap(), supply: self.total_supply() }
};

Ok(ret)
}
}

Expand Down Expand Up @@ -288,26 +296,8 @@ pub fn invoke(params: u32) -> u32 {
3839021839 => {
// Mint
let params: MintParams = deserialize_params(params);
let owner = params.initial_owner;
let (cid, res) = token_actor.mint(params).unwrap();

// TODO: we need to know if the cid changed. having mint return it kinda works but is messy
// but we also can't reload state inside those calls
let new_cid = sdk::sself::root().unwrap();
if cid == new_cid {
return_ipld(&res).unwrap()
} else {
token_state = Token::<_, FvmMessenger>::load_state(&bs, &root_cid).unwrap();
token_actor = BasicToken {
util: Token::wrap(bs, FvmMessenger::default(), 1, &mut token_state),
};

return_ipld(&MintReturn {
balance: token_actor.balance_of(owner).unwrap(),
supply: token_actor.total_supply(),
})
.unwrap()
}
let res = token_actor.mint(params).unwrap();
return_ipld(&res).unwrap()
}
_ => {
sdk::vm::abort(
Expand Down
2 changes: 1 addition & 1 deletion testing/fil_token_integration/tests/more_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn more_tests() {
println!("minting return data {:#?}", &ret_val);
let mint_result: MintReturn = ret_val.msg_receipt.return_data.deserialize().unwrap();
// tokens were burned so supply reduces back to zero
println!("minted - total supply: {:?}", &mint_result.supply);
println!("minted - total supply: {:?}, balance: {:?}", &mint_result.supply, &mint_result.balance);
assert_eq!(mint_result.supply, TokenAmount::from_atto(0));

// check balance of test actor, should also be zero
Expand Down

0 comments on commit 965e280

Please sign in to comment.