Skip to content

Commit

Permalink
Have VM trait expose a dyn blockstore (#1328)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
alexytsu authored Jul 14, 2023
1 parent f84baa9 commit 8e82ef7
Show file tree
Hide file tree
Showing 23 changed files with 439 additions and 381 deletions.
15 changes: 4 additions & 11 deletions test_vm/src/deals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use fil_actors_runtime::cbor::serialize;
use fil_actors_runtime::runtime::Policy;
use fil_actors_runtime::test_utils::make_piece_cid;
use fil_actors_runtime::STORAGE_MARKET_ACTOR_ADDR;
use fvm_ipld_blockstore::Blockstore;
use fvm_shared::address::Address;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::crypto::signature::{Signature, SignatureType};
Expand Down Expand Up @@ -45,21 +44,15 @@ impl Default for DealOptions {
// A helper for staging and publishing deals.
// Note that this doesn't check trace expectations,
// see https://github.com/filecoin-project/builtin-actors/issues/1302.
pub struct DealBatcher<'vm, BS>
where
BS: Blockstore,
{
v: &'vm dyn VM<BS>,
pub struct DealBatcher<'vm> {
v: &'vm dyn VM,
deals: Vec<DealProposal>,
default_options: DealOptions,
published: bool,
}

impl<'vm, BS> DealBatcher<'vm, BS>
where
BS: Blockstore,
{
pub fn new(v: &'vm dyn VM<BS>, opts: DealOptions) -> Self {
impl<'vm> DealBatcher<'vm> {
pub fn new(v: &'vm dyn VM, opts: DealOptions) -> Self {
DealBatcher { v, deals: vec![], default_options: opts, published: false }
}

Expand Down
14 changes: 7 additions & 7 deletions test_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ pub mod trace;
pub mod util;

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

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

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

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

impl<'bs, BS> VM<BS> for TestVM<'bs, BS>
impl<'bs, BS> VM for TestVM<'bs, BS>
where
BS: Blockstore,
{
fn blockstore(&self) -> Box<&BS> {
Box::new(self.store)
fn blockstore(&self) -> &dyn Blockstore {
self.store
}

fn epoch(&self) -> ChainEpoch {
Expand Down Expand Up @@ -326,7 +326,7 @@ where
self.total_fil.clone()
}

fn primitives(&self) -> &FakePrimitives {
fn primitives(&self) -> &dyn Primitives {
&self.primitives
}
}
Expand Down
22 changes: 22 additions & 0 deletions test_vm/src/util/blockstore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;

/// A DynBlockstore is used to make the blockstore trait object consumable by functions that
/// accept a generic BS: Blockstore parameter rather than a dyn Blockstore
pub struct DynBlockstore<'bs>(&'bs dyn Blockstore);

impl<'bs> Blockstore for DynBlockstore<'bs> {
fn get(&self, k: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
self.0.get(k)
}

fn put_keyed(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
self.0.put_keyed(k, block)
}
}

impl<'bs> DynBlockstore<'bs> {
pub fn wrap(blockstore: &'bs dyn Blockstore) -> Self {
Self(blockstore)
}
}
Loading

0 comments on commit 8e82ef7

Please sign in to comment.