From 965e280bdb8ecb4f4273499d2245ba5e533f8b09 Mon Sep 17 00:00:00 2001 From: Andrew Bright Date: Tue, 13 Sep 2022 12:57:35 +1000 Subject: [PATCH] wip better state reloading mechanism --- fil_fungible_token/src/token/mod.rs | 7 ++++ .../actors/basic_token_actor/Cargo.toml | 1 - .../actors/basic_token_actor/src/lib.rs | 36 +++++++------------ .../fil_token_integration/tests/more_tests.rs | 2 +- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/fil_fungible_token/src/token/mod.rs b/fil_fungible_token/src/token/mod.rs index 395e2fe6..d4eac445 100644 --- a/fil_fungible_token/src/token/mod.rs +++ b/fil_fungible_token/src/token/mod.rs @@ -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 { + 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. /// diff --git a/testing/fil_token_integration/actors/basic_token_actor/Cargo.toml b/testing/fil_token_integration/actors/basic_token_actor/Cargo.toml index 1557dc7b..978fe30b 100644 --- a/testing/fil_token_integration/actors/basic_token_actor/Cargo.toml +++ b/testing/fil_token_integration/actors/basic_token_actor/Cargo.toml @@ -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" } diff --git a/testing/fil_token_integration/actors/basic_token_actor/src/lib.rs b/testing/fil_token_integration/actors/basic_token_actor/src/lib.rs index 94fbdb6a..2cf71114 100644 --- a/testing/fil_token_integration/actors/basic_token_actor/src/lib.rs +++ b/testing/fil_token_integration/actors/basic_token_actor/src/lib.rs @@ -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::{ @@ -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 { + let owner = params.initial_owner; let (mut hook, ret) = self.util.mint( &caller_address(), ¶ms.initial_owner, @@ -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) } } @@ -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( diff --git a/testing/fil_token_integration/tests/more_tests.rs b/testing/fil_token_integration/tests/more_tests.rs index ed3df4d9..a1ed80e4 100644 --- a/testing/fil_token_integration/tests/more_tests.rs +++ b/testing/fil_token_integration/tests/more_tests.rs @@ -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