Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: make the mock runtime generic over the blockstore #802

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions runtime/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::rc::Rc;
use anyhow::anyhow;
use cid::multihash::{Code, Multihash as OtherMultihash};
use cid::Cid;
use fvm_ipld_blockstore::MemoryBlockstore;
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
use fvm_ipld_encoding::de::DeserializeOwned;
use fvm_ipld_encoding::{Cbor, CborStore, RawBytes};
use fvm_shared::address::Payload;
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn make_builtin(bz: &[u8]) -> Cid {
Cid::new_v1(IPLD_RAW, OtherMultihash::wrap(0, bz).expect("name too long"))
}

pub struct MockRuntime {
pub struct MockRuntime<BS = MemoryBlockstore> {
pub epoch: ChainEpoch,
pub miner: Address,
pub base_fee: TokenAmount,
Expand All @@ -124,7 +124,7 @@ pub struct MockRuntime {

// VM Impl
pub in_call: bool,
pub store: Rc<MemoryBlockstore>,
pub store: Rc<BS>,
pub in_transaction: bool,

// Expectations
Expand Down Expand Up @@ -249,6 +249,12 @@ impl Expectations {

impl Default for MockRuntime {
fn default() -> Self {
Self::new(Default::default())
}
}

impl<BS> MockRuntime<BS> {
pub fn new(store: BS) -> Self {
Self {
epoch: Default::default(),
miner: Address::new_id(0),
Expand All @@ -265,7 +271,7 @@ impl Default for MockRuntime {
state: Default::default(),
balance: Default::default(),
in_call: Default::default(),
store: Default::default(),
store: Rc::new(store),
in_transaction: Default::default(),
expectations: Default::default(),
policy: Default::default(),
Expand Down Expand Up @@ -391,7 +397,7 @@ pub fn expect_abort<T: fmt::Debug>(exit_code: ExitCode, res: Result<T, ActorErro
expect_abort_contains_message(exit_code, "", res);
}

impl MockRuntime {
impl<BS: Blockstore> MockRuntime<BS> {
///// Runtime access for tests /////

pub fn get_state<T: Cbor>(&self) -> T {
Expand Down Expand Up @@ -655,7 +661,7 @@ impl MockRuntime {
}
}

impl MessageInfo for MockRuntime {
impl<BS> MessageInfo for MockRuntime<BS> {
fn caller(&self) -> Address {
self.caller
}
Expand All @@ -667,7 +673,7 @@ impl MessageInfo for MockRuntime {
}
}

impl Runtime<Rc<MemoryBlockstore>> for MockRuntime {
impl<BS: Blockstore> Runtime<Rc<BS>> for MockRuntime<BS> {
fn network_version(&self) -> NetworkVersion {
self.network_version
}
Expand Down Expand Up @@ -880,7 +886,7 @@ impl Runtime<Rc<MemoryBlockstore>> for MockRuntime {
ret
}

fn store(&self) -> &Rc<MemoryBlockstore> {
fn store(&self) -> &Rc<BS> {
&self.store
}

Expand Down Expand Up @@ -1010,7 +1016,7 @@ impl Runtime<Rc<MemoryBlockstore>> for MockRuntime {
}
}

impl Primitives for MockRuntime {
impl<BS> Primitives for MockRuntime<BS> {
fn verify_signature(
&self,
signature: &Signature,
Expand Down Expand Up @@ -1085,7 +1091,7 @@ impl Primitives for MockRuntime {
}
}

impl Verifier for MockRuntime {
impl<BS> Verifier for MockRuntime<BS> {
fn verify_seal(&self, seal: &SealVerifyInfo) -> anyhow::Result<()> {
let exp = self
.expectations
Expand Down Expand Up @@ -1213,7 +1219,7 @@ impl Verifier for MockRuntime {
}
}

impl RuntimePolicy for MockRuntime {
impl<BS> RuntimePolicy for MockRuntime<BS> {
fn policy(&self) -> &Policy {
&self.policy
}
Expand Down