diff --git a/testing/integration/examples/integration.rs b/testing/integration/examples/integration.rs index 485fbe3a4..27b3fa46e 100644 --- a/testing/integration/examples/integration.rs +++ b/testing/integration/examples/integration.rs @@ -1,4 +1,5 @@ use fvm::executor::{ApplyKind, Executor}; +use fvm_integration_tests::dummy::DummyExterns; use fvm_integration_tests::tester::{Account, Tester}; use fvm_ipld_blockstore::MemoryBlockstore; use fvm_ipld_encoding::tuple::*; @@ -50,7 +51,7 @@ pub fn main() { .unwrap(); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Send message let message = Message { diff --git a/testing/integration/src/dummy.rs b/testing/integration/src/dummy.rs index 3e3ba86a2..3d6c50fa3 100644 --- a/testing/integration/src/dummy.rs +++ b/testing/integration/src/dummy.rs @@ -1,5 +1,6 @@ use fvm::externs::{Consensus, Externs, Rand}; - +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; pub struct DummyExterns; impl Externs for DummyExterns {} @@ -11,10 +12,13 @@ impl Rand for DummyExterns { _round: fvm_shared::clock::ChainEpoch, _entropy: &[u8], ) -> anyhow::Result<[u8; 32]> { - let msg = "mel was here".as_bytes(); - let mut out = [0u8; 32]; - out[..msg.len()].copy_from_slice(msg); - Ok(out) + let rng: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(32) + .map(char::from) + .collect(); + + Ok(<[u8; 32]>::try_from(rng.into_bytes()).unwrap()) } fn get_beacon_randomness( @@ -23,7 +27,13 @@ impl Rand for DummyExterns { _round: fvm_shared::clock::ChainEpoch, _entropy: &[u8], ) -> anyhow::Result<[u8; 32]> { - todo!() + let rng: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(32) + .map(char::from) + .collect(); + + Ok(<[u8; 32]>::try_from(rng.into_bytes()).unwrap()) } } @@ -34,6 +44,6 @@ impl Consensus for DummyExterns { _h2: &[u8], _extra: &[u8], ) -> anyhow::Result<(Option, i64)> { - todo!() + Ok((None, 0)) } } diff --git a/testing/integration/src/lib.rs b/testing/integration/src/lib.rs index e2e0aca53..f59b9ebb8 100644 --- a/testing/integration/src/lib.rs +++ b/testing/integration/src/lib.rs @@ -1,4 +1,4 @@ mod builtin; -mod dummy; +pub mod dummy; pub mod error; pub mod tester; diff --git a/testing/integration/src/tester.rs b/testing/integration/src/tester.rs index 8461ceda4..e5e3bdbdd 100644 --- a/testing/integration/src/tester.rs +++ b/testing/integration/src/tester.rs @@ -2,6 +2,7 @@ use anyhow::{anyhow, Context, Result}; use cid::Cid; use fvm::call_manager::DefaultCallManager; use fvm::executor::DefaultExecutor; +use fvm::externs::Externs; use fvm::machine::{DefaultMachine, Engine, Machine, NetworkConfig}; use fvm::state_tree::{ActorState, StateTree}; use fvm::{init_actor, system_actor, DefaultKernel}; @@ -18,20 +19,18 @@ use multihash::Code; use crate::builtin::{ fetch_builtin_code_cid, import_builtin_actors, set_init_actor, set_sys_actor, }; -use crate::dummy; -use crate::dummy::DummyExterns; use crate::error::Error::{FailedToFlushTree, NoManifestInformation, NoRootCid}; const DEFAULT_BASE_FEE: u64 = 100; pub trait Store: Blockstore + Sized + 'static {} -pub type IntegrationExecutor = - DefaultExecutor>>>; +pub type IntegrationExecutor = + DefaultExecutor>>>; pub type Account = (ActorID, Address); -pub struct Tester { +pub struct Tester { // Network version used in the test nv: NetworkVersion, // Builtin actors root Cid used in the Machine @@ -41,14 +40,15 @@ pub struct Tester { // Custom code cid deployed by developer code_cids: Vec, // Executor used to interact with deployed actors. - pub executor: Option>, + pub executor: Option>, // State tree constructed before instantiating the Machine pub state_tree: Option>, } -impl Tester +impl Tester where B: Blockstore, + E: Externs, { pub fn new(nv: NetworkVersion, stv: StateTreeVersion, blockstore: B) -> Result { // Load the builtin actors bundles into the blockstore. @@ -152,7 +152,7 @@ where } /// Sets the Machine and the Executor in our Tester structure. - pub fn instantiate_machine(&mut self) -> Result<()> { + pub fn instantiate_machine(&mut self, externs: E) -> Result<()> { // Take the state tree and leave None behind. let mut state_tree = self.state_tree.take().unwrap(); @@ -176,10 +176,13 @@ where &Engine::new_default((&mc.network.clone()).into())?, &mc, blockstore, - dummy::DummyExterns, + externs, )?; - let executor = DefaultExecutor::>>::new(machine); + let executor = + DefaultExecutor::>>>::new( + machine, + ); executor .engine() .preload(executor.blockstore(), &self.code_cids)?; diff --git a/testing/integration/tests/fil_integer_overflow.rs b/testing/integration/tests/fil_integer_overflow.rs index 15ed40a82..71e8a25e9 100644 --- a/testing/integration/tests/fil_integer_overflow.rs +++ b/testing/integration/tests/fil_integer_overflow.rs @@ -1,4 +1,5 @@ use fvm::executor::{ApplyKind, Executor}; +use fvm_integration_tests::dummy::DummyExterns; use fvm_integration_tests::tester::{Account, Tester}; use fvm_ipld_blockstore::MemoryBlockstore; use fvm_ipld_encoding::tuple::*; @@ -20,7 +21,7 @@ pub struct State { } // Utility function to instantiation integration tester -fn instantiate_tester() -> (Account, Tester, Address) { +fn instantiate_tester() -> (Account, Tester, Address) { // Instantiate tester let mut tester = Tester::new( NetworkVersion::V15, @@ -60,7 +61,7 @@ fn integer_overflow() { let (sender, mut tester, actor_address) = instantiate_tester(); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Params setup let x: i64 = 10000000000; diff --git a/testing/integration/tests/fil_syscall.rs b/testing/integration/tests/fil_syscall.rs index 83fde8a86..be260e934 100644 --- a/testing/integration/tests/fil_syscall.rs +++ b/testing/integration/tests/fil_syscall.rs @@ -1,5 +1,6 @@ use fvm::call_manager::backtrace::Cause; use fvm::executor::{ApplyFailure, ApplyKind, Executor}; +use fvm_integration_tests::dummy::DummyExterns; use fvm_integration_tests::tester::{Account, Tester}; use fvm_ipld_blockstore::MemoryBlockstore; use fvm_ipld_encoding::tuple::*; @@ -36,7 +37,9 @@ pub struct State { } // Utility function to instantiation integration tester -fn instantiate_tester(wasm_bin: &[u8]) -> (Account, Tester, Address) { +fn instantiate_tester( + wasm_bin: &[u8], +) -> (Account, Tester, Address) { // Instantiate tester let mut tester = Tester::new( NetworkVersion::V15, @@ -70,7 +73,7 @@ fn non_existing_syscall() { let (sender, mut tester, actor_address) = instantiate_tester(&wasm_bin); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Params setup let params = RawBytes::new(Vec::::new()); @@ -132,7 +135,7 @@ fn malformed_syscall_parameter() { let (sender, mut tester, actor_address) = instantiate_tester(&wasm_bin); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Params setup let params = RawBytes::new(Vec::::new()); diff --git a/testing/integration/tests/lib.rs b/testing/integration/tests/lib.rs index 26e30379c..0e452c1fe 100644 --- a/testing/integration/tests/lib.rs +++ b/testing/integration/tests/lib.rs @@ -9,6 +9,7 @@ use std::rc::Rc; use anyhow::anyhow; use cid::Cid; use fvm::executor::{ApplyKind, Executor, ThreadedExecutor}; +use fvm_integration_tests::dummy::DummyExterns; use fvm_integration_tests::tester::{Account, IntegrationExecutor, Tester}; use fvm_ipld_blockstore::{Blockstore, MemoryBlockstore}; use fvm_ipld_encoding::tuple::*; @@ -68,7 +69,7 @@ fn hello_world() { .unwrap(); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Send message let message = Message { @@ -120,7 +121,7 @@ fn ipld() { .unwrap(); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Send message let message = Message { @@ -178,26 +179,28 @@ fn native_stack_overflow() { .unwrap(); // Instantiate machine - tester.instantiate_machine().unwrap(); - - let exec_test = |exec: &mut ThreadedExecutor>, method| { - // Send message - let message = Message { - from: sender[0].1, - to: actor_address, - gas_limit: 10_000_000_000, - method_num: method, - sequence: method - 1, - ..Message::default() + tester.instantiate_machine(DummyExterns).unwrap(); + + let exec_test = + |exec: &mut ThreadedExecutor>, + method| { + // Send message + let message = Message { + from: sender[0].1, + to: actor_address, + gas_limit: 10_000_000_000, + method_num: method, + sequence: method - 1, + ..Message::default() + }; + + let res = exec + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + res.msg_receipt.exit_code.value() }; - let res = exec - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - res.msg_receipt.exit_code.value() - }; - let mut executor = ThreadedExecutor(tester.executor.unwrap()); // on method 0 the test actor should run out of stack @@ -242,7 +245,7 @@ fn test_exitcode(wat: &str, code: ExitCode) { .unwrap(); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); // Send message let message = Message { @@ -396,7 +399,7 @@ fn backtraces() { .unwrap(); // Instantiate machine - tester.instantiate_machine().unwrap(); + tester.instantiate_machine(DummyExterns).unwrap(); let executor = tester.executor.as_mut().unwrap();