Skip to content

Commit 965e280

Browse files
committed
wip better state reloading mechanism
1 parent b74df73 commit 965e280

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

fil_fungible_token/src/token/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ where
9999
self.state = state;
100100
}
101101

102+
/// Loads a fresh copy of the state from a blockstore from a given cid, replacing existing state
103+
/// The old state is returned to enable comparisons and the like but can be safely dropped otherwise
104+
pub fn load_replace(&mut self, cid: &Cid) -> Result<TokenState> {
105+
let new_state = TokenState::load(&self.bs, cid)?;
106+
Ok(std::mem::replace(self.state, new_state))
107+
}
108+
102109
/// Opens an atomic transaction on TokenState which allows a closure to make multiple
103110
/// modifications to the state tree.
104111
///

testing/fil_token_integration/actors/basic_token_actor/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ repository = "https://github.com/helix-collective/filecoin"
55
edition = "2021"
66

77
[dependencies]
8-
cid = { version = "0.8.5", default-features = false }
98
fvm_ipld_blockstore = { version = "0.1.1" }
109
fvm_ipld_encoding = { version = "0.2.2" }
1110
fvm_sdk = { version = "2.0.0-alpha.2" }

testing/fil_token_integration/actors/basic_token_actor/src/lib.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod util;
22

3-
use cid::Cid;
43
use fil_fungible_token::runtime::blockstore::Blockstore;
54
use fil_fungible_token::runtime::messaging::FvmMessenger;
65
use fil_fungible_token::token::types::{
@@ -146,7 +145,8 @@ pub struct MintParams {
146145
impl Cbor for MintParams {}
147146

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

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

163-
Ok((cid, ret))
163+
let new_cid = sdk::sself::root().unwrap();
164+
let ret = if cid == new_cid {
165+
ret
166+
} else {
167+
self.util.load_replace(&new_cid).unwrap();
168+
MintReturn { balance: self.balance_of(owner).unwrap(), supply: self.total_supply() }
169+
};
170+
171+
Ok(ret)
164172
}
165173
}
166174

@@ -288,26 +296,8 @@ pub fn invoke(params: u32) -> u32 {
288296
3839021839 => {
289297
// Mint
290298
let params: MintParams = deserialize_params(params);
291-
let owner = params.initial_owner;
292-
let (cid, res) = token_actor.mint(params).unwrap();
293-
294-
// TODO: we need to know if the cid changed. having mint return it kinda works but is messy
295-
// but we also can't reload state inside those calls
296-
let new_cid = sdk::sself::root().unwrap();
297-
if cid == new_cid {
298-
return_ipld(&res).unwrap()
299-
} else {
300-
token_state = Token::<_, FvmMessenger>::load_state(&bs, &root_cid).unwrap();
301-
token_actor = BasicToken {
302-
util: Token::wrap(bs, FvmMessenger::default(), 1, &mut token_state),
303-
};
304-
305-
return_ipld(&MintReturn {
306-
balance: token_actor.balance_of(owner).unwrap(),
307-
supply: token_actor.total_supply(),
308-
})
309-
.unwrap()
310-
}
299+
let res = token_actor.mint(params).unwrap();
300+
return_ipld(&res).unwrap()
311301
}
312302
_ => {
313303
sdk::vm::abort(

testing/fil_token_integration/tests/more_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn more_tests() {
117117
println!("minting return data {:#?}", &ret_val);
118118
let mint_result: MintReturn = ret_val.msg_receipt.return_data.deserialize().unwrap();
119119
// tokens were burned so supply reduces back to zero
120-
println!("minted - total supply: {:?}", &mint_result.supply);
120+
println!("minted - total supply: {:?}, balance: {:?}", &mint_result.supply, &mint_result.balance);
121121
assert_eq!(mint_result.supply, TokenAmount::from_atto(0));
122122

123123
// check balance of test actor, should also be zero

0 commit comments

Comments
 (0)