Skip to content

Commit c4ad8b5

Browse files
authored
Refactor TestVM fixtures to take Address and TokenAmount by reference (#1246)
* refactor test vm to use references for Address and TokenAmount * run `cargo fix --workspace --tests --broken-code`
1 parent ebbf25e commit c4ad8b5

21 files changed

+1090
-1075
lines changed

test_vm/src/lib.rs

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,21 @@ where
158158
let sys_st = SystemState::new(store).unwrap();
159159
let sys_head = v.put_store(&sys_st);
160160
let sys_value = faucet_total.clone(); // delegate faucet funds to system so we can construct faucet by sending to bls addr
161-
v.set_actor(SYSTEM_ACTOR_ADDR, actor(*SYSTEM_ACTOR_CODE_ID, sys_head, 0, sys_value, None));
161+
v.set_actor(&SYSTEM_ACTOR_ADDR, actor(*SYSTEM_ACTOR_CODE_ID, sys_head, 0, sys_value, None));
162162

163163
// init
164164
let init_st = InitState::new(store, "integration-test".to_string()).unwrap();
165165
let init_head = v.put_store(&init_st);
166166
v.set_actor(
167-
INIT_ACTOR_ADDR,
167+
&INIT_ACTOR_ADDR,
168168
actor(*INIT_ACTOR_CODE_ID, init_head, 0, TokenAmount::zero(), None),
169169
);
170170

171171
// reward
172172

173173
let reward_head = v.put_store(&RewardState::new(StoragePower::zero()));
174174
v.set_actor(
175-
REWARD_ACTOR_ADDR,
175+
&REWARD_ACTOR_ADDR,
176176
actor(*REWARD_ACTOR_CODE_ID, reward_head, 0, reward_total, None),
177177
);
178178

@@ -189,30 +189,30 @@ where
189189
];
190190
let cron_head = v.put_store(&CronState { entries: builtin_entries });
191191
v.set_actor(
192-
CRON_ACTOR_ADDR,
192+
&CRON_ACTOR_ADDR,
193193
actor(*CRON_ACTOR_CODE_ID, cron_head, 0, TokenAmount::zero(), None),
194194
);
195195

196196
// power
197197
let power_head = v.put_store(&PowerState::new(&v.store).unwrap());
198198
v.set_actor(
199-
STORAGE_POWER_ACTOR_ADDR,
199+
&STORAGE_POWER_ACTOR_ADDR,
200200
actor(*POWER_ACTOR_CODE_ID, power_head, 0, TokenAmount::zero(), None),
201201
);
202202

203203
// market
204204
let market_head = v.put_store(&MarketState::new(&v.store).unwrap());
205205
v.set_actor(
206-
STORAGE_MARKET_ACTOR_ADDR,
206+
&STORAGE_MARKET_ACTOR_ADDR,
207207
actor(*MARKET_ACTOR_CODE_ID, market_head, 0, TokenAmount::zero(), None),
208208
);
209209

