diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 6ca51f67..91cfe5e3 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -17,7 +17,7 @@ version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97" dependencies = [ - "gimli 0.27.0", + "gimli 0.27.1", ] [[package]] @@ -95,9 +95,9 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" [[package]] name = "async-trait" -version = "0.1.61" +version = "0.1.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "705339e0e4a9690e2908d2b3d049d85682cf19fbd5782494498fbf7003a6a282" +checksum = "1cd7fce9ba8c3c042128ce72d8b2ddbf3a05747efb67ea0313c635e10bda47a2" dependencies = [ "proc-macro2", "quote", @@ -132,7 +132,7 @@ dependencies = [ "cfg-if 1.0.0", "libc", "miniz_oxide", - "object 0.30.2", + "object 0.30.3", "rustc-demangle", ] @@ -389,18 +389,18 @@ dependencies = [ [[package]] name = "cbor4ii" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebce4be718fce03915a0a6220eaca81becdab6485d6ad57d483ed6ff76def7b7" +checksum = "b544cf8c89359205f4f990d0e6f3828db42df85b5dac95d09157a250eb0749c4" dependencies = [ "serde", ] [[package]] name = "cc" -version = "1.0.78" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" [[package]] name = "cfg-if" @@ -793,9 +793,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d1075c37807dcf850c379432f0df05ba52cc30f279c5cfc43cc221ce7f8579" +checksum = "322296e2f2e5af4270b54df9e85a02ff037e271af20ba3e7fe1575515dc840b8" dependencies = [ "cc", "cxxbridge-flags", @@ -805,9 +805,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5044281f61b27bc598f2f6647d480aed48d2bf52d6eb0b627d84c0361b17aa70" +checksum = "017a1385b05d631e7875b1f151c9f012d37b53491e2a87f65bff5c262b2111d8" dependencies = [ "cc", "codespan-reporting", @@ -820,15 +820,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61b50bc93ba22c27b0d31128d2d130a0a6b3d267ae27ef7e4fae2167dfe8781c" +checksum = "c26bbb078acf09bc1ecda02d4223f03bdd28bd4874edcb0379138efc499ce971" [[package]] name = "cxxbridge-macro" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e61fda7e62115119469c7b3591fd913ecca96fb766cfd3f2e2502ab7bc87a5" +checksum = "357f40d1f06a24b60ae1fe122542c1fb05d28d32acb2aed064e84bc2ad1e252e" dependencies = [ "proc-macro2", "quote", @@ -1080,9 +1080,9 @@ dependencies = [ [[package]] name = "either" -version = "1.8.0" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" [[package]] name = "errno" @@ -1221,7 +1221,7 @@ dependencies = [ "fr32", "fvm 2.2.0", "fvm 3.0.0-alpha.20", - "fvm 3.0.0-alpha.24", + "fvm 3.0.0-rc.1", "fvm_ipld_blockstore", "fvm_ipld_encoding 0.2.3", "fvm_ipld_encoding 0.3.2", @@ -1486,9 +1486,9 @@ dependencies = [ [[package]] name = "fvm" -version = "3.0.0-alpha.24" +version = "3.0.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "560201e145435e4a3741fe5e6f37b7854273af9f344ca13957232ccb58834a1f" +checksum = "9efd2ca858542b73acd5f679bac84ab9de2af8ccb35a3528e414e08b85e55982" dependencies = [ "anyhow", "blake2b_simd", @@ -1873,9 +1873,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.27.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dec7af912d60cdbd3677c1af9352ebae6fb8394d165568a2234df0fa00f87793" +checksum = "221996f774192f0f718773def8201c4ae31f02616a54ccfc2d358bb0e5cefdec" [[package]] name = "glob" @@ -2292,7 +2292,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b20a59d985586e4a5aef64564ac77299f8586d8be6cf9106a5a40207e8908efb" dependencies = [ - "rustix 0.36.6", + "rustix 0.36.7", ] [[package]] @@ -2566,9 +2566,9 @@ dependencies = [ [[package]] name = "object" -version = "0.30.2" +version = "0.30.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b8c786513eb403643f2a88c244c2aaa270ef2153f55094587d0c48a3cf22a83" +checksum = "ea86265d3d3dcb6a27fc51bd29a4bf387fae9d2986b823079d4986af253eb439" dependencies = [ "memchr", ] @@ -2833,9 +2833,9 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.10.1" +version = "1.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3" +checksum = "356a0625f1954f730c0201cdab48611198dc6ce21f4acff55089b5a78e6e835b" dependencies = [ "crossbeam-channel", "crossbeam-deque", @@ -3009,9 +3009,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.36.6" +version = "0.36.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" +checksum = "d4fdebc4b395b7fbb9ab11e462e20ed9051e7b16e42d24042c776eca0ac81b03" dependencies = [ "bitflags 1.3.2", "errno", @@ -3520,9 +3520,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "toml" -version = "0.5.10" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" dependencies = [ "serde", ] @@ -3535,9 +3535,9 @@ checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" [[package]] name = "unicode-bidi" -version = "0.3.8" +version = "0.3.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58" [[package]] name = "unicode-ident" @@ -3685,9 +3685,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.97.0" +version = "0.99.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b98123a0d2bacf9286239231b116cbd66c65d9b89793f7c9bba3a3ae7f1b15f3" +checksum = "9ef3b717afc67f848f412d4f02c127dd3e35a0eecd58c684580414df4fde01d3" dependencies = [ "indexmap", "url", @@ -3695,12 +3695,12 @@ dependencies = [ [[package]] name = "wasmprinter" -version = "0.2.46" +version = "0.2.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595cca929e47a7bec3c941b5a8e133f51b17e6d9dd8c82ab97902196f5a07b42" +checksum = "27c13dff901f9354fa9a6a877152d9c5642513645985635c9b83bcca99e40ea1" dependencies = [ "anyhow", - "wasmparser 0.97.0", + "wasmparser 0.99.0", ] [[package]] @@ -3851,9 +3851,9 @@ dependencies = [ [[package]] name = "which" -version = "4.3.0" +version = "4.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c831fbbee9e129a8cf93e7747a82da9d95ba8e16621cae60ec2cdc849bacb7b" +checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" dependencies = [ "either", "libc", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 3be514f3..8b68a0b1 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -34,7 +34,7 @@ serde_json = "1.0.46" memmap = "0.7" rust-gpu-tools = { version = "0.5", optional = true, default-features = false } fr32 = { version = "~5.0", default-features = false } -fvm3 = { package = "fvm", version = "=3.0.0-alpha.24", default-features = false, features = ["hyperspace"] } +fvm3 = { package = "fvm", version = "3.0.0-rc.1", default-features = false } fvm3_shared = { package = "fvm_shared", version = "3.0.0-alpha.20" } fvm3_ipld_encoding = { package = "fvm_ipld_encoding", version = "0.3.3" } fvm3h1 = { package = "fvm", git = "https://github.com/filecoin-project/ref-fvm", branch = "release/hyperspace-nv19", default-features = false, features = ["hyperspace"] } diff --git a/rust/src/fvm/machine.rs b/rust/src/fvm/machine.rs index 6f639baf..3045a435 100644 --- a/rust/src/fvm/machine.rs +++ b/rust/src/fvm/machine.rs @@ -6,6 +6,7 @@ use cid::Cid; use fvm3::executor::ApplyKind as ApplyKind3; use fvm3::gas::GasCharge; use fvm3::trace::ExecutionEvent; +use fvm3_ipld_encoding::ipld_block::IpldBlock; use fvm3_ipld_encoding::tuple::{Deserialize_tuple, Serialize_tuple}; use fvm3_ipld_encoding::{to_vec, CborStore, RawBytes}; use fvm3_shared::address::Address; @@ -204,19 +205,38 @@ fn fvm_machine_execute_message( let exec_trace = if !apply_ret.exec_trace.is_empty() { let mut trace_iter = apply_ret.exec_trace.into_iter(); - build_lotus_trace( - (&mut trace_iter) - // Skip gas charges before the first call, if any. Lotus can't handle them. - .find(|item| !matches!(item, &ExecutionEvent::GasCharge(_))) - .expect("already checked trace for emptiness"), - &mut trace_iter, - ) - .ok() - .and_then(|t| to_vec(&t).ok()) - .map(|trace| trace.into_boxed_slice().into()) + let mut initial_gas_charges = Vec::new(); + loop { + match trace_iter.next() { + Some(gc @ ExecutionEvent::GasCharge(_)) => initial_gas_charges.push(gc), + Some(ExecutionEvent::Call { + from, + to, + method, + params, + value, + }) => { + break build_lotus_trace( + from, + to, + method, + params, + value, + &mut initial_gas_charges.into_iter().chain(&mut trace_iter), + ) + .ok() + } + // Skip anything unexpected. + Some(_) => {} + // Return none if we don't even have a call. + None => break None, + } + } } else { None - }; + } + .and_then(|t| to_vec(&t).ok()) + .map(|trace| trace.into_boxed_slice().into()); let failure_info = apply_ret .failure_info @@ -312,12 +332,12 @@ destructor!( destructor!(destroy_fvm_machine_flush_response, Result>); -#[derive(Clone, Debug, Serialize_tuple, Deserialize_tuple)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] struct LotusGasCharge { pub name: Cow<'static, str>, pub total_gas: u64, pub compute_gas: u64, - pub storage_gas: u64, + pub other_gas: u64, } #[derive(Clone, Debug, Serialize_tuple, Deserialize_tuple)] @@ -337,32 +357,25 @@ pub struct LotusReceipt { } fn build_lotus_trace( - new_call: ExecutionEvent, + from: u64, + to: Address, + method: u64, + params: Option, + value: TokenAmount, trace_iter: &mut impl Iterator, ) -> anyhow::Result { let mut new_trace = LotusTrace { - msg: match new_call { - ExecutionEvent::Call { - from, - to, - method, - params, - value, - } => Message { - version: 0, - from: Address::new_id(from), - to, - value, - sequence: 0, - method_num: method, - params: params.map(|b| b.data).unwrap_or_default().into(), - gas_limit: 0, - gas_fee_cap: TokenAmount::default(), - gas_premium: TokenAmount::default(), - }, - _ => { - return Err(anyhow!("expected ExecutionEvent of type Call")); - } + msg: Message { + version: 0, + from: Address::new_id(from), + to, + value, + sequence: 0, + method_num: method, + params: params.map(|b| b.data).unwrap_or_default().into(), + gas_limit: 0, + gas_fee_cap: TokenAmount::default(), + gas_premium: TokenAmount::default(), }, msg_receipt: LotusReceipt { exit_code: ExitCode::OK, @@ -376,10 +389,16 @@ fn build_lotus_trace( while let Some(trace) = trace_iter.next() { match trace { - ExecutionEvent::Call { .. } => { - new_trace - .subcalls - .push(build_lotus_trace(trace, trace_iter)?); + ExecutionEvent::Call { + from, + to, + method, + params, + value, + } => { + new_trace.subcalls.push(build_lotus_trace( + from, to, method, params, value, trace_iter, + )?); } ExecutionEvent::CallReturn(exit_code, return_data) => { new_trace.msg_receipt = LotusReceipt { @@ -416,7 +435,7 @@ fn build_lotus_trace( name, total_gas: (compute_gas + other_gas).round_up(), compute_gas: compute_gas.round_up(), - storage_gas: other_gas.round_up(), + other_gas: other_gas.round_up(), }); } _ => (), // ignore unknown events. @@ -428,7 +447,9 @@ fn build_lotus_trace( #[cfg(test)] mod test { - use crate::fvm::machine::build_lotus_trace; + use crate::fvm::machine::{build_lotus_trace, LotusGasCharge}; + use fvm3::gas::Gas; + use fvm3::gas::GasCharge; use fvm3::kernel::SyscallError; use fvm3::trace::ExecutionEvent; use fvm3_shared::address::Address; @@ -447,8 +468,9 @@ mod test { }; let return_result = ExecutionEvent::CallError(SyscallError::new(IllegalArgument, "illegal")); + let initial_gas_charge = GasCharge::new("gas_test", Gas::new(1), Gas::new(2)); let trace = vec![ - call_event.clone(), + ExecutionEvent::GasCharge(initial_gas_charge.clone()), call_event.clone(), return_result.clone(), call_event.clone(), @@ -460,10 +482,28 @@ mod test { let mut trace_iter = trace.into_iter(); - let lotus_trace = build_lotus_trace(trace_iter.next().unwrap(), &mut trace_iter).unwrap(); + let lotus_trace = build_lotus_trace( + 0, + Address::new_id(0), + 0, + None, + TokenAmount::default(), + &mut trace_iter, + ) + .unwrap(); assert!(trace_iter.next().is_none()); + assert_eq!(lotus_trace.gas_charges.len(), 1); + assert_eq!( + *lotus_trace.gas_charges.get(0).unwrap(), + LotusGasCharge { + name: initial_gas_charge.clone().name, + total_gas: initial_gas_charge.total().round_up(), + compute_gas: initial_gas_charge.compute_gas.round_up(), + other_gas: initial_gas_charge.other_gas.round_up(), + } + ); assert_eq!(lotus_trace.subcalls.len(), 2); assert_eq!(lotus_trace.subcalls[0].subcalls.len(), 0); assert_eq!(lotus_trace.subcalls[1].subcalls.len(), 1);