From 5067b28a3754bd394b58d3eba99dc86b31dca31b Mon Sep 17 00:00:00 2001 From: rllola Date: Wed, 8 Feb 2023 15:11:39 +0100 Subject: [PATCH 1/2] show gas usage; added some utilitary functions; --- Makefile | 20 +- testing/Cargo.lock | 162 +++- testing/Cargo.toml | 1 + testing/src/lib.rs | 3 +- testing/src/setup.rs | 36 + testing/tests/account.rs | 294 ++++--- testing/tests/datacap.rs | 699 ++++++++--------- testing/tests/init.rs | 225 +++--- testing/tests/market.rs | 1429 +++++++++++++++++----------------- testing/tests/miner.rs | 558 +++++++------ testing/tests/multisig.rs | 364 +++++---- testing/tests/power.rs | 520 ++++++------- testing/tests/precompiles.rs | 436 ++++++----- testing/tests/send.rs | 254 +++--- testing/tests/verifreg.rs | 411 +++++----- 15 files changed, 2832 insertions(+), 2580 deletions(-) create mode 100644 testing/src/setup.rs diff --git a/Makefile b/Makefile index 6d654723..603ab684 100644 --- a/Makefile +++ b/Makefile @@ -71,34 +71,34 @@ test_integration: build build_builtin_actors cd testing && cargo test test_miner_integration: build build_builtin_actors - cd testing && cargo test miner_test + cd testing && cargo test miner_test -- --nocapture test_market_integration: build build_builtin_actors - cd testing && cargo test market_test + cd testing && cargo test market_test -- --nocapture test_power_integration: build build_builtin_actors - cd testing && cargo test power_test + cd testing && cargo test power_test -- --nocapture test_verifreg_integration: build build_builtin_actors - cd testing && cargo test verifreg_test + cd testing && cargo test verifreg_test -- --nocapture test_datacap_integration: build build_builtin_actors - cd testing && cargo test datacap_test + cd testing && cargo test datacap_test -- --nocapture test_init_integration: build build_builtin_actors - cd testing && cargo test init_test + cd testing && cargo test init_test -- --nocapture test_account_integration: build build_builtin_actors - cd testing && cargo test account_test + cd testing && cargo test account_test -- --nocapture test_multisig_integration: build build_builtin_actors - cd testing && cargo test multisig_test + cd testing && cargo test multisig_test -- --nocapture test_precompiles_integration: build build_builtin_actors - cd testing && cargo test precompiles_test + cd testing && cargo test precompiles_test -- --nocapture test_send_integration: build build_builtin_actors - cd testing && cargo test send_test + cd testing && cargo test send_test -- --nocapture ################ DEPS ################ diff --git a/testing/Cargo.lock b/testing/Cargo.lock index e12432b4..210120a5 100644 --- a/testing/Cargo.lock +++ b/testing/Cargo.lock @@ -341,6 +341,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.11.1" @@ -718,6 +730,28 @@ dependencies = [ "serde", ] +[[package]] +name = "csv" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" +dependencies = [ + "bstr", + "csv-core", + "itoa 0.4.8", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "ctor" version = "0.1.26" @@ -869,6 +903,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "ec-gpu" version = "0.1.0" @@ -906,6 +961,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "errno" version = "0.2.8" @@ -1850,6 +1911,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "856b5cb0902c2b6d65d5fd97dfa30f9b70c7538e770b98eab5ed52d8db923e01" + [[package]] name = "hex" version = "0.4.3" @@ -1984,6 +2051,18 @@ dependencies = [ "windows-sys 0.42.0", ] +[[package]] +name = "is-terminal" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0a45d56fe973d6db23972bf5bc46f988a4a2385deac9cc29572f09daef" +dependencies = [ + "hermit-abi 0.3.0", + "io-lifetimes 1.0.4", + "rustix 0.36.6", + "windows-sys 0.45.0", +] + [[package]] name = "itertools" version = "0.8.2" @@ -2002,6 +2081,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + [[package]] name = "itoa" version = "1.0.5" @@ -2443,7 +2528,7 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" dependencies = [ - "hermit-abi", + "hermit-abi 0.2.6", "libc", ] @@ -2572,6 +2657,20 @@ dependencies = [ "yansi", ] +[[package]] +name = "prettytable-rs" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eea25e07510aa6ab6547308ebe3c036016d162b8da920dbb079e3ba8acf3d95a" +dependencies = [ + "csv", + "encode_unicode", + "is-terminal", + "lazy_static", + "term", + "unicode-width", +] + [[package]] name = "proc-macro-crate" version = "1.1.3" @@ -2709,6 +2808,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "regalloc2" version = "0.4.2" @@ -2732,6 +2842,12 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-syntax" version = "0.6.28" @@ -2901,7 +3017,7 @@ version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" dependencies = [ - "itoa", + "itoa 1.0.5", "ryu", "serde", ] @@ -3267,6 +3383,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + [[package]] name = "testing" version = "0.1.0" @@ -3289,6 +3416,7 @@ dependencies = [ "hex", "libipld-core 0.13.1", "multihash", + "prettytable-rs", "rand_core", "serde", "serde_tuple", @@ -3387,6 +3515,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "unicode-xid" version = "0.2.4" @@ -3706,6 +3840,30 @@ dependencies = [ "windows_x86_64_msvc 0.42.1", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc 0.42.1", + "windows_i686_gnu 0.42.1", + "windows_i686_msvc 0.42.1", + "windows_x86_64_gnu 0.42.1", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc 0.42.1", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.1" diff --git a/testing/Cargo.toml b/testing/Cargo.toml index f045eca0..06a775d7 100644 --- a/testing/Cargo.toml +++ b/testing/Cargo.toml @@ -22,6 +22,7 @@ libipld-core = { version = "0.13.1", features = ["serde-codec"] } cid = "0.8.6" rand_core = "0.6.4" anyhow = "1.0.47" +prettytable-rs = "0.10" fil_actors_runtime = { version = "10.0.0-alpha.1", git = "https://github.com/filecoin-project/builtin-actors", tag = "dev/20230114-pre-rc.3", features = ["m2-native"] } fil_actor_eam = { version = "10.0.0-alpha.1", git = "https://github.com/filecoin-project/builtin-actors", tag = "dev/20230114-pre-rc.3" } diff --git a/testing/src/lib.rs b/testing/src/lib.rs index eadef2b9..90397cff 100644 --- a/testing/src/lib.rs +++ b/testing/src/lib.rs @@ -1 +1,2 @@ -pub mod helpers; \ No newline at end of file +pub mod helpers; +pub mod setup; diff --git a/testing/src/setup.rs b/testing/src/setup.rs new file mode 100644 index 00000000..f4fc1860 --- /dev/null +++ b/testing/src/setup.rs @@ -0,0 +1,36 @@ +use cid::Cid; +use fvm::machine::Manifest; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Tester; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use fvm_integration_tests::bundle; +use fvm_ipld_encoding::CborStore; +use fvm_integration_tests::dummy::DummyExterns; + +pub fn setup_tester() -> (Tester, Manifest) { + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + return (tester, manifest) +} + +pub fn load_evm(path: &str) -> Vec { + let wasm_path = std::env::current_dir() + .unwrap() + .join(path) + .canonicalize() + .unwrap(); + let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); + + hex::decode(evm_hex).unwrap() +} \ No newline at end of file diff --git a/testing/tests/account.rs b/testing/tests/account.rs index 35dbc99f..fd16cbbb 100644 --- a/testing/tests/account.rs +++ b/testing/tests/account.rs @@ -1,136 +1,124 @@ -#[cfg(test)] -mod tests { - use bls_signatures::Serialize; - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_evm::{Method as EvmMethods}; - use fil_actors_runtime::EAM_ACTOR_ADDR; - use fvm::executor::{ApplyKind, Executor}; - use fvm::state_tree::ActorState; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::strict_bytes; - use fvm_ipld_encoding::tuple::*; - use fvm_ipld_encoding::RawBytes; - use fvm_ipld_encoding::serde_bytes; - use fvm_shared::address::Address; - use fvm_shared::econ::TokenAmount; - use fvm_shared::message::Message; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use fvm_ipld_encoding::CborStore; - use multihash::Code; - use std::env; - use fvm::machine::Manifest; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/AccountApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[derive(Debug, Serialize_tuple, Deserialize_tuple)] - pub struct AuthenticateMessageParams { - #[serde(with = "serde_bytes")] - pub signature: Vec, - #[serde(with = "serde_bytes")] - pub message: Vec, - } - - #[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Eq, Clone, Debug)] - pub struct UniversalReceiverParams { - /// Asset type - pub type_: u32, - /// Payload corresponding to asset type - pub payload: RawBytes, - } - - #[test] - fn account_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let sender: [Account; 1] = tester.create_accounts().unwrap(); - - // Instantiate Account Actor with a BLS address (not secure private key! Only use for tests!) - let bls_private_key_provider = bls_signatures::PrivateKey::new(hex::decode("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef").unwrap()); - let worker = Address::new_bls(&bls_private_key_provider.public_key().as_bytes()).unwrap(); - - let state_tree = tester.state_tree.as_mut().unwrap(); - let assigned_addr = state_tree.register_new_address(&worker).unwrap(); - let state = fvm::account_actor::State { address: worker }; - - let cid = state_tree - .store() - .put_cbor(&state, Code::Blake2b256) - .unwrap(); - - let actor_state = ActorState { - code: *manifest.get_account_code(), - state: cid, - sequence: 0, - balance: TokenAmount::from_whole(1_000_000), - delegated_address: Some(worker), - }; +use bls_signatures::Serialize; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::EAM_ACTOR_ADDR; +use fvm::executor::{ApplyKind, Executor}; +use fvm::state_tree::ActorState; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::Account; +use fvm_ipld_encoding::serde_bytes; +use fvm_ipld_encoding::strict_bytes; +use fvm_ipld_encoding::tuple::*; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use multihash::Code; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::setup; + +#[macro_use] +extern crate prettytable; +use prettytable::Table; + +type GasResult = Vec<(String, i64)>; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/AccountApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[derive(Debug, Serialize_tuple, Deserialize_tuple)] +pub struct AuthenticateMessageParams { + #[serde(with = "serde_bytes")] + pub signature: Vec, + #[serde(with = "serde_bytes")] + pub message: Vec, +} - state_tree.set_actor(assigned_addr, actor_state).unwrap(); +#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Eq, Clone, Debug)] +pub struct UniversalReceiverParams { + /// Asset type + pub type_: u32, + /// Payload corresponding to asset type + pub payload: RawBytes, +} - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); +#[test] +fn account_tests() { + println!("Testing solidity API"); - let executor = tester.executor.as_mut().unwrap(); + let mut gas_result: GasResult = vec![]; - println!("Calling init actor (EVM)"); + let (mut tester, manifest) = setup::setup_tester(); - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); + let sender: [Account; 1] = tester.create_accounts().unwrap(); - let constructor_params = CreateExternalParams(evm_bin); + // Instantiate Account Actor with a BLS address (not secure private key! Only use for tests!) + let bls_private_key_provider = bls_signatures::PrivateKey::new( + hex::decode("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef").unwrap(), + ); + let worker = Address::new_bls(&bls_private_key_provider.public_key().as_bytes()).unwrap(); - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; + let state_tree = tester.state_tree.as_mut().unwrap(); + let assigned_addr = state_tree.register_new_address(&worker).unwrap(); + let state = fvm::account_actor::State { address: worker }; + + let cid = state_tree + .store() + .put_cbor(&state, Code::Blake2b256) + .unwrap(); + + let actor_state = ActorState { + code: *manifest.get_account_code(), + state: cid, + sequence: 0, + balance: TokenAmount::from_whole(1_000_000), + delegated_address: Some(worker), + }; + + state_tree.set_actor(assigned_addr, actor_state).unwrap(); - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + let executor = tester.executor.as_mut().unwrap(); - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - let actor_id = exec_return.actor_id; + println!("Calling init actor (EVM)"); - println!("Calling `authenticate_message`"); + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); - let sig = bls_private_key_provider.sign(hex::decode("8bd82a5828000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a091a00800000f45831039216096d4dbc72ebdb22d0c8bd4af3f40e4396592200a95300d7ce319fb740048e01eece7a181cdad6fbe25374a9cb4542006778346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d19629d1a0008517e470001001d1bf8004800038d7ea4c680004800038d7ea4c68000").unwrap()); - dbg!(hex::encode(sig.as_bytes())); - dbg!(hex::encode(worker.to_bytes())); + let constructor_params = CreateExternalParams(evm_bin); - // create message - let message = Message { + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + let actor_id = exec_return.actor_id; + + println!("Calling `authenticate_message`"); + + let sig = bls_private_key_provider.sign(hex::decode("8bd82a5828000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a091a00800000f45831039216096d4dbc72ebdb22d0c8bd4af3f40e4396592200a95300d7ce319fb740048e01eece7a181cdad6fbe25374a9cb4542006778346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d19629d1a0008517e470001001d1bf8004800038d7ea4c680004800038d7ea4c68000").unwrap()); + dbg!(hex::encode(sig.as_bytes())); + dbg!(hex::encode(worker.to_bytes())); + + // create message + let message = Message { from: sender[0].1, to: Address::new_id(actor_id), gas_limit: 1000000000, @@ -141,20 +129,21 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + gas_result.push(("authenticate_message".into(), res.msg_receipt.gas_used)); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `authenticate_message (actor ID)`"); + println!("Calling `authenticate_message (actor ID)`"); - let sig = bls_private_key_provider.sign(hex::decode("8bd82a5828000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a091a00800000f45831039216096d4dbc72ebdb22d0c8bd4af3f40e4396592200a95300d7ce319fb740048e01eece7a181cdad6fbe25374a9cb4542006778346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d19629d1a0008517e470001001d1bf8004800038d7ea4c680004800038d7ea4c68000").unwrap()); - dbg!(hex::encode(sig.as_bytes())); - dbg!(hex::encode(worker.to_bytes())); + let sig = bls_private_key_provider.sign(hex::decode("8bd82a5828000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a091a00800000f45831039216096d4dbc72ebdb22d0c8bd4af3f40e4396592200a95300d7ce319fb740048e01eece7a181cdad6fbe25374a9cb4542006778346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d19629d1a0008517e470001001d1bf8004800038d7ea4c680004800038d7ea4c68000").unwrap()); + dbg!(hex::encode(sig.as_bytes())); + dbg!(hex::encode(worker.to_bytes())); - // create message - let message = Message { + // create message + let message = Message { from: sender[0].1, to: Address::new_id(actor_id), gas_limit: 1000000000, @@ -165,15 +154,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + gas_result.push(("authenticate_message (actor ID)".into(), res.msg_receipt.gas_used)); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `universal_receiver_hook`"); + println!("Calling `universal_receiver_hook`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(actor_id), gas_limit: 1000000000, @@ -184,15 +174,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + gas_result.push(("universal_receiver_hook".into(), res.msg_receipt.gas_used)); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `universal_receiver_hook (actor ID)`"); + println!("Calling `universal_receiver_hook (actor ID)`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(actor_id), gas_limit: 1000000000, @@ -203,12 +194,19 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - } + gas_result.push(("universal_receiver_hook (actor ID)".into(), res.msg_receipt.gas_used)); + assert_eq!(res.msg_receipt.exit_code.value(), 0); -} + let mut table = Table::new(); + table.add_row(row!["Function", "Gas"]); + gas_result.iter().for_each(|(description, gas)| { + table.add_row(row![description, gas]); + }); + + table.printstd(); +} diff --git a/testing/tests/datacap.rs b/testing/tests/datacap.rs index d9eff080..9bf1a628 100644 --- a/testing/tests/datacap.rs +++ b/testing/tests/datacap.rs @@ -1,329 +1,323 @@ -#[cfg(test)] -mod tests { - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_evm::Method as EvmMethods; - use fil_actors_runtime::{ - runtime::builtins, DATACAP_TOKEN_ACTOR_ADDR, EAM_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, - VERIFIED_REGISTRY_ACTOR_ADDR, - }; - use fvm::executor::{ApplyKind, Executor}; - use fvm::machine::Manifest; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::strict_bytes; - use fvm_ipld_encoding::CborStore; - use fvm_ipld_encoding::RawBytes; - use fvm_shared::address::Address; - use fvm_shared::econ::TokenAmount; - use fvm_shared::message::Message; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use std::env; - - use testing::helpers; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/DataCapApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[test] - fn datacap_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens - // and make it a valid address to use. - let sender: [Account; 300] = tester.create_accounts().unwrap(); - - // Set datacap actor - let state_tree = tester.state_tree.as_mut().unwrap(); - helpers::set_datacap_actor( - state_tree, - *manifest.code_by_id(builtins::Type::DataCap as u32).unwrap(), - ) + +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::{ + runtime::builtins, DATACAP_TOKEN_ACTOR_ADDR, EAM_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, + VERIFIED_REGISTRY_ACTOR_ADDR, +}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::strict_bytes; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; +use std::env; + +use testing::helpers; +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/DataCapApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[test] +fn datacap_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens + // and make it a valid address to use. + let sender: [Account; 300] = tester.create_accounts().unwrap(); + + // Set datacap actor + let state_tree = tester.state_tree.as_mut().unwrap(); + helpers::set_datacap_actor( + state_tree, + *manifest.code_by_id(builtins::Type::DataCap as u32).unwrap(), + ) + .unwrap(); + helpers::set_verifiedregistry_actor( + state_tree, + *manifest + .code_by_id(builtins::Type::VerifiedRegistry as u32) + .unwrap(), + ) + .unwrap(); + + // Create embryo address to deploy the contract on it (assign some FILs to it) + let tmp = hex::decode("DAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5").unwrap(); + let embryo_eth_address = tmp.as_slice(); + let embryo_delegated_address = Address::new_delegated(10, embryo_eth_address).unwrap(); + tester + .create_placeholder(&embryo_delegated_address, TokenAmount::from_whole(100)) .unwrap(); - helpers::set_verifiedregistry_actor( - state_tree, - *manifest - .code_by_id(builtins::Type::VerifiedRegistry as u32) - .unwrap(), + + println!( + "Embryo address delegated type [{}]", + embryo_delegated_address + ); + println!( + "Embryo address delegated type on hex [{}]", + hex::encode(embryo_delegated_address.to_bytes()) + ); + // println!("Embryo address ID type on decimal [{}]",embryo_actor_id); + // println!("Embryo address ID type on hex [{}]",hex::encode(Address::new_id(embryo_actor_id).to_bytes())); + + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[0].0, + hex::encode(&sender[0].1.to_bytes()) ) - .unwrap(); + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[1].0, + hex::encode(&sender[1].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[2].0, + hex::encode(&sender[2].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[3].0, + hex::encode(&sender[3].1.to_bytes()) + ) + ); - // Create embryo address to deploy the contract on it (assign some FILs to it) - let tmp = hex::decode("DAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5").unwrap(); - let embryo_eth_address = tmp.as_slice(); - let embryo_delegated_address = Address::new_delegated(10, embryo_eth_address).unwrap(); - tester - .create_placeholder(&embryo_delegated_address, TokenAmount::from_whole(100)) - .unwrap(); - - println!( - "Embryo address delegated type [{}]", - embryo_delegated_address - ); - println!( - "Embryo address delegated type on hex [{}]", - hex::encode(embryo_delegated_address.to_bytes()) - ); - // println!("Embryo address ID type on decimal [{}]",embryo_actor_id); - // println!("Embryo address ID type on hex [{}]",hex::encode(Address::new_id(embryo_actor_id).to_bytes())); - - println!( - "{}", - format!( - "Sender address id [{}] and bytes [{}]", - &sender[0].0, - hex::encode(&sender[0].1.to_bytes()) - ) - ); - println!( - "{}", - format!( - "Sender address id [{}] and bytes [{}]", - &sender[1].0, - hex::encode(&sender[1].1.to_bytes()) - ) - ); - println!( - "{}", - format!( - "Sender address id [{}] and bytes [{}]", - &sender[2].0, - hex::encode(&sender[2].1.to_bytes()) - ) - ); - println!( - "{}", - format!( - "Sender address id [{}] and bytes [{}]", - &sender[3].0, - hex::encode(&sender[3].1.to_bytes()) - ) - ); - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - // Try to call "constructor" - println!("Try to call constructor on verifreg actor"); - - let root_key = Address::new_id(199); - - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: VERIFIED_REGISTRY_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - params: RawBytes::serialize(root_key).unwrap(), - ..Message::default() - }; + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); + let executor = tester.executor.as_mut().unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + // Try to call "constructor" + println!("Try to call constructor on verifreg actor"); - // Try to call "constructor" - println!("Try to call constructor on datacap actor"); + let root_key = Address::new_id(199); - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: DATACAP_TOKEN_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - params: RawBytes::serialize(Address::new_id(200)).unwrap(), - ..Message::default() - }; + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: VERIFIED_REGISTRY_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + params: RawBytes::serialize(root_key).unwrap(), + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - // First we deploy the contract in order to actually have an actor running on the embryo address - println!("Calling init actor (EVM)"); + // Try to call "constructor" + println!("Try to call constructor on datacap actor"); - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: DATACAP_TOKEN_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + params: RawBytes::serialize(Address::new_id(200)).unwrap(), + ..Message::default() + }; - let constructor_params = CreateExternalParams(evm_bin); + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); - let message = Message { - from: embryo_delegated_address, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 0, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; + assert_eq!(res.msg_receipt.exit_code.value(), 0); - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + // First we deploy the contract in order to actually have an actor running on the embryo address + println!("Calling init actor (EVM)"); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + let constructor_params = CreateExternalParams(evm_bin); - println!( - "Contract address ID type on decimal [{}]", - exec_return.actor_id - ); - println!( - "Contract address ID type on hex [{}]", - hex::encode(Address::new_id(exec_return.actor_id).to_bytes()) - ); - match exec_return.robust_address { - Some(addr) => println!("Contract address robust type [{}]", addr), - None => (), - } + let message = Message { + from: embryo_delegated_address, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 0, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; - println!( - "Contract address eth address type [{}]", - hex::encode(exec_return.eth_address.0) - ); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - let contract_actor_id = exec_return.actor_id; + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!( + "Contract address ID type on decimal [{}]", + exec_return.actor_id + ); + println!( + "Contract address ID type on hex [{}]", + hex::encode(Address::new_id(exec_return.actor_id).to_bytes()) + ); + match exec_return.robust_address { + Some(addr) => println!("Contract address robust type [{}]", addr), + None => (), + } - // We need to mint tokens for the contract actor address in order to be able to execute methods like transfer, etc - // NOTICE: The only address that can mint tokens is the governor, which is defined on the ref-fvm repo (on integration module) - // NOTICE: We firt deploy the contract because the embryo address by its own cannot receive minted tokens. - println!("Minting some tokens on datacap actor"); + println!( + "Contract address eth address type [{}]", + hex::encode(exec_return.eth_address.0) + ); - let mint_params_1 = fil_actor_datacap::MintParams { - to: Address::new_id(contract_actor_id), - amount: TokenAmount::from_whole(1000), - operators: vec![Address::new_id(sender[0].0), Address::new_id(sender[1].0)], - }; + let contract_actor_id = exec_return.actor_id; - let message = Message { - from: Address::new_id(200), - to: DATACAP_TOKEN_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 116935346, // Coming from get_method_nums command - sequence: 0, - params: RawBytes::serialize(mint_params_1).unwrap(), - ..Message::default() - }; + // We need to mint tokens for the contract actor address in order to be able to execute methods like transfer, etc + // NOTICE: The only address that can mint tokens is the governor, which is defined on the ref-fvm repo (on integration module) + // NOTICE: We firt deploy the contract because the embryo address by its own cannot receive minted tokens. + println!("Minting some tokens on datacap actor"); - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let mint_params_1 = fil_actor_datacap::MintParams { + to: Address::new_id(contract_actor_id), + amount: TokenAmount::from_whole(1000), + operators: vec![Address::new_id(sender[0].0), Address::new_id(sender[1].0)], + }; - assert_eq!(res.msg_receipt.exit_code.value(), 0); + let message = Message { + from: Address::new_id(200), + to: DATACAP_TOKEN_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 116935346, // Coming from get_method_nums command + sequence: 0, + params: RawBytes::serialize(mint_params_1).unwrap(), + ..Message::default() + }; - println!("Minting more tokens on datacap actor"); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - let mint_params_2 = fil_actor_datacap::MintParams { - to: Address::new_id(sender[0].0), - amount: TokenAmount::from_whole(1000), - operators: vec![Address::new_id(contract_actor_id)], - }; + assert_eq!(res.msg_receipt.exit_code.value(), 0); - let message = Message { - from: Address::new_id(200), - to: DATACAP_TOKEN_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 116935346, // Coming from get_method_nums command - sequence: 1, - params: RawBytes::serialize(mint_params_2).unwrap(), - ..Message::default() - }; + println!("Minting more tokens on datacap actor"); + + let mint_params_2 = fil_actor_datacap::MintParams { + to: Address::new_id(sender[0].0), + amount: TokenAmount::from_whole(1000), + operators: vec![Address::new_id(contract_actor_id)], + }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let message = Message { + from: Address::new_id(200), + to: DATACAP_TOKEN_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 116935346, // Coming from get_method_nums command + sequence: 1, + params: RawBytes::serialize(mint_params_2).unwrap(), + ..Message::default() + }; - assert_eq!(res.msg_receipt.exit_code.value(), 0); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - println!("Calling `name`"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 0, - params: RawBytes::new(hex::decode("4406FDDE03").unwrap()), - ..Message::default() - }; + println!("Calling `name`"); - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 0, + params: RawBytes::new(hex::decode("4406FDDE03").unwrap()), + ..Message::default() + }; - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000074461746143617000000000000000000000000000000000000000000000000000"); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - println!("Calling `symbol`"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000074461746143617000000000000000000000000000000000000000000000000000"); - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 1, - params: RawBytes::new(hex::decode("4495D89B41").unwrap()), - ..Message::default() - }; + println!("Calling `symbol`"); - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 1, + params: RawBytes::new(hex::decode("4495D89B41").unwrap()), + ..Message::default() + }; - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000044443415000000000000000000000000000000000000000000000000000000000"); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - println!("Calling `total_supply`"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000044443415000000000000000000000000000000000000000000000000000000000"); - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 2, - params: RawBytes::new(hex::decode("443940E9EE").unwrap()), - ..Message::default() - }; + println!("Calling `total_supply`"); - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 2, + params: RawBytes::new(hex::decode("443940E9EE").unwrap()), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096c6b935b8bbd4000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096c6b935b8bbd4000000000000000000000000000000000000000000000000000"); - println!("Calling `balance`"); + println!("Calling `balance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -333,16 +327,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `allowance`"); + println!("Calling `allowance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -352,16 +346,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `transfer`"); + println!("Calling `transfer`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -371,18 +365,18 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - dbg!(&res); + dbg!(&res); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009361a08405e8fd8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081bc16d674ec800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009361a08405e8fd8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081bc16d674ec800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `transfer_from`"); + println!("Calling `transfer_from`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -392,16 +386,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5902400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000935fe46d2f741100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853444835ec58000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc427e27bb1627000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5902400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000935fe46d2f741100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853444835ec58000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc427e27bb1627000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `burn`"); + println!("Calling `burn`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -411,16 +405,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009360c2789aae87400000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009360c2789aae87400000000000000000000000000000000000000000000000000"); - println!("Calling `burn_from`"); + println!("Calling `burn_from`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -430,16 +424,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000935f0661c4399ac0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc41a01c4fdbb0c0000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000935f0661c4399ac0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc41a01c4fdbb0c0000000000000000000000000000000000"); - println!("Calling `allowance`"); + println!("Calling `allowance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -449,16 +443,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc41a01c4fdbb0c0000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc41a01c4fdbb0c0000000000000000000000000000000000"); - println!("Calling `increase_allowance`"); + println!("Calling `increase_allowance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -468,16 +462,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acfa952f0445dea00000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acfa952f0445dea00000000000000000000000000000000000"); - println!("Calling `decrease_allowance`"); + println!("Calling `decrease_allowance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -487,16 +481,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc45f65568000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc45f65568000000000000000000000000000000000000000"); - println!("Calling `revoke_allowance`"); + println!("Calling `revoke_allowance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -506,11 +500,10 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc45f65568000000000000000000000000000000000000000"); - } + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc45f65568000000000000000000000000000000000000000"); } diff --git a/testing/tests/init.rs b/testing/tests/init.rs index f2447dc8..fd5bd86c 100644 --- a/testing/tests/init.rs +++ b/testing/tests/init.rs @@ -1,123 +1,121 @@ -#[cfg(test)] -mod tests { - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_evm::{Method as EvmMethods}; - use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR}; - use fvm::executor::{ApplyKind, Executor}; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::RawBytes; - use fvm_ipld_encoding::{strict_bytes}; - use fvm_shared::address::Address; - use fvm_shared::message::Message; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use std::env; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use fvm::machine::Manifest; - use fvm_ipld_encoding::CborStore; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/InitApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[test] - fn init_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let sender: [Account; 1] = tester.create_accounts().unwrap(); - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Calling `exec`"); - - //let multisig_cid = Cid::from_str("bafk2bzaceawblu7i2c2ghriamzkiqixbt6fcvvs2ka3kn5q7xgpd7rijlyauu").unwrap(); - let multisig_cid = *manifest.code_by_id(builtins::Type::Multisig as u32).unwrap(); - // This value is used as input data for the exec contract call down below - dbg!(hex::encode(multisig_cid.to_bytes())); - - let mulstisig_cid_hex = hex::encode(multisig_cid.to_bytes()); - let mut params = "".to_owned(); - - params.push_str("59012408853AFC0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000A0000000000000000000000000000000000000000000000000000000000000002700"); - params.push_str(mulstisig_cid_hex.as_str()); - params.push_str("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031848255011EAF1C8A4BBFEEB0870B1745B1F57503470B71165501DFE49184D46ADC8F89D44638BEB45F78FCAD2590010000000000000000000000000000000000"); - - let message = Message { + +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::strict_bytes; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::message::Message; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; +use std::env; + +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/InitApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[test] +fn init_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + let sender: [Account; 1] = tester.create_accounts().unwrap(); + + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!("Calling `exec`"); + + //let multisig_cid = Cid::from_str("bafk2bzaceawblu7i2c2ghriamzkiqixbt6fcvvs2ka3kn5q7xgpd7rijlyauu").unwrap(); + let multisig_cid = *manifest + .code_by_id(builtins::Type::Multisig as u32) + .unwrap(); + // This value is used as input data for the exec contract call down below + dbg!(hex::encode(multisig_cid.to_bytes())); + + let mulstisig_cid_hex = hex::encode(multisig_cid.to_bytes()); + let mut params = "".to_owned(); + + params.push_str("59012408853AFC0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000A0000000000000000000000000000000000000000000000000000000000000002700"); + params.push_str(mulstisig_cid_hex.as_str()); + params.push_str("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031848255011EAF1C8A4BBFEEB0870B1745B1F57503470B71165501DFE49184D46ADC8F89D44638BEB45F78FCAD2590010000000000000000000000000000000000"); + + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, method_num: EvmMethods::InvokeContract as u64, sequence: 1, - params: RawBytes::new( hex::decode(params).unwrap()), + params: RawBytes::new(hex::decode(params).unwrap()), ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - dbg!(&res); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020066000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001502a526fc35cb62e5b1810ab92c02e0b37c8e7f47dd0000000000000000000000"); + dbg!(&res); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020066000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001502a526fc35cb62e5b1810ab92c02e0b37c8e7f47dd0000000000000000000000"); - println!("Calling `exec4`"); + println!("Calling `exec4`"); - let mut params = "".to_owned(); + let mut params = "".to_owned(); - params.push_str("5901A4782128310000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000C00000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000002700"); - params.push_str(mulstisig_cid_hex.as_str()); - params.push_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005F82D82A5827000155A0E402204EB19534C71DDAFC0C89DFD7E8D9AEA0EEEBFD4D27C00DA1C3FBE2DD56C74A035831848255011EAF1C8A4BBFEEB0870B1745B1F57503470B71165501DFE49184D46ADC8F89D44638BEB45F78FCAD2590010000000000000000000000000000000000000000000000000000000000000000000006666F6F6261720000000000000000000000000000000000000000000000000000"); + params.push_str("5901A4782128310000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000C00000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000002700"); + params.push_str(mulstisig_cid_hex.as_str()); + params.push_str("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005F82D82A5827000155A0E402204EB19534C71DDAFC0C89DFD7E8D9AEA0EEEBFD4D27C00DA1C3FBE2DD56C74A035831848255011EAF1C8A4BBFEEB0870B1745B1F57503470B71165501DFE49184D46ADC8F89D44638BEB45F78FCAD2590010000000000000000000000000000000000000000000000000000000000000000000006666F6F6261720000000000000000000000000000000000000000000000000000"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -127,12 +125,11 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - // FIXME: "caller f0101 of method 3 must be built-in" - // However there is no exported method for EXEC4 yet. Tests cant pass until the builtin-actors export the exec4 method. - assert_eq!(res.msg_receipt.exit_code.value(), 33); - } + // FIXME: "caller f0101 of method 3 must be built-in" + // However there is no exported method for EXEC4 yet. Tests cant pass until the builtin-actors export the exec4 method. + assert_eq!(res.msg_receipt.exit_code.value(), 33); } diff --git a/testing/tests/market.rs b/testing/tests/market.rs index 504a878d..af46807c 100644 --- a/testing/tests/market.rs +++ b/testing/tests/market.rs @@ -1,440 +1,444 @@ -#[cfg(test)] -mod tests { - use bls_signatures::Serialize; - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_evm::Method as EvmMethods; - use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, STORAGE_MARKET_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, REWARD_ACTOR_ADDR}; - use fvm::executor::{ApplyKind, Executor}; - use fvm::machine::Manifest; - use fvm::state_tree::ActorState; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::BytesDe; - use fvm_ipld_encoding::BytesSer; - use fvm_ipld_encoding::CborStore; - use fvm_ipld_encoding::RawBytes; - use fvm_ipld_encoding::{serde_bytes, strict_bytes, tuple::*}; - use fvm_shared::address::Address; - use fvm_shared::clock::ChainEpoch; - use fvm_shared::crypto::signature::Signature; - use fvm_shared::econ::TokenAmount; - use fvm_shared::message::Message; - use fvm_shared::piece::PaddedPieceSize; - use fvm_shared::sector::RegisteredPoStProof; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use libipld_core::ipld::Ipld; - use multihash::Code; - use rand_core::OsRng; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use std::env; - use std::str::FromStr; - - use testing::helpers; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MarketApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[derive(Clone, Debug, PartialEq, Eq)] - pub enum Label { - String(String), - Bytes(Vec), - } - /// Serialize the Label like an untagged enum. - impl serde::Serialize for Label { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - match self { - Label::String(v) => v.serialize(serializer), - Label::Bytes(v) => BytesSer(v).serialize(serializer), - } +use bls_signatures::Serialize; +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::{ + runtime::builtins, EAM_ACTOR_ADDR, REWARD_ACTOR_ADDR, STORAGE_MARKET_ACTOR_ADDR, + STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, +}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm::state_tree::ActorState; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::BytesDe; +use fvm_ipld_encoding::BytesSer; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_ipld_encoding::{serde_bytes, strict_bytes, tuple::*}; +use fvm_shared::address::Address; +use fvm_shared::clock::ChainEpoch; +use fvm_shared::crypto::signature::Signature; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::piece::PaddedPieceSize; +use fvm_shared::sector::RegisteredPoStProof; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use libipld_core::ipld::Ipld; +use multihash::Code; +use rand_core::OsRng; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; +use std::env; +use std::str::FromStr; + +use testing::helpers; +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MarketApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Label { + String(String), + Bytes(Vec), +} + +/// Serialize the Label like an untagged enum. +impl serde::Serialize for Label { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + Label::String(v) => v.serialize(serializer), + Label::Bytes(v) => BytesSer(v).serialize(serializer), } } +} - /// Deserialize the Label like an untagged enum. - impl<'de> serde::Deserialize<'de> for Label { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - Ipld::deserialize(deserializer) - .and_then(|ipld| ipld.try_into().map_err(serde::de::Error::custom)) - } +/// Deserialize the Label like an untagged enum. +impl<'de> serde::Deserialize<'de> for Label { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + Ipld::deserialize(deserializer) + .and_then(|ipld| ipld.try_into().map_err(serde::de::Error::custom)) } +} - impl TryFrom for Label { - type Error = String; - - fn try_from(ipld: Ipld) -> Result { - match ipld { - Ipld::String(s) => Ok(Label::String(s)), - Ipld::Bytes(b) => Ok(Label::Bytes(b)), - other => Err(format!( - "Expected `Ipld::String` or `Ipld::Bytes`, got {:#?}", - other - )), - } +impl TryFrom for Label { + type Error = String; + + fn try_from(ipld: Ipld) -> Result { + match ipld { + Ipld::String(s) => Ok(Label::String(s)), + Ipld::Bytes(b) => Ok(Label::Bytes(b)), + other => Err(format!( + "Expected `Ipld::String` or `Ipld::Bytes`, got {:#?}", + other + )), } } +} - #[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] - pub struct ClientDealProposal { - pub proposal: DealProposal, - pub client_signature: Signature, - } +#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] +pub struct ClientDealProposal { + pub proposal: DealProposal, + pub client_signature: Signature, +} - #[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] - pub struct PublishStorageDealsParams { - pub deals: Vec, - } +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] +pub struct PublishStorageDealsParams { + pub deals: Vec, +} - #[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] - pub struct DealProposal { - pub piece_cid: Cid, - pub piece_size: PaddedPieceSize, - pub verified_deal: bool, - pub client: Address, - pub provider: Address, - pub label: Label, - pub start_epoch: ChainEpoch, - pub end_epoch: ChainEpoch, - pub storage_price_per_epoch: TokenAmount, - pub provider_collateral: TokenAmount, - pub client_collateral: TokenAmount, - } +#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] +pub struct DealProposal { + pub piece_cid: Cid, + pub piece_size: PaddedPieceSize, + pub verified_deal: bool, + pub client: Address, + pub provider: Address, + pub label: Label, + pub start_epoch: ChainEpoch, + pub end_epoch: ChainEpoch, + pub storage_price_per_epoch: TokenAmount, + pub provider_collateral: TokenAmount, + pub client_collateral: TokenAmount, +} - #[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] - pub struct CreateMinerParams { - pub owner: Address, - pub worker: Address, - pub window_post_proof_type: RegisteredPoStProof, - #[serde(with = "strict_bytes")] - pub peer: Vec, - pub multiaddrs: Vec, - } +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] +pub struct CreateMinerParams { + pub owner: Address, + pub worker: Address, + pub window_post_proof_type: RegisteredPoStProof, + #[serde(with = "strict_bytes")] + pub peer: Vec, + pub multiaddrs: Vec, +} - pub const AUTHENTICATE_MESSAGE_METHOD: u64 = 2643134072; +pub const AUTHENTICATE_MESSAGE_METHOD: u64 = 2643134072; - #[derive(Serialize_tuple, Deserialize_tuple)] - pub struct AuthenticateMessageParams { - #[serde(with = "serde_bytes")] - pub signature: Vec, - #[serde(with = "serde_bytes")] - pub message: Vec, - } +#[derive(Serialize_tuple, Deserialize_tuple)] +pub struct AuthenticateMessageParams { + #[serde(with = "serde_bytes")] + pub signature: Vec, + #[serde(with = "serde_bytes")] + pub message: Vec, +} - #[test] - fn market_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let sender: [Account; 1] = tester.create_accounts().unwrap(); - //let client: [Account; 1] = tester.create_accounts().unwrap(); - - // Set storagemarket actor - let state_tree = tester.state_tree.as_mut().unwrap(); - helpers::set_storagemarket_actor(state_tree, *manifest.code_by_id(builtins::Type::Market as u32).unwrap()) - .unwrap(); - // Set storagepower actor - helpers::set_storagepower_actor(state_tree, *manifest.code_by_id(builtins::Type::Power as u32).unwrap()) - .unwrap(); - helpers::set_reward_actor(state_tree, *manifest.code_by_id(builtins::Type::Reward as u32).unwrap()) - .unwrap(); - - /*********************************************** - * - * Instantiate Account Actor with a BLS address - * - ***********************************************/ - let bls_private_key_client = bls_signatures::PrivateKey::generate(&mut OsRng); - let client = Address::new_bls(&bls_private_key_client.public_key().as_bytes()).unwrap(); - - let state_tree = tester.state_tree.as_mut().unwrap(); - let assigned_addr = state_tree.register_new_address(&client).unwrap(); - let state = fvm::account_actor::State { address: client }; - - let cid = state_tree - .store() - .put_cbor(&state, Code::Blake2b256) - .unwrap(); - - let actor_state = ActorState { - code: *manifest.get_account_code(), - state: cid, - sequence: 0, - balance: TokenAmount::from_whole(1_000_000), - delegated_address: Some(client), - }; - - state_tree.set_actor(assigned_addr, actor_state).unwrap(); - - let bls_private_key_provider = bls_signatures::PrivateKey::generate(&mut OsRng); - let worker = Address::new_bls(&bls_private_key_provider.public_key().as_bytes()).unwrap(); - - let state_tree = tester.state_tree.as_mut().unwrap(); - let assigned_addr = state_tree.register_new_address(&worker).unwrap(); - let state = fvm::account_actor::State { address: worker }; - - let cid = state_tree - .store() - .put_cbor(&state, Code::Blake2b256) - .unwrap(); - - let actor_state = ActorState { - code: *manifest.get_account_code(), - state: cid, - sequence: 0, - balance: TokenAmount::from_whole(1_000_000), - delegated_address: Some(worker), - }; - - state_tree.set_actor(assigned_addr, actor_state).unwrap(); - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - // Try to call "constructor" - println!("Try to call constructor on storage power actor"); - - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: STORAGE_POWER_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - ..Message::default() - }; - - let res = executor +#[test] +fn market_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + let sender: [Account; 1] = tester.create_accounts().unwrap(); + //let client: [Account; 1] = tester.create_accounts().unwrap(); + + // Set storagemarket actor + let state_tree = tester.state_tree.as_mut().unwrap(); + helpers::set_storagemarket_actor( + state_tree, + *manifest.code_by_id(builtins::Type::Market as u32).unwrap(), + ) + .unwrap(); + // Set storagepower actor + helpers::set_storagepower_actor( + state_tree, + *manifest.code_by_id(builtins::Type::Power as u32).unwrap(), + ) + .unwrap(); + helpers::set_reward_actor( + state_tree, + *manifest.code_by_id(builtins::Type::Reward as u32).unwrap(), + ) + .unwrap(); + + /*********************************************** + * + * Instantiate Account Actor with a BLS address + * + ***********************************************/ + let bls_private_key_client = bls_signatures::PrivateKey::generate(&mut OsRng); + let client = Address::new_bls(&bls_private_key_client.public_key().as_bytes()).unwrap(); + + let state_tree = tester.state_tree.as_mut().unwrap(); + let assigned_addr = state_tree.register_new_address(&client).unwrap(); + let state = fvm::account_actor::State { address: client }; + + let cid = state_tree + .store() + .put_cbor(&state, Code::Blake2b256) + .unwrap(); + + let actor_state = ActorState { + code: *manifest.get_account_code(), + state: cid, + sequence: 0, + balance: TokenAmount::from_whole(1_000_000), + delegated_address: Some(client), + }; + + state_tree.set_actor(assigned_addr, actor_state).unwrap(); + + let bls_private_key_provider = bls_signatures::PrivateKey::generate(&mut OsRng); + let worker = Address::new_bls(&bls_private_key_provider.public_key().as_bytes()).unwrap(); + + let state_tree = tester.state_tree.as_mut().unwrap(); + let assigned_addr = state_tree.register_new_address(&worker).unwrap(); + let state = fvm::account_actor::State { address: worker }; + + let cid = state_tree + .store() + .put_cbor(&state, Code::Blake2b256) + .unwrap(); + + let actor_state = ActorState { + code: *manifest.get_account_code(), + state: cid, + sequence: 0, + balance: TokenAmount::from_whole(1_000_000), + delegated_address: Some(worker), + }; + + state_tree.set_actor(assigned_addr, actor_state).unwrap(); + + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + // Try to call "constructor" + println!("Try to call constructor on storage power actor"); + + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: STORAGE_POWER_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + ..Message::default() + }; + + let res = executor .execute_message(message, ApplyKind::Implicit, 100) .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - // Try to call "constructor" - println!("Try to call constructor on storage market actor"); - - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: STORAGE_MARKET_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - // Try to call "constructor" - println!("Try to call constructor on reward actor"); - - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: REWARD_ACTOR_ADDR, - gas_limit: 1000000000, - params: RawBytes::new(vec![0]), // I have to send the power start value (0) - method_num: 1, - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); - - dbg!(&res); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Create Miner actor to be able to publish deal"); - - let constructor_params = CreateMinerParams { - owner: sender[0].1, - worker, - window_post_proof_type: fvm_shared::sector::RegisteredPoStProof::StackedDRGWindow2KiBV1, - peer: vec![1, 2, 3], - multiaddrs: vec![BytesDe(vec![1, 2, 3])], - }; - - let message = Message { - from: sender[0].1, - to: Address::new_id(4), - gas_limit: 1000000000, - method_num: 2, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 1, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Adding a deal!"); - - let proposal = DealProposal { - piece_cid: Cid::from_str( - "baga6ea4seaqlkg6mss5qs56jqtajg5ycrhpkj2b66cgdkukf2qjmmzz6ayksuci", - ) - .unwrap(), - piece_size: PaddedPieceSize(8388608), - verified_deal: false, - client: client, - provider: Address::new_id(103), - label: Label::String( - "mAXCg5AIg8YBXbFjtdBy1iZjpDYAwRSt0elGLF5GvTqulEii1VcM".to_string(), - ), - start_epoch: ChainEpoch::from(25245), - end_epoch: ChainEpoch::from(545150), - storage_price_per_epoch: TokenAmount::from_atto(1_100_000_000_000_i64), - provider_collateral: TokenAmount::from_atto(1_000_000_000_000_000_i64), - client_collateral: TokenAmount::from_atto(1_000_000_000_000_000_i64), - }; - - let deal = RawBytes::serialize(&proposal).unwrap(); - let sig = bls_private_key_client.sign(deal.to_vec()); - - dbg!("serialized deal {}", hex::encode(deal.to_vec())); - dbg!("sig deal {}", hex::encode(sig.as_bytes())); - - let params = AuthenticateMessageParams { - signature: sig.as_bytes(), - message: deal.to_vec(), - }; - - let message = Message { - from: client, // from need to be the miner - to: client, - gas_limit: 1000000000, - method_num: AUTHENTICATE_MESSAGE_METHOD, - sequence: 0, - params: RawBytes::serialize(params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let message = Message { - from: client, - to: Address::new_id(5), - gas_limit: 1000000000, - method_num: 2, - sequence: 1, - value: TokenAmount::from_whole(100), - params: RawBytes::serialize(client).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let message = Message { - from: worker, - to: Address::new_id(5), - gas_limit: 1000000000, - method_num: 2, - sequence: 0, - value: TokenAmount::from_whole(100_000), - params: RawBytes::serialize(Address::new_id(103)).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let deal = ClientDealProposal { - proposal, - client_signature: Signature::new_bls(sig.as_bytes()), - }; - - let params = PublishStorageDealsParams { deals: vec![deal] }; - - let message = Message { - from: worker, // from need to be the miner - to: Address::new_id(5), - gas_limit: 1000000000, - method_num: 4, - sequence: 1, - params: RawBytes::serialize(params).unwrap(), - //params: RawBytes::new(hex::decode("8181828bd82a5828000181e2039220206b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b190800f4420068420066656c6162656c0a1a0008ca0a42000a42000a42000a584d028bd82a5828000181e2039220206b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b190800f4420068420066656c6162656c0a1a0008ca0a42000a42000a42000a").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - dbg!(&res); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Calling `add_balance`"); - - let message = Message { + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + // Try to call "constructor" + println!("Try to call constructor on storage market actor"); + + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: STORAGE_MARKET_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + // Try to call "constructor" + println!("Try to call constructor on reward actor"); + + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: REWARD_ACTOR_ADDR, + gas_limit: 1000000000, + params: RawBytes::new(vec![0]), // I have to send the power start value (0) + method_num: 1, + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); + + dbg!(&res); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Create Miner actor to be able to publish deal"); + + let constructor_params = CreateMinerParams { + owner: sender[0].1, + worker, + window_post_proof_type: fvm_shared::sector::RegisteredPoStProof::StackedDRGWindow2KiBV1, + peer: vec![1, 2, 3], + multiaddrs: vec![BytesDe(vec![1, 2, 3])], + }; + + let message = Message { + from: sender[0].1, + to: Address::new_id(4), + gas_limit: 1000000000, + method_num: 2, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 1, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!("Adding a deal!"); + + let proposal = DealProposal { + piece_cid: Cid::from_str( + "baga6ea4seaqlkg6mss5qs56jqtajg5ycrhpkj2b66cgdkukf2qjmmzz6ayksuci", + ) + .unwrap(), + piece_size: PaddedPieceSize(8388608), + verified_deal: false, + client: client, + provider: Address::new_id(103), + label: Label::String("mAXCg5AIg8YBXbFjtdBy1iZjpDYAwRSt0elGLF5GvTqulEii1VcM".to_string()), + start_epoch: ChainEpoch::from(25245), + end_epoch: ChainEpoch::from(545150), + storage_price_per_epoch: TokenAmount::from_atto(1_100_000_000_000_i64), + provider_collateral: TokenAmount::from_atto(1_000_000_000_000_000_i64), + client_collateral: TokenAmount::from_atto(1_000_000_000_000_000_i64), + }; + + let deal = RawBytes::serialize(&proposal).unwrap(); + let sig = bls_private_key_client.sign(deal.to_vec()); + + dbg!("serialized deal {}", hex::encode(deal.to_vec())); + dbg!("sig deal {}", hex::encode(sig.as_bytes())); + + let params = AuthenticateMessageParams { + signature: sig.as_bytes(), + message: deal.to_vec(), + }; + + let message = Message { + from: client, // from need to be the miner + to: client, + gas_limit: 1000000000, + method_num: AUTHENTICATE_MESSAGE_METHOD, + sequence: 0, + params: RawBytes::serialize(params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let message = Message { + from: client, + to: Address::new_id(5), + gas_limit: 1000000000, + method_num: 2, + sequence: 1, + value: TokenAmount::from_whole(100), + params: RawBytes::serialize(client).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let message = Message { + from: worker, + to: Address::new_id(5), + gas_limit: 1000000000, + method_num: 2, + sequence: 0, + value: TokenAmount::from_whole(100_000), + params: RawBytes::serialize(Address::new_id(103)).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let deal = ClientDealProposal { + proposal, + client_signature: Signature::new_bls(sig.as_bytes()), + }; + + let params = PublishStorageDealsParams { deals: vec![deal] }; + + let message = Message { + from: worker, // from need to be the miner + to: Address::new_id(5), + gas_limit: 1000000000, + method_num: 4, + sequence: 1, + params: RawBytes::serialize(params).unwrap(), + //params: RawBytes::new(hex::decode("8181828bd82a5828000181e2039220206b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b190800f4420068420066656c6162656c0a1a0008ca0a42000a42000a42000a584d028bd82a5828000181e2039220206b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b190800f4420068420066656c6162656c0a1a0008ca0a42000a42000a42000a").unwrap()), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + dbg!(&res); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Calling `add_balance`"); + + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -445,16 +449,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `withdraw_balance`"); + println!("Calling `withdraw_balance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -464,16 +468,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016400000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016400000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `get_balance`"); + println!("Calling `get_balance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -483,284 +487,283 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009056bc75e2d63100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000807f3556c02eb7800000000000000000000000000000000000000000000000000"); - - println!("Calling `get_deal_data_commitment`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 5, - params: RawBytes::new( - hex::decode( - "5824915BD52A0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000028000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a09000000000000000000000000000000000000000000000000"); - - println!("Calling `get_deal_client`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 6, - params: RawBytes::new( - hex::decode( - "5824AABB67B40000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000065" - ); - - println!("Calling `get_deal_provider`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 7, - params: RawBytes::new( - hex::decode( - "58240E2F33670000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000067" - ); - - println!("Calling `get_deal_label`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 8, - params: RawBytes::new( - hex::decode( - "5824B6D312EA0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d000000000000000000000000"); - - println!("Calling `get_deal_term`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 9, - params: RawBytes::new( - hex::decode( - "58249CFC4C330000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5840000000000000000000000000000000000000000000000000000000000000629d000000000000000000000000000000000000000000000000000000000007eee1"); - - println!("Calling `get_deal_total_price`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 10, - params: RawBytes::new( - hex::decode( - "5824614C34150000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000807efc7ed5e24f800000000000000000000000000000000000000000000000000"); - - println!("Calling `get_deal_client_collateral`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 11, - params: RawBytes::new( - hex::decode( - "5824D5E7B9DB0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007038d7ea4c6800000000000000000000000000000000000000000000000000000"); - - println!("Calling `get_deal_provider_collateral`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 12, - params: RawBytes::new( - hex::decode( - "58242F2229FE0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007038d7ea4c6800000000000000000000000000000000000000000000000000000"); - - println!("Calling `get_deal_verified`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 13, - params: RawBytes::new( - hex::decode( - "58243219A6290000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000000" - ); - - println!("Calling `get_deal_activation`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 14, - params: RawBytes::new( - hex::decode( - "5824F5C036580000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "584000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - - println!("Calling `publish_storage_deals`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 15, - params: RawBytes::new( - hex::decode( - "58246E65901F0000000000000000000000000000000000000000000000000000000000000000", - ) - .unwrap(), - ), // FIXME this arguments are not correct - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - // FIXME - assert_eq!(res.msg_receipt.exit_code.value(), 33); - } + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009056bc75e2d63100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000807f3556c02eb7800000000000000000000000000000000000000000000000000"); + + println!("Calling `get_deal_data_commitment`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 5, + params: RawBytes::new( + hex::decode( + "5824915BD52A0000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000028000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a09000000000000000000000000000000000000000000000000"); + + println!("Calling `get_deal_client`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 6, + params: RawBytes::new( + hex::decode( + "5824AABB67B40000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000065" + ); + + println!("Calling `get_deal_provider`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 7, + params: RawBytes::new( + hex::decode( + "58240E2F33670000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000067" + ); + + println!("Calling `get_deal_label`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 8, + params: RawBytes::new( + hex::decode( + "5824B6D312EA0000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d000000000000000000000000"); + + println!("Calling `get_deal_term`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 9, + params: RawBytes::new( + hex::decode( + "58249CFC4C330000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5840000000000000000000000000000000000000000000000000000000000000629d000000000000000000000000000000000000000000000000000000000007eee1"); + + println!("Calling `get_deal_total_price`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 10, + params: RawBytes::new( + hex::decode( + "5824614C34150000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000807efc7ed5e24f800000000000000000000000000000000000000000000000000"); + + println!("Calling `get_deal_client_collateral`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 11, + params: RawBytes::new( + hex::decode( + "5824D5E7B9DB0000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007038d7ea4c6800000000000000000000000000000000000000000000000000000"); + + println!("Calling `get_deal_provider_collateral`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 12, + params: RawBytes::new( + hex::decode( + "58242F2229FE0000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007038d7ea4c6800000000000000000000000000000000000000000000000000000"); + + println!("Calling `get_deal_verified`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 13, + params: RawBytes::new( + hex::decode( + "58243219A6290000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000000" + ); + + println!("Calling `get_deal_activation`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 14, + params: RawBytes::new( + hex::decode( + "5824F5C036580000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "584000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + + println!("Calling `publish_storage_deals`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 15, + params: RawBytes::new( + hex::decode( + "58246E65901F0000000000000000000000000000000000000000000000000000000000000000", + ) + .unwrap(), + ), // FIXME this arguments are not correct + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + // FIXME + assert_eq!(res.msg_receipt.exit_code.value(), 33); } diff --git a/testing/tests/miner.rs b/testing/tests/miner.rs index 447faf58..95176b75 100644 --- a/testing/tests/miner.rs +++ b/testing/tests/miner.rs @@ -1,168 +1,163 @@ -#[cfg(test)] -mod tests { - use bls_signatures::Serialize; - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_init::ExecReturn; - use fil_actor_evm::{Method as EvmMethods}; - use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, INIT_ACTOR_ADDR}; - use fvm::executor::{ApplyKind, Executor}; - use fvm::state_tree::ActorState; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::CborStore; - use fvm_ipld_encoding::RawBytes; - use fvm_ipld_encoding::{strict_bytes, tuple::*, BytesDe}; - use fvm_shared::address::Address; - use fvm_shared::econ::TokenAmount; - use fvm_shared::message::Message; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use multihash::Code; - use rand_core::OsRng; - use std::env; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use fvm::machine::Manifest; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MinerApiTest.bin"; - - #[derive(Serialize_tuple, Deserialize_tuple, Clone, Debug)] - struct State { - empty: bool, - } - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[test] - fn miner_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let sender: [Account; 1] = tester.create_accounts().unwrap(); - - /*********************************************** - * - * Instantiate Account Actor with a BLS address - * - ***********************************************/ - - let bls_private_key = bls_signatures::PrivateKey::generate(&mut OsRng); - let worker = Address::new_bls(&bls_private_key.public_key().as_bytes()).unwrap(); - - let state_tree = tester.state_tree.as_mut().unwrap(); - let assigned_addr = state_tree.register_new_address(&worker).unwrap(); - let state = fvm::account_actor::State { address: worker }; - - let cid = state_tree - .store() - .put_cbor(&state, Code::Blake2b256) - .unwrap(); - - let actor_state = ActorState { - code: *manifest.get_account_code(), - state: cid, - sequence: 0, - balance: TokenAmount::from_atto(10000), - delegated_address: Some(worker), - }; - - state_tree.set_actor(assigned_addr, actor_state).unwrap(); - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - /************************** - * - * Machine instantiated - * - **************************/ - - println!("Create Miner actor for solidity contract to interact with"); - - let constructor_params = fil_actor_miner::MinerConstructorParams { - owner: Address::new_id(103), - worker, - control_addresses: vec![], - window_post_proof_type: fvm_shared::sector::RegisteredPoStProof::StackedDRGWindow2KiBV1, - peer_id: vec![1, 2, 3], - multi_addresses: vec![BytesDe(vec![1, 2, 3])], - }; - - let exec_params = fil_actor_init::ExecParams { - // CID of StorageMiner actor. You get this as output from builtin-actors compiling process - code_cid: *manifest.code_by_id(builtins::Type::Miner as u32).unwrap(), - constructor_params: RawBytes::serialize(constructor_params).unwrap(), - }; - - let message = Message { - from: sender[0].1, - to: INIT_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 2, - params: RawBytes::serialize(exec_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - let exec_return: ExecReturn = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - dbg!(hex::encode(&exec_return.id_address.to_bytes())); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 1, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Calling `change_owner_address`"); - - dbg!(&sender[0]); - - let message = Message { +use bls_signatures::Serialize; +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actor_init::ExecReturn; +use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, INIT_ACTOR_ADDR}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm::state_tree::ActorState; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_ipld_encoding::{strict_bytes, tuple::*, BytesDe}; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use multihash::Code; +use rand_core::OsRng; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MinerApiTest.bin"; + +#[derive(Serialize_tuple, Deserialize_tuple, Clone, Debug)] +struct State { + empty: bool, +} + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[test] +fn miner_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + let sender: [Account; 1] = tester.create_accounts().unwrap(); + + /*********************************************** + * + * Instantiate Account Actor with a BLS address + * + ***********************************************/ + + let bls_private_key = bls_signatures::PrivateKey::generate(&mut OsRng); + let worker = Address::new_bls(&bls_private_key.public_key().as_bytes()).unwrap(); + + let state_tree = tester.state_tree.as_mut().unwrap(); + let assigned_addr = state_tree.register_new_address(&worker).unwrap(); + let state = fvm::account_actor::State { address: worker }; + + let cid = state_tree + .store() + .put_cbor(&state, Code::Blake2b256) + .unwrap(); + + let actor_state = ActorState { + code: *manifest.get_account_code(), + state: cid, + sequence: 0, + balance: TokenAmount::from_atto(10000), + delegated_address: Some(worker), + }; + + state_tree.set_actor(assigned_addr, actor_state).unwrap(); + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + /************************** + * + * Machine instantiated + * + **************************/ + + println!("Create Miner actor for solidity contract to interact with"); + + let constructor_params = fil_actor_miner::MinerConstructorParams { + owner: Address::new_id(103), + worker, + control_addresses: vec![], + window_post_proof_type: fvm_shared::sector::RegisteredPoStProof::StackedDRGWindow2KiBV1, + peer_id: vec![1, 2, 3], + multi_addresses: vec![BytesDe(vec![1, 2, 3])], + }; + + let exec_params = fil_actor_init::ExecParams { + // CID of StorageMiner actor. You get this as output from builtin-actors compiling process + code_cid: *manifest.code_by_id(builtins::Type::Miner as u32).unwrap(), + constructor_params: RawBytes::serialize(constructor_params).unwrap(), + }; + + let message = Message { + from: sender[0].1, + to: INIT_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 2, + params: RawBytes::serialize(exec_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + let exec_return: ExecReturn = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + dbg!(hex::encode(&exec_return.id_address.to_bytes())); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 1, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!("Calling `change_owner_address`"); + + dbg!(&sender[0]); + + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -172,18 +167,18 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - dbg!(&res); + dbg!(&res); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `get_beneficiary`"); + println!("Calling `get_beneficiary`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -193,16 +188,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5903200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020067000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5903200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020067000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `change_beneficiary`"); + println!("Calling `change_beneficiary`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -212,16 +207,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `get_owner`"); + println!("Calling `get_owner`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -231,16 +226,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002006700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020066000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002006700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020066000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `get_available_balance`"); + println!("Calling `get_available_balance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -250,16 +245,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `get_vesting_funds`"); + println!("Calling `get_vesting_funds`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -269,16 +264,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); - println!("Calling `repay_debt`"); + println!("Calling `repay_debt`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -288,16 +283,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `confirm_change_worker_address`"); + println!("Calling `confirm_change_worker_address`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -307,16 +302,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `get_peer_id`"); + println!("Calling `get_peer_id`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -326,16 +321,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"); - println!("Calling `get_multiaddresses`"); + println!("Calling `get_multiaddresses`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -345,16 +340,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"); - println!("Calling `change_worker_address`"); + println!("Calling `change_worker_address`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -364,16 +359,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `is_controlling_address`"); + println!("Calling `is_controlling_address`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -383,19 +378,19 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000000" - ); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000000" + ); - println!("Calling `get_sector_size`"); + println!("Calling `get_sector_size`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -405,19 +400,19 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000800" - ); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000800" + ); - println!("Calling `change_multiaddresses`"); + println!("Calling `change_multiaddresses`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -427,16 +422,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `change_peer_id`"); + println!("Calling `change_peer_id`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -446,16 +441,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); - println!("Calling `withdraw_balance`"); + println!("Calling `withdraw_balance`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -465,11 +460,10 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); - } + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); } diff --git a/testing/tests/multisig.rs b/testing/tests/multisig.rs index ace158cf..e6eb4bc2 100644 --- a/testing/tests/multisig.rs +++ b/testing/tests/multisig.rs @@ -1,173 +1,172 @@ -#[cfg(test)] -mod tests { - use bls_signatures::Serialize; - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_init::ExecReturn; - use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, INIT_ACTOR_ADDR}; - use fil_actor_evm::{Method as EvmMethods}; - use fvm::executor::{ApplyKind, Executor}; - use fvm::state_tree::ActorState; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::CborStore; - use fvm_ipld_encoding::RawBytes; - use fvm_ipld_encoding::{strict_bytes}; - use fvm_shared::address::Address; - use fvm_shared::econ::TokenAmount; - use fvm_shared::message::Message; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use multihash::Code; - use rand_core::OsRng; - use std::env; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use fvm::machine::Manifest; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MultisigApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[test] - fn multisig_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let sender: [Account; 1] = tester.create_accounts().unwrap(); - - println!("Sender address id"); - dbg!(&sender[0].0); - println!("Sender address bytes"); - dbg!(hex::encode(&sender[0].1.to_bytes())); - /*********************************************** - * - * Instantiate Account Actor with a BLS address - * - ***********************************************/ - - let bls_private_key = bls_signatures::PrivateKey::generate(&mut OsRng); - let worker = Address::new_bls(&bls_private_key.public_key().as_bytes()).unwrap(); - - let state_tree = tester.state_tree.as_mut().unwrap(); - let assigned_addr = state_tree.register_new_address(&worker).unwrap(); - let state = fvm::account_actor::State { address: worker }; - - let cid = state_tree - .store() - .put_cbor(&state, Code::Blake2b256) - .unwrap(); - - let actor_state = ActorState { - // CID of Accounts actor. You get this as output from builtin-actors compiling process - code: *manifest.code_by_id(builtins::Type::Multisig as u32).unwrap(), - state: cid, - sequence: 0, - balance: TokenAmount::from_atto(10000), - delegated_address: Some(worker), - }; - - state_tree.set_actor(assigned_addr, actor_state).unwrap(); - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - /************************** - * - * Machine instantiated - * - **************************/ - - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 0, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Contract actor id"); - dbg!(&exec_return.actor_id); - let contract_actor_id = exec_return.actor_id; - - println!("Create Multisig actor for solidity contract to interact with"); - - let constructor_params = fil_actor_multisig::ConstructorParams { - num_approvals_threshold: 1, - signers: vec![ - Address::new_id(sender[0].0), - Address::new_id(contract_actor_id), - ], - start_epoch: 1670873233, - unlock_duration: 1670873234, - }; - - let exec_params = fil_actor_init::ExecParams { - // CID of MultiSig actor. You get this as output from builtin-actors compiling process - code_cid: *manifest.code_by_id(builtins::Type::Multisig as u32).unwrap(), - constructor_params: RawBytes::serialize(constructor_params).unwrap(), - }; - - let message = Message { - from: sender[0].1, - to: INIT_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 2, - sequence: 1, - params: RawBytes::serialize(exec_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - let exec_return: ExecReturn = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Multisig actor address bytes"); - dbg!(hex::encode(&exec_return.id_address.to_bytes())); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Calling `propose`"); - let message = Message { +use bls_signatures::Serialize; +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actor_init::ExecReturn; +use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, INIT_ACTOR_ADDR}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm::state_tree::ActorState; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::strict_bytes; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use multihash::Code; +use rand_core::OsRng; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MultisigApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[test] +fn multisig_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + let sender: [Account; 1] = tester.create_accounts().unwrap(); + + println!("Sender address id"); + dbg!(&sender[0].0); + println!("Sender address bytes"); + dbg!(hex::encode(&sender[0].1.to_bytes())); + /*********************************************** + * + * Instantiate Account Actor with a BLS address + * + ***********************************************/ + + let bls_private_key = bls_signatures::PrivateKey::generate(&mut OsRng); + let worker = Address::new_bls(&bls_private_key.public_key().as_bytes()).unwrap(); + + let state_tree = tester.state_tree.as_mut().unwrap(); + let assigned_addr = state_tree.register_new_address(&worker).unwrap(); + let state = fvm::account_actor::State { address: worker }; + + let cid = state_tree + .store() + .put_cbor(&state, Code::Blake2b256) + .unwrap(); + + let actor_state = ActorState { + // CID of Accounts actor. You get this as output from builtin-actors compiling process + code: *manifest + .code_by_id(builtins::Type::Multisig as u32) + .unwrap(), + state: cid, + sequence: 0, + balance: TokenAmount::from_atto(10000), + delegated_address: Some(worker), + }; + + state_tree.set_actor(assigned_addr, actor_state).unwrap(); + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + /************************** + * + * Machine instantiated + * + **************************/ + + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 0, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!("Contract actor id"); + dbg!(&exec_return.actor_id); + let contract_actor_id = exec_return.actor_id; + + println!("Create Multisig actor for solidity contract to interact with"); + + let constructor_params = fil_actor_multisig::ConstructorParams { + num_approvals_threshold: 1, + signers: vec![ + Address::new_id(sender[0].0), + Address::new_id(contract_actor_id), + ], + start_epoch: 1670873233, + unlock_duration: 1670873234, + }; + + let exec_params = fil_actor_init::ExecParams { + // CID of MultiSig actor. You get this as output from builtin-actors compiling process + code_cid: *manifest + .code_by_id(builtins::Type::Multisig as u32) + .unwrap(), + constructor_params: RawBytes::serialize(constructor_params).unwrap(), + }; + + let message = Message { + from: sender[0].1, + to: INIT_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 2, + sequence: 1, + params: RawBytes::serialize(exec_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + let exec_return: ExecReturn = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!("Multisig actor address bytes"); + dbg!(hex::encode(&exec_return.id_address.to_bytes())); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Calling `propose`"); + + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -177,16 +176,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - // FIXME - assert_eq!(res.msg_receipt.exit_code.value(), 33); + // FIXME + assert_eq!(res.msg_receipt.exit_code.value(), 33); - println!("Calling `add_signer`"); + println!("Calling `add_signer`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -196,13 +195,12 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - // FIXME - assert_eq!(res.msg_receipt.exit_code.value(), 33); + // FIXME + assert_eq!(res.msg_receipt.exit_code.value(), 33); - // FIXME: As propose is failing, we cannot execute the rest of the methods... - } + // FIXME: As propose is failing, we cannot execute the rest of the methods... } diff --git a/testing/tests/power.rs b/testing/tests/power.rs index 4f37aafd..4b44134d 100644 --- a/testing/tests/power.rs +++ b/testing/tests/power.rs @@ -1,262 +1,260 @@ -#[cfg(test)] -mod tests { - use bls_signatures::Serialize; - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_evm::Method as EvmMethods; - use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR,}; - use fvm::executor::{ApplyKind, Executor}; - use fvm::state_tree::ActorState; - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::BytesDe; - use fvm_ipld_encoding::CborStore; - use fvm_ipld_encoding::RawBytes; - use fvm_ipld_encoding::{strict_bytes, tuple::*}; - use fvm_shared::address::Address; - use fvm_shared::econ::TokenAmount; - use fvm_shared::message::Message; - use fvm_shared::sector::RegisteredPoStProof; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use multihash::Code; - use rand_core::OsRng; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use std::env; - use fvm::machine::Manifest; - - use testing::helpers::set_storagepower_actor; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/PowerApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] - pub struct CreateMinerParams { - pub owner: Address, - pub worker: Address, - pub window_post_proof_type: RegisteredPoStProof, - #[serde(with = "strict_bytes")] - pub peer: Vec, - pub multiaddrs: Vec, - } - - #[test] - fn power_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let sender: [Account; 1] = tester.create_accounts().unwrap(); - - // Set power actor - let state_tree = tester.state_tree.as_mut().unwrap(); - set_storagepower_actor(state_tree, *manifest.code_by_id(builtins::Type::Power as u32).unwrap()).unwrap(); - - /*********************************************** - * - * Instantiate Account Actor with a BLS address - * - ***********************************************/ - - let bls_private_key_provider = bls_signatures::PrivateKey::generate(&mut OsRng); - let worker = Address::new_bls(&bls_private_key_provider.public_key().as_bytes()).unwrap(); - - let state_tree = tester.state_tree.as_mut().unwrap(); - let assigned_addr = state_tree.register_new_address(&worker).unwrap(); - let state = fvm::account_actor::State { address: worker }; - - let cid = state_tree - .store() - .put_cbor(&state, Code::Blake2b256) - .unwrap(); - - let actor_state = ActorState { - code: *manifest.get_account_code(), - state: cid, - sequence: 0, - balance: TokenAmount::from_whole(1_000_000), - delegated_address: Some(worker), - }; - - state_tree.set_actor(assigned_addr, actor_state).unwrap(); - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - // Try to call "constructor" - println!("Try to call constructor on storage power actor"); - - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: STORAGE_POWER_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Create Miner actor to be able to claim power"); - - let constructor_params = CreateMinerParams { - owner: sender[0].1, - worker, - window_post_proof_type: fvm_shared::sector::RegisteredPoStProof::StackedDRGWindow2KiBV1, - peer: vec![1, 2, 3], - multiaddrs: vec![BytesDe(vec![1, 2, 3])], - }; - - let message = Message { - from: sender[0].1, - to: Address::new_id(4), - gas_limit: 1000000000, - method_num: 2, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 1, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Calling `miner_count`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 2, - params: RawBytes::new(hex::decode("4487A8D7D6").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000001" - ); - - println!("Calling `network_raw_power`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 3, - params: RawBytes::new(hex::decode("446C3D7356").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - - println!("Calling `miner_raw_power`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 4, - params: RawBytes::new( - hex::decode( - "58245D8543640000000000000000000000000000000000000000000000000000000000000066", - ) - .unwrap(), - ), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); - - println!("Calling `miner_consensus_count`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 5, - params: RawBytes::new(hex::decode("44DF16B842").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!( - hex::encode(res.msg_receipt.return_data.bytes()), - "58200000000000000000000000000000000000000000000000000000000000000000" - ); - } + +use bls_signatures::Serialize; +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::{ + runtime::builtins, EAM_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, +}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm::state_tree::ActorState; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::BytesDe; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_ipld_encoding::{strict_bytes, tuple::*}; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::sector::RegisteredPoStProof; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use multihash::Code; +use rand_core::OsRng; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::helpers::set_storagepower_actor; +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/PowerApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[derive(Serialize_tuple, Deserialize_tuple, Debug, Clone, Eq, PartialEq)] +pub struct CreateMinerParams { + pub owner: Address, + pub worker: Address, + pub window_post_proof_type: RegisteredPoStProof, + #[serde(with = "strict_bytes")] + pub peer: Vec, + pub multiaddrs: Vec, +} + +#[test] +fn power_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + let sender: [Account; 1] = tester.create_accounts().unwrap(); + + // Set power actor + let state_tree = tester.state_tree.as_mut().unwrap(); + set_storagepower_actor( + state_tree, + *manifest.code_by_id(builtins::Type::Power as u32).unwrap(), + ) + .unwrap(); + + /*********************************************** + * + * Instantiate Account Actor with a BLS address + * + ***********************************************/ + + let bls_private_key_provider = bls_signatures::PrivateKey::generate(&mut OsRng); + let worker = Address::new_bls(&bls_private_key_provider.public_key().as_bytes()).unwrap(); + + let state_tree = tester.state_tree.as_mut().unwrap(); + let assigned_addr = state_tree.register_new_address(&worker).unwrap(); + let state = fvm::account_actor::State { address: worker }; + + let cid = state_tree + .store() + .put_cbor(&state, Code::Blake2b256) + .unwrap(); + + let actor_state = ActorState { + code: *manifest.get_account_code(), + state: cid, + sequence: 0, + balance: TokenAmount::from_whole(1_000_000), + delegated_address: Some(worker), + }; + + state_tree.set_actor(assigned_addr, actor_state).unwrap(); + + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + // Try to call "constructor" + println!("Try to call constructor on storage power actor"); + + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: STORAGE_POWER_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Create Miner actor to be able to claim power"); + + let constructor_params = CreateMinerParams { + owner: sender[0].1, + worker, + window_post_proof_type: fvm_shared::sector::RegisteredPoStProof::StackedDRGWindow2KiBV1, + peer: vec![1, 2, 3], + multiaddrs: vec![BytesDe(vec![1, 2, 3])], + }; + + let message = Message { + from: sender[0].1, + to: Address::new_id(4), + gas_limit: 1000000000, + method_num: 2, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 1, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!("Calling `miner_count`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 2, + params: RawBytes::new(hex::decode("4487A8D7D6").unwrap()), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000001" + ); + + println!("Calling `network_raw_power`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 3, + params: RawBytes::new(hex::decode("446C3D7356").unwrap()), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + + println!("Calling `miner_raw_power`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 4, + params: RawBytes::new( + hex::decode( + "58245D8543640000000000000000000000000000000000000000000000000000000000000066", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + + println!("Calling `miner_consensus_count`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 5, + params: RawBytes::new(hex::decode("44DF16B842").unwrap()), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000000" + ); } diff --git a/testing/tests/precompiles.rs b/testing/tests/precompiles.rs index c74f5c8c..306215e4 100644 --- a/testing/tests/precompiles.rs +++ b/testing/tests/precompiles.rs @@ -1,114 +1,150 @@ -#[cfg(test)] -mod tests { - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::bundle; - use fvm_ipld_encoding::{strict_bytes}; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use fvm_ipld_blockstore::MemoryBlockstore; - use std::env; - use fvm_shared::address::Address; - use fvm_shared::message::Message; - use fvm::executor::{ApplyKind, Executor}; - use fil_actor_eam::Return; - use fil_actor_evm::{Method as EvmMethods}; - use fvm_ipld_encoding::RawBytes; - use fil_actors_runtime::{EAM_ACTOR_ADDR}; - use fvm_shared::econ::TokenAmount; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - - const WASM_COMPILED_PATH: &str = - "../build/v0.8/tests/PrecompilesApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[test] - fn precompiles_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car").expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens - // and make it a valid address to use. - let sender: [Account; 300] = tester.create_accounts().unwrap(); - - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[0].0, hex::encode(&sender[0].1.to_bytes()))); - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[1].0, hex::encode(&sender[1].1.to_bytes()))); - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[2].0, hex::encode(&sender[2].1.to_bytes()))); - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[3].0, hex::encode(&sender[3].1.to_bytes()))); - - // Create embryo address to deploy the contract on it (assign some FILs to it) - let tmp = hex::decode("DAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5").unwrap(); - let embryo_eth_address = tmp.as_slice(); - let embryo_delegated_address = Address::new_delegated(10, embryo_eth_address).unwrap(); - tester.create_placeholder(&embryo_delegated_address,TokenAmount::from_whole(100)).unwrap(); - - - println!("Embryo address delegated type [{}]", embryo_delegated_address); - println!("Embryo address delegated type on hex [{}]",hex::encode(embryo_delegated_address.to_bytes())); - // println!("Embryo address ID type on decimal [{}]",embryo_actor_id); - // println!("Embryo address ID type on hex [{}]",hex::encode(Address::new_id(embryo_actor_id).to_bytes())); - - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - - // First we deploy the contract in order to actually have an actor running on the embryo address - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 0, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - let exec_return : Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Contract address ID type on decimal [{}]",exec_return.actor_id); - println!("Contract address ID type on hex [{}]", hex::encode(Address::new_id(exec_return.actor_id).to_bytes())); - match exec_return.robust_address { - Some(addr) => println!("Contract address robust type [{}]",addr), - None => () - } +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::EAM_ACTOR_ADDR; +use fvm::executor::{ApplyKind, Executor}; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::strict_bytes; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/PrecompilesApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[test] +fn precompiles_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens + // and make it a valid address to use. + let sender: [Account; 300] = tester.create_accounts().unwrap(); + + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[0].0, + hex::encode(&sender[0].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[1].0, + hex::encode(&sender[1].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[2].0, + hex::encode(&sender[2].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[3].0, + hex::encode(&sender[3].1.to_bytes()) + ) + ); + + // Create embryo address to deploy the contract on it (assign some FILs to it) + let tmp = hex::decode("DAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5").unwrap(); + let embryo_eth_address = tmp.as_slice(); + let embryo_delegated_address = Address::new_delegated(10, embryo_eth_address).unwrap(); + tester + .create_placeholder(&embryo_delegated_address, TokenAmount::from_whole(100)) + .unwrap(); + + println!( + "Embryo address delegated type [{}]", + embryo_delegated_address + ); + println!( + "Embryo address delegated type on hex [{}]", + hex::encode(embryo_delegated_address.to_bytes()) + ); + // println!("Embryo address ID type on decimal [{}]",embryo_actor_id); + // println!("Embryo address ID type on hex [{}]",hex::encode(Address::new_id(embryo_actor_id).to_bytes())); + + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + // First we deploy the contract in order to actually have an actor running on the embryo address + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 0, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!( + "Contract address ID type on decimal [{}]", + exec_return.actor_id + ); + println!( + "Contract address ID type on hex [{}]", + hex::encode(Address::new_id(exec_return.actor_id).to_bytes()) + ); + match exec_return.robust_address { + Some(addr) => println!("Contract address robust type [{}]", addr), + None => (), + } - println!("Contract address eth address type [{}]",hex::encode(exec_return.eth_address.0)); + println!( + "Contract address eth address type [{}]", + hex::encode(exec_return.eth_address.0) + ); - let contract_actor_id = exec_return.actor_id; + let contract_actor_id = exec_return.actor_id; - println!("Calling `resolve_address`"); + println!("Calling `resolve_address`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -118,77 +154,94 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58200000000000000000000000000000000000000000000000000000000000000064"); - - - - println!("Calling `get_actor_type`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 2, - params: RawBytes::new(hex::decode("58247CF8C4440000000000000000000000000000000000000000000000000000000000000064").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58200000000000000000000000000000000000000000000000000000000000000003"); - - println!("Calling `lookup_delegated_address (empty response)`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 3, - params: RawBytes::new(hex::decode("58249898B39A0000000000000000000000000000000000000000000000000000000000000064").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "584000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); - - - println!("Calling `lookup_delegated_address (address found)`"); - - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 4, - params: RawBytes::new(hex::decode("58249898B39A0000000000000000000000000000000000000000000000000000000000000190").unwrap()), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "586000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000016040adafea492d9c6733ae3d56b7ed1adb60692c98bc500000000000000000000"); - - - println!("Calling `get_ripemd160_hash`"); - - let message = Message { + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000064" + ); + + println!("Calling `get_actor_type`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 2, + params: RawBytes::new( + hex::decode( + "58247CF8C4440000000000000000000000000000000000000000000000000000000000000064", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000003" + ); + + println!("Calling `lookup_delegated_address (empty response)`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 3, + params: RawBytes::new( + hex::decode( + "58249898B39A0000000000000000000000000000000000000000000000000000000000000064", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "584000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); + + println!("Calling `lookup_delegated_address (address found)`"); + + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 4, + params: RawBytes::new( + hex::decode( + "58249898B39A0000000000000000000000000000000000000000000000000000000000000190", + ) + .unwrap(), + ), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "586000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000016040adafea492d9c6733ae3d56b7ed1adb60692c98bc500000000000000000000"); + + println!("Calling `get_ripemd160_hash`"); + + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -198,16 +251,16 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001b8cf5bc0e542a1620184208f78c0cff516cce96"); + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001b8cf5bc0e542a1620184208f78c0cff516cce96"); - println!("Calling `resolve_eth_address`"); + println!("Calling `resolve_eth_address`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -217,12 +270,13 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58200000000000000000000000000000000000000000000000000000000000000191"); - - } + assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!( + hex::encode(res.msg_receipt.return_data.bytes()), + "58200000000000000000000000000000000000000000000000000000000000000191" + ); } diff --git a/testing/tests/send.rs b/testing/tests/send.rs index c0e88d2e..fcfc3ba6 100644 --- a/testing/tests/send.rs +++ b/testing/tests/send.rs @@ -1,119 +1,148 @@ -#[cfg(test)] -mod tests { - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::bundle; - use fvm_ipld_encoding::{strict_bytes}; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use fvm_ipld_blockstore::MemoryBlockstore; - use std::env; - use fvm_shared::address::Address; - use fvm_shared::message::Message; - use fvm::executor::{ApplyKind, Executor}; - use fil_actor_eam::Return; - use fil_actor_evm::{Method as EvmMethods}; - use fvm_ipld_encoding::RawBytes; - use fil_actors_runtime::{EAM_ACTOR_ADDR}; - use fvm_shared::econ::TokenAmount; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - - const WASM_COMPILED_PATH: &str = - "../build/v0.8/tests/SendApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[test] - fn send_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car").expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens - // and make it a valid address to use. - let sender: [Account; 300] = tester.create_accounts().unwrap(); - - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[0].0, hex::encode(&sender[0].1.to_bytes()))); - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[1].0, hex::encode(&sender[1].1.to_bytes()))); - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[2].0, hex::encode(&sender[2].1.to_bytes()))); - println!("{}", format!("Sender address id [{}] and bytes [{}]", &sender[3].0, hex::encode(&sender[3].1.to_bytes()))); - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - - // First we deploy the contract in order to actually have an actor running on the embryo address - println!("Calling init actor (EVM)"); - - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); - - let constructor_params = CreateExternalParams(evm_bin); - - let message = Message { - from: sender[0].1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 0, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; - - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); - - assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let exec_return : Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - - println!("Contract address ID type on decimal [{}]",exec_return.actor_id); - println!("Contract address ID type on hex [{}]", hex::encode(Address::new_id(exec_return.actor_id).to_bytes())); - match exec_return.robust_address { - Some(addr) => println!("Contract address robust type [{}]",addr), - None => () - } - println!("Contract address eth address type [{}]",hex::encode(exec_return.eth_address.0)); +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::EAM_ACTOR_ADDR; +use fvm::executor::{ApplyKind, Executor}; +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::strict_bytes; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::econ::TokenAmount; +use fvm_shared::message::Message; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/SendApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[test] +fn send_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens + // and make it a valid address to use. + let sender: [Account; 300] = tester.create_accounts().unwrap(); + + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[0].0, + hex::encode(&sender[0].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[1].0, + hex::encode(&sender[1].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[2].0, + hex::encode(&sender[2].1.to_bytes()) + ) + ); + println!( + "{}", + format!( + "Sender address id [{}] and bytes [{}]", + &sender[3].0, + hex::encode(&sender[3].1.to_bytes()) + ) + ); + + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + // First we deploy the contract in order to actually have an actor running on the embryo address + println!("Calling init actor (EVM)"); + + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); + + let constructor_params = CreateExternalParams(evm_bin); + + let message = Message { + from: sender[0].1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 0, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; + + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); + + assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + + println!( + "Contract address ID type on decimal [{}]", + exec_return.actor_id + ); + println!( + "Contract address ID type on hex [{}]", + hex::encode(Address::new_id(exec_return.actor_id).to_bytes()) + ); + match exec_return.robust_address { + Some(addr) => println!("Contract address robust type [{}]", addr), + None => (), + } - let contract_actor_id = exec_return.actor_id; + println!( + "Contract address eth address type [{}]", + hex::encode(exec_return.eth_address.0) + ); + let contract_actor_id = exec_return.actor_id; - // Send some tokens to the smart contract - let message = Message { - from: sender[0].1, - to: Address::new_id(contract_actor_id), - gas_limit: 1000000000, - method_num: 0, - value: TokenAmount::from_atto(100), - sequence: 1, - ..Message::default() - }; + // Send some tokens to the smart contract + let message = Message { + from: sender[0].1, + to: Address::new_id(contract_actor_id), + gas_limit: 1000000000, + method_num: 0, + value: TokenAmount::from_atto(100), + sequence: 1, + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `send`"); + println!("Calling `send`"); - let message = Message { + let message = Message { from: sender[0].1, to: Address::new_id(contract_actor_id), gas_limit: 1000000000, @@ -123,10 +152,9 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); - } + assert_eq!(res.msg_receipt.exit_code.value(), 0); } diff --git a/testing/tests/verifreg.rs b/testing/tests/verifreg.rs index d1ad7629..014967aa 100644 --- a/testing/tests/verifreg.rs +++ b/testing/tests/verifreg.rs @@ -1,187 +1,181 @@ -#[cfg(test)] -mod tests { - use fvm_integration_tests::bundle; - use fvm_integration_tests::dummy::DummyExterns; - use fvm_integration_tests::tester::{Account, Tester}; - use fvm_ipld_encoding::{strict_bytes, tuple::*}; - use fvm_shared::bigint::bigint_ser; - - use cid::Cid; - use fil_actor_eam::Return; - use fil_actor_evm::Method as EvmMethods; - use fil_actors_runtime::{ - runtime::builtins, DATACAP_TOKEN_ACTOR_ADDR, EAM_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, - VERIFIED_REGISTRY_ACTOR_ADDR, - }; - use fvm::executor::{ApplyKind, Executor}; - use fvm::machine::Manifest; - use fvm_ipld_blockstore::MemoryBlockstore; - use fvm_ipld_encoding::CborStore; - use fvm_ipld_encoding::RawBytes; - use fvm_shared::address::Address; - use fvm_shared::message::Message; - use fvm_shared::sector::StoragePower; - use fvm_shared::state::StateTreeVersion; - use fvm_shared::version::NetworkVersion; - use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; - use std::env; - use testing::helpers; - - const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/VerifRegApiTest.bin"; - - #[derive(SerdeSerialize, SerdeDeserialize)] - #[serde(transparent)] - pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); - - #[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] - pub struct VerifierParams { - pub address: Address, - #[serde(with = "bigint_ser")] - pub allowance: StoragePower, - } - - #[test] - fn verifreg_tests() { - println!("Testing solidity API"); - - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); - - let accounts: [Account; 2] = tester.create_accounts().unwrap(); - let (sender, _verified_client) = (accounts[0], accounts[1]); - // register address so we can use senderID 199 which is the governor address of the verifreg - // actor - let _accounts: [Account; 200] = tester.create_accounts().unwrap(); - - // Set verifreg actor - let state_tree = tester.state_tree.as_mut().unwrap(); - helpers::set_verifiedregistry_actor( - state_tree, - *manifest - .code_by_id(builtins::Type::VerifiedRegistry as u32) - .unwrap(), - ) - .unwrap(); - helpers::set_datacap_actor( - state_tree, - *manifest.code_by_id(builtins::Type::DataCap as u32).unwrap(), - ) - .unwrap(); - - // Instantiate machine - tester.instantiate_machine(DummyExterns).unwrap(); - - let executor = tester.executor.as_mut().unwrap(); - - // Try to call "constructor" - println!("Try to call constructor on storage verifreg actor"); - let root_key = Address::new_id(199); +use fvm_integration_tests::bundle; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::{Account, Tester}; +use fvm_ipld_encoding::{strict_bytes, tuple::*}; +use fvm_shared::bigint::bigint_ser; + +use cid::Cid; +use fil_actor_eam::Return; +use fil_actor_evm::Method as EvmMethods; +use fil_actors_runtime::{ + runtime::builtins, DATACAP_TOKEN_ACTOR_ADDR, EAM_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, + VERIFIED_REGISTRY_ACTOR_ADDR, +}; +use fvm::executor::{ApplyKind, Executor}; +use fvm::machine::Manifest; +use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_ipld_encoding::CborStore; +use fvm_ipld_encoding::RawBytes; +use fvm_shared::address::Address; +use fvm_shared::message::Message; +use fvm_shared::sector::StoragePower; +use fvm_shared::state::StateTreeVersion; +use fvm_shared::version::NetworkVersion; +use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; + +use testing::helpers; +use testing::setup; + +const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/VerifRegApiTest.bin"; + +#[derive(SerdeSerialize, SerdeDeserialize)] +#[serde(transparent)] +pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); + +#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] +pub struct VerifierParams { + pub address: Address, + #[serde(with = "bigint_ser")] + pub allowance: StoragePower, +} - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: VERIFIED_REGISTRY_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - params: RawBytes::serialize(root_key).unwrap(), - ..Message::default() - }; +#[test] +fn verifreg_tests() { + println!("Testing solidity API"); + + let bs = MemoryBlockstore::default(); + let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") + .expect("Unable to read actor devnet file file"); + let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); + + let (manifest_version, manifest_data_cid): (u32, Cid) = + bs.get_cbor(&bundle_root).unwrap().unwrap(); + let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + + let mut tester = + Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + + let accounts: [Account; 2] = tester.create_accounts().unwrap(); + let (sender, _verified_client) = (accounts[0], accounts[1]); + // register address so we can use senderID 199 which is the governor address of the verifreg + // actor + let _accounts: [Account; 200] = tester.create_accounts().unwrap(); + + // Set verifreg actor + let state_tree = tester.state_tree.as_mut().unwrap(); + helpers::set_verifiedregistry_actor( + state_tree, + *manifest + .code_by_id(builtins::Type::VerifiedRegistry as u32) + .unwrap(), + ) + .unwrap(); + helpers::set_datacap_actor( + state_tree, + *manifest.code_by_id(builtins::Type::DataCap as u32).unwrap(), + ) + .unwrap(); + + // Instantiate machine + tester.instantiate_machine(DummyExterns).unwrap(); + + let executor = tester.executor.as_mut().unwrap(); + + // Try to call "constructor" + println!("Try to call constructor on storage verifreg actor"); + + let root_key = Address::new_id(199); + + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: VERIFIED_REGISTRY_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + params: RawBytes::serialize(root_key).unwrap(), + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - // Try to call "constructor" - println!("Try to call constructor on datacap actor"); + // Try to call "constructor" + println!("Try to call constructor on datacap actor"); - let message = Message { - from: SYSTEM_ACTOR_ADDR, - to: DATACAP_TOKEN_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 1, - params: RawBytes::serialize(VERIFIED_REGISTRY_ACTOR_ADDR).unwrap(), - ..Message::default() - }; + let message = Message { + from: SYSTEM_ACTOR_ADDR, + to: DATACAP_TOKEN_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 1, + params: RawBytes::serialize(VERIFIED_REGISTRY_ACTOR_ADDR).unwrap(), + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Implicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Implicit, 100) + .unwrap(); - dbg!(&res); + dbg!(&res); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling init actor (EVM)"); + println!("Calling init actor (EVM)"); - let wasm_path = env::current_dir() - .unwrap() - .join(WASM_COMPILED_PATH) - .canonicalize() - .unwrap(); - let evm_hex = std::fs::read(wasm_path).expect("Unable to read file"); - let evm_bin = hex::decode(evm_hex).unwrap(); + let evm_bin = setup::load_evm(WASM_COMPILED_PATH); - let constructor_params = CreateExternalParams(evm_bin); + let constructor_params = CreateExternalParams(evm_bin); - let message = Message { - from: sender.1, - to: EAM_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 4, - sequence: 0, - params: RawBytes::serialize(constructor_params).unwrap(), - ..Message::default() - }; + let message = Message { + from: sender.1, + to: EAM_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 4, + sequence: 0, + params: RawBytes::serialize(constructor_params).unwrap(), + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); //use fil_actors + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); //use fil_actors - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); - let contract_actor = exec_return.actor_id; + let exec_return: Return = RawBytes::deserialize(&res.msg_receipt.return_data).unwrap(); + let contract_actor = exec_return.actor_id; - let verifier_allowance = fvm_shared::sector::StoragePower::from(2 * 1024u64); - let params = VerifierParams { - address: Address::new_id(contract_actor), - allowance: verifier_allowance, - }; + let verifier_allowance = fvm_shared::sector::StoragePower::from(2 * 1024u64); + let params = VerifierParams { + address: Address::new_id(contract_actor), + allowance: verifier_allowance, + }; - println!("Registering contract-actor as verifier"); - // by this call we register our contract actor as a verifier - let message = Message { - //from: sender.1, - from: Address::new_id(199), - to: VERIFIED_REGISTRY_ACTOR_ADDR, - gas_limit: 1000000000, - method_num: 2, - sequence: 0, - params: RawBytes::serialize(params).unwrap(), - ..Message::default() - }; + println!("Registering contract-actor as verifier"); + // by this call we register our contract actor as a verifier + let message = Message { + //from: sender.1, + from: Address::new_id(199), + to: VERIFIED_REGISTRY_ACTOR_ADDR, + gas_limit: 1000000000, + method_num: 2, + sequence: 0, + params: RawBytes::serialize(params).unwrap(), + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - dbg!(&res); + dbg!(&res); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `add_verified_client`"); - let message = Message { + println!("Calling `add_verified_client`"); + let message = Message { from: sender.1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -191,15 +185,15 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `get_claims`"); + println!("Calling `get_claims`"); - let message = Message { + let message = Message { from: sender.1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -210,15 +204,15 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - // Should not fail as actor would return an empty list of claims - assert_eq!(res.msg_receipt.exit_code.value(), 0); + // Should not fail as actor would return an empty list of claims + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `extend_claim_term`"); - let message = Message { + println!("Calling `extend_claim_term`"); + let message = Message { from: sender.1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -228,15 +222,15 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `remove_expired_allocations`"); + println!("Calling `remove_expired_allocations`"); - let message = Message { + let message = Message { from: sender.1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -247,14 +241,14 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - println!("Calling `remove_expired_claims`"); - let message = Message { + println!("Calling `remove_expired_claims`"); + let message = Message { from: sender.1, to: Address::new_id(exec_return.actor_id), gas_limit: 1000000000, @@ -264,32 +258,31 @@ mod tests { ..Message::default() }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0); + assert_eq!(res.msg_receipt.exit_code.value(), 0); - /* - universal_receiver_hook in Verifreg can only be call by DataCap actor. + /* + universal_receiver_hook in Verifreg can only be call by DataCap actor. - TODO: investigate how it is supposed to work. + TODO: investigate how it is supposed to work. - println!("Calling `universal_receiver_hook`"); - let message = Message { - from: sender.1, - to: Address::new_id(exec_return.actor_id), - gas_limit: 1000000000, - method_num: EvmMethods::InvokeContract as u64, - sequence: 6, - params: RawBytes::new(hex::decode("58A4AA7E0F7B000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000C9000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000031245AB0000000000000000000000000000000000000000000000000000000000").unwrap()), - ..Message::default() - }; + println!("Calling `universal_receiver_hook`"); + let message = Message { + from: sender.1, + to: Address::new_id(exec_return.actor_id), + gas_limit: 1000000000, + method_num: EvmMethods::InvokeContract as u64, + sequence: 6, + params: RawBytes::new(hex::decode("58A4AA7E0F7B000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000C9000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000031245AB0000000000000000000000000000000000000000000000000000000000").unwrap()), + ..Message::default() + }; - let res = executor - .execute_message(message, ApplyKind::Explicit, 100) - .unwrap(); + let res = executor + .execute_message(message, ApplyKind::Explicit, 100) + .unwrap(); - assert_eq!(res.msg_receipt.exit_code.value(), 0);*/ - } + assert_eq!(res.msg_receipt.exit_code.value(), 0);*/ } From 944861a781f95c5b4a13d8ead611fa0efb6d50ac Mon Sep 17 00:00:00 2001 From: rllola Date: Wed, 8 Feb 2023 16:20:43 +0100 Subject: [PATCH 2/2] show gas usage everywhere --- testing/src/lib.rs | 17 +++++++++++++ testing/tests/account.rs | 15 ++---------- testing/tests/datacap.rs | 43 +++++++++++++++------------------ testing/tests/init.rs | 31 ++++++++---------------- testing/tests/market.rs | 46 ++++++++++++++++++++--------------- testing/tests/miner.rs | 47 ++++++++++++++++++++---------------- testing/tests/multisig.rs | 28 +++++++-------------- testing/tests/power.rs | 30 +++++++++-------------- testing/tests/precompiles.rs | 32 ++++++++++++++---------- testing/tests/send.rs | 21 ++++++---------- testing/tests/verifreg.rs | 44 ++++++++++++++------------------- 11 files changed, 167 insertions(+), 187 deletions(-) diff --git a/testing/src/lib.rs b/testing/src/lib.rs index 90397cff..b077a6ca 100644 --- a/testing/src/lib.rs +++ b/testing/src/lib.rs @@ -1,2 +1,19 @@ +#[macro_use] +extern crate prettytable; + pub mod helpers; pub mod setup; + +use prettytable::Table; + +pub type GasResult = Vec<(String, i64)>; + +pub fn create_gas_table(gas_result: GasResult) -> Table { + let mut table = Table::new(); + table.add_row(row!["Function", "Gas"]); + gas_result.iter().for_each(|(description, gas)| { + table.add_row(row![description, gas]); + }); + + table +} \ No newline at end of file diff --git a/testing/tests/account.rs b/testing/tests/account.rs index fd16cbbb..3610fec5 100644 --- a/testing/tests/account.rs +++ b/testing/tests/account.rs @@ -18,12 +18,7 @@ use multihash::Code; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::setup; - -#[macro_use] -extern crate prettytable; -use prettytable::Table; - -type GasResult = Vec<(String, i64)>; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/AccountApiTest.bin"; @@ -201,12 +196,6 @@ fn account_tests() { gas_result.push(("universal_receiver_hook (actor ID)".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); - - let mut table = Table::new(); - table.add_row(row!["Function", "Gas"]); - gas_result.iter().for_each(|(description, gas)| { - table.add_row(row![description, gas]); - }); - + let table = testing::create_gas_table(gas_result); table.printstd(); } diff --git a/testing/tests/datacap.rs b/testing/tests/datacap.rs index 9bf1a628..f79eb6b3 100644 --- a/testing/tests/datacap.rs +++ b/testing/tests/datacap.rs @@ -1,5 +1,3 @@ - -use cid::Cid; use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actors_runtime::{ @@ -7,24 +5,18 @@ use fil_actors_runtime::{ VERIFIED_REGISTRY_ACTOR_ADDR, }; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::strict_bytes; -use fvm_ipld_encoding::CborStore; use fvm_ipld_encoding::RawBytes; use fvm_shared::address::Address; use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; -use std::env; use testing::helpers; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/DataCapApiTest.bin"; @@ -36,17 +28,8 @@ pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); fn datacap_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, manifest) = setup::setup_tester(); // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens // and make it a valid address to use. @@ -274,6 +257,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("name".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000074461746143617000000000000000000000000000000000000000000000000000"); @@ -293,6 +277,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("symbol".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000044443415000000000000000000000000000000000000000000000000000000000"); @@ -312,6 +297,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("total_supply".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000096c6b935b8bbd4000000000000000000000000000000000000000000000000000"); @@ -331,6 +317,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("balance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -350,6 +337,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("allowance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -369,8 +357,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); - dbg!(&res); - + gas_result.push(("transfer".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009361a08405e8fd8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000081bc16d674ec800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -390,6 +377,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("transfer_from".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5902400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000935fe46d2f741100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000853444835ec58000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc427e27bb1627000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -409,6 +397,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("burn".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009360c2789aae87400000000000000000000000000000000000000000000000000"); @@ -428,6 +417,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("burn_from".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000935f0661c4399ac0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc41a01c4fdbb0c0000000000000000000000000000000000"); @@ -447,6 +437,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("allowance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc41a01c4fdbb0c0000000000000000000000000000000000"); @@ -466,6 +457,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("increase_allowance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acfa952f0445dea00000000000000000000000000000000000"); @@ -485,6 +477,7 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("decrease_allowance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc45f65568000000000000000000000000000000000000000"); @@ -504,6 +497,10 @@ fn datacap_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("revoke_allowance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102f050fe938943acc45f65568000000000000000000000000000000000000000"); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/init.rs b/testing/tests/init.rs index fd5bd86c..8f883a0f 100644 --- a/testing/tests/init.rs +++ b/testing/tests/init.rs @@ -1,25 +1,17 @@ - -use cid::Cid; use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR}; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::strict_bytes; -use fvm_ipld_encoding::CborStore; use fvm_ipld_encoding::RawBytes; use fvm_shared::address::Address; use fvm_shared::message::Message; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; -use std::env; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/InitApiTest.bin"; @@ -31,17 +23,9 @@ pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); fn init_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); + let mut gas_result: GasResult = vec![]; - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let (mut tester, manifest) = setup::setup_tester(); let sender: [Account; 1] = tester.create_accounts().unwrap(); @@ -103,7 +87,7 @@ fn init_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); - dbg!(&res); + gas_result.push(("exec".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020066000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001502a526fc35cb62e5b1810ab92c02e0b37c8e7f47dd0000000000000000000000"); @@ -129,7 +113,12 @@ fn init_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("exec4".into(), res.msg_receipt.gas_used)); + // FIXME: "caller f0101 of method 3 must be built-in" // However there is no exported method for EXEC4 yet. Tests cant pass until the builtin-actors export the exec4 method. assert_eq!(res.msg_receipt.exit_code.value(), 33); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/market.rs b/testing/tests/market.rs index af46807c..6af19b28 100644 --- a/testing/tests/market.rs +++ b/testing/tests/market.rs @@ -1,4 +1,3 @@ - use bls_signatures::Serialize; use cid::Cid; use fil_actor_eam::Return; @@ -8,12 +7,9 @@ use fil_actors_runtime::{ STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, }; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; use fvm::state_tree::ActorState; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::BytesDe; use fvm_ipld_encoding::BytesSer; use fvm_ipld_encoding::CborStore; @@ -26,17 +22,15 @@ use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; use fvm_shared::piece::PaddedPieceSize; use fvm_shared::sector::RegisteredPoStProof; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use libipld_core::ipld::Ipld; use multihash::Code; use rand_core::OsRng; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; -use std::env; use std::str::FromStr; use testing::helpers; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MarketApiTest.bin"; @@ -139,17 +133,8 @@ pub struct AuthenticateMessageParams { fn market_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, manifest) = setup::setup_tester(); let sender: [Account; 1] = tester.create_accounts().unwrap(); //let client: [Account; 1] = tester.create_accounts().unwrap(); @@ -453,6 +438,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("add_balance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -472,6 +458,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("withdraw_balance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016400000000000000000000000000000000000000000000000000000000000000"); @@ -491,6 +478,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_balance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5901600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009056bc75e2d63100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000807f3556c02eb7800000000000000000000000000000000000000000000000000"); @@ -515,6 +503,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_data_commitment".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000028000181e203922020b51bcc94bb0977c984c093770289dea4e83ef08c355145d412c6673e06152a09000000000000000000000000000000000000000000000000"); @@ -539,6 +528,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_client".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -566,6 +556,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_provider".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -593,6 +584,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_label".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000346d41584367354149673859425862466a7464427931695a6a704459417752537430656c474c463547765471756c4569693156634d000000000000000000000000"); @@ -617,6 +609,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_term".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5840000000000000000000000000000000000000000000000000000000000000629d000000000000000000000000000000000000000000000000000000000007eee1"); @@ -641,6 +634,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_total_price".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000807efc7ed5e24f800000000000000000000000000000000000000000000000000"); @@ -665,6 +659,10 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(( + "get_deal_client_collateral".into(), + res.msg_receipt.gas_used, + )); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007038d7ea4c6800000000000000000000000000000000000000000000000000000"); @@ -689,6 +687,10 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(( + "get_deal_provider_collateral".into(), + res.msg_receipt.gas_used, + )); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007038d7ea4c6800000000000000000000000000000000000000000000000000000"); @@ -713,6 +715,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_verified".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -740,6 +743,7 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_deal_activation".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "584000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -764,6 +768,10 @@ fn market_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("publish_storage_deals".into(), res.msg_receipt.gas_used)); // FIXME assert_eq!(res.msg_receipt.exit_code.value(), 33); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/miner.rs b/testing/tests/miner.rs index 95176b75..11c5c5ce 100644 --- a/testing/tests/miner.rs +++ b/testing/tests/miner.rs @@ -1,30 +1,24 @@ - use bls_signatures::Serialize; -use cid::Cid; use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actor_init::ExecReturn; use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, INIT_ACTOR_ADDR}; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; use fvm::state_tree::ActorState; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::CborStore; use fvm_ipld_encoding::RawBytes; use fvm_ipld_encoding::{strict_bytes, tuple::*, BytesDe}; use fvm_shared::address::Address; use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use multihash::Code; use rand_core::OsRng; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MinerApiTest.bin"; @@ -41,17 +35,8 @@ pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); fn miner_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, manifest) = setup::setup_tester(); let sender: [Account; 1] = tester.create_accounts().unwrap(); @@ -171,8 +156,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); - dbg!(&res); - + gas_result.push(("change_owner_address".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -192,6 +176,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_beneficiary".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5903200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000020067000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -211,6 +196,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("change_beneficiary".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -230,6 +216,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_owner".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002006700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020066000000000000000000000000000000000000000000000000000000000000"); @@ -249,6 +236,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_available_balance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -268,6 +256,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_vesting_funds".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); @@ -287,6 +276,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("repay_debt".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -306,6 +296,10 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(( + "confirm_change_worker_address".into(), + res.msg_receipt.gas_used, + )); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -325,6 +319,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_peer_id".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"); @@ -344,6 +339,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_multiaddresses".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000"); @@ -363,6 +359,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("change_worker_address".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -382,6 +379,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("is_controlling_address".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -404,6 +402,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_sector_size".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -426,6 +425,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("change_multiaddresses".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -445,6 +445,7 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("change_peer_id".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "40"); @@ -464,6 +465,10 @@ fn miner_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("withdraw_balance".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/multisig.rs b/testing/tests/multisig.rs index e6eb4bc2..13c725dc 100644 --- a/testing/tests/multisig.rs +++ b/testing/tests/multisig.rs @@ -1,30 +1,24 @@ - use bls_signatures::Serialize; -use cid::Cid; use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actor_init::ExecReturn; use fil_actors_runtime::{runtime::builtins, EAM_ACTOR_ADDR, INIT_ACTOR_ADDR}; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; use fvm::state_tree::ActorState; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::strict_bytes; use fvm_ipld_encoding::CborStore; use fvm_ipld_encoding::RawBytes; use fvm_shared::address::Address; use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use multihash::Code; use rand_core::OsRng; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/MultisigApiTest.bin"; @@ -36,17 +30,8 @@ pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); fn multisig_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, manifest) = setup::setup_tester(); let sender: [Account; 1] = tester.create_accounts().unwrap(); @@ -180,6 +165,7 @@ fn multisig_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("propose".into(), res.msg_receipt.gas_used)); // FIXME assert_eq!(res.msg_receipt.exit_code.value(), 33); @@ -199,8 +185,12 @@ fn multisig_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("add_signer".into(), res.msg_receipt.gas_used)); // FIXME assert_eq!(res.msg_receipt.exit_code.value(), 33); // FIXME: As propose is failing, we cannot execute the rest of the methods... + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/power.rs b/testing/tests/power.rs index 4b44134d..8b757559 100644 --- a/testing/tests/power.rs +++ b/testing/tests/power.rs @@ -1,18 +1,13 @@ - use bls_signatures::Serialize; -use cid::Cid; use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actors_runtime::{ runtime::builtins, EAM_ACTOR_ADDR, STORAGE_POWER_ACTOR_ADDR, SYSTEM_ACTOR_ADDR, }; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; use fvm::state_tree::ActorState; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::BytesDe; use fvm_ipld_encoding::CborStore; use fvm_ipld_encoding::RawBytes; @@ -21,14 +16,13 @@ use fvm_shared::address::Address; use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; use fvm_shared::sector::RegisteredPoStProof; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use multihash::Code; use rand_core::OsRng; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::helpers::set_storagepower_actor; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/PowerApiTest.bin"; @@ -50,17 +44,8 @@ pub struct CreateMinerParams { fn power_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, manifest) = setup::setup_tester(); let sender: [Account; 1] = tester.create_accounts().unwrap(); @@ -187,6 +172,7 @@ fn power_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("miner_count".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -209,6 +195,7 @@ fn power_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("network_raw_power".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -233,6 +220,7 @@ fn power_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("miner_raw_power".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "58c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); @@ -252,9 +240,13 @@ fn power_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("miner_consensus_count".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), "58200000000000000000000000000000000000000000000000000000000000000000" ); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/precompiles.rs b/testing/tests/precompiles.rs index 306215e4..cc804c7d 100644 --- a/testing/tests/precompiles.rs +++ b/testing/tests/precompiles.rs @@ -1,22 +1,18 @@ - use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actors_runtime::EAM_ACTOR_ADDR; use fvm::executor::{ApplyKind, Executor}; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::strict_bytes; use fvm_ipld_encoding::RawBytes; use fvm_shared::address::Address; use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/PrecompilesApiTest.bin"; @@ -28,13 +24,8 @@ pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); fn precompiles_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, _manifest) = setup::setup_tester(); // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens // and make it a valid address to use. @@ -158,6 +149,7 @@ fn precompiles_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("resolve_address".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -185,6 +177,7 @@ fn precompiles_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_actor_type".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), @@ -212,6 +205,10 @@ fn precompiles_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(( + "lookup_delegated_address (empty response)".into(), + res.msg_receipt.gas_used, + )); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "584000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"); @@ -236,6 +233,10 @@ fn precompiles_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(( + "lookup_delegated_address (address found)".into(), + res.msg_receipt.gas_used, + )); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "586000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000016040adafea492d9c6733ae3d56b7ed1adb60692c98bc500000000000000000000"); @@ -255,6 +256,7 @@ fn precompiles_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_ripemd160_hash".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!(hex::encode(res.msg_receipt.return_data.bytes()), "5860000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001b8cf5bc0e542a1620184208f78c0cff516cce96"); @@ -274,9 +276,13 @@ fn precompiles_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("resolve_eth_address".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); assert_eq!( hex::encode(res.msg_receipt.return_data.bytes()), "58200000000000000000000000000000000000000000000000000000000000000191" ); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/send.rs b/testing/tests/send.rs index fcfc3ba6..a9ff50ae 100644 --- a/testing/tests/send.rs +++ b/testing/tests/send.rs @@ -1,22 +1,18 @@ - use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actors_runtime::EAM_ACTOR_ADDR; use fvm::executor::{ApplyKind, Executor}; -use fvm_integration_tests::bundle; use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_blockstore::MemoryBlockstore; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::strict_bytes; use fvm_ipld_encoding::RawBytes; use fvm_shared::address::Address; use fvm_shared::econ::TokenAmount; use fvm_shared::message::Message; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/SendApiTest.bin"; @@ -28,13 +24,8 @@ pub struct CreateExternalParams(#[serde(with = "strict_bytes")] pub Vec); fn send_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, _manifest) = setup::setup_tester(); // As the governor address for datacap is 200, we create many many address in order to initialize the ID 200 with some tokens // and make it a valid address to use. @@ -156,5 +147,9 @@ fn send_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("send".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); + + let table = testing::create_gas_table(gas_result); + table.printstd(); } diff --git a/testing/tests/verifreg.rs b/testing/tests/verifreg.rs index 014967aa..273a151a 100644 --- a/testing/tests/verifreg.rs +++ b/testing/tests/verifreg.rs @@ -1,11 +1,3 @@ - -use fvm_integration_tests::bundle; -use fvm_integration_tests::dummy::DummyExterns; -use fvm_integration_tests::tester::{Account, Tester}; -use fvm_ipld_encoding::{strict_bytes, tuple::*}; -use fvm_shared::bigint::bigint_ser; - -use cid::Cid; use fil_actor_eam::Return; use fil_actor_evm::Method as EvmMethods; use fil_actors_runtime::{ @@ -13,19 +5,19 @@ use fil_actors_runtime::{ VERIFIED_REGISTRY_ACTOR_ADDR, }; use fvm::executor::{ApplyKind, Executor}; -use fvm::machine::Manifest; -use fvm_ipld_blockstore::MemoryBlockstore; -use fvm_ipld_encoding::CborStore; +use fvm_integration_tests::dummy::DummyExterns; +use fvm_integration_tests::tester::Account; use fvm_ipld_encoding::RawBytes; +use fvm_ipld_encoding::{strict_bytes, tuple::*}; use fvm_shared::address::Address; +use fvm_shared::bigint::bigint_ser; use fvm_shared::message::Message; use fvm_shared::sector::StoragePower; -use fvm_shared::state::StateTreeVersion; -use fvm_shared::version::NetworkVersion; use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize}; use testing::helpers; use testing::setup; +use testing::GasResult; const WASM_COMPILED_PATH: &str = "../build/v0.8/tests/VerifRegApiTest.bin"; @@ -44,17 +36,8 @@ pub struct VerifierParams { fn verifreg_tests() { println!("Testing solidity API"); - let bs = MemoryBlockstore::default(); - let actors = std::fs::read("./builtin-actors/output/builtin-actors-devnet-wasm.car") - .expect("Unable to read actor devnet file file"); - let bundle_root = bundle::import_bundle(&bs, &actors).unwrap(); - - let (manifest_version, manifest_data_cid): (u32, Cid) = - bs.get_cbor(&bundle_root).unwrap().unwrap(); - let manifest = Manifest::load(&bs, &manifest_data_cid, manifest_version).unwrap(); - - let mut tester = - Tester::new(NetworkVersion::V18, StateTreeVersion::V5, bundle_root, bs).unwrap(); + let mut gas_result: GasResult = vec![]; + let (mut tester, manifest) = setup::setup_tester(); let accounts: [Account; 2] = tester.create_accounts().unwrap(); let (sender, _verified_client) = (accounts[0], accounts[1]); @@ -170,8 +153,6 @@ fn verifreg_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); - dbg!(&res); - assert_eq!(res.msg_receipt.exit_code.value(), 0); println!("Calling `add_verified_client`"); @@ -189,6 +170,7 @@ fn verifreg_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("add_verified_client".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); println!("Calling `get_claims`"); @@ -208,6 +190,7 @@ fn verifreg_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("get_claims".into(), res.msg_receipt.gas_used)); // Should not fail as actor would return an empty list of claims assert_eq!(res.msg_receipt.exit_code.value(), 0); @@ -226,6 +209,7 @@ fn verifreg_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("extend_claim_term".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); println!("Calling `remove_expired_allocations`"); @@ -245,6 +229,10 @@ fn verifreg_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(( + "remove_expired_allocations".into(), + res.msg_receipt.gas_used, + )); assert_eq!(res.msg_receipt.exit_code.value(), 0); println!("Calling `remove_expired_claims`"); @@ -262,6 +250,7 @@ fn verifreg_tests() { .execute_message(message, ApplyKind::Explicit, 100) .unwrap(); + gas_result.push(("remove_expired_claims".into(), res.msg_receipt.gas_used)); assert_eq!(res.msg_receipt.exit_code.value(), 0); /* @@ -285,4 +274,7 @@ fn verifreg_tests() { .unwrap(); assert_eq!(res.msg_receipt.exit_code.value(), 0);*/ + + let table = testing::create_gas_table(gas_result); + table.printstd(); }