Skip to content

Commit b2721f8

Browse files
Test VM should use Rc<Blockstore> for consistency with the MockRuntime (#1454)
* test vm should use Rc<Blockstore> * use builder for RC<blockstore> * review changes
1 parent 09d50ad commit b2721f8

23 files changed

+126
-121
lines changed

test_vm/src/lib.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use fvm_shared::{MethodNum, METHOD_SEND};
3838
use serde::ser;
3939
use std::cell::{RefCell, RefMut};
4040
use std::collections::{BTreeMap, HashMap};
41+
use std::rc::Rc;
4142
use vm_api::trace::InvocationTrace;
4243
use vm_api::{new_actor, ActorState, MessageResult, VMError, VM};
4344

@@ -50,9 +51,9 @@ mod messaging;
5051
pub use messaging::*;
5152

5253
/// An in-memory rust-execution VM for testing builtin-actors that yields sensible stack traces and debug info
53-
pub struct TestVM<'bs> {
54+
pub struct TestVM {
5455
pub primitives: FakePrimitives,
55-
pub store: &'bs MemoryBlockstore,
56+
pub store: Rc<MemoryBlockstore>,
5657
pub state_root: RefCell<Cid>,
5758
circulating_supply: RefCell<TokenAmount>,
5859
actors_dirty: RefCell<bool>,
@@ -62,13 +63,15 @@ pub struct TestVM<'bs> {
6263
invocations: RefCell<Vec<InvocationTrace>>,
6364
}
6465

65-
impl<'bs> TestVM<'bs> {
66-
pub fn new(store: &'bs MemoryBlockstore) -> TestVM<'bs> {
66+
impl TestVM {
67+
pub fn new(store: impl Into<Rc<MemoryBlockstore>>) -> TestVM {
68+
let store = store.into();
6769
let mut actors =
68-
Hamt::<&'bs MemoryBlockstore, ActorState, BytesKey, Sha256>::new_with_config(
69-
store,
70+
Hamt::<Rc<MemoryBlockstore>, ActorState, BytesKey, Sha256>::new_with_config(
71+
Rc::clone(&store),
7072
DEFAULT_HAMT_CONFIG,
7173
);
74+
7275
TestVM {
7376
primitives: FakePrimitives {},
7477
store,
@@ -82,15 +85,17 @@ impl<'bs> TestVM<'bs> {
8285
}
8386
}
8487

85-
pub fn new_with_singletons(store: &'bs MemoryBlockstore) -> TestVM<'bs> {
88+
pub fn new_with_singletons(store: impl Into<Rc<MemoryBlockstore>>) -> TestVM {
8689
let reward_total = TokenAmount::from_whole(1_100_000_000i64);
8790
let faucet_total = TokenAmount::from_whole(1_000_000_000i64);
8891

89-
let v = TestVM::<'_>::new(store);
92+
let store = store.into();
93+
94+
let v = TestVM::new(Rc::clone(&store));
9095
v.set_circulating_supply(&reward_total + &faucet_total);
9196

9297
// system
93-
let sys_st = SystemState::new(store).unwrap();
98+
let sys_st = SystemState::new(&store).unwrap();
9499
let sys_head = v.put_store(&sys_st);
95100
let sys_value = faucet_total.clone(); // delegate faucet funds to system so we can construct faucet by sending to bls addr
96101
v.set_actor(
@@ -99,7 +104,7 @@ impl<'bs> TestVM<'bs> {
99104
);
100105

101106
// init
102-
let init_st = InitState::new(store, "integration-test".to_string()).unwrap();
107+
let init_st = InitState::new(&store, "integration-test".to_string()).unwrap();
103108
let init_head = v.put_store(&init_st);
104109
v.set_actor(
105110
&INIT_ACTOR_ADDR,
@@ -239,9 +244,9 @@ impl<'bs> TestVM<'bs> {
239244
pub fn checkpoint(&self) -> Cid {
240245
// persist cache on top of latest checkpoint and clear
241246
let mut actors =
242-
Hamt::<&'bs MemoryBlockstore, ActorState, BytesKey, Sha256>::load_with_config(
247+
Hamt::<Rc<MemoryBlockstore>, ActorState, BytesKey, Sha256>::load_with_config(
243248
&self.state_root.borrow(),
244-
self.store,
249+
Rc::clone(&self.store),
245250
DEFAULT_HAMT_CONFIG,
246251
)
247252
.unwrap();
@@ -275,9 +280,9 @@ impl<'bs> TestVM<'bs> {
275280
}
276281
}
277282

278-
impl<'bs> VM for TestVM<'bs> {
283+
impl VM for TestVM {
279284
fn blockstore(&self) -> &dyn Blockstore {
280-
self.store
285+
self.store.as_ref()
281286
}
282287

283288
fn epoch(&self) -> ChainEpoch {
@@ -364,7 +369,7 @@ impl<'bs> VM for TestVM<'bs> {
364369
}
365370
fn resolve_id_address(&self, address: &Address) -> Option<Address> {
366371
let st: InitState = get_state(self, &INIT_ACTOR_ADDR).unwrap();
367-
st.resolve_address(self.store, address).unwrap()
372+
st.resolve_address(&self.store, address).unwrap()
368373
}
369374

370375
fn set_epoch(&self, epoch: ChainEpoch) {
@@ -386,9 +391,9 @@ impl<'bs> VM for TestVM<'bs> {
386391
return Some(act.clone());
387392
}
388393
// go to persisted map
389-
let actors = Hamt::<&'bs MemoryBlockstore, ActorState, BytesKey, Sha256>::load_with_config(
394+
let actors = Hamt::<Rc<MemoryBlockstore>, ActorState, BytesKey, Sha256>::load_with_config(
390395
&self.state_root.borrow(),
391-
self.store,
396+
Rc::clone(&self.store),
392397
DEFAULT_HAMT_CONFIG,
393398
)
394399
.unwrap();

test_vm/src/messaging.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ use vm_api::{new_actor, ActorState, VM};
6262

6363
use fil_actors_runtime::test_blockstores::MemoryBlockstore;
6464
use std::ops::Add;
65+
use std::rc::Rc;
6566

6667
use crate::{TestVM, TEST_VM_INVALID_POST, TEST_VM_RAND_ARRAY};
6768

@@ -82,7 +83,7 @@ pub struct InternalMessage {
8283
pub params: Option<IpldBlock>,
8384
}
8485

85-
impl MessageInfo for InvocationCtx<'_, '_> {
86+
impl MessageInfo for InvocationCtx<'_> {
8687
fn nonce(&self) -> u64 {
8788
self.top.originator_call_seq
8889
}
@@ -103,8 +104,8 @@ impl MessageInfo for InvocationCtx<'_, '_> {
103104
}
104105
}
105106

106-
pub struct InvocationCtx<'invocation, 'bs> {
107-
pub v: &'invocation TestVM<'bs>,
107+
pub struct InvocationCtx<'invocation> {
108+
pub v: &'invocation TestVM,
108109
pub top: TopCtx,
109110
pub msg: InternalMessage,
110111
pub allow_side_effects: RefCell<bool>,
@@ -114,7 +115,7 @@ pub struct InvocationCtx<'invocation, 'bs> {
114115
pub subinvocations: RefCell<Vec<InvocationTrace>>,
115116
}
116117

117-
impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
118+
impl<'invocation> InvocationCtx<'invocation> {
118119
fn resolve_target(
119120
&'invocation self,
120121
target: &Address,
@@ -152,7 +153,7 @@ impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
152153
}
153154

154155
let mut st: InitState = get_state(self.v, &INIT_ACTOR_ADDR).unwrap();
155-
let (target_id, existing) = st.map_addresses_to_id(self.v.store, target, None).unwrap();
156+
let (target_id, existing) = st.map_addresses_to_id(&self.v.store, target, None).unwrap();
156157
assert!(!existing, "should never have existing actor when no f4 address is specified");
157158
let target_id_addr = Address::new_id(target_id);
158159
let mut init_actor = self.v.actor(&INIT_ACTOR_ADDR).unwrap();
@@ -298,8 +299,8 @@ impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
298299
}
299300
}
300301

301-
impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
302-
type Blockstore = &'bs MemoryBlockstore;
302+
impl<'invocation> Runtime for InvocationCtx<'invocation> {
303+
type Blockstore = Rc<MemoryBlockstore>;
303304

304305
fn create_actor(
305306
&self,
@@ -341,7 +342,7 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
341342
Ok(())
342343
}
343344

344-
fn store(&self) -> &&'bs MemoryBlockstore {
345+
fn store(&self) -> &Rc<MemoryBlockstore> {
345346
&self.v.store
346347
}
347348

@@ -645,7 +646,7 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
645646
}
646647
}
647648

648-
impl Primitives for InvocationCtx<'_, '_> {
649+
impl Primitives for InvocationCtx<'_> {
649650
fn verify_signature(
650651
&self,
651652
signature: &Signature,
@@ -684,7 +685,7 @@ impl Primitives for InvocationCtx<'_, '_> {
684685
}
685686
}
686687

687-
impl Verifier for InvocationCtx<'_, '_> {
688+
impl Verifier for InvocationCtx<'_> {
688689
fn verify_post(&self, verify_info: &WindowPoStVerifyInfo) -> Result<(), anyhow::Error> {
689690
for proof in &verify_info.proofs {
690691
if proof.proof_bytes.eq(&TEST_VM_INVALID_POST.as_bytes().to_vec()) {
@@ -720,7 +721,7 @@ impl Verifier for InvocationCtx<'_, '_> {
720721
}
721722
}
722723

723-
impl RuntimePolicy for InvocationCtx<'_, '_> {
724+
impl RuntimePolicy for InvocationCtx<'_> {
724725
fn policy(&self) -> &Policy {
725726
self.policy
726727
}

test_vm/tests/suite/authenticate_message_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ use test_vm::TestVM;
55
#[test]
66
fn account_authenticate_message() {
77
let store = MemoryBlockstore::new();
8-
let v = TestVM::new_with_singletons(&store);
8+
let v = TestVM::new_with_singletons(store);
99
account_authenticate_message_test(&v);
1010
}

test_vm/tests/suite/batch_onboarding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use test_vm::TestVM;
88
#[test_case(true; "v2")]
99
fn batch_onboarding(v2: bool) {
1010
let store = MemoryBlockstore::new();
11-
let v = TestVM::new_with_singletons(&store);
11+
let v = TestVM::new_with_singletons(store);
1212

1313
batch_onboarding_test(&v, v2);
1414
}

test_vm/tests/suite/batch_onboarding_deals_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ use test_vm::TestVM;
55
#[test]
66
fn batch_onboarding_deals() {
77
let store = MemoryBlockstore::new();
8-
let v = TestVM::new_with_singletons(&store);
8+
let v = TestVM::new_with_singletons(store);
99
batch_onboarding_deals_test(&v);
1010
}

test_vm/tests/suite/change_beneficiary_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ use test_vm::TestVM;
88
#[test]
99
fn change_beneficiary_success() {
1010
let store = MemoryBlockstore::new();
11-
let v = TestVM::new_with_singletons(&store);
11+
let v = TestVM::new_with_singletons(store);
1212
change_beneficiary_success_test(&v);
1313
}
1414

1515
#[test]
1616
fn change_beneficiary_back_owner_success() {
1717
let store = MemoryBlockstore::new();
18-
let v = TestVM::new_with_singletons(&store);
18+
let v = TestVM::new_with_singletons(store);
1919
change_beneficiary_back_owner_success_test(&v);
2020
}
2121

2222
#[test]
2323
fn change_beneficiary_fail() {
2424
let store = MemoryBlockstore::new();
25-
let v = TestVM::new_with_singletons(&store);
25+
let v = TestVM::new_with_singletons(store);
2626
change_beneficiary_fail_test(&v);
2727
}

test_vm/tests/suite/change_owner_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ use test_vm::TestVM;
77
#[test]
88
fn change_owner_success() {
99
let store = MemoryBlockstore::new();
10-
let v = TestVM::new_with_singletons(&store);
10+
let v = TestVM::new_with_singletons(store);
1111
change_owner_success_test(&v);
1212
}
1313

1414
#[test]
1515
fn keep_beneficiary_when_owner_changed() {
1616
let store = MemoryBlockstore::new();
17-
let v = TestVM::new_with_singletons(&store);
17+
let v = TestVM::new_with_singletons(store);
1818
keep_beneficiary_when_owner_changed_test(&v);
1919
}
2020

2121
#[test]
2222
fn change_owner_fail() {
2323
let store = MemoryBlockstore::new();
24-
let v = TestVM::new_with_singletons(&store);
24+
let v = TestVM::new_with_singletons(store);
2525
change_owner_fail_test(&v);
2626
}

test_vm/tests/suite/commit_post_test.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,58 @@ use fil_actors_integration_tests::tests::{
66
};
77
use fil_actors_runtime::test_blockstores::MemoryBlockstore;
88
use test_vm::TestVM;
9-
109
#[test]
1110
fn submit_post_succeeds() {
1211
let store = MemoryBlockstore::new();
13-
let v = TestVM::new_with_singletons(&store);
12+
let v = TestVM::new_with_singletons(store);
1413
submit_post_succeeds_test(&v);
1514
}
1615

1716
#[test]
1817
fn skip_sector() {
1918
let store = MemoryBlockstore::new();
20-
let v = TestVM::new_with_singletons(&store);
19+
let v = TestVM::new_with_singletons(store);
2120
skip_sector_test(&v);
2221
}
2322

2423
#[test]
2524
fn missed_first_post_deadline() {
2625
let store = MemoryBlockstore::new();
27-
let v = TestVM::new_with_singletons(&store);
26+
let v = TestVM::new_with_singletons(store);
2827
missed_first_post_deadline_test(&v);
2928
}
3029

3130
#[test]
3231
fn overdue_precommit() {
3332
let store = MemoryBlockstore::new();
34-
let v = TestVM::new_with_singletons(&store);
33+
let v = TestVM::new_with_singletons(store);
3534
overdue_precommit_test(&v);
3635
}
3736

3837
#[test]
3938
fn aggregate_bad_sector_number() {
4039
let store = MemoryBlockstore::new();
41-
let v = TestVM::new_with_singletons(&store);
40+
let v = TestVM::new_with_singletons(store);
4241
aggregate_bad_sector_number_test(&v);
4342
}
4443

4544
#[test]
4645
fn aggregate_size_limits() {
4746
let store = MemoryBlockstore::new();
48-
let v = TestVM::new_with_singletons(&store);
47+
let v = TestVM::new_with_singletons(store);
4948
aggregate_size_limits_test(&v);
5049
}
5150

5251
#[test]
5352
fn aggregate_bad_sender() {
5453
let store = MemoryBlockstore::new();
55-
let v = TestVM::new_with_singletons(&store);
54+
let v = TestVM::new_with_singletons(store);
5655
aggregate_bad_sender_test(&v);
5756
}
5857

5958
#[test]
6059
fn aggregate_one_precommit_expires() {
6160
let store = MemoryBlockstore::new();
62-
let v = TestVM::new_with_singletons(&store);
61+
let v = TestVM::new_with_singletons(store);
6362
aggregate_one_precommit_expires_test(&v);
6463
}

test_vm/tests/suite/datacap_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use test_vm::TestVM;
66
#[test]
77
fn datacap_transfer() {
88
let store = MemoryBlockstore::new();
9-
let v = TestVM::new_with_singletons(&store);
9+
let v = TestVM::new_with_singletons(store);
1010
datacap_transfer_test(&v);
1111
}
1212

1313
/* Call name & symbol */
1414
#[test]
1515
fn call_name_symbol() {
1616
let store = MemoryBlockstore::new();
17-
let v = TestVM::new_with_singletons(&store);
17+
let v = TestVM::new_with_singletons(store);
1818
call_name_symbol_test(&v);
1919
}

0 commit comments

Comments
 (0)