210210
// verifreg
211211
// initialize verifreg root signer
212212
v.apply_message(
213-
INIT_ACTOR_ADDR,
214-
Address::new_bls(VERIFREG_ROOT_KEY).unwrap(),
215-
TokenAmount::zero(),
213+
&INIT_ACTOR_ADDR,
214+
&Address::new_bls(VERIFREG_ROOT_KEY).unwrap(),
215+
&TokenAmount::zero(),
216216
METHOD_SEND,
217217
None::<RawBytes>,
218218
)
@@ -233,9 +233,9 @@ where
233233
.unwrap();
234234
let msig_ctor_ret: ExecReturn = v
235235
.apply_message(
236-
SYSTEM_ACTOR_ADDR,
237-
INIT_ACTOR_ADDR,
238-
TokenAmount::zero(),
236+
&SYSTEM_ACTOR_ADDR,
237+
&INIT_ACTOR_ADDR,
238+
&TokenAmount::zero(),
239239
fil_actor_init::Method::Exec as u64,
240240
Some(fil_actor_init::ExecParams {
241241
code_cid: *MULTISIG_ACTOR_CODE_ID,
@@ -252,36 +252,36 @@ where
252252
// verifreg
253253
let verifreg_head = v.put_store(&VerifRegState::new(&v.store, root_msig_addr).unwrap());
254254
v.set_actor(
255-
VERIFIED_REGISTRY_ACTOR_ADDR,
255+
&VERIFIED_REGISTRY_ACTOR_ADDR,
256256
actor(*VERIFREG_ACTOR_CODE_ID, verifreg_head, 0, TokenAmount::zero(), None),
257257
);
258258

259259
// Ethereum Address Manager
260260
v.set_actor(
261-
EAM_ACTOR_ADDR,
261+
&EAM_ACTOR_ADDR,
262262
actor(*EAM_ACTOR_CODE_ID, EMPTY_ARR_CID, 0, TokenAmount::zero(), None),
263263
);
264264

265265
// datacap
266266
let datacap_head =
267267
v.put_store(&DataCapState::new(&v.store, VERIFIED_REGISTRY_ACTOR_ADDR).unwrap());
268268
v.set_actor(
269-
DATACAP_TOKEN_ACTOR_ADDR,
269+
&DATACAP_TOKEN_ACTOR_ADDR,
270270
actor(*DATACAP_TOKEN_ACTOR_CODE_ID, datacap_head, 0, TokenAmount::zero(), None),
271271
);
272272

273273
// burnt funds
274274
let burnt_funds_head = v.put_store(&AccountState { address: BURNT_FUNDS_ACTOR_ADDR });
275275
v.set_actor(
276-
BURNT_FUNDS_ACTOR_ADDR,
276+
&BURNT_FUNDS_ACTOR_ADDR,
277277
actor(*ACCOUNT_ACTOR_CODE_ID, burnt_funds_head, 0, TokenAmount::zero(), None),
278278
);
279279

280280
// create a faucet with 1 billion FIL for setting up test accounts
281281
v.apply_message(
282-
SYSTEM_ACTOR_ADDR,
283-
Address::new_bls(FAUCET_ROOT_KEY).unwrap(),
284-
faucet_total,
282+
&SYSTEM_ACTOR_ADDR,
283+
&Address::new_bls(FAUCET_ROOT_KEY).unwrap(),
284+
&faucet_total,
285285
METHOD_SEND,
286286
None::<RawBytes>,
287287
)
@@ -305,7 +305,7 @@ where
305305
}
306306
}
307307

308-
pub fn get_miner_balance(&self, maddr: Address) -> MinerBalances {
308+
pub fn get_miner_balance(&self, maddr: &Address) -> MinerBalances {
309309
let a = self.get_actor(maddr).unwrap();
310310
let st = self.get_state::<MinerState>(maddr).unwrap();
311311
MinerBalances {
@@ -316,15 +316,15 @@ where
316316
}
317317
}
318318

319-
pub fn get_miner_info(&self, maddr: Address) -> MinerInfo {
319+
pub fn get_miner_info(&self, maddr: &Address) -> MinerInfo {
320320
let st = self.get_state::<MinerState>(maddr).unwrap();
321321
self.store.get_cbor::<MinerInfo>(&st.info).unwrap().unwrap()
322322
}
323323

324324
pub fn get_network_stats(&self) -> NetworkStats {
325-
let power_state = self.get_state::<PowerState>(STORAGE_POWER_ACTOR_ADDR).unwrap();
326-
let reward_state = self.get_state::<RewardState>(REWARD_ACTOR_ADDR).unwrap();
327-
let market_state = self.get_state::<MarketState>(STORAGE_MARKET_ACTOR_ADDR).unwrap();
325+
let power_state = self.get_state::<PowerState>(&STORAGE_POWER_ACTOR_ADDR).unwrap();
326+
let reward_state = self.get_state::<RewardState>(&REWARD_ACTOR_ADDR).unwrap();
327+
let market_state = self.get_state::<MarketState>(&STORAGE_MARKET_ACTOR_ADDR).unwrap();
328328

329329
NetworkStats {
330330
total_raw_byte_power: power_state.total_raw_byte_power,
@@ -354,9 +354,9 @@ where
354354
self.store.put_cbor(obj, Code::Blake2b256).unwrap()
355355
}
356356

357-
pub fn get_actor(&self, addr: Address) -> Option<Actor> {
357+
pub fn get_actor(&self, addr: &Address) -> Option<Actor> {
358358
// check for inclusion in cache of changed actors
359-
if let Some(act) = self.actors_cache.borrow().get(&addr) {
359+
if let Some(act) = self.actors_cache.borrow().get(addr) {
360360
return Some(act.clone());
361361
}
362362
// go to persisted map
@@ -365,14 +365,14 @@ where
365365
.unwrap();
366366
let actor = actors.get(&addr.to_bytes()).unwrap().cloned();
367367
actor.iter().for_each(|a| {
368-
self.actors_cache.borrow_mut().insert(addr, a.clone());
368+
self.actors_cache.borrow_mut().insert(*addr, a.clone());
369369
});
370370
actor
371371
}
372372

373373
// blindly overwrite the actor at this address whether it previously existed or not
374-
pub fn set_actor(&self, key: Address, a: Actor) {
375-
self.actors_cache.borrow_mut().insert(key, a);
374+
pub fn set_actor(&self, key: &Address, a: Actor) {
375+
self.actors_cache.borrow_mut().insert(*key, a);
376376
self.actors_dirty.replace(true);
377377
}
378378

@@ -397,11 +397,11 @@ where
397397
}
398398

399399
pub fn normalize_address(&self, addr: &Address) -> Option<Address> {
400-
let st = self.get_state::<InitState>(INIT_ACTOR_ADDR).unwrap();
400+
let st = self.get_state::<InitState>(&INIT_ACTOR_ADDR).unwrap();
401401
st.resolve_address::<BS>(self.store, addr).unwrap()
402402
}
403403

404-
pub fn get_state<T: DeserializeOwned>(&self, addr: Address) -> Option<T> {
404+
pub fn get_state<T: DeserializeOwned>(&self, addr: &Address) -> Option<T> {
405405
let a_opt = self.get_actor(addr);
406406
if a_opt == None {
407407
return None;
@@ -410,7 +410,7 @@ where
410410
self.store.get_cbor::<T>(&a.head).unwrap()
411411
}
412412

413-
pub fn mutate_state<S, F>(&self, addr: Address, f: F)
413+
pub fn mutate_state<S, F>(&self, addr: &Address, f: F)
414414
where
415415
S: Serialize + DeserializeOwned,
416416
F: FnOnce(&mut S),
@@ -428,13 +428,13 @@ where
428428

429429
pub fn apply_message<S: serde::Serialize>(
430430
&self,
431-
from: Address,
432-
to: Address,
433-
value: TokenAmount,
431+
from: &Address,
432+
to: &Address,
433+
value: &TokenAmount,
434434
method: MethodNum,
435435
params: Option<S>,
436436
) -> Result<MessageResult, TestVMError> {
437-
let from_id = self.normalize_address(&from).unwrap();
437+
let from_id = &self.normalize_address(from).unwrap();
438438
let mut a = self.get_actor(from_id).unwrap();
439439
let call_seq = a.call_seq_num;
440440
a.call_seq_num = call_seq + 1;
@@ -449,15 +449,15 @@ where
449449
// big.Mul(big.NewInt(1e9), big.NewInt(1e18))
450450
// make top level context with internal context
451451
let top = TopCtx {
452-
originator_stable_addr: from,
452+
originator_stable_addr: *from,
453453
originator_call_seq: call_seq,
454454
new_actor_addr_count: RefCell::new(0),
455455
circ_supply: TokenAmount::from_whole(1_000_000_000),
456456
};
457457
let msg = InternalMessage {
458-
from: from_id,
459-
to,
460-
value,
458+
from: *from_id,
459+
to: *to,
460+
value: value.clone(),
461461
method,
462462
params: params.map(|p| IpldBlock::serialize_cbor(&p).unwrap().unwrap()),
463463
};
@@ -623,7 +623,7 @@ where
623623
{
624624
fn resolve_target(&'invocation self, target: &Address) -> Result<(Actor, Address), ActorError> {
625625
if let Some(a) = self.v.normalize_address(target) {
626-
if let Some(act) = self.v.get_actor(a) {
626+
if let Some(act) = self.v.get_actor(&a) {
627627
return Ok((act, a));
628628
}
629629
};
@@ -634,7 +634,7 @@ where
634634
Payload::Delegated(da)
635635
// Validate that there's an actor at the target ID (we don't care what is there,
636636
// just that something is there).
637-
if self.v.get_actor(Address::new_id(da.namespace())).is_some() =>
637+
if self.v.get_actor(&Address::new_id(da.namespace())).is_some() =>
638638
{
639639
false
640640
}
@@ -654,13 +654,13 @@ where
654654
));
655655
}
656656

657-
let mut st = self.v.get_state::<InitState>(INIT_ACTOR_ADDR).unwrap();
657+
let mut st = self.v.get_state::<InitState>(&INIT_ACTOR_ADDR).unwrap();
658658
let (target_id, existing) = st.map_addresses_to_id(self.v.store, target, None).unwrap();
659659
assert!(!existing, "should never have existing actor when no f4 address is specified");
660660
let target_id_addr = Address::new_id(target_id);
661-
let mut init_actor = self.v.get_actor(INIT_ACTOR_ADDR).unwrap();
661+
let mut init_actor = self.v.get_actor(&INIT_ACTOR_ADDR).unwrap();
662662
init_actor.head = self.v.store.put_cbor(&st, Code::Blake2b256).unwrap();
663-
self.v.set_actor(INIT_ACTOR_ADDR, init_actor);
663+
self.v.set_actor(&INIT_ACTOR_ADDR, init_actor);
664664

665665
let new_actor_msg = InternalMessage {
666666
from: SYSTEM_ACTOR_ADDR,
@@ -693,7 +693,7 @@ where
693693
}
694694
}
695695

696-
Ok((self.v.get_actor(target_id_addr).unwrap(), target_id_addr))
696+
Ok((self.v.get_actor(&target_id_addr).unwrap(), target_id_addr))
697697
}
698698

699699
fn gather_trace(
@@ -720,7 +720,7 @@ where
720720
let prior_root = self.v.checkpoint();
721721

722722
// Transfer funds
723-
let mut from_actor = self.v.get_actor(self.msg.from).unwrap();
723+
let mut from_actor = self.v.get_actor(&self.msg.from).unwrap();
724724
if !self.msg.value.is_zero() {
725725
if self.msg.value.is_negative() {
726726
return Err(ActorError::unchecked(
@@ -744,9 +744,9 @@ where
744744

745745
// Load, deduct, store from actor before loading to actor to handle self-send case
746746
from_actor.balance -= &self.msg.value;
747-
self.v.set_actor(self.msg.from, from_actor);
747+
self.v.set_actor(&self.msg.from, from_actor);
748748

749-
let (mut to_actor, to_addr) = self.resolve_target(&self.msg.to)?;
749+
let (mut to_actor, ref to_addr) = self.resolve_target(&self.msg.to)?;
750750
to_actor.balance = to_actor.balance.add(&self.msg.value);
751751
self.v.set_actor(to_addr, to_actor);
752752

@@ -811,7 +811,7 @@ where
811811
));
812812
}
813813
}
814-
let addr = Address::new_id(actor_id);
814+
let addr = &Address::new_id(actor_id);
815815
let actor = match self.v.get_actor(addr) {
816816
Some(mut act) if act.code == *PLACEHOLDER_ACTOR_CODE_ID => {
817817
act.code = code_id;
@@ -940,7 +940,7 @@ where
940940
));
941941
}
942942
self.caller_validated.replace(true);
943-
let to_match = ACTOR_TYPES.get(&self.v.get_actor(self.msg.from).unwrap().code).unwrap();
943+
let to_match = ACTOR_TYPES.get(&self.v.get_actor(&self.msg.from).unwrap().code).unwrap();
944944
if types.into_iter().any(|t| *t == *to_match) {
945945
return Ok(());
946946
}
@@ -951,7 +951,7 @@ where
951951
}
952952

953953
fn current_balance(&self) -> TokenAmount {
954-
self.v.get_actor(self.to()).unwrap().balance
954+
self.v.get_actor(&self.to()).unwrap().balance
955955
}
956956

957957
fn resolve_address(&self, addr: &Address) -> Option<ActorID> {
@@ -964,15 +964,15 @@ where
964964
}
965965

966966
fn get_actor_code_cid(&self, id: &ActorID) -> Option<Cid> {
967-
let maybe_act = self.v.get_actor(Address::new_id(*id));
967+
let maybe_act = self.v.get_actor(&Address::new_id(*id));
968968
match maybe_act {
969969
None => None,
970970
Some(act) => Some(act.code),
971971
}
972972
}
973973

974974
fn lookup_delegated_address(&self, id: ActorID) -> Option<Address> {
975-
self.v.get_actor(Address::new_id(id)).and_then(|act| act.predictable_address)
975+
self.v.get_actor(&Address::new_id(id)).and_then(|act| act.predictable_address)
976976
}
977977

978978
fn send(
@@ -1036,19 +1036,19 @@ where
10361036
}
10371037

10381038
fn get_state_root(&self) -> Result<Cid, ActorError> {
1039-
Ok(self.v.get_actor(self.to()).unwrap().head)
1039+
Ok(self.v.get_actor(&self.to()).unwrap().head)
10401040
}
10411041

10421042
fn set_state_root(&self, root: &Cid) -> Result<(), ActorError> {
1043-
let maybe_act = self.v.get_actor(self.to());
1043+
let maybe_act = self.v.get_actor(&self.to());
10441044
match maybe_act {
10451045
None => Err(ActorError::unchecked(
10461046
ExitCode::SYS_ASSERTION_FAILED,
10471047
"actor does not exist".to_string(),
10481048
)),
10491049
Some(mut act) if !self.read_only() => {
10501050
act.head = *root;
1051-
self.v.set_actor(self.to(), act);
1051+
self.v.set_actor(&self.to(), act);
10521052
Ok(())
10531053
}
10541054
_ => Err(ActorError::unchecked(
@@ -1068,7 +1068,7 @@ where
10681068
let result = f(&mut st, self);
10691069
self.allow_side_effects.replace(true);
10701070
let ret = result?;
1071-
let mut act = self.v.get_actor(self.to()).unwrap();
1071+
let mut act = self.v.get_actor(&self.to()).unwrap();
10721072
act.head = self.v.store.put_cbor(&st, Code::Blake2b256).unwrap();
10731073

10741074
if self.read_only {
@@ -1078,7 +1078,7 @@ where
10781078
));
10791079
}
10801080

1081-
self.v.set_actor(self.to(), act);
1081+
self.v.set_actor(&self.to(), act);
10821082
Ok(ret)
10831083
}
10841084

@@ -1112,7 +1112,7 @@ where
11121112
}
11131113

11141114
fn actor_balance(&self, id: ActorID) -> Option<TokenAmount> {
1115-
self.v.get_actor(Address::new_id(id)).map(|act| act.balance)
1115+
self.v.get_actor(&Address::new_id(id)).map(|act| act.balance)
11161116
}
11171117

11181118
fn gas_available(&self) -> u64 {

0 commit comments

Comments
 (0)