Skip to content

Commit 8e82ef7

Browse files
authored
Have VM trait expose a dyn blockstore (#1328)
* wip * refactor VM trait to return a dyn Blockstore * refactor miner_withdrawal_tests to not need BS: Blockstore * VM trait should use abstract primitives (#1329)
1 parent f84baa9 commit 8e82ef7

23 files changed

+439
-381
lines changed

test_vm/src/deals.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use fil_actors_runtime::cbor::serialize;
88
use fil_actors_runtime::runtime::Policy;
99
use fil_actors_runtime::test_utils::make_piece_cid;
1010
use fil_actors_runtime::STORAGE_MARKET_ACTOR_ADDR;
11-
use fvm_ipld_blockstore::Blockstore;
1211
use fvm_shared::address::Address;
1312
use fvm_shared::clock::ChainEpoch;
1413
use fvm_shared::crypto::signature::{Signature, SignatureType};
@@ -45,21 +44,15 @@ impl Default for DealOptions {
4544
// A helper for staging and publishing deals.
4645
// Note that this doesn't check trace expectations,
4746
// see https://github.com/filecoin-project/builtin-actors/issues/1302.
48-
pub struct DealBatcher<'vm, BS>
49-
where
50-
BS: Blockstore,
51-
{
52-
v: &'vm dyn VM<BS>,
47+
pub struct DealBatcher<'vm> {
48+
v: &'vm dyn VM,
5349
deals: Vec<DealProposal>,
5450
default_options: DealOptions,
5551
published: bool,
5652
}
5753

58-
impl<'vm, BS> DealBatcher<'vm, BS>
59-
where
60-
BS: Blockstore,
61-
{
62-
pub fn new(v: &'vm dyn VM<BS>, opts: DealOptions) -> Self {
54+
impl<'vm> DealBatcher<'vm> {
55+
pub fn new(v: &'vm dyn VM, opts: DealOptions) -> Self {
6356
DealBatcher { v, deals: vec![], default_options: opts, published: false }
6457
}
6558

test_vm/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ pub mod trace;
8383
pub mod util;
8484

8585
/// An abstract VM that is injected into integration tests
86-
pub trait VM<BS: Blockstore> {
86+
pub trait VM {
8787
/// Returns the underlying blockstore of the VM
88-
fn blockstore(&self) -> Box<&BS>;
88+
fn blockstore(&self) -> &dyn Blockstore;
8989

9090
/// Get the state root of the specified actor
9191
fn actor_root(&self, address: &Address) -> Option<Cid>;
@@ -122,7 +122,7 @@ pub trait VM<BS: Blockstore> {
122122
fn actor_manifest(&self) -> BiBTreeMap<Cid, Type>;
123123

124124
/// Provides access to VM primitives
125-
fn primitives(&self) -> &FakePrimitives;
125+
fn primitives(&self) -> &dyn Primitives;
126126

127127
/// Get the current runtime policy
128128
fn policy(&self) -> Policy;
@@ -186,12 +186,12 @@ pub const FAUCET_ROOT_KEY: &[u8] = &[153; fvm_shared::address::BLS_PUB_LEN];
186186
pub const TEST_FAUCET_ADDR: Address = Address::new_id(FIRST_NON_SINGLETON_ADDR + 2);
187187
pub const FIRST_TEST_USER_ADDR: ActorID = FIRST_NON_SINGLETON_ADDR + 3;
188188

189-
impl<'bs, BS> VM<BS> for TestVM<'bs, BS>
189+
impl<'bs, BS> VM for TestVM<'bs, BS>
190190
where
191191
BS: Blockstore,
192192
{
193-
fn blockstore(&self) -> Box<&BS> {
194-
Box::new(self.store)
193+
fn blockstore(&self) -> &dyn Blockstore {
194+
self.store
195195
}
196196

197197
fn epoch(&self) -> ChainEpoch {
@@ -326,7 +326,7 @@ where
326326
self.total_fil.clone()
327327
}
328328

329-
fn primitives(&self) -> &FakePrimitives {
329+
fn primitives(&self) -> &dyn Primitives {
330330
&self.primitives
331331
}
332332
}

test_vm/src/util/blockstore.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use cid::Cid;
2+
use fvm_ipld_blockstore::Blockstore;
3+
4+
/// A DynBlockstore is used to make the blockstore trait object consumable by functions that
5+
/// accept a generic BS: Blockstore parameter rather than a dyn Blockstore
6+
pub struct DynBlockstore<'bs>(&'bs dyn Blockstore);
7+
8+
impl<'bs> Blockstore for DynBlockstore<'bs> {
9+
fn get(&self, k: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
10+
self.0.get(k)
11+
}
12+
13+
fn put_keyed(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
14+
self.0.put_keyed(k, block)
15+
}
16+
}
17+
18+
impl<'bs> DynBlockstore<'bs> {
19+
pub fn wrap(blockstore: &'bs dyn Blockstore) -> Self {
20+
Self(blockstore)
21+
}
22+
}

0 commit comments

Comments
 (0)