Skip to content

Commit fa22fb5

Browse files
authored
test: make the mock runtime generic over the blockstore (#802)
This can be useful for tests that need to track read/write stats. Specifically, we use this in the EVM tests. (ported from next, originally by @aakoshh)
1 parent cc2c9d1 commit fa22fb5

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

runtime/src/test_utils.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::rc::Rc;
99
use anyhow::anyhow;
1010
use cid::multihash::{Code, Multihash as OtherMultihash};
1111
use cid::Cid;
12-
use fvm_ipld_blockstore::MemoryBlockstore;
12+
use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore};
1313
use fvm_ipld_encoding::de::DeserializeOwned;
1414
use fvm_ipld_encoding::{Cbor, CborStore, RawBytes};
1515
use fvm_shared::address::Payload;
@@ -103,7 +103,7 @@ pub fn make_builtin(bz: &[u8]) -> Cid {
103103
Cid::new_v1(IPLD_RAW, OtherMultihash::wrap(0, bz).expect("name too long"))
104104
}
105105

106-
pub struct MockRuntime {
106+
pub struct MockRuntime<BS = MemoryBlockstore> {
107107
pub epoch: ChainEpoch,
108108
pub miner: Address,
109109
pub base_fee: TokenAmount,
@@ -124,7 +124,7 @@ pub struct MockRuntime {
124124

125125
// VM Impl
126126
pub in_call: bool,
127-
pub store: Rc<MemoryBlockstore>,
127+
pub store: Rc<BS>,
128128
pub in_transaction: bool,
129129

130130
// Expectations
@@ -249,6 +249,12 @@ impl Expectations {
249249

250250
impl Default for MockRuntime {
251251
fn default() -> Self {
252+
Self::new(Default::default())
253+
}
254+
}
255+
256+
impl<BS> MockRuntime<BS> {
257+
pub fn new(store: BS) -> Self {
252258
Self {
253259
epoch: Default::default(),
254260
miner: Address::new_id(0),
@@ -265,7 +271,7 @@ impl Default for MockRuntime {
265271
state: Default::default(),
266272
balance: Default::default(),
267273
in_call: Default::default(),
268-
store: Default::default(),
274+
store: Rc::new(store),
269275
in_transaction: Default::default(),
270276
expectations: Default::default(),
271277
policy: Default::default(),
@@ -391,7 +397,7 @@ pub fn expect_abort<T: fmt::Debug>(exit_code: ExitCode, res: Result<T, ActorErro
391397
expect_abort_contains_message(exit_code, "", res);
392398
}
393399

394-
impl MockRuntime {
400+
impl<BS: Blockstore> MockRuntime<BS> {
395401
///// Runtime access for tests /////
396402

397403
pub fn get_state<T: Cbor>(&self) -> T {
@@ -655,7 +661,7 @@ impl MockRuntime {
655661
}
656662
}
657663

658-
impl MessageInfo for MockRuntime {
664+
impl<BS> MessageInfo for MockRuntime<BS> {
659665
fn caller(&self) -> Address {
660666
self.caller
661667
}
@@ -667,7 +673,7 @@ impl MessageInfo for MockRuntime {
667673
}
668674
}
669675

670-
impl Runtime<Rc<MemoryBlockstore>> for MockRuntime {
676+
impl<BS: Blockstore> Runtime<Rc<BS>> for MockRuntime<BS> {
671677
fn network_version(&self) -> NetworkVersion {
672678
self.network_version
673679
}
@@ -880,7 +886,7 @@ impl Runtime<Rc<MemoryBlockstore>> for MockRuntime {
880886
ret
881887
}
882888

883-
fn store(&self) -> &Rc<MemoryBlockstore> {
889+
fn store(&self) -> &Rc<BS> {
884890
&self.store
885891
}
886892

@@ -1010,7 +1016,7 @@ impl Runtime<Rc<MemoryBlockstore>> for MockRuntime {
10101016
}
10111017
}
10121018

1013-
impl Primitives for MockRuntime {
1019+
impl<BS> Primitives for MockRuntime<BS> {
10141020
fn verify_signature(
10151021
&self,
10161022
signature: &Signature,
@@ -1085,7 +1091,7 @@ impl Primitives for MockRuntime {
10851091
}
10861092
}
10871093

1088-
impl Verifier for MockRuntime {
1094+
impl<BS> Verifier for MockRuntime<BS> {
10891095
fn verify_seal(&self, seal: &SealVerifyInfo) -> anyhow::Result<()> {
10901096
let exp = self
10911097
.expectations
@@ -1213,7 +1219,7 @@ impl Verifier for MockRuntime {
12131219
}
12141220
}
12151221

1216-
impl RuntimePolicy for MockRuntime {
1222+
impl<BS> RuntimePolicy for MockRuntime<BS> {
12171223
fn policy(&self) -> &Policy {
12181224
&self.policy
12191225
}

0 commit comments

Comments
 (0)