Skip to content

Commit 424669b

Browse files
authored
replace concrete blockstore in TestVM with a generic trait (#1259)
* replace concrete blockstore in TestVM with a generic trait * fix test expectation
1 parent 1df034d commit 424669b

26 files changed

+274
-197
lines changed

actors/market/src/testing.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{
22
collections::{BTreeMap, BTreeSet},
33
convert::TryFrom,
4-
fmt::Debug,
54
};
65

76
use cid::Cid;
@@ -61,7 +60,7 @@ pub struct StateSummary {
6160
}
6261

6362
/// Checks internal invariants of market state
64-
pub fn check_state_invariants<BS: Blockstore + Debug>(
63+
pub fn check_state_invariants<BS: Blockstore>(
6564
state: &State,
6665
store: &BS,
6766
balance: &TokenAmount,
@@ -229,7 +228,10 @@ pub fn check_state_invariants<BS: Blockstore + Debug>(
229228
let ret = pending_proposals.for_each(|key, _| {
230229
let proposal_cid = Cid::try_from(key.0.to_owned())?;
231230

232-
acc.require(proposal_cids.contains(&proposal_cid), format!("pending proposal with cid {proposal_cid} not found within proposals {pending_proposals:?}"));
231+
acc.require(
232+
proposal_cids.contains(&proposal_cid),
233+
format!("pending proposal with cid {proposal_cid} not found within proposals"),
234+
);
233235

234236
pending_proposal_count += 1;
235237
Ok(())

actors/market/tests/market_actor_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ fn fail_when_deal_is_activated_but_proposal_is_not_found() {
13551355
&rt,
13561356
&[
13571357
Regex::new("no deal proposal for deal state \\d+").unwrap(),
1358-
Regex::new("pending proposal with cid \\w+ not found within proposals .*").unwrap(),
1358+
Regex::new("pending proposal with cid \\w+ not found within proposals").unwrap(),
13591359
Regex::new("deal op found for deal id \\d+ with missing proposal at epoch \\d+")
13601360
.unwrap(),
13611361
],

actors/miner/tests/sectors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn make_sector(i: u64) -> SectorOnChainInfo {
1717
}
1818
}
1919

20-
fn setup_sectors(store: &'_ MemoryBlockstore) -> Sectors<'_, MemoryBlockstore> {
20+
fn setup_sectors(store: &'_ MemoryBlockstore) -> Sectors<MemoryBlockstore> {
2121
sectors_arr_mbs(store, vec![make_sector(0), make_sector(1), make_sector(5)])
2222
}
2323

actors/miner/tests/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,7 +2967,7 @@ pub fn test_sector(
29672967
pub fn sectors_arr_mbs(
29682968
store: &'_ MemoryBlockstore,
29692969
sectors_info: Vec<SectorOnChainInfo>,
2970-
) -> Sectors<'_, MemoryBlockstore> {
2970+
) -> Sectors<MemoryBlockstore> {
29712971
let empty_array =
29722972
Amt::<(), _>::new_with_bit_width(store, SECTORS_AMT_BITWIDTH).flush().unwrap();
29732973
let mut sectors = Sectors::load(store, &empty_array).unwrap();
@@ -3294,7 +3294,7 @@ pub fn select_sectors(sectors: &[SectorOnChainInfo], field: &BitField) -> Vec<Se
32943294
#[allow(dead_code)]
32953295
pub fn require_no_expiration_groups_before(
32963296
epoch: ChainEpoch,
3297-
queue: &mut ExpirationQueue<'_, MemoryBlockstore>,
3297+
queue: &mut ExpirationQueue<MemoryBlockstore>,
32983298
) {
32993299
queue.amt.flush().unwrap();
33003300

state/src/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ macro_rules! get_state {
113113
// to match the Manifest implementation in the FVM.
114114
// It could be replaced with a custom mapping trait (while Rust doesn't support
115115
// abstract collection traits).
116-
pub fn check_state_invariants<'a, BS: Blockstore + Debug>(
116+
pub fn check_state_invariants<'a, BS: Blockstore>(
117117
manifest: &BiBTreeMap<Cid, Type>,
118118
policy: &Policy,
119119
tree: Tree<'a, BS>,

test_vm/src/lib.rs

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use fil_actors_runtime::{
3434
use fil_actors_runtime::{MessageAccumulator, DATACAP_TOKEN_ACTOR_ADDR};
3535
use fil_builtin_actors_state::check::check_state_invariants;
3636
use fil_builtin_actors_state::check::Tree;
37+
use fvm_ipld_blockstore::Blockstore;
3738
use fvm_ipld_blockstore::MemoryBlockstore;
3839
use fvm_ipld_encoding::ipld_block::IpldBlock;
3940
use fvm_ipld_encoding::tuple::*;
@@ -74,8 +75,11 @@ use std::ops::Add;
7475

7576
pub mod util;
7677

77-
pub struct TestVM<'bs> {
78-
pub store: &'bs MemoryBlockstore,
78+
pub struct TestVM<'bs, BS>
79+
where
80+
BS: Blockstore,
81+
{
82+
pub store: &'bs BS,
7983
pub state_root: RefCell<Cid>,
8084
total_fil: TokenAmount,
8185
actors_dirty: RefCell<bool>,
@@ -121,8 +125,11 @@ pub const TEST_FAUCET_ADDR: Address = Address::new_id(FIRST_NON_SINGLETON_ADDR +
121125
pub const FIRST_TEST_USER_ADDR: ActorID = FIRST_NON_SINGLETON_ADDR + 3;
122126

123127
// accounts for verifreg root signer and msig
124-
impl<'bs> TestVM<'bs> {
125-
pub fn new(store: &'bs MemoryBlockstore) -> TestVM<'bs> {
128+
impl<'bs, BS> TestVM<'bs, BS>
129+
where
130+
BS: Blockstore,
131+
{
132+
pub fn new(store: &'bs MemoryBlockstore) -> TestVM<'bs, MemoryBlockstore> {
126133
let mut actors = Hamt::<&'bs MemoryBlockstore, Actor, BytesKey, Sha256>::new(store);
127134
TestVM {
128135
store,
@@ -140,11 +147,12 @@ impl<'bs> TestVM<'bs> {
140147
Self { total_fil, ..self }
141148
}
142149

143-
pub fn new_with_singletons(store: &'bs MemoryBlockstore) -> TestVM<'bs> {
150+
pub fn new_with_singletons(store: &'bs MemoryBlockstore) -> TestVM<'bs, MemoryBlockstore> {
144151
let reward_total = TokenAmount::from_whole(1_100_000_000i64);
145152
let faucet_total = TokenAmount::from_whole(1_000_000_000i64);
146153

147-
let v = TestVM::new(store).with_total_fil(&reward_total + &faucet_total);
154+
let v = TestVM::<'_, MemoryBlockstore>::new(store)
155+
.with_total_fil(&reward_total + &faucet_total);
148156

149157
// system
150158
let sys_st = SystemState::new(store).unwrap();
@@ -283,7 +291,7 @@ impl<'bs> TestVM<'bs> {
283291
v
284292
}
285293

286-
pub fn with_epoch(self, epoch: ChainEpoch) -> TestVM<'bs> {
294+
pub fn with_epoch(self, epoch: ChainEpoch) -> TestVM<'bs, BS> {
287295
self.checkpoint();
288296
TestVM {
289297
store: self.store,
@@ -352,11 +360,9 @@ impl<'bs> TestVM<'bs> {
352360
return Some(act.clone());
353361
}
354362
// go to persisted map
355-
let actors = Hamt::<&'bs MemoryBlockstore, Actor, BytesKey, Sha256>::load(
356-
&self.state_root.borrow(),
357-
self.store,
358-
)
359-
.unwrap();
363+
let actors =
364+
Hamt::<&'bs BS, Actor, BytesKey, Sha256>::load(&self.state_root.borrow(), self.store)
365+
.unwrap();
360366
let actor = actors.get(&addr.to_bytes()).unwrap().cloned();
361367
actor.iter().for_each(|a| {
362368
self.actors_cache.borrow_mut().insert(addr, a.clone());
@@ -372,11 +378,9 @@ impl<'bs> TestVM<'bs> {
372378

373379
pub fn checkpoint(&self) -> Cid {
374380
// persist cache on top of latest checkpoint and clear
375-
let mut actors = Hamt::<&'bs MemoryBlockstore, Actor, BytesKey, Sha256>::load(
376-
&self.state_root.borrow(),
377-
self.store,
378-
)
379-
.unwrap();
381+
let mut actors =
382+
Hamt::<&'bs BS, Actor, BytesKey, Sha256>::load(&self.state_root.borrow(), self.store)
383+
.unwrap();
380384
for (addr, act) in self.actors_cache.borrow().iter() {
381385
actors.set(addr.to_bytes().into(), act.clone()).unwrap();
382386
}
@@ -394,7 +398,7 @@ impl<'bs> TestVM<'bs> {
394398

395399
pub fn normalize_address(&self, addr: &Address) -> Option<Address> {
396400
let st = self.get_state::<InitState>(INIT_ACTOR_ADDR).unwrap();
397-
st.resolve_address::<MemoryBlockstore>(self.store, addr).unwrap()
401+
st.resolve_address::<BS>(self.store, addr).unwrap()
398402
}
399403

400404
pub fn get_state<T: DeserializeOwned>(&self, addr: Address) -> Option<T> {
@@ -497,11 +501,9 @@ impl<'bs> TestVM<'bs> {
497501
/// Checks the state invariants and returns broken invariants.
498502
pub fn check_state_invariants(&self) -> anyhow::Result<MessageAccumulator> {
499503
self.checkpoint();
500-
let actors = Hamt::<&'bs MemoryBlockstore, Actor, BytesKey, Sha256>::load(
501-
&self.state_root.borrow(),
502-
self.store,
503-
)
504-
.unwrap();
504+
let actors =
505+
Hamt::<&'bs BS, Actor, BytesKey, Sha256>::load(&self.state_root.borrow(), self.store)
506+
.unwrap();
505507

506508
let mut manifest = BiBTreeMap::new();
507509
actors
@@ -571,7 +573,10 @@ impl InternalMessage {
571573
}
572574
}
573575

574-
impl MessageInfo for InvocationCtx<'_, '_> {
576+
impl<BS> MessageInfo for InvocationCtx<'_, '_, BS>
577+
where
578+
BS: Blockstore,
579+
{
575580
fn nonce(&self) -> u64 {
576581
self.top.originator_call_seq
577582
}
@@ -598,8 +603,11 @@ pub const TEST_VM_RAND_ARRAY: [u8; 32] = [
598603
];
599604
pub const TEST_VM_INVALID_POST: &str = "i_am_invalid_post";
600605

601-
pub struct InvocationCtx<'invocation, 'bs> {
602-
v: &'invocation TestVM<'bs>,
606+
pub struct InvocationCtx<'invocation, 'bs, BS>
607+
where
608+
BS: Blockstore,
609+
{
610+
v: &'invocation TestVM<'bs, BS>,
603611
top: TopCtx,
604612
msg: InternalMessage,
605613
allow_side_effects: RefCell<bool>,
@@ -609,7 +617,10 @@ pub struct InvocationCtx<'invocation, 'bs> {
609617
subinvocations: RefCell<Vec<InvocationTrace>>,
610618
}
611619

612-
impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
620+
impl<'invocation, 'bs, BS> InvocationCtx<'invocation, 'bs, BS>
621+
where
622+
BS: Blockstore,
623+
{
613624
fn resolve_target(&'invocation self, target: &Address) -> Result<(Actor, Address), ActorError> {
614625
if let Some(a) = self.v.normalize_address(target) {
615626
if let Some(act) = self.v.get_actor(a) {
@@ -779,8 +790,11 @@ impl<'invocation, 'bs> InvocationCtx<'invocation, 'bs> {
779790
}
780791
}
781792

782-
impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
783-
type Blockstore = &'bs MemoryBlockstore;
793+
impl<'invocation, 'bs, BS> Runtime for InvocationCtx<'invocation, 'bs, BS>
794+
where
795+
BS: Blockstore,
796+
{
797+
type Blockstore = &'bs BS;
784798

785799
fn create_actor(
786800
&self,
@@ -822,7 +836,7 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
822836
Ok(())
823837
}
824838

825-
fn store(&self) -> &&'bs MemoryBlockstore {
839+
fn store(&self) -> &&'bs BS {
826840
&self.v.store
827841
}
828842

@@ -1123,7 +1137,10 @@ impl<'invocation, 'bs> Runtime for InvocationCtx<'invocation, 'bs> {
11231137
}
11241138
}
11251139

1126-
impl Primitives for TestVM<'_> {
1140+
impl<BS> Primitives for TestVM<'_, BS>
1141+
where
1142+
BS: Blockstore,
1143+
{
11271144
// A "valid" signature has its bytes equal to the plaintext.
11281145
// Anything else is considered invalid.
11291146
fn verify_signature(
@@ -1179,7 +1196,10 @@ impl Primitives for TestVM<'_> {
11791196
}
11801197
}
11811198

1182-
impl Primitives for InvocationCtx<'_, '_> {
1199+
impl<BS> Primitives for InvocationCtx<'_, '_, BS>
1200+
where
1201+
BS: Blockstore,
1202+
{
11831203
fn verify_signature(
11841204
&self,
11851205
signature: &Signature,
@@ -1218,7 +1238,10 @@ impl Primitives for InvocationCtx<'_, '_> {
12181238
}
12191239
}
12201240

1221-
impl Verifier for InvocationCtx<'_, '_> {
1241+
impl<BS> Verifier for InvocationCtx<'_, '_, BS>
1242+
where
1243+
BS: Blockstore,
1244+
{
12221245
fn verify_seal(&self, _vi: &SealVerifyInfo) -> Result<(), anyhow::Error> {
12231246
Ok(())
12241247
}
@@ -1258,7 +1281,10 @@ impl Verifier for InvocationCtx<'_, '_> {
12581281
}
12591282
}
12601283

1261-
impl RuntimePolicy for InvocationCtx<'_, '_> {
1284+
impl<BS> RuntimePolicy for InvocationCtx<'_, '_, BS>
1285+
where
1286+
BS: Blockstore,
1287+
{
12621288
fn policy(&self) -> &Policy {
12631289
self.policy
12641290
}

0 commit comments

Comments
 (0)