From 70645fc799d06f2c4282cdc364e9910edbdcd563 Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Fri, 22 Sep 2023 11:08:06 +0200 Subject: [PATCH 01/11] wip --- Cargo.lock | 233 ++++++++++++++++++------ Cargo.toml | 3 + src/interpreter/fvm4.rs | 381 ++++++++++++++++++++++++++++++++++++++++ src/interpreter/mod.rs | 1 + src/interpreter/vm.rs | 5 +- src/shim/address.rs | 37 ++++ src/shim/gas.rs | 11 ++ 7 files changed, 618 insertions(+), 53 deletions(-) create mode 100644 src/interpreter/fvm4.rs diff --git a/Cargo.lock b/Cargo.lock index 658fb58026f..3a7d55c03db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2341,7 +2341,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29b2c17d1a2242f5fa495132a9647ee2834c1f0c97e783c32c105b8e0ec91234" dependencies = [ "frc42_macros", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2355,7 +2355,7 @@ version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7b70cbb70ebbf04391f5ff8202365b0c7672158ac1f3919e532c40b01908c22" dependencies = [ - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2371,7 +2371,7 @@ checksum = "9ce000fa16b909b7992a2ee56a558df8e470a14aa720c93df000ec5de95044cd" dependencies = [ "fil_actors_shared", "frc42_macros", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2386,7 +2386,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9058122bfe5348537a82db4df6e769be2561a460c6e7ef66276d4a3b1953378f" dependencies = [ "fil_actors_shared", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 3.6.0", "hex", "serde", @@ -2402,7 +2402,7 @@ dependencies = [ "cid", "fil_actor_evm_shared_state", "frc42_macros", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 3.6.0", "hex-literal", "num-derive", @@ -2420,9 +2420,9 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2451,8 +2451,8 @@ dependencies = [ "fil_actor_system_state", "fil_actors_shared", "fvm_ipld_bitfield", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "lazy_static", @@ -2476,9 +2476,9 @@ dependencies = [ "fil_actors_shared", "frc42_macros", "fvm_ipld_bitfield", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "libipld-core", @@ -2499,11 +2499,11 @@ dependencies = [ "fil_actor_verifreg_state", "fil_actors_shared", "frc42_macros", - "fvm_ipld_amt", + "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_ipld_bitfield", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "itertools 0.11.0", @@ -2526,9 +2526,9 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "indexmap 2.0.0", @@ -2548,9 +2548,9 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "integer-encoding", @@ -2566,7 +2566,7 @@ version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cd59f7583b2848c92a6f4e9ac2880d6bfb8842044d7afbf76eda09adeff2fd6" dependencies = [ - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "lazy_static", @@ -2582,7 +2582,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d06e7a6a039277591862d1bf61b163447cc6900aeb51dfdf92500e64d74e9fc7" dependencies = [ "cid", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "num-derive", "num-traits", @@ -2599,8 +2599,8 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2617,11 +2617,11 @@ dependencies = [ "anyhow", "cid", "filecoin-proofs-api", - "fvm_ipld_amt", + "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_ipld_bitfield", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "multihash 0.18.1", @@ -2818,11 +2818,13 @@ dependencies = [ "futures", "fvm 2.7.0", "fvm 3.8.0", - "fvm_ipld_blockstore", + "fvm 4.0.0-alpha.1", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_ipld_car", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "fvm_shared 3.6.0", + "fvm_shared 4.0.0-alpha.1", "gethostname", "git-version", "hex", @@ -3145,10 +3147,10 @@ dependencies = [ "derive_more", "filecoin-proofs-api", "fvm-wasm-instrument 0.2.0", - "fvm_ipld_amt", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 2.6.0", "lazy_static", "log", @@ -3181,10 +3183,10 @@ dependencies = [ "derive_more", "filecoin-proofs-api", "fvm-wasm-instrument 0.4.0", - "fvm_ipld_amt", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", - "fvm_ipld_hamt", + "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "fvm_shared 3.6.0", "lazy_static", "log", @@ -3206,6 +3208,44 @@ dependencies = [ "yastl", ] +[[package]] +name = "fvm" +version = "4.0.0-alpha.1" +source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +dependencies = [ + "anyhow", + "arbitrary", + "blake2b_simd", + "byteorder", + "cid", + "derive_more", + "filecoin-proofs-api", + "fvm-wasm-instrument 0.4.0", + "fvm_ipld_amt 0.6.1 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_hamt 0.8.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_shared 4.0.0-alpha.1", + "lazy_static", + "log", + "minstant", + "multihash 0.18.1", + "num-traits", + "num_cpus", + "once_cell", + "quickcheck", + "rand", + "rayon", + "replace_with", + "serde", + "serde_tuple", + "thiserror", + "wasmtime", + "wasmtime-environ", + "wasmtime-runtime", + "yastl", +] + [[package]] name = "fvm-wasm-instrument" version = "0.2.0" @@ -3235,8 +3275,23 @@ checksum = "7c0b0ee51ca8defa9717a72e1d35c8cbb85bd8320a835911410b63b9a63dffec" dependencies = [ "anyhow", "cid", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.11.0", + "once_cell", + "serde", + "thiserror", +] + +[[package]] +name = "fvm_ipld_amt" +version = "0.6.1" +source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +dependencies = [ + "anyhow", + "cid", + "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", "itertools 0.11.0", "once_cell", "serde", @@ -3249,7 +3304,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da94287cafa663c2e295fe45c4c9dbf5ab7b52f648568f9ae3823deaf9873a89" dependencies = [ - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "thiserror", "unsigned-varint", @@ -3266,6 +3321,16 @@ dependencies = [ "multihash 0.18.1", ] +[[package]] +name = "fvm_ipld_blockstore" +version = "0.2.0" +source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +dependencies = [ + "anyhow", + "cid", + "multihash 0.18.1", +] + [[package]] name = "fvm_ipld_car" version = "0.7.1" @@ -3274,8 +3339,8 @@ checksum = "6190f03442b67b21a3d4e115c4d4dd3468aed24e27ebb074218822c1b3df41ba" dependencies = [ "cid", "futures", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "thiserror", "unsigned-varint", @@ -3289,7 +3354,23 @@ checksum = "90608092e31d9a06236268c58f7c36668ab4b2a48afafe3a97e08f094ad7ae50" dependencies = [ "anyhow", "cid", - "fvm_ipld_blockstore", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "multihash 0.18.1", + "serde", + "serde_ipld_dagcbor", + "serde_repr", + "serde_tuple", + "thiserror", +] + +[[package]] +name = "fvm_ipld_encoding" +version = "0.4.0" +source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +dependencies = [ + "anyhow", + "cid", + "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", "multihash 0.18.1", "serde", "serde_ipld_dagcbor", @@ -3308,8 +3389,27 @@ dependencies = [ "byteorder", "cid", "forest_hash_utils", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libipld-core", + "multihash 0.18.1", + "once_cell", + "serde", + "sha2 0.10.7", + "thiserror", +] + +[[package]] +name = "fvm_ipld_hamt" +version = "0.8.0" +source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +dependencies = [ + "anyhow", + "byteorder", + "cid", + "forest_hash_utils", + "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", "libipld-core", "multihash 0.18.1", "once_cell", @@ -3333,8 +3433,8 @@ dependencies = [ "data-encoding", "data-encoding-macro", "filecoin-proofs-api", - "fvm_ipld_blockstore", - "fvm_ipld_encoding", + "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static", "libsecp256k1", "log", @@ -3365,7 +3465,36 @@ dependencies = [ "data-encoding", "data-encoding-macro", "filecoin-proofs-api", - "fvm_ipld_encoding", + "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static", + "libsecp256k1", + "multihash 0.18.1", + "num-bigint", + "num-derive", + "num-integer", + "num-traits", + "quickcheck", + "serde", + "serde_tuple", + "thiserror", + "unsigned-varint", +] + +[[package]] +name = "fvm_shared" +version = "4.0.0-alpha.1" +source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +dependencies = [ + "anyhow", + "arbitrary", + "bitflags 2.4.0", + "blake2b_simd", + "bls-signatures", + "cid", + "data-encoding", + "data-encoding-macro", + "filecoin-proofs-api", + "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", "lazy_static", "libsecp256k1", "multihash 0.18.1", diff --git a/Cargo.toml b/Cargo.toml index 2deb5bf1b39..4882b2bf7c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,11 +68,14 @@ fs_extra = "1.2" futures = "0.3" fvm2 = { package = "fvm", version = "~2.7", default-features = false } fvm3 = { package = "fvm", default-features = false, version = "~3.8", features = ["arb"] } +fvm4 = { package = "fvm", default-features = false, features = ["arb", "nv21-dev"], git = "https://github.com/filecoin-project/ref-fvm.git", sha = "5603bcf" } fvm_ipld_blockstore = "0.2" fvm_ipld_car = "0.7" fvm_ipld_encoding = "0.4" fvm_shared2 = { package = "fvm_shared", version = "~2.6" } fvm_shared3 = { package = "fvm_shared", version = "~3.6", features = ["testing", "proofs"] } +fvm_shared4 = { package = "fvm_shared", features = ["testing", "proofs"], git = "https://github.com/filecoin-project/ref-fvm.git", sha = "5603bcf" } + gethostname = "0.4" git-version = "0.3" hex = { version = "0.4", features = ["serde"] } diff --git a/src/interpreter/fvm4.rs b/src/interpreter/fvm4.rs new file mode 100644 index 00000000000..a44ee4d1d04 --- /dev/null +++ b/src/interpreter/fvm4.rs @@ -0,0 +1,381 @@ +// Copyright 2019-2023 ChainSafe Systems +// SPDX-License-Identifier: Apache-2.0, MIT + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::{cell::Ref, sync::Arc}; + +use crate::blocks::{BlockHeader, Tipset}; +use crate::chain::{ + index::{ChainIndex, ResolveNullTipset}, + ChainStore, +}; +use crate::interpreter::errors::Error; +use crate::networks::ChainConfig; +use crate::shim::{ + address::Address, gas::price_list_by_network_version, state_tree::StateTree, + version::NetworkVersion, +}; +use crate::utils::encoding::from_slice_with_fallback; +use anyhow::{bail, Context as _}; +use cid::Cid; +use fvm4::{ + externs::{Chain, Consensus, Externs, Rand}, + gas::{Gas, GasTracker}, +}; +use fvm_ipld_blockstore::{ + tracking::{BSStats, TrackingBlockstore}, + Blockstore, +}; +use fvm_shared4::{ + clock::ChainEpoch, + consensus::{ConsensusFault, ConsensusFaultType}, +}; +use tracing::error; + +use crate::interpreter::resolve_to_key_addr; + +pub struct ForestExterns { + rand: Box, + heaviest_tipset: Arc, + epoch: ChainEpoch, + root: Cid, + chain_index: Arc>>, + chain_config: Arc, + bail: AtomicBool, +} + +impl ForestExterns { + pub fn new( + rand: impl Rand + 'static, + heaviest_tipset: Arc, + epoch: ChainEpoch, + root: Cid, + chain_index: Arc>>, + chain_config: Arc, + ) -> Self { + ForestExterns { + rand: Box::new(rand), + heaviest_tipset, + epoch, + root, + chain_index, + chain_config, + bail: AtomicBool::new(false), + } + } + + fn get_lookback_tipset_state_root_for_round(&self, height: ChainEpoch) -> anyhow::Result { + let (_, st) = ChainStore::get_lookback_tipset_for_round( + self.chain_index.clone(), + Arc::clone(&self.chain_config), + Arc::clone(&self.heaviest_tipset), + height, + )?; + Ok(st) + } + + fn worker_key_at_lookback( + &self, + miner_addr: &Address, + height: ChainEpoch, + ) -> anyhow::Result<(Address, i64)> { + if height < self.epoch - self.chain_config.policy.chain_finality { + bail!( + "cannot get worker key (current epoch: {}, height: {})", + self.epoch, + height + ); + } + + let prev_root = self.get_lookback_tipset_state_root_for_round(height)?; + let lb_state = StateTree::new_from_root(Arc::clone(&self.chain_index.db), &prev_root)?; + + let actor = lb_state + .get_actor(miner_addr)? + .ok_or_else(|| anyhow::anyhow!("actor not found {:?}", miner_addr))?; + + let tbs = TrackingBlockstore::new(&self.chain_index.db); + + let ms = fil_actor_interface::miner::State::load(&tbs, actor.code, actor.state)?; + + let worker = ms.info(&tbs)?.worker.into(); + + let state = StateTree::new_from_root(Arc::clone(&self.chain_index.db), &self.root)?; + + let addr = resolve_to_key_addr(&state, &tbs, &worker)?; + + let network_version = self.chain_config.network_version(self.epoch); + let gas_used = cal_gas_used_from_stats(tbs.stats.borrow(), network_version)?; + + Ok((addr, gas_used.round_up() as i64)) + } + + fn verify_block_signature(&self, bh: &BlockHeader) -> anyhow::Result { + let (worker_addr, gas_used) = + self.worker_key_at_lookback(bh.miner_address(), bh.epoch())?; + + bh.check_block_signature(&worker_addr)?; + + Ok(gas_used) + } + + /// Signifies whether or not we have to bail due to database lookup problems. + /// + /// NOTE: Unfortunately there isn't a better a way of dealing with this, because FVM swallows + /// both errors and panics alike, so we have to deal with this in the client code. + pub fn bail(&self) -> bool { + self.bail.load(Ordering::Relaxed) + } +} + +impl Externs for ForestExterns {} + +impl Chain for ForestExterns { + fn get_tipset_cid(&self, epoch: ChainEpoch) -> anyhow::Result { + let ts = self + .chain_index + .tipset_by_height( + epoch, + self.heaviest_tipset.clone(), + ResolveNullTipset::TakeOlder, + ) + .context("Failed to get tipset cid")?; + ts.key().cid() + } +} + +impl Rand for ForestExterns { + fn get_chain_randomness(&self, round: ChainEpoch) -> anyhow::Result<[u8; 32]> { + self.rand.get_chain_randomness(round) + } + + fn get_beacon_randomness(&self, round: ChainEpoch) -> anyhow::Result<[u8; 32]> { + self.rand.get_beacon_randomness(round) + } +} + +impl Consensus for ForestExterns { + // See https://github.com/filecoin-project/lotus/blob/v1.18.0/chain/vm/fvm.go#L102-L216 for reference implementation + fn verify_consensus_fault( + &self, + h1: &[u8], + h2: &[u8], + extra: &[u8], + ) -> anyhow::Result<(Option, i64)> { + let mut total_gas: i64 = 0; + + // Note that block syntax is not validated. Any validly signed block will be + // accepted pursuant to the below conditions. Whether or not it could + // ever have been accepted in a chain is not checked/does not matter here. + // for that reason when checking block parent relationships, rather than + // instantiating a Tipset to do so (which runs a syntactic check), we do + // it directly on the CIDs. + + // (0) cheap preliminary checks + + // are blocks the same? + if h1 == h2 { + bail!( + "no consensus fault: submitted blocks are the same: {:?}, {:?}", + h1, + h2 + ); + }; + let bh_1 = from_slice_with_fallback::(h1)?; + let bh_2 = from_slice_with_fallback::(h2)?; + + if bh_1.cid() == bh_2.cid() { + bail!("no consensus fault: submitted blocks are the same"); + } + + // (1) check conditions necessary to any consensus fault + + if bh_1.miner_address() != bh_2.miner_address() { + bail!( + "no consensus fault: blocks not mined by same miner: {:?}, {:?}", + bh_1.miner_address(), + bh_2.miner_address() + ); + }; + // block a must be earlier or equal to block b, epoch wise (ie at least as early + // in the chain). + if bh_2.epoch() < bh_1.epoch() { + bail!( + "first block must not be of higher height than second: {:?}, {:?}", + bh_1.epoch(), + bh_2.epoch() + ); + }; + + let mut fault_type: Option = None; + + // (2) check for the consensus faults themselves + + // (a) double-fork mining fault + if bh_1.epoch() == bh_2.epoch() { + fault_type = Some(ConsensusFaultType::DoubleForkMining); + }; + + // (b) time-offset mining fault + // strictly speaking no need to compare heights based on double fork mining + // check above, but at same height this would be a different fault. + if bh_1.parents() == bh_2.parents() && bh_1.epoch() != bh_2.epoch() { + fault_type = Some(ConsensusFaultType::TimeOffsetMining); + }; + + // (c) parent-grinding fault + // Here extra is the "witness", a third block that shows the connection between + // A and B as A's sibling and B's parent. + // Specifically, since A is of lower height, it must be that B was mined + // omitting A from its tipset + if !extra.is_empty() { + let bh_3 = from_slice_with_fallback::(extra)?; + if bh_1.parents() == bh_3.parents() + && bh_1.epoch() == bh_3.epoch() + && bh_2.parents().cids.contains(*bh_3.cid()) + && !bh_2.parents().cids.contains(*bh_1.cid()) + { + fault_type = Some(ConsensusFaultType::ParentGrinding); + } + }; + + match fault_type { + None => { + // (3) return if no consensus fault + Ok((None, total_gas)) + } + Some(fault_type) => { + // (4) expensive final checks + + let bail = |err| { + // When a lookup error occurs we should just bail terminating all the + // computations. + error!("database lookup error: {err}"); + self.bail.store(true, Ordering::Relaxed); + Err(err) + }; + + // check blocks are properly signed by their respective miner + // note we do not need to check extra's: it is a parent to block b + // which itself is signed, so it was willingly included by the miner + for block_header in [&bh_1, &bh_2] { + let res = self.verify_block_signature(block_header); + match res { + // invalid consensus fault: cannot verify block header signature + Err(Error::Signature(_)) => return Ok((None, total_gas)), + Err(Error::Lookup(err)) => return bail(err), + Ok(gas_used) => total_gas += gas_used, + } + } + + let ret = Some(ConsensusFault { + target: bh_1.miner_address().into(), + epoch: bh_2.epoch(), + fault_type, + }); + + Ok((ret, total_gas)) + } + } + } +} + +fn cal_gas_used_from_stats( + stats: Ref, + network_version: NetworkVersion, +) -> anyhow::Result { + let price_list = price_list_by_network_version(network_version); + let gas_tracker = GasTracker::new(Gas::new(u64::MAX), Gas::new(0), false); + // num of reads + for _ in 0..stats.r { + gas_tracker + .apply_charge(price_list.on_block_open_base().into())? + .stop(); + } + + // num of writes + if stats.w > 0 { + // total bytes written + gas_tracker + .apply_charge(price_list.on_block_link(stats.bw).into())? + .stop(); + for _ in 1..stats.w { + gas_tracker + .apply_charge(price_list.on_block_link(0).into())? + .stop(); + } + } + Ok(gas_tracker.gas_used()) +} + +#[cfg(test)] +mod tests { + use std::{cell::RefCell, iter::repeat}; + + use anyhow::ensure; + + use super::*; + + #[test] + fn test_cal_gas_used_from_stats_1_read() -> anyhow::Result<()> { + test_cal_gas_used_from_stats_inner(1, &[]) + } + + #[test] + fn test_cal_gas_used_from_stats_1_write() -> anyhow::Result<()> { + test_cal_gas_used_from_stats_inner(0, &[100]) + } + + #[test] + fn test_cal_gas_used_from_stats_multi_read() -> anyhow::Result<()> { + test_cal_gas_used_from_stats_inner(10, &[]) + } + + #[test] + fn test_cal_gas_used_from_stats_multi_write() -> anyhow::Result<()> { + test_cal_gas_used_from_stats_inner(0, &[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]) + } + + #[test] + fn test_cal_gas_used_from_stats_1_read_1_write() -> anyhow::Result<()> { + test_cal_gas_used_from_stats_inner(1, &[100]) + } + + #[test] + fn test_cal_gas_used_from_stats_multi_read_multi_write() -> anyhow::Result<()> { + test_cal_gas_used_from_stats_inner(10, &[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]) + } + + fn test_cal_gas_used_from_stats_inner( + read_count: usize, + write_bytes: &[usize], + ) -> anyhow::Result<()> { + let network_version = NetworkVersion::V18; + let stats = BSStats { + r: read_count, + w: write_bytes.len(), + br: 0, // Not used in current logic + bw: write_bytes.iter().sum(), + }; + let result = cal_gas_used_from_stats(RefCell::new(stats).borrow(), network_version)?; + + // Simulates logic in old GasBlockStore + let price_list = price_list_by_network_version(network_version); + let tracker = GasTracker::new(Gas::new(u64::MAX), Gas::new(0), false); + repeat(()).take(read_count).for_each(|_| { + tracker + .apply_charge(price_list.on_block_open_base().into()) + .unwrap() + .stop(); + }); + for &bytes in write_bytes { + tracker + .apply_charge(price_list.on_block_link(bytes).into())? + .stop() + } + let expected = tracker.gas_used(); + + ensure!(result == expected); + Ok(()) + } +} diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 63c94c1139f..fbd8a74062e 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -4,6 +4,7 @@ mod errors; mod fvm2; pub mod fvm3; +mod fvm4; mod vm; use crate::shim::{ diff --git a/src/interpreter/vm.rs b/src/interpreter/vm.rs index 837d2f897d8..c168b46a1c1 100644 --- a/src/interpreter/vm.rs +++ b/src/interpreter/vm.rs @@ -7,7 +7,10 @@ use crate::blocks::Tipset; use crate::chain::block_messages; use crate::chain::index::ChainIndex; use crate::chain::store::Error; -use crate::interpreter::{fvm2::ForestExternsV2, fvm3::ForestExterns as ForestExternsV3}; +use crate::interpreter::{ + fvm2::ForestExternsV2, fvm3::ForestExterns as ForestExternsV3, + fvm4::ForestExterns as ForestExternsV4, +}; use crate::message::ChainMessage; use crate::message::Message as MessageTrait; use crate::networks::{ChainConfig, NetworkChain}; diff --git a/src/shim/address.rs b/src/shim/address.rs index 04a930be286..1ca018c8c33 100644 --- a/src/shim/address.rs +++ b/src/shim/address.rs @@ -12,6 +12,7 @@ use data_encoding_macro::new_encoding; use fvm_shared2::address::Address as Address_v2; use fvm_shared3::address::Address as Address_v3; pub use fvm_shared3::address::{Error, Network, Payload, Protocol, BLS_PUB_LEN, PAYLOAD_HASH_LEN}; +use fvm_shared4::address::Address as Address_v4; use integer_encoding::VarInt; use num_traits::FromPrimitive; use once_cell::sync::Lazy; @@ -296,6 +297,18 @@ impl From for Address_v3 { } } +impl From for Address_v4 { + fn from(other: StrictAddress) -> Self { + other.0.into() + } +} + +impl From for Address { + fn from(other: Address_v4) -> Self { + (&other).into() + } +} + impl From for Address { fn from(other: Address_v3) -> Self { Address(other) @@ -318,6 +331,22 @@ impl From<&Address_v2> for Address { } } +impl From<&Address_v4> for Address { + fn from(other: &Address_v4) -> Self { + Address::from( + Address_v3::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { + panic!("Couldn't convert from FVM4 address to FVM3 address: {other}, {e}") + }), + ) + } +} + +impl From
for Address_v4 { + fn from(other: Address) -> Address_v4 { + (&other).into() + } +} + impl From<&Address_v3> for Address { fn from(other: &Address_v3) -> Self { Address(*other) @@ -338,6 +367,14 @@ impl From<&Address> for Address_v2 { } } +impl From<&Address> for Address_v4 { + fn from(other: &Address) -> Self { + Address_v4::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { + panic!("Couldn't convert from FVM3 address to FVM4 address: {other}, {e}") + }) + } +} + impl From
for Address_v3 { fn from(other: Address) -> Self { (&other).into() diff --git a/src/shim/gas.rs b/src/shim/gas.rs index f4c44135c2a..7a98d264852 100644 --- a/src/shim/gas.rs +++ b/src/shim/gas.rs @@ -13,6 +13,11 @@ use fvm3::gas::{ price_list_by_network_version as price_list_by_network_version_v3, PriceList as PriceListV3, MILLIGAS_PRECISION, }; +pub use fvm4::gas::Gas as GasV4; +pub use fvm4::gas::GasCharge as GasChargeV4; +use fvm4::gas::{ + price_list_by_network_version as price_list_by_network_version_v4, PriceList as PriceListV4, +}; use crate::shim::version::NetworkVersion; @@ -73,6 +78,12 @@ impl From for GasV3 { } } +impl From for GasV4 { + fn from(value: Gas) -> Self { + GasV4::from_milligas(value.0.as_milligas()) + } +} + impl From for Gas { fn from(value: GasV3) -> Self { Gas(value) From 4a1be5511fce53138e4637d87b9f50022466c300 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 27 Sep 2023 15:02:11 +0800 Subject: [PATCH 02/11] Make it compile --- Cargo.lock | 191 ++++++++++++++-------------------------- Cargo.toml | 4 +- src/interpreter/vm.rs | 76 +++++++++++++++- src/shim/econ.rs | 53 ++++++++--- src/shim/error.rs | 15 +++- src/shim/executor.rs | 23 +++++ src/shim/externs.rs | 12 +++ src/shim/gas.rs | 63 ++++++++++--- src/shim/kernel.rs | 47 ++++++++-- src/shim/machine/mod.rs | 8 +- src/shim/message.rs | 61 ++++++++----- src/shim/state_tree.rs | 51 +++++++---- src/shim/trace.rs | 38 +++++++- src/shim/version.rs | 70 +++++++++------ 14 files changed, 481 insertions(+), 231 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a7d55c03db..b33b2c7886f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2341,7 +2341,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29b2c17d1a2242f5fa495132a9647ee2834c1f0c97e783c32c105b8e0ec91234" dependencies = [ "frc42_macros", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2355,7 +2355,7 @@ version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7b70cbb70ebbf04391f5ff8202365b0c7672158ac1f3919e532c40b01908c22" dependencies = [ - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2371,7 +2371,7 @@ checksum = "9ce000fa16b909b7992a2ee56a558df8e470a14aa720c93df000ec5de95044cd" dependencies = [ "fil_actors_shared", "frc42_macros", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2386,7 +2386,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9058122bfe5348537a82db4df6e769be2561a460c6e7ef66276d4a3b1953378f" dependencies = [ "fil_actors_shared", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 3.6.0", "hex", "serde", @@ -2402,7 +2402,7 @@ dependencies = [ "cid", "fil_actor_evm_shared_state", "frc42_macros", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 3.6.0", "hex-literal", "num-derive", @@ -2420,9 +2420,9 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2451,8 +2451,8 @@ dependencies = [ "fil_actor_system_state", "fil_actors_shared", "fvm_ipld_bitfield", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "lazy_static", @@ -2476,9 +2476,9 @@ dependencies = [ "fil_actors_shared", "frc42_macros", "fvm_ipld_bitfield", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "libipld-core", @@ -2499,11 +2499,11 @@ dependencies = [ "fil_actor_verifreg_state", "fil_actors_shared", "frc42_macros", - "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_amt", "fvm_ipld_bitfield", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "itertools 0.11.0", @@ -2526,9 +2526,9 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "indexmap 2.0.0", @@ -2548,9 +2548,9 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "integer-encoding", @@ -2566,7 +2566,7 @@ version = "6.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cd59f7583b2848c92a6f4e9ac2880d6bfb8842044d7afbf76eda09adeff2fd6" dependencies = [ - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "lazy_static", @@ -2582,7 +2582,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d06e7a6a039277591862d1bf61b163447cc6900aeb51dfdf92500e64d74e9fc7" dependencies = [ "cid", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "num-derive", "num-traits", @@ -2599,8 +2599,8 @@ dependencies = [ "cid", "fil_actors_shared", "frc42_macros", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "num-derive", @@ -2617,11 +2617,11 @@ dependencies = [ "anyhow", "cid", "filecoin-proofs-api", - "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_amt", "fvm_ipld_bitfield", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", "multihash 0.18.1", @@ -2818,13 +2818,13 @@ dependencies = [ "futures", "fvm 2.7.0", "fvm 3.8.0", - "fvm 4.0.0-alpha.1", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm 4.0.0-alpha.2", + "fvm_ipld_blockstore", "fvm_ipld_car", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", - "fvm_shared 4.0.0-alpha.1", + "fvm_shared 4.0.0-alpha.2", "gethostname", "git-version", "hex", @@ -3147,10 +3147,10 @@ dependencies = [ "derive_more", "filecoin-proofs-api", "fvm-wasm-instrument 0.2.0", - "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_amt", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 2.6.0", "lazy_static", "log", @@ -3183,10 +3183,10 @@ dependencies = [ "derive_more", "filecoin-proofs-api", "fvm-wasm-instrument 0.4.0", - "fvm_ipld_amt 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_hamt 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_amt", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", "fvm_shared 3.6.0", "lazy_static", "log", @@ -3210,8 +3210,9 @@ dependencies = [ [[package]] name = "fvm" -version = "4.0.0-alpha.1" -source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +version = "4.0.0-alpha.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6cb612a56eb37b1e36aa69de550b41f47cb7c482344af13a9aa7d54e1eb325f" dependencies = [ "anyhow", "arbitrary", @@ -3221,11 +3222,11 @@ dependencies = [ "derive_more", "filecoin-proofs-api", "fvm-wasm-instrument 0.4.0", - "fvm_ipld_amt 0.6.1 (git+https://github.com/filecoin-project/ref-fvm.git)", - "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", - "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", - "fvm_ipld_hamt 0.8.0 (git+https://github.com/filecoin-project/ref-fvm.git)", - "fvm_shared 4.0.0-alpha.1", + "fvm_ipld_amt", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", + "fvm_ipld_hamt", + "fvm_shared 4.0.0-alpha.2", "lazy_static", "log", "minstant", @@ -3239,6 +3240,7 @@ dependencies = [ "replace_with", "serde", "serde_tuple", + "static_assertions", "thiserror", "wasmtime", "wasmtime-environ", @@ -3275,23 +3277,8 @@ checksum = "7c0b0ee51ca8defa9717a72e1d35c8cbb85bd8320a835911410b63b9a63dffec" dependencies = [ "anyhow", "cid", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "itertools 0.11.0", - "once_cell", - "serde", - "thiserror", -] - -[[package]] -name = "fvm_ipld_amt" -version = "0.6.1" -source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" -dependencies = [ - "anyhow", - "cid", - "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", - "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", "itertools 0.11.0", "once_cell", "serde", @@ -3304,7 +3291,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da94287cafa663c2e295fe45c4c9dbf5ab7b52f648568f9ae3823deaf9873a89" dependencies = [ - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "serde", "thiserror", "unsigned-varint", @@ -3321,16 +3308,6 @@ dependencies = [ "multihash 0.18.1", ] -[[package]] -name = "fvm_ipld_blockstore" -version = "0.2.0" -source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" -dependencies = [ - "anyhow", - "cid", - "multihash 0.18.1", -] - [[package]] name = "fvm_ipld_car" version = "0.7.1" @@ -3339,8 +3316,8 @@ checksum = "6190f03442b67b21a3d4e115c4d4dd3468aed24e27ebb074218822c1b3df41ba" dependencies = [ "cid", "futures", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", "serde", "thiserror", "unsigned-varint", @@ -3354,23 +3331,7 @@ checksum = "90608092e31d9a06236268c58f7c36668ab4b2a48afafe3a97e08f094ad7ae50" dependencies = [ "anyhow", "cid", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "multihash 0.18.1", - "serde", - "serde_ipld_dagcbor", - "serde_repr", - "serde_tuple", - "thiserror", -] - -[[package]] -name = "fvm_ipld_encoding" -version = "0.4.0" -source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" -dependencies = [ - "anyhow", - "cid", - "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_blockstore", "multihash 0.18.1", "serde", "serde_ipld_dagcbor", @@ -3389,27 +3350,8 @@ dependencies = [ "byteorder", "cid", "forest_hash_utils", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", - "libipld-core", - "multihash 0.18.1", - "once_cell", - "serde", - "sha2 0.10.7", - "thiserror", -] - -[[package]] -name = "fvm_ipld_hamt" -version = "0.8.0" -source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" -dependencies = [ - "anyhow", - "byteorder", - "cid", - "forest_hash_utils", - "fvm_ipld_blockstore 0.2.0 (git+https://github.com/filecoin-project/ref-fvm.git)", - "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", "libipld-core", "multihash 0.18.1", "once_cell", @@ -3433,8 +3375,8 @@ dependencies = [ "data-encoding", "data-encoding-macro", "filecoin-proofs-api", - "fvm_ipld_blockstore 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_blockstore", + "fvm_ipld_encoding", "lazy_static", "libsecp256k1", "log", @@ -3465,7 +3407,7 @@ dependencies = [ "data-encoding", "data-encoding-macro", "filecoin-proofs-api", - "fvm_ipld_encoding 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fvm_ipld_encoding", "lazy_static", "libsecp256k1", "multihash 0.18.1", @@ -3482,8 +3424,9 @@ dependencies = [ [[package]] name = "fvm_shared" -version = "4.0.0-alpha.1" -source = "git+https://github.com/filecoin-project/ref-fvm.git#5603bcff9fa3c30e931f956a3e759714e1e3cc9f" +version = "4.0.0-alpha.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dc4430d91e5269db5f32f8bb747810546e95485644c2d3ee432b7f6b15c7323" dependencies = [ "anyhow", "arbitrary", @@ -3494,7 +3437,7 @@ dependencies = [ "data-encoding", "data-encoding-macro", "filecoin-proofs-api", - "fvm_ipld_encoding 0.4.0 (git+https://github.com/filecoin-project/ref-fvm.git)", + "fvm_ipld_encoding", "lazy_static", "libsecp256k1", "multihash 0.18.1", diff --git a/Cargo.toml b/Cargo.toml index 4882b2bf7c4..207129842fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,13 +68,13 @@ fs_extra = "1.2" futures = "0.3" fvm2 = { package = "fvm", version = "~2.7", default-features = false } fvm3 = { package = "fvm", default-features = false, version = "~3.8", features = ["arb"] } -fvm4 = { package = "fvm", default-features = false, features = ["arb", "nv21-dev"], git = "https://github.com/filecoin-project/ref-fvm.git", sha = "5603bcf" } +fvm4 = { package = "fvm", default-features = false, version = "~4.0.0-alpha.2", features = ["arb"] } fvm_ipld_blockstore = "0.2" fvm_ipld_car = "0.7" fvm_ipld_encoding = "0.4" fvm_shared2 = { package = "fvm_shared", version = "~2.6" } fvm_shared3 = { package = "fvm_shared", version = "~3.6", features = ["testing", "proofs"] } -fvm_shared4 = { package = "fvm_shared", features = ["testing", "proofs"], git = "https://github.com/filecoin-project/ref-fvm.git", sha = "5603bcf" } +fvm_shared4 = { package = "fvm_shared", version = "~4.0.0-alpha.2", features = ["testing", "proofs"] } gethostname = "0.4" git-version = "0.3" diff --git a/src/interpreter/vm.rs b/src/interpreter/vm.rs index c168b46a1c1..c1f596f660b 100644 --- a/src/interpreter/vm.rs +++ b/src/interpreter/vm.rs @@ -42,6 +42,13 @@ use fvm3::{ NetworkConfig as NetworkConfig_v3, }, }; +use fvm4::{ + executor::{DefaultExecutor as DefaultExecutor_v4, Executor as Executor_v4}, + machine::{ + DefaultMachine as DefaultMachine_v4, Machine as Machine_v4, + NetworkConfig as NetworkConfig_v4, + }, +}; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::{to_vec, RawBytes}; use fvm_shared2::clock::ChainEpoch; @@ -51,13 +58,19 @@ pub(in crate::interpreter) type ForestMachineV2 = DefaultMachine_v2, ForestExternsV2>; pub(in crate::interpreter) type ForestMachineV3 = DefaultMachine_v3, ForestExternsV3>; +pub(in crate::interpreter) type ForestMachineV4 = + DefaultMachine_v4, ForestExternsV4>; type ForestKernelV2 = fvm2::DefaultKernel>>; type ForestKernelV3 = fvm3::DefaultKernel>>; +type ForestKernelV4 = + fvm4::DefaultKernel>>; + type ForestExecutorV2 = DefaultExecutor_v2>; type ForestExecutorV3 = DefaultExecutor_v3>; +type ForestExecutorV4 = DefaultExecutor_v4>; /// Comes from const IMPLICIT_MESSAGE_GAS_LIMIT: i64 = i64::MAX / 2; @@ -122,6 +135,7 @@ impl BlockMessages { pub enum VM { VM2(ForestExecutorV2), VM3(ForestExecutorV3), + VM4(ForestExecutorV4), } pub struct ExecutionContext { @@ -167,7 +181,35 @@ where enable_tracing: VMTrace, ) -> Result { let network_version = chain_config.network_version(epoch); - if network_version >= NetworkVersion::V18 { + if network_version >= NetworkVersion::V21 { + let mut config = NetworkConfig_v4::new(network_version.into()); + // ChainId defines the chain ID used in the Ethereum JSON-RPC endpoint. + config.chain_id((chain_config.eth_chain_id as u64).into()); + if let NetworkChain::Devnet(_) = chain_config.network { + config.enable_actor_debugging(); + } + + let engine = multi_engine.v4.get(&config)?; + let mut context = config.for_epoch(epoch, timestamp, state_tree_root); + context.set_base_fee(base_fee.into()); + context.set_circulating_supply(circ_supply.into()); + context.tracing = enable_tracing.is_traced(); + + let fvm: ForestMachineV4 = ForestMachineV4::new( + &context, + Arc::clone(&chain_index.db), + ForestExternsV4::new( + RandWrapper::from(rand), + heaviest_tipset, + epoch, + state_tree_root, + chain_index, + chain_config, + ), + )?; + let exec: ForestExecutorV4 = DefaultExecutor_v4::new(engine, fvm)?; + Ok(VM::VM4(exec)) + } else if network_version >= NetworkVersion::V18 { let mut config = NetworkConfig_v3::new(network_version.into()); // ChainId defines the chain ID used in the Ethereum JSON-RPC endpoint. config.chain_id((chain_config.eth_chain_id as u64).into()); @@ -226,6 +268,7 @@ where match self { VM::VM2(fvm_executor) => Ok(fvm_executor.flush()?), VM::VM3(fvm_executor) => Ok(fvm_executor.flush()?), + VM::VM4(fvm_executor) => Ok(fvm_executor.flush()?), } } @@ -246,6 +289,16 @@ where Ok(None) } } + VM::VM4(fvm_executor) => { + if let Some(id) = fvm_executor.state_tree().lookup_id(&addr.into())? { + Ok(fvm_executor + .state_tree() + .get_actor(id)? + .map(ActorState::from)) + } else { + Ok(None) + } + } } } @@ -395,6 +448,14 @@ where )?; Ok(ret.into()) } + VM::VM4(fvm_executor) => { + let ret = fvm_executor.execute_message( + msg.into(), + fvm4::executor::ApplyKind::Implicit, + raw_length, + )?; + Ok(ret.into()) + } } } @@ -432,6 +493,19 @@ where bail!("encountered a database lookup error"); } + ret.into() + } + VM::VM4(fvm_executor) => { + let ret = fvm_executor.execute_message( + unsigned.into(), + fvm4::executor::ApplyKind::Explicit, + raw_length, + )?; + + if fvm_executor.externs().bail() { + bail!("encountered a database lookup error"); + } + ret.into() } }; diff --git a/src/shim/econ.rs b/src/shim/econ.rs index 576e704cc87..9b688e8f186 100644 --- a/src/shim/econ.rs +++ b/src/shim/econ.rs @@ -6,6 +6,7 @@ use std::ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Sub, SubAssign}; use fvm_shared2::econ::TokenAmount as TokenAmount_v2; use fvm_shared3::econ::TokenAmount as TokenAmount_v3; pub use fvm_shared3::{BLOCK_GAS_LIMIT, TOTAL_FILECOIN_BASE}; +use fvm_shared4::econ::TokenAmount as TokenAmount_v4; use num_bigint::BigInt; use num_traits::Zero; use once_cell::sync::Lazy; @@ -115,15 +116,9 @@ impl TokenAmount { } } -impl From for TokenAmount { - fn from(other: TokenAmount_v3) -> Self { - TokenAmount(other) - } -} - impl From for TokenAmount { fn from(other: TokenAmount_v2) -> Self { - TokenAmount::from(TokenAmount_v3::from_atto(other.atto().clone())) + (&other).into() } } @@ -133,9 +128,27 @@ impl From<&TokenAmount_v2> for TokenAmount { } } +impl From for TokenAmount { + fn from(other: TokenAmount_v3) -> Self { + TokenAmount(other) + } +} + impl From<&TokenAmount_v3> for TokenAmount { fn from(other: &TokenAmount_v3) -> Self { - TokenAmount(other.clone()) + other.clone().into() + } +} + +impl From for TokenAmount { + fn from(other: TokenAmount_v4) -> Self { + (&other).into() + } +} + +impl From<&TokenAmount_v4> for TokenAmount { + fn from(other: &TokenAmount_v4) -> Self { + TokenAmount(TokenAmount_v3::from_atto(other.atto().clone())) } } @@ -145,21 +158,33 @@ impl From for TokenAmount_v3 { } } +impl From for TokenAmount_v2 { + fn from(other: TokenAmount) -> TokenAmount_v2 { + (&other).into() + } +} + +impl From<&TokenAmount> for TokenAmount_v2 { + fn from(other: &TokenAmount) -> TokenAmount_v2 { + TokenAmount_v2::from_atto(other.atto().clone()) + } +} + impl From<&TokenAmount> for TokenAmount_v3 { fn from(other: &TokenAmount) -> TokenAmount_v3 { other.0.clone() } } -impl From for TokenAmount_v2 { - fn from(other: TokenAmount) -> TokenAmount_v2 { - TokenAmount_v2::from_atto(other.atto().clone()) +impl From for TokenAmount_v4 { + fn from(other: TokenAmount) -> TokenAmount_v4 { + (&other).into() } } -impl From<&TokenAmount> for TokenAmount_v2 { - fn from(other: &TokenAmount) -> TokenAmount_v2 { - TokenAmount_v2::from_atto(other.atto().clone()) +impl From<&TokenAmount> for TokenAmount_v4 { + fn from(other: &TokenAmount) -> TokenAmount_v4 { + TokenAmount_v4::from_atto(other.atto().clone()) } } diff --git a/src/shim/error.rs b/src/shim/error.rs index 85f6caa9b3f..20ef554a863 100644 --- a/src/shim/error.rs +++ b/src/shim/error.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0, MIT use fvm_shared2::error::ExitCode as ExitCodeV2; use fvm_shared3::error::ExitCode as ExitCodeV3; +use fvm_shared4::error::ExitCode as ExitCodeV4; use serde::{Deserialize, Serialize}; /// `Newtype` wrapper for the FVM `ExitCode`. @@ -37,6 +38,12 @@ impl From for ExitCode { } } +impl From for ExitCode { + fn from(value: ExitCodeV4) -> Self { + value.value().into() + } +} + impl From for ExitCode { fn from(value: ExitCodeV3) -> Self { Self(value) @@ -45,7 +52,7 @@ impl From for ExitCode { impl From for ExitCode { fn from(value: ExitCodeV2) -> Self { - Self::from(value.value()) + value.value().into() } } @@ -60,3 +67,9 @@ impl From for ExitCodeV3 { value.0 } } + +impl From for ExitCodeV4 { + fn from(value: ExitCode) -> Self { + Self::new(value.0.value()) + } +} diff --git a/src/shim/executor.rs b/src/shim/executor.rs index e888aa251ff..a20a951b059 100644 --- a/src/shim/executor.rs +++ b/src/shim/executor.rs @@ -6,16 +6,19 @@ use crate::shim::econ::TokenAmount; use cid::Cid; use fvm2::executor::ApplyRet as ApplyRet_v2; use fvm3::executor::ApplyRet as ApplyRet_v3; +use fvm4::executor::ApplyRet as ApplyRet_v4; use fvm_ipld_encoding::RawBytes; use fvm_shared2::receipt::Receipt as Receipt_v2; use fvm_shared3::error::ExitCode; pub use fvm_shared3::receipt::Receipt as Receipt_v3; +use fvm_shared4::receipt::Receipt as Receipt_v4; use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[derive(Clone, Debug)] pub enum ApplyRet { V2(Box), V3(Box), + V4(Box), } impl From for ApplyRet { @@ -30,11 +33,18 @@ impl From for ApplyRet { } } +impl From for ApplyRet { + fn from(other: ApplyRet_v4) -> Self { + ApplyRet::V4(Box::new(other)) + } +} + impl ApplyRet { pub fn failure_info(&self) -> Option { match self { ApplyRet::V2(v2) => v2.failure_info.as_ref().map(|failure| failure.to_string()), ApplyRet::V3(v3) => v3.failure_info.as_ref().map(|failure| failure.to_string()), + ApplyRet::V4(v4) => v4.failure_info.as_ref().map(|failure| failure.to_string()), } } @@ -42,6 +52,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => (&v2.miner_tip).into(), ApplyRet::V3(v3) => (&v3.miner_tip).into(), + ApplyRet::V4(v4) => (&v4.miner_tip).into(), } } @@ -49,6 +60,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => (&v2.penalty).into(), ApplyRet::V3(v3) => (&v3.penalty).into(), + ApplyRet::V4(v4) => (&v4.penalty).into(), } } @@ -56,6 +68,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => Receipt::V2(v2.msg_receipt.clone()), ApplyRet::V3(v3) => Receipt::V3(v3.msg_receipt.clone()), + ApplyRet::V4(v4) => Receipt::V4(v4.msg_receipt.clone()), } } @@ -63,6 +76,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => (&v2.refund).into(), ApplyRet::V3(v3) => (&v3.refund).into(), + ApplyRet::V4(v4) => (&v4.refund).into(), } } @@ -70,6 +84,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => (&v2.base_fee_burn).into(), ApplyRet::V3(v3) => (&v3.base_fee_burn).into(), + ApplyRet::V4(v4) => (&v4.base_fee_burn).into(), } } @@ -77,6 +92,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => (&v2.over_estimation_burn).into(), ApplyRet::V3(v3) => (&v3.over_estimation_burn).into(), + ApplyRet::V4(v4) => (&v4.over_estimation_burn).into(), } } @@ -84,6 +100,7 @@ impl ApplyRet { match self { ApplyRet::V2(v2) => v2.exec_trace.iter().cloned().map(Into::into).collect(), ApplyRet::V3(v3) => v3.exec_trace.iter().cloned().map(Into::into).collect(), + ApplyRet::V4(v4) => v4.exec_trace.iter().cloned().map(Into::into).collect(), } } } @@ -92,6 +109,7 @@ impl ApplyRet { pub enum Receipt { V2(Receipt_v2), V3(Receipt_v3), + V4(Receipt_v4), } impl Serialize for Receipt { @@ -102,6 +120,7 @@ impl Serialize for Receipt { match self { Receipt::V2(v2) => v2.serialize(serializer), Receipt::V3(v3) => v3.serialize(serializer), + Receipt::V4(v4) => v4.serialize(serializer), } } } @@ -120,6 +139,7 @@ impl Receipt { match self { Receipt::V2(v2) => ExitCode::new(v2.exit_code.value()), Receipt::V3(v3) => v3.exit_code, + Receipt::V4(v4) => ExitCode::new(v4.exit_code.value()), } } @@ -127,6 +147,7 @@ impl Receipt { match self { Receipt::V2(v2) => RawBytes::from(v2.return_data.to_vec()), Receipt::V3(v3) => v3.return_data.clone(), + Receipt::V4(v4) => RawBytes::from(v4.return_data.to_vec()), } } @@ -134,12 +155,14 @@ impl Receipt { match self { Receipt::V2(v2) => v2.gas_used as u64, Receipt::V3(v3) => v3.gas_used, + Receipt::V4(v4) => v4.gas_used, } } pub fn events_root(&self) -> Option { match self { Receipt::V2(_) => None, Receipt::V3(v3) => v3.events_root, + Receipt::V4(v4) => v4.events_root, } } } diff --git a/src/shim/externs.rs b/src/shim/externs.rs index 088caeb31cc..ae903474b6c 100644 --- a/src/shim/externs.rs +++ b/src/shim/externs.rs @@ -3,8 +3,10 @@ use fvm2::externs::Rand as Rand_v2; use fvm3::externs::Rand as Rand_v3; +use fvm4::externs::Rand as Rand_v4; use fvm_shared2::clock::ChainEpoch as ChainEpoch_v2; use fvm_shared3::clock::ChainEpoch as ChainEpoch_v3; +use fvm_shared4::clock::ChainEpoch as ChainEpoch_v4; #[derive(Clone, Debug)] pub struct RandWrapper { @@ -45,6 +47,16 @@ impl Rand_v3 for RandWrapper { } } +impl Rand_v4 for RandWrapper { + fn get_chain_randomness(&self, round: ChainEpoch_v4) -> anyhow::Result<[u8; 32]> { + self.chain_rand.get_chain_randomness(round) + } + + fn get_beacon_randomness(&self, round: ChainEpoch_v4) -> anyhow::Result<[u8; 32]> { + self.chain_rand.get_beacon_randomness(round) + } +} + impl From for RandWrapper { fn from(chain_rand: T) -> Self { RandWrapper { chain_rand } diff --git a/src/shim/gas.rs b/src/shim/gas.rs index 7a98d264852..c3d1d38ef76 100644 --- a/src/shim/gas.rs +++ b/src/shim/gas.rs @@ -6,18 +6,13 @@ use fvm2::gas::{ price_list_by_network_version as price_list_by_network_version_v2, Gas as GasV2, GasCharge as GasChargeV2, PriceList as PriceListV2, }; -pub use fvm3::gas::Gas as GasV3; -pub use fvm3::gas::GasCharge as GasChargeV3; -pub use fvm3::gas::GasTracker; use fvm3::gas::{ - price_list_by_network_version as price_list_by_network_version_v3, PriceList as PriceListV3, + price_list_by_network_version as price_list_by_network_version_v3, Gas as GasV3, MILLIGAS_PRECISION, }; -pub use fvm4::gas::Gas as GasV4; -pub use fvm4::gas::GasCharge as GasChargeV4; -use fvm4::gas::{ - price_list_by_network_version as price_list_by_network_version_v4, PriceList as PriceListV4, -}; +pub use fvm3::gas::{GasCharge as GasChargeV3, GasTracker, PriceList as PriceListV3}; +use fvm4::gas::price_list_by_network_version as price_list_by_network_version_v4; +pub use fvm4::gas::{Gas as GasV4, GasCharge as GasChargeV4, PriceList as PriceListV4}; use crate::shim::version::NetworkVersion; @@ -128,18 +123,43 @@ impl From for GasCharge { } } +impl From for GasCharge { + fn from(value: GasChargeV4) -> Self { + GasChargeV3 { + name: value.name, + compute_gas: GasV3::from_milligas(value.compute_gas.as_milligas()), + other_gas: GasV3::from_milligas(value.other_gas.as_milligas()), + // FIXME: elapsed: value.elapsed.into(), + elapsed: Default::default(), + } + .into() + } +} + +impl From for GasChargeV2 { + fn from(value: GasCharge) -> Self { + Self { + name: value.0.name, + compute_gas: GasV2::from_milligas(value.0.compute_gas.as_milligas() as _), + storage_gas: GasV2::from_milligas(value.0.other_gas.as_milligas() as _), + } + } +} + impl From for GasChargeV3 { fn from(value: GasCharge) -> Self { value.0 } } -impl From for GasChargeV2 { +impl From for GasChargeV4 { fn from(value: GasCharge) -> Self { - GasChargeV2 { + Self { name: value.0.name, - compute_gas: GasV2::from_milligas(value.0.compute_gas.as_milligas() as i64), - storage_gas: GasV2::from_milligas(value.0.other_gas.as_milligas() as i64), + compute_gas: GasV4::from_milligas(value.0.compute_gas.as_milligas() as _), + other_gas: GasV4::from_milligas(value.0.other_gas.as_milligas() as _), + // FIXME: elapsed: value.elapsed.into(), + elapsed: Default::default(), } } } @@ -147,6 +167,7 @@ impl From for GasChargeV2 { pub enum PriceList { V2(&'static PriceListV2), V3(&'static PriceListV3), + V4(&'static PriceListV4), } impl PriceList { @@ -154,6 +175,7 @@ impl PriceList { match self { PriceList::V2(list) => list.on_block_open_base().into(), PriceList::V3(list) => list.on_block_open_base().into(), + PriceList::V4(list) => list.on_block_open_base().into(), } } @@ -163,6 +185,9 @@ impl PriceList { PriceList::V3(list) => list .on_block_link(fvm3::kernel::SupportedHashes::Blake2b256, data_size) .into(), + PriceList::V4(list) => list + .on_block_link(fvm4::kernel::SupportedHashes::Blake2b256, data_size) + .into(), } } @@ -170,6 +195,7 @@ impl PriceList { match self { PriceList::V2(list) => list.on_chain_message(msg_size).into(), PriceList::V3(list) => list.on_chain_message(msg_size).into(), + PriceList::V4(list) => list.on_chain_message(msg_size).into(), } } } @@ -179,16 +205,25 @@ impl From<&'static PriceListV2> for PriceList { PriceList::V2(value) } } + impl From<&'static PriceListV3> for PriceList { fn from(value: &'static PriceListV3) -> Self { PriceList::V3(value) } } +impl From<&'static PriceListV4> for PriceList { + fn from(value: &'static PriceListV4) -> Self { + PriceList::V4(value) + } +} + pub fn price_list_by_network_version(network_version: NetworkVersion) -> PriceList { if network_version < NetworkVersion::V18 { price_list_by_network_version_v2(network_version.into()).into() - } else { + } else if network_version < NetworkVersion::V21 { price_list_by_network_version_v3(network_version.into()).into() + } else { + price_list_by_network_version_v4(network_version.into()).into() } } diff --git a/src/shim/kernel.rs b/src/shim/kernel.rs index a16ce54baba..9e738d40956 100644 --- a/src/shim/kernel.rs +++ b/src/shim/kernel.rs @@ -4,8 +4,10 @@ use self::ErrorNumber as NShim; use self::SyscallError as EShim; use fvm2::kernel::SyscallError as E2; use fvm3::kernel::SyscallError as E3; +use fvm4::kernel::SyscallError as E4; use fvm_shared2::error::ErrorNumber as N2; use fvm_shared3::error::ErrorNumber as N3; +use fvm_shared4::error::ErrorNumber as N4; use itertools::Either; use std::fmt; @@ -26,7 +28,7 @@ macro_rules! error_number { fn from(value: N2) -> Self { match value { $(N2::$variant => Self::$variant,)* - u => Self::Unknown(UnknownErrorNumber(Either::Left(u))), + u => Self::Unknown(UnknownErrorNumber::N2(u)), } } } @@ -35,16 +37,25 @@ macro_rules! error_number { fn from(value: N3) -> Self { match value { $(N3::$variant => Self::$variant,)* - u => Self::Unknown(UnknownErrorNumber(Either::Right(u))), + u => Self::Unknown(UnknownErrorNumber::N3(u)), + } + } + } + + impl From for ErrorNumber { + fn from(value: N4) -> Self { + match value { + $(N4::$variant => Self::$variant,)* + u => Self::Unknown(UnknownErrorNumber::N4(u)), } } } impl ErrorNumber { - fn as_unshimmed(&self) -> Either { + fn as_unshimmed(&self) -> Either> { match self { - $(Self::$variant => Either::Right(N3::$variant),)* - Self::Unknown(UnknownErrorNumber(u)) => *u, + $(Self::$variant => Either::Left(N3::$variant),)* + Self::Unknown(u) => u.as_unshimmed(), } } } @@ -52,7 +63,21 @@ macro_rules! error_number { } #[derive(Debug, Clone)] -pub struct UnknownErrorNumber(Either); +pub enum UnknownErrorNumber { + N2(N2), + N3(N3), + N4(N4), +} + +impl UnknownErrorNumber { + fn as_unshimmed(&self) -> Either> { + match self { + Self::N2(n) => Either::Right(Either::Right(*n)), + Self::N3(n) => Either::Left(*n), + Self::N4(n) => Either::Right(Either::Left(*n)), + } + } +} error_number! { IllegalArgument, @@ -101,3 +126,13 @@ impl From for EShim { } } } + +impl From for EShim { + fn from(value: E4) -> Self { + let E4(message, number) = value; + Self { + message, + number: number.into(), + } + } +} diff --git a/src/shim/machine/mod.rs b/src/shim/machine/mod.rs index 09bfe0ec80f..da72f2b0eb0 100644 --- a/src/shim/machine/mod.rs +++ b/src/shim/machine/mod.rs @@ -3,12 +3,14 @@ use fvm2::machine::MultiEngine as MultiEngine_v2; use fvm3::engine::MultiEngine as MultiEngine_v3; +use fvm4::engine::MultiEngine as MultiEngine_v4; mod manifest; pub use manifest::*; pub struct MultiEngine { pub v2: MultiEngine_v2, pub v3: MultiEngine_v3, + pub v4: MultiEngine_v4, } impl Default for MultiEngine { @@ -19,9 +21,11 @@ impl Default for MultiEngine { impl MultiEngine { pub fn new(concurrency: Result) -> MultiEngine { + let concurrency = concurrency.ok(); MultiEngine { - v2: MultiEngine_v2::new(), - v3: MultiEngine_v3::new(concurrency.unwrap_or(1)), // `1` is default concurrency value in `fvm3` + v2: Default::default(), + v3: concurrency.map_or_else(Default::default, MultiEngine_v3::new), + v4: concurrency.map_or_else(Default::default, MultiEngine_v4::new), } } } diff --git a/src/shim/message.rs b/src/shim/message.rs index 7d4440f92ec..e00403925ab 100644 --- a/src/shim/message.rs +++ b/src/shim/message.rs @@ -8,6 +8,7 @@ use fvm_ipld_encoding::{Error as EncError, RawBytes}; use fvm_shared2::message::Message as Message_v2; pub use fvm_shared3::message::Message as Message_v3; pub use fvm_shared3::METHOD_SEND; +use fvm_shared4::message::Message as Message_v4; use serde::{Deserialize, Serialize}; use crate::shim::{address::Address, econ::TokenAmount}; @@ -33,9 +34,9 @@ pub struct Message { pub gas_premium: TokenAmount, } -impl From for Message { - fn from(other: Message_v3) -> Self { - Message { +impl From for Message { + fn from(other: Message_v4) -> Self { + Self { version: other.version, from: other.from.into(), to: other.to.into(), @@ -50,9 +51,16 @@ impl From for Message { } } -impl From for Message_v3 { +impl From for Message_v4 { fn from(other: Message) -> Self { - Message_v3 { + (&other).into() + } +} + +impl From<&Message> for Message_v4 { + fn from(other: &Message) -> Self { + let other: Message = other.clone(); + Self { version: other.version, from: other.from.into(), to: other.to.into(), @@ -67,10 +75,33 @@ impl From for Message_v3 { } } +impl From for Message { + fn from(other: Message_v3) -> Self { + Self { + version: other.version, + from: other.from.into(), + to: other.to.into(), + sequence: other.sequence, + value: other.value.into(), + method_num: other.method_num, + params: other.params, + gas_limit: other.gas_limit, + gas_fee_cap: other.gas_fee_cap.into(), + gas_premium: other.gas_premium.into(), + } + } +} + +impl From for Message_v3 { + fn from(other: Message) -> Self { + (&other).into() + } +} + impl From<&Message> for Message_v3 { fn from(other: &Message) -> Self { let other: Message = other.clone(); - Message_v3 { + Self { version: other.version, from: other.from.into(), to: other.to.into(), @@ -87,7 +118,7 @@ impl From<&Message> for Message_v3 { impl From for Message { fn from(other: Message_v2) -> Self { - Message { + Self { version: other.version as u64, from: other.from.into(), to: other.to.into(), @@ -104,7 +135,7 @@ impl From for Message { impl From for Message_v2 { fn from(other: Message) -> Self { - Message_v2 { + Self { version: other.version as i64, from: other.from.into(), to: other.to.into(), @@ -121,19 +152,7 @@ impl From for Message_v2 { impl From<&Message> for Message_v2 { fn from(other: &Message) -> Self { - let other: Message = other.clone(); - Message_v2 { - version: other.version as i64, - from: other.from.into(), - to: other.to.into(), - sequence: other.sequence, - value: other.value.into(), - method_num: other.method_num, - params: other.params, - gas_limit: other.gas_limit as i64, - gas_fee_cap: other.gas_fee_cap.into(), - gas_premium: other.gas_premium.into(), - } + other.clone().into() } } diff --git a/src/shim/state_tree.rs b/src/shim/state_tree.rs index f2e74c91e7b..c413b82f109 100644 --- a/src/shim/state_tree.rs +++ b/src/shim/state_tree.rs @@ -9,6 +9,7 @@ use anyhow::{anyhow, bail, Context}; use cid::Cid; pub use fvm2::state_tree::{ActorState as ActorStateV2, StateTree as StateTreeV2}; pub use fvm3::state_tree::{ActorState as ActorStateV3, StateTree as StateTreeV3}; +pub use fvm4::state_tree::{ActorState as ActorStateV4, StateTree as StateTreeV4}; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::repr::{Deserialize_repr, Serialize_repr}; use fvm_shared2::state::StateTreeVersion as StateTreeVersionV2; @@ -293,18 +294,6 @@ impl DerefMut for ActorState { } } -impl From for ActorState { - fn from(value: ActorStateV3) -> Self { - ActorState(value) - } -} - -impl From<&ActorStateV3> for ActorState { - fn from(value: &ActorStateV3) -> Self { - ActorState(value.clone()) - } -} - impl From for ActorState { fn from(value: ActorStateV2) -> Self { ActorState(ActorStateV3 { @@ -329,9 +318,35 @@ impl From<&ActorStateV2> for ActorState { } } -impl From for ActorStateV3 { - fn from(other: ActorState) -> Self { - other.0 +impl From for ActorState { + fn from(value: ActorStateV3) -> Self { + ActorState(value) + } +} + +impl From<&ActorStateV3> for ActorState { + fn from(value: &ActorStateV3) -> Self { + value.clone().into() + } +} + +impl From for ActorState { + fn from(value: ActorStateV4) -> Self { + ActorState(ActorStateV3 { + code: value.code, + state: value.state, + sequence: value.sequence, + balance: TokenAmount::from(value.balance).into(), + delegated_address: value + .delegated_address + .map(|addr| Address::from(addr).into()), + }) + } +} + +impl From<&ActorStateV4> for ActorState { + fn from(value: &ActorStateV4) -> Self { + value.clone().into() } } @@ -357,6 +372,12 @@ impl From<&ActorState> for ActorStateV2 { } } +impl From for ActorStateV3 { + fn from(other: ActorState) -> Self { + other.0 + } +} + #[cfg(test)] mod tests { use super::StateTree; diff --git a/src/shim/trace.rs b/src/shim/trace.rs index 25ffabf3cf3..924e1e0eead 100644 --- a/src/shim/trace.rs +++ b/src/shim/trace.rs @@ -8,6 +8,7 @@ use crate::shim::{ }; use fvm2::trace::ExecutionEvent as E2; use fvm3::trace::ExecutionEvent as E3; +use fvm4::trace::ExecutionEvent as E4; use fvm_ipld_encoding::{ipld_block::IpldBlock, RawBytes}; use itertools::Either; @@ -19,7 +20,7 @@ pub enum ExecutionEvent { CallAbort(ShimExitCode), CallError(ShimSyscallError), Log(String), - Unknown(Either), + Unknown(Either>), } #[derive(Debug, Clone, Eq, PartialEq)] @@ -66,7 +67,7 @@ impl From for ExecutionEvent { E2::CallAbort(ab) => EShim::CallAbort(ab.into()), E2::CallError(err) => EShim::CallError(err.into()), E2::Log(s) => EShim::Log(s), - e => EShim::Unknown(Either::Left(e)), + e => EShim::Unknown(Either::Right(Either::Right(e))), } } } @@ -97,7 +98,38 @@ impl From for ExecutionEvent { data: Either::Right(data), }), E3::CallError(err) => EShim::CallError(err.into()), - e => EShim::Unknown(Either::Right(e)), + e => EShim::Unknown(Either::Left(e)), + } + } +} + +impl From for ExecutionEvent { + fn from(value: E4) -> Self { + match value { + E4::GasCharge(gc) => EShim::GasCharge(gc.into()), + E4::Call { + from, + to, + method, + params, + value, + gas_limit, + read_only, + } => EShim::Call(Call { + from, + to: to.into(), + method_num: method, + params: Either::Right(params), + value: value.into(), + gas_limit: Some(gas_limit), + read_only: Some(read_only), + }), + E4::CallReturn(exit_code, data) => EShim::CallReturn(CallReturn { + exit_code: Some(exit_code.into()), + data: Either::Right(data), + }), + E4::CallError(err) => EShim::CallError(err.into()), + e => EShim::Unknown(Either::Right(Either::Left(e))), } } } diff --git a/src/shim/version.rs b/src/shim/version.rs index b278d1fa75d..1fb4ab21de2 100644 --- a/src/shim/version.rs +++ b/src/shim/version.rs @@ -4,6 +4,7 @@ use std::ops::{Deref, DerefMut}; pub use fvm_shared2::version::NetworkVersion as NetworkVersion_v2; use fvm_shared3::version::NetworkVersion as NetworkVersion_v3; +use fvm_shared4::version::NetworkVersion as NetworkVersion_v4; use serde::{Deserialize, Serialize}; /// Specifies the network version @@ -22,34 +23,35 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd, Serialize, Deserialize)] #[repr(transparent)] #[serde(transparent)] -pub struct NetworkVersion(pub NetworkVersion_v3); +pub struct NetworkVersion(pub NetworkVersion_v4); impl NetworkVersion { - pub const V0: Self = Self(NetworkVersion_v3::new(0)); - pub const V1: Self = Self(NetworkVersion_v3::new(1)); - pub const V2: Self = Self(NetworkVersion_v3::new(2)); - pub const V3: Self = Self(NetworkVersion_v3::new(3)); - pub const V4: Self = Self(NetworkVersion_v3::new(4)); - pub const V5: Self = Self(NetworkVersion_v3::new(5)); - pub const V6: Self = Self(NetworkVersion_v3::new(6)); - pub const V7: Self = Self(NetworkVersion_v3::new(7)); - pub const V8: Self = Self(NetworkVersion_v3::new(8)); - pub const V9: Self = Self(NetworkVersion_v3::new(9)); - pub const V10: Self = Self(NetworkVersion_v3::new(10)); - pub const V11: Self = Self(NetworkVersion_v3::new(11)); - pub const V12: Self = Self(NetworkVersion_v3::new(12)); - pub const V13: Self = Self(NetworkVersion_v3::new(13)); - pub const V14: Self = Self(NetworkVersion_v3::new(14)); - pub const V15: Self = Self(NetworkVersion_v3::new(15)); - pub const V16: Self = Self(NetworkVersion_v3::new(16)); - pub const V17: Self = Self(NetworkVersion_v3::new(17)); - pub const V18: Self = Self(NetworkVersion_v3::new(18)); - pub const V19: Self = Self(NetworkVersion_v3::new(19)); - pub const V20: Self = Self(NetworkVersion_v3::new(20)); + pub const V0: Self = Self(NetworkVersion_v4::new(0)); + pub const V1: Self = Self(NetworkVersion_v4::new(1)); + pub const V2: Self = Self(NetworkVersion_v4::new(2)); + pub const V3: Self = Self(NetworkVersion_v4::new(3)); + pub const V4: Self = Self(NetworkVersion_v4::new(4)); + pub const V5: Self = Self(NetworkVersion_v4::new(5)); + pub const V6: Self = Self(NetworkVersion_v4::new(6)); + pub const V7: Self = Self(NetworkVersion_v4::new(7)); + pub const V8: Self = Self(NetworkVersion_v4::new(8)); + pub const V9: Self = Self(NetworkVersion_v4::new(9)); + pub const V10: Self = Self(NetworkVersion_v4::new(10)); + pub const V11: Self = Self(NetworkVersion_v4::new(11)); + pub const V12: Self = Self(NetworkVersion_v4::new(12)); + pub const V13: Self = Self(NetworkVersion_v4::new(13)); + pub const V14: Self = Self(NetworkVersion_v4::new(14)); + pub const V15: Self = Self(NetworkVersion_v4::new(15)); + pub const V16: Self = Self(NetworkVersion_v4::new(16)); + pub const V17: Self = Self(NetworkVersion_v4::new(17)); + pub const V18: Self = Self(NetworkVersion_v4::new(18)); + pub const V19: Self = Self(NetworkVersion_v4::new(19)); + pub const V20: Self = Self(NetworkVersion_v4::new(20)); + pub const V21: Self = Self(NetworkVersion_v4::new(21)); } impl Deref for NetworkVersion { - type Target = NetworkVersion_v3; + type Target = NetworkVersion_v4; fn deref(&self) -> &Self::Target { &self.0 } @@ -63,24 +65,36 @@ impl DerefMut for NetworkVersion { impl From for NetworkVersion { fn from(value: NetworkVersion_v2) -> Self { - NetworkVersion(NetworkVersion_v3::from(value as u32)) + NetworkVersion((value as u32).into()) } } impl From for NetworkVersion { fn from(value: NetworkVersion_v3) -> Self { + NetworkVersion(u32::from(value).into()) + } +} + +impl From for NetworkVersion { + fn from(value: NetworkVersion_v4) -> Self { NetworkVersion(value) } } +impl From for NetworkVersion_v2 { + fn from(other: NetworkVersion) -> NetworkVersion_v2 { + u32::from(other.0).try_into().expect("Infallible") + } +} + impl From for NetworkVersion_v3 { fn from(other: NetworkVersion) -> Self { - other.0 + u32::from(other.0).into() } } -impl From for NetworkVersion_v2 { - fn from(other: NetworkVersion) -> NetworkVersion_v2 { - NetworkVersion_v2::try_from(u32::from(other.0)).unwrap() +impl From for NetworkVersion_v4 { + fn from(other: NetworkVersion) -> Self { + other.0 } } From cbedcd3b18066461fcef6f78acbdd8c5b1d86469 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 27 Sep 2023 15:16:12 +0800 Subject: [PATCH 03/11] add tracking ticket to FIXME --- src/shim/gas.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shim/gas.rs b/src/shim/gas.rs index c3d1d38ef76..6ad894fb4b7 100644 --- a/src/shim/gas.rs +++ b/src/shim/gas.rs @@ -129,7 +129,8 @@ impl From for GasCharge { name: value.name, compute_gas: GasV3::from_milligas(value.compute_gas.as_milligas()), other_gas: GasV3::from_milligas(value.other_gas.as_milligas()), - // FIXME: elapsed: value.elapsed.into(), + // FIXME: https://github.com/filecoin-project/ref-fvm/pull/1897 + // elapsed: value.elapsed.into(), elapsed: Default::default(), } .into() @@ -158,7 +159,8 @@ impl From for GasChargeV4 { name: value.0.name, compute_gas: GasV4::from_milligas(value.0.compute_gas.as_milligas() as _), other_gas: GasV4::from_milligas(value.0.other_gas.as_milligas() as _), - // FIXME: elapsed: value.elapsed.into(), + // FIXME: https://github.com/filecoin-project/ref-fvm/pull/1897 + // elapsed: value.elapsed.into(), elapsed: Default::default(), } } From 90717e157b857e6d6a494c3a254e926bc9311f45 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 27 Sep 2023 16:42:18 +0800 Subject: [PATCH 04/11] update shim to use latest fvm types --- src/shim/address.rs | 99 ++++++++--------- src/shim/bigint.rs | 14 +-- src/shim/clock.rs | 12 ++- src/shim/crypto.rs | 10 +- src/shim/deal.rs | 2 +- src/shim/econ.rs | 59 ++++++----- src/shim/mod.rs | 7 ++ src/shim/piece.rs | 52 +++++++-- src/shim/randomness.rs | 30 ++++-- src/shim/state_tree.rs | 152 ++++++++++++++++++--------- src/shim/state_tree_v0.rs | 2 +- src/shim/version.rs | 49 ++++----- src/statediff/mod.rs | 2 +- src/tool/subcommands/snapshot_cmd.rs | 2 +- 14 files changed, 306 insertions(+), 186 deletions(-) diff --git a/src/shim/address.rs b/src/shim/address.rs index 1ca018c8c33..7ec8f13ede9 100644 --- a/src/shim/address.rs +++ b/src/shim/address.rs @@ -11,8 +11,9 @@ use data_encoding::Encoding; use data_encoding_macro::new_encoding; use fvm_shared2::address::Address as Address_v2; use fvm_shared3::address::Address as Address_v3; -pub use fvm_shared3::address::{Error, Network, Payload, Protocol, BLS_PUB_LEN, PAYLOAD_HASH_LEN}; use fvm_shared4::address::Address as Address_v4; +use fvm_shared4::address::Address as Address_latest; +pub use fvm_shared4::address::{Error, Network, Payload, Protocol, BLS_PUB_LEN, PAYLOAD_HASH_LEN}; use integer_encoding::VarInt; use num_traits::FromPrimitive; use once_cell::sync::Lazy; @@ -109,7 +110,7 @@ impl Drop for NetworkGuard { )] #[serde(transparent)] #[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))] -pub struct Address(Address_v3); +pub struct Address(Address_latest); impl Address { pub const SYSTEM_ACTOR: Address = Address::new_id(0); @@ -126,23 +127,23 @@ impl Address { pub const BURNT_FUNDS_ACTOR: Address = Address::new_id(99); pub const fn new_id(id: u64) -> Self { - Address(Address_v3::new_id(id)) + Address(Address_latest::new_id(id)) } pub fn new_actor(data: &[u8]) -> Self { - Address(Address_v3::new_actor(data)) + Address(Address_latest::new_actor(data)) } pub fn new_bls(pubkey: &[u8]) -> Result { - Address_v3::new_bls(pubkey).map(Address::from) + Address_latest::new_bls(pubkey).map(Address::from) } pub fn new_secp256k1(pubkey: &[u8]) -> Result { - Address_v3::new_secp256k1(pubkey).map(Address::from) + Address_latest::new_secp256k1(pubkey).map(Address::from) } pub fn new_delegated(ns: u64, subaddress: &[u8]) -> Result { - Ok(Self(Address_v3::new_delegated(ns, subaddress)?)) + Ok(Self(Address_latest::new_delegated(ns, subaddress)?)) } pub fn protocol(&self) -> Protocol { @@ -154,12 +155,12 @@ impl Address { } pub fn from_bytes(bz: &[u8]) -> Result { - Address_v3::from_bytes(bz).map(Address) + Address_latest::from_bytes(bz).map(Address) } } impl FromStr for Address { - type Err = ::Err; + type Err = ::Err; fn from_str(s: &str) -> Result { Network::Testnet @@ -177,7 +178,7 @@ const ADDRESS_ENCODER: Encoding = new_encoding! { impl Display for Address { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use fvm_shared3::address::CHECKSUM_HASH_LEN; + use fvm_shared4::address::CHECKSUM_HASH_LEN; const MAINNET_PREFIX: &str = "f"; const TESTNET_PREFIX: &str = "t"; @@ -234,7 +235,7 @@ impl Display for Address { } impl Deref for Address { - type Target = Address_v3; + type Target = Address_latest; fn deref(&self) -> &Self::Target { &self.0 } @@ -266,7 +267,7 @@ impl Display for StrictAddress { } impl FromStr for StrictAddress { - type Err = ::Err; + type Err = ::Err; fn from_str(s: &str) -> Result { let fvm_addr = CurrentNetwork::get().parse_address(s)?; @@ -305,72 +306,64 @@ impl From for Address_v4 { impl From for Address { fn from(other: Address_v4) -> Self { - (&other).into() - } -} - -impl From for Address { - fn from(other: Address_v3) -> Self { Address(other) } } -impl From for Address { - fn from(other: Address_v2) -> Self { - (&other).into() +impl From<&Address_v4> for Address { + fn from(other: &Address_v4) -> Self { + Address(*other) } } -impl From<&Address_v2> for Address { - fn from(other: &Address_v2) -> Self { +impl From<&Address_v3> for Address { + fn from(other: &Address_v3) -> Self { Address::from( - Address_v3::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { - panic!("Couldn't convert from FVM2 address to FVM3 address: {other}, {e}") + Address_v4::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { + panic!("Couldn't convert from FVM3 address to FVM4 address: {other}, {e}") }), ) } } -impl From<&Address_v4> for Address { - fn from(other: &Address_v4) -> Self { +impl From for Address { + fn from(other: Address_v3) -> Self { + (&other).into() + } +} + +impl From<&Address_v2> for Address { + fn from(other: &Address_v2) -> Self { Address::from( - Address_v3::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { - panic!("Couldn't convert from FVM4 address to FVM3 address: {other}, {e}") + Address_v4::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { + panic!("Couldn't convert from FVM2 address to FVM4 address: {other}, {e}") }), ) } } -impl From
for Address_v4 { - fn from(other: Address) -> Address_v4 { +impl From for Address { + fn from(other: Address_v2) -> Self { (&other).into() } } -impl From<&Address_v3> for Address { - fn from(other: &Address_v3) -> Self { - Address(*other) - } -} - -impl From
for Address_v2 { - fn from(other: Address) -> Address_v2 { - (&other).into() +impl From
for Address_v4 { + fn from(other: Address) -> Address_v4 { + other.0 } } -impl From<&Address> for Address_v2 { +impl From<&Address> for Address_v4 { fn from(other: &Address) -> Self { - Address_v2::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { - panic!("Couldn't convert from FVM3 address to FVM2 address: {other}, {e}") - }) + other.0 } } -impl From<&Address> for Address_v4 { +impl From<&Address> for Address_v3 { fn from(other: &Address) -> Self { - Address_v4::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { - panic!("Couldn't convert from FVM3 address to FVM4 address: {other}, {e}") + Address_v3::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { + panic!("Couldn't convert from FVM4 address to FVM3 address: {other}, {e}") }) } } @@ -381,9 +374,17 @@ impl From
for Address_v3 { } } -impl From<&Address> for Address_v3 { +impl From<&Address> for Address_v2 { fn from(other: &Address) -> Self { - other.0 + Address_v2::from_bytes(&other.to_bytes()).unwrap_or_else(|e| { + panic!("Couldn't convert from FVM4 address to FVM2 address: {other}, {e}") + }) + } +} + +impl From
for Address_v2 { + fn from(other: Address) -> Address_v2 { + (&other).into() } } diff --git a/src/shim/bigint.rs b/src/shim/bigint.rs index beccf2f7a91..52f6e5bc0f7 100644 --- a/src/shim/bigint.rs +++ b/src/shim/bigint.rs @@ -3,22 +3,22 @@ use std::ops::{Deref, DerefMut}; -pub use fvm_shared3::bigint::bigint_ser::{BigIntDe, BigIntSer}; -use fvm_shared3::bigint::{bigint_ser, BigInt as BigInt_v3}; +pub use fvm_shared4::bigint::bigint_ser::{BigIntDe, BigIntSer}; +use fvm_shared4::bigint::{bigint_ser, BigInt as BigInt_latest}; use serde::{Deserialize, Serialize}; #[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(transparent)] -pub struct BigInt(#[serde(with = "bigint_ser")] BigInt_v3); +pub struct BigInt(#[serde(with = "bigint_ser")] BigInt_latest); impl BigInt { - pub fn inner(&self) -> &BigInt_v3 { + pub fn inner(&self) -> &BigInt_latest { &self.0 } } impl Deref for BigInt { - type Target = BigInt_v3; + type Target = BigInt_latest; fn deref(&self) -> &Self::Target { &self.0 } @@ -30,8 +30,8 @@ impl DerefMut for BigInt { } } -impl From for BigInt { - fn from(other: BigInt_v3) -> Self { +impl From for BigInt { + fn from(other: BigInt_latest) -> Self { BigInt(other) } } diff --git a/src/shim/clock.rs b/src/shim/clock.rs index 1de29f318f9..9cf4c5266e4 100644 --- a/src/shim/clock.rs +++ b/src/shim/clock.rs @@ -1,8 +1,8 @@ // Copyright 2019-2023 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -pub use fvm_shared2::ALLOWABLE_CLOCK_DRIFT; -pub use fvm_shared2::BLOCKS_PER_EPOCH; +pub use fvm_shared4::ALLOWABLE_CLOCK_DRIFT; +pub use fvm_shared4::BLOCKS_PER_EPOCH; pub const SECONDS_IN_DAY: i64 = 86400; pub const EPOCHS_IN_DAY: i64 = SECONDS_IN_DAY / EPOCH_DURATION_SECONDS; @@ -27,4 +27,12 @@ mod tests { fvm_shared3::clock::EPOCH_DURATION_SECONDS ) } + + #[test] + fn fvm4_shim_of_const_epoch_duration_seconds() { + assert_eq!( + super::EPOCH_DURATION_SECONDS, + fvm_shared4::clock::EPOCH_DURATION_SECONDS + ) + } } diff --git a/src/shim/crypto.rs b/src/shim/crypto.rs index 76ac5734b9e..66455f9466f 100644 --- a/src/shim/crypto.rs +++ b/src/shim/crypto.rs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0, MIT use std::borrow::Cow; +use super::fvm_shared_latest::{self, commcid::Commitment}; +pub use super::fvm_shared_latest::{IPLD_RAW, TICKET_RANDOMNESS_LOOKBACK}; use bls_signatures::{verify_messages, PublicKey as BlsPubKey, Signature as BlsSignature}; use cid::Cid; use fvm_ipld_encoding::{ @@ -9,8 +11,6 @@ use fvm_ipld_encoding::{ repr::{Deserialize_repr, Serialize_repr}, ser, strict_bytes, }; -use fvm_shared2::commcid::Commitment; -pub use fvm_shared2::{IPLD_RAW, TICKET_RANDOMNESS_LOOKBACK}; use num::FromPrimitive; use num_derive::FromPrimitive; @@ -88,7 +88,7 @@ impl Signature { /// Checks if a signature is valid given data and address. pub fn verify(&self, data: &[u8], addr: &crate::shim::address::Address) -> Result<(), String> { - use fvm_shared3::crypto::signature::ops::{verify_bls_sig, verify_secp256k1_sig}; + use fvm_shared4::crypto::signature::ops::{verify_bls_sig, verify_secp256k1_sig}; match self.sig_type { SignatureType::Bls => verify_bls_sig(&self.bytes, data, addr), SignatureType::Secp256k1 => verify_secp256k1_sig(&self.bytes, data, addr), @@ -155,14 +155,14 @@ pub fn verify_bls_sig( data: &[u8], addr: &crate::shim::address::Address, ) -> Result<(), String> { - fvm_shared3::crypto::signature::ops::verify_bls_sig(signature, data, &addr.into()) + fvm_shared_latest::crypto::signature::ops::verify_bls_sig(signature, data, &addr.into()) } /// Extracts the raw replica commitment from a CID /// assuming that it has the correct hashing function and /// serialization types pub fn cid_to_replica_commitment_v1(c: &Cid) -> Result { - fvm_shared2::commcid::cid_to_replica_commitment_v1(c) + fvm_shared_latest::commcid::cid_to_replica_commitment_v1(c) } /// Signature variants for Filecoin signatures. diff --git a/src/shim/deal.rs b/src/shim/deal.rs index b08438790e2..5e163b60b30 100644 --- a/src/shim/deal.rs +++ b/src/shim/deal.rs @@ -1,4 +1,4 @@ // Copyright 2019-2023 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -pub use fvm_shared3::deal::DealID; +pub use super::fvm_shared_latest::deal::DealID; diff --git a/src/shim/econ.rs b/src/shim/econ.rs index 9b688e8f186..969666a01c0 100644 --- a/src/shim/econ.rs +++ b/src/shim/econ.rs @@ -3,6 +3,7 @@ use std::ops::{Add, AddAssign, Deref, DerefMut, Mul, MulAssign, Sub, SubAssign}; +use super::fvm_shared_latest::econ::TokenAmount as TokenAmount_latest; use fvm_shared2::econ::TokenAmount as TokenAmount_v2; use fvm_shared3::econ::TokenAmount as TokenAmount_v3; pub use fvm_shared3::{BLOCK_GAS_LIMIT, TOTAL_FILECOIN_BASE}; @@ -24,12 +25,12 @@ pub static TOTAL_FILECOIN: Lazy = // FIXME: Consider 'type TokenAmount = TokenAmount_v3' #[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug, Default)] #[serde(transparent)] -pub struct TokenAmount(TokenAmount_v3); +pub struct TokenAmount(TokenAmount_latest); #[cfg(test)] impl quickcheck::Arbitrary for TokenAmount { fn arbitrary(g: &mut quickcheck::Gen) -> Self { - use fvm_shared3::bigint::MAX_BIGINT_SIZE; + use fvm_shared4::bigint::MAX_BIGINT_SIZE; use num::BigUint; // During serialization/deserialization, permissible length of the byte // representation (plus a leading positive sign byte for non-zero @@ -47,7 +48,7 @@ impl quickcheck::Arbitrary for TokenAmount { impl Zero for TokenAmount { fn zero() -> Self { - TokenAmount(TokenAmount_v3::zero()) + TokenAmount(TokenAmount_latest::zero()) } fn is_zero(&self) -> bool { self.0.is_zero() @@ -55,7 +56,7 @@ impl Zero for TokenAmount { } impl Deref for TokenAmount { - type Target = TokenAmount_v3; + type Target = TokenAmount_latest; fn deref(&self) -> &Self::Target { &self.0 @@ -77,10 +78,10 @@ impl std::fmt::Display for TokenAmount { impl TokenAmount { /// The logical number of decimal places of a token unit. - pub const DECIMALS: usize = TokenAmount_v3::DECIMALS; + pub const DECIMALS: usize = TokenAmount_latest::DECIMALS; /// The logical precision of a token unit. - pub const PRECISION: u64 = TokenAmount_v3::PRECISION; + pub const PRECISION: u64 = TokenAmount_latest::PRECISION; /// Returns the quantity of indivisible units. pub fn atto(&self) -> &BigInt { @@ -124,67 +125,67 @@ impl From for TokenAmount { impl From<&TokenAmount_v2> for TokenAmount { fn from(other: &TokenAmount_v2) -> Self { - TokenAmount::from(TokenAmount_v3::from_atto(other.atto().clone())) + Self(TokenAmount_latest::from_atto(other.atto().clone())) } } -impl From for TokenAmount { - fn from(other: TokenAmount_v3) -> Self { - TokenAmount(other) +impl From<&TokenAmount_v3> for TokenAmount { + fn from(other: &TokenAmount_v3) -> Self { + Self(TokenAmount_latest::from_atto(other.atto().clone())) } } -impl From<&TokenAmount_v3> for TokenAmount { - fn from(other: &TokenAmount_v3) -> Self { - other.clone().into() +impl From for TokenAmount { + fn from(other: TokenAmount_v3) -> Self { + (&other).into() } } impl From for TokenAmount { fn from(other: TokenAmount_v4) -> Self { - (&other).into() + TokenAmount(other) } } impl From<&TokenAmount_v4> for TokenAmount { fn from(other: &TokenAmount_v4) -> Self { - TokenAmount(TokenAmount_v3::from_atto(other.atto().clone())) + other.clone().into() } } -impl From for TokenAmount_v3 { +impl From for TokenAmount_v2 { fn from(other: TokenAmount) -> Self { - other.0 + (&other).into() } } -impl From for TokenAmount_v2 { - fn from(other: TokenAmount) -> TokenAmount_v2 { - (&other).into() +impl From<&TokenAmount> for TokenAmount_v2 { + fn from(other: &TokenAmount) -> Self { + Self::from_atto(other.atto().clone()) } } -impl From<&TokenAmount> for TokenAmount_v2 { - fn from(other: &TokenAmount) -> TokenAmount_v2 { - TokenAmount_v2::from_atto(other.atto().clone()) +impl From for TokenAmount_v3 { + fn from(other: TokenAmount) -> Self { + (&other).into() } } impl From<&TokenAmount> for TokenAmount_v3 { - fn from(other: &TokenAmount) -> TokenAmount_v3 { - other.0.clone() + fn from(other: &TokenAmount) -> Self { + Self::from_atto(other.atto().clone()) } } impl From for TokenAmount_v4 { - fn from(other: TokenAmount) -> TokenAmount_v4 { - (&other).into() + fn from(other: TokenAmount) -> Self { + other.0 } } impl From<&TokenAmount> for TokenAmount_v4 { - fn from(other: &TokenAmount) -> TokenAmount_v4 { - TokenAmount_v4::from_atto(other.atto().clone()) + fn from(other: &TokenAmount) -> Self { + other.0.clone() } } diff --git a/src/shim/mod.rs b/src/shim/mod.rs index 3f82c30e33f..a10694ea85c 100644 --- a/src/shim/mod.rs +++ b/src/shim/mod.rs @@ -21,3 +21,10 @@ pub mod state_tree; pub mod state_tree_v0; pub mod trace; pub mod version; + +mod fvm_shared_latest { + pub use fvm_shared4::*; +} +mod fvm_latest { + pub use fvm4::*; +} diff --git a/src/shim/piece.rs b/src/shim/piece.rs index e3be0dd1af9..0412f18ac1c 100644 --- a/src/shim/piece.rs +++ b/src/shim/piece.rs @@ -1,33 +1,35 @@ // Copyright 2019-2023 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +use super::fvm_shared_latest::piece as piece_latest; use cid::Cid; use fvm_shared2::piece as piece_v2; use fvm_shared3::piece as piece_v3; +use fvm_shared4::piece as piece_v4; use serde::{Deserialize, Serialize}; /// Piece information for part or a whole file. #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] #[serde(transparent)] -pub struct PieceInfo(piece_v3::PieceInfo); +pub struct PieceInfo(piece_latest::PieceInfo); impl PieceInfo { pub fn new(cid: Cid, size: PaddedPieceSize) -> Self { - Self(piece_v3::PieceInfo { + Self(piece_latest::PieceInfo { cid, size: size.into(), }) } } -impl From for piece_v3::PieceInfo { +impl From for piece_v4::PieceInfo { fn from(value: PieceInfo) -> Self { value.0 } } -impl From for PieceInfo { - fn from(value: piece_v3::PieceInfo) -> Self { +impl From for PieceInfo { + fn from(value: piece_v4::PieceInfo) -> Self { Self(value) } } @@ -43,7 +45,25 @@ impl From for piece_v2::PieceInfo { impl From for PieceInfo { fn from(value: piece_v2::PieceInfo) -> Self { - Self(piece_v3::PieceInfo { + Self(piece_latest::PieceInfo { + size: PaddedPieceSize::from(value.size).into(), + cid: value.cid, + }) + } +} + +impl From for piece_v3::PieceInfo { + fn from(value: PieceInfo) -> Self { + Self { + size: PaddedPieceSize::from(value.0.size).into(), + cid: value.0.cid, + } + } +} + +impl From for PieceInfo { + fn from(value: piece_v3::PieceInfo) -> Self { + Self(piece_latest::PieceInfo { size: PaddedPieceSize::from(value.size).into(), cid: value.cid, }) @@ -53,17 +73,29 @@ impl From for PieceInfo { /// Size of a piece in bytes with padding. #[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)] #[serde(transparent)] -pub struct PaddedPieceSize(piece_v3::PaddedPieceSize); +pub struct PaddedPieceSize(piece_latest::PaddedPieceSize); -impl From for piece_v3::PaddedPieceSize { +impl From for piece_v4::PaddedPieceSize { fn from(value: PaddedPieceSize) -> Self { value.0 } } +impl From for PaddedPieceSize { + fn from(value: piece_v4::PaddedPieceSize) -> Self { + Self(value) + } +} + +impl From for piece_v3::PaddedPieceSize { + fn from(value: PaddedPieceSize) -> Self { + Self(value.0 .0) + } +} + impl From for PaddedPieceSize { fn from(value: piece_v3::PaddedPieceSize) -> Self { - Self(value) + Self(piece_latest::PaddedPieceSize(value.0)) } } @@ -75,6 +107,6 @@ impl From for piece_v2::PaddedPieceSize { impl From for PaddedPieceSize { fn from(value: piece_v2::PaddedPieceSize) -> Self { - Self(piece_v3::PaddedPieceSize(value.0)) + Self(piece_latest::PaddedPieceSize(value.0)) } } diff --git a/src/shim/randomness.rs b/src/shim/randomness.rs index 042c302c292..bb9c8226e66 100644 --- a/src/shim/randomness.rs +++ b/src/shim/randomness.rs @@ -3,8 +3,10 @@ use std::ops::{Deref, DerefMut}; +use super::fvm_shared_latest::randomness::Randomness as Randomness_latest; use fvm_shared2::randomness::Randomness as Randomness_v2; use fvm_shared3::randomness::Randomness as Randomness_v3; +use fvm_shared4::randomness::Randomness as Randomness_v4; use serde::{Deserialize, Serialize}; /// Represents a shim over `Randomness` from `fvm_shared` with convenience @@ -27,16 +29,16 @@ use serde::{Deserialize, Serialize}; /// ``` #[derive(PartialEq, Eq, Default, Clone, Debug, Deserialize, Serialize)] #[serde(transparent)] -pub struct Randomness(Randomness_v3); +pub struct Randomness(Randomness_latest); impl Randomness { pub fn new(rand: Vec) -> Self { - Randomness(Randomness_v3(rand)) + Randomness(Randomness_latest(rand)) } } impl Deref for Randomness { - type Target = Randomness_v3; + type Target = Randomness_latest; fn deref(&self) -> &Self::Target { &self.0 } @@ -48,26 +50,38 @@ impl DerefMut for Randomness { } } +impl From for Randomness { + fn from(other: Randomness_v4) -> Self { + Randomness(other) + } +} + impl From for Randomness { fn from(other: Randomness_v3) -> Self { - Randomness(other) + Randomness(Randomness_latest(other.0)) } } impl From for Randomness { fn from(other: Randomness_v2) -> Self { - Randomness(Randomness_v3(other.0)) + Randomness(Randomness_latest(other.0)) } } -impl From for Randomness_v3 { +impl From for Randomness_v4 { fn from(other: Randomness) -> Self { other.0 } } +impl From for Randomness_v3 { + fn from(other: Randomness) -> Self { + Self(other.0 .0) + } +} + impl From for Randomness_v2 { - fn from(other: Randomness) -> Randomness_v2 { - Randomness_v2(other.0 .0) + fn from(other: Randomness) -> Self { + Self(other.0 .0) } } diff --git a/src/shim/state_tree.rs b/src/shim/state_tree.rs index c413b82f109..bdeddc7b351 100644 --- a/src/shim/state_tree.rs +++ b/src/shim/state_tree.rs @@ -10,12 +10,14 @@ use cid::Cid; pub use fvm2::state_tree::{ActorState as ActorStateV2, StateTree as StateTreeV2}; pub use fvm3::state_tree::{ActorState as ActorStateV3, StateTree as StateTreeV3}; pub use fvm4::state_tree::{ActorState as ActorStateV4, StateTree as StateTreeV4}; +pub use fvm4::state_tree::{ActorState as ActorState_latest, StateTree as StateTree_latest}; use fvm_ipld_blockstore::Blockstore; use fvm_ipld_encoding::repr::{Deserialize_repr, Serialize_repr}; use fvm_shared2::state::StateTreeVersion as StateTreeVersionV2; pub use fvm_shared3::state::StateRoot; use fvm_shared3::state::StateTreeVersion as StateTreeVersionV3; pub use fvm_shared3::ActorID; +use fvm_shared4::state::StateTreeVersion as StateTreeVersionV4; use num::FromPrimitive; use num_derive::FromPrimitive; use serde::{Deserialize, Serialize}; @@ -35,15 +37,28 @@ pub enum StateTreeVersion { V5, } +impl From for StateTreeVersion { + fn from(value: StateTreeVersionV4) -> Self { + match value { + StateTreeVersionV4::V0 => Self::V0, + StateTreeVersionV4::V1 => Self::V1, + StateTreeVersionV4::V2 => Self::V2, + StateTreeVersionV4::V3 => Self::V3, + StateTreeVersionV4::V4 => Self::V4, + StateTreeVersionV4::V5 => Self::V5, + } + } +} + impl From for StateTreeVersion { fn from(value: StateTreeVersionV3) -> Self { match value { - StateTreeVersionV3::V0 => StateTreeVersion::V0, - StateTreeVersionV3::V1 => StateTreeVersion::V1, - StateTreeVersionV3::V2 => StateTreeVersion::V2, - StateTreeVersionV3::V3 => StateTreeVersion::V3, - StateTreeVersionV3::V4 => StateTreeVersion::V4, - StateTreeVersionV3::V5 => StateTreeVersion::V5, + StateTreeVersionV3::V0 => Self::V0, + StateTreeVersionV3::V1 => Self::V1, + StateTreeVersionV3::V2 => Self::V2, + StateTreeVersionV3::V3 => Self::V3, + StateTreeVersionV3::V4 => Self::V4, + StateTreeVersionV3::V5 => Self::V5, } } } @@ -64,11 +79,11 @@ impl TryFrom for StateTreeVersionV2 { fn try_from(value: StateTreeVersion) -> anyhow::Result { Ok(match value { - StateTreeVersion::V0 => StateTreeVersionV2::V0, - StateTreeVersion::V1 => StateTreeVersionV2::V1, - StateTreeVersion::V2 => StateTreeVersionV2::V2, - StateTreeVersion::V3 => StateTreeVersionV2::V3, - StateTreeVersion::V4 => StateTreeVersionV2::V4, + StateTreeVersion::V0 => Self::V0, + StateTreeVersion::V1 => Self::V1, + StateTreeVersion::V2 => Self::V2, + StateTreeVersion::V3 => Self::V3, + StateTreeVersion::V4 => Self::V4, StateTreeVersion::V5 => bail!("Impossible conversion"), }) } @@ -79,12 +94,27 @@ impl TryFrom for StateTreeVersionV3 { fn try_from(value: StateTreeVersion) -> anyhow::Result { Ok(match value { - StateTreeVersion::V0 => StateTreeVersionV3::V0, - StateTreeVersion::V1 => StateTreeVersionV3::V1, - StateTreeVersion::V2 => StateTreeVersionV3::V2, - StateTreeVersion::V3 => StateTreeVersionV3::V3, - StateTreeVersion::V4 => StateTreeVersionV3::V4, - StateTreeVersion::V5 => StateTreeVersionV3::V5, + StateTreeVersion::V0 => Self::V0, + StateTreeVersion::V1 => Self::V1, + StateTreeVersion::V2 => Self::V2, + StateTreeVersion::V3 => Self::V3, + StateTreeVersion::V4 => Self::V4, + StateTreeVersion::V5 => Self::V5, + }) + } +} + +impl TryFrom for StateTreeVersionV4 { + type Error = anyhow::Error; + + fn try_from(value: StateTreeVersion) -> anyhow::Result { + Ok(match value { + StateTreeVersion::V0 => Self::V0, + StateTreeVersion::V1 => Self::V1, + StateTreeVersion::V2 => Self::V2, + StateTreeVersion::V3 => Self::V3, + StateTreeVersion::V4 => Self::V4, + StateTreeVersion::V5 => Self::V5, }) } } @@ -103,6 +133,8 @@ pub enum StateTree { FvmV2(StateTreeV2>), // fvm-3 support state tree versions 5. FvmV3(StateTreeV3>), + // fvm-3 support state tree versions *. + FvmV4(StateTreeV4>), } impl StateTree @@ -111,7 +143,9 @@ where { /// Constructor for a HAMT state tree given an IPLD store pub fn new(store: Arc, version: StateTreeVersion) -> anyhow::Result { - if let Ok(st) = StateTreeV3::new(store.clone(), version.try_into()?) { + if let Ok(st) = StateTreeV4::new(store.clone(), version.try_into()?) { + Ok(StateTree::FvmV4(st)) + } else if let Ok(st) = StateTreeV3::new(store.clone(), version.try_into()?) { Ok(StateTree::FvmV3(st)) } else if let Ok(st) = StateTreeV2::new(store, version.try_into()?) { Ok(StateTree::FvmV2(st)) @@ -121,7 +155,9 @@ where } pub fn new_from_root(store: Arc, c: &Cid) -> anyhow::Result { - if let Ok(st) = StateTreeV3::new_from_root(store.clone(), c) { + if let Ok(st) = StateTreeV4::new_from_root(store.clone(), c) { + Ok(StateTree::FvmV4(st)) + } else if let Ok(st) = StateTreeV3::new_from_root(store.clone(), c) { Ok(StateTree::FvmV3(st)) } else if let Ok(st) = StateTreeV2::new_from_root(store.clone(), c) { Ok(StateTree::FvmV2(st)) @@ -140,6 +176,17 @@ where .map_err(|e| anyhow!("{e}"))? .map(Into::into)), StateTree::FvmV3(st) => { + let id = st.lookup_id(&addr.into())?; + if let Some(id) = id { + Ok(st + .get_actor(id) + .map_err(|e| anyhow!("{e}"))? + .map(Into::into)) + } else { + Ok(None) + } + } + StateTree::FvmV4(st) => { let id = st.lookup_id(addr)?; if let Some(id) = id { Ok(st @@ -169,6 +216,7 @@ where match self { StateTree::FvmV2(st) => st.store(), StateTree::FvmV3(st) => st.store(), + StateTree::FvmV4(st) => st.store(), StateTree::V0(st) => st.store(), } } @@ -253,7 +301,7 @@ where #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] #[serde(transparent)] #[cfg_attr(test, derive(derive_quickcheck_arbitrary::Arbitrary))] -pub struct ActorState(ActorStateV3); +pub struct ActorState(ActorState_latest); impl ActorState { pub fn new( @@ -263,7 +311,7 @@ impl ActorState { sequence: u64, address: Option
, ) -> Self { - Self(ActorStateV3::new( + Self(ActorState_latest::new( code, state, balance.into(), @@ -273,7 +321,7 @@ impl ActorState { } /// Construct a new empty actor with the specified code. pub fn new_empty(code: Cid, delegated_address: Option
) -> Self { - Self(ActorStateV3::new_empty( + Self(ActorState_latest::new_empty( code, delegated_address.map(Into::into), )) @@ -281,7 +329,7 @@ impl ActorState { } impl Deref for ActorState { - type Target = ActorStateV3; + type Target = ActorState_latest; fn deref(&self) -> &Self::Target { &self.0 @@ -294,21 +342,9 @@ impl DerefMut for ActorState { } } -impl From for ActorState { - fn from(value: ActorStateV2) -> Self { - ActorState(ActorStateV3 { - code: value.code, - state: value.state, - sequence: value.sequence, - balance: TokenAmount::from(value.balance).into(), - delegated_address: None, - }) - } -} - impl From<&ActorStateV2> for ActorState { fn from(value: &ActorStateV2) -> Self { - ActorState(ActorStateV3 { + Self(ActorState_latest { code: value.code, state: value.state, sequence: value.sequence, @@ -318,9 +354,23 @@ impl From<&ActorStateV2> for ActorState { } } +impl From for ActorState { + fn from(value: ActorStateV2) -> Self { + (&value).into() + } +} + impl From for ActorState { fn from(value: ActorStateV3) -> Self { - ActorState(value) + Self(ActorState_latest { + code: value.code, + state: value.state, + sequence: value.sequence, + balance: TokenAmount::from(value.balance).into(), + delegated_address: value + .delegated_address + .map(|addr| Address::from(addr).into()), + }) } } @@ -332,15 +382,7 @@ impl From<&ActorStateV3> for ActorState { impl From for ActorState { fn from(value: ActorStateV4) -> Self { - ActorState(ActorStateV3 { - code: value.code, - state: value.state, - sequence: value.sequence, - balance: TokenAmount::from(value.balance).into(), - delegated_address: value - .delegated_address - .map(|addr| Address::from(addr).into()), - }) + ActorState(value) } } @@ -352,7 +394,7 @@ impl From<&ActorStateV4> for ActorState { impl From for ActorStateV2 { fn from(other: ActorState) -> ActorStateV2 { - ActorStateV2 { + Self { code: other.code, state: other.state, sequence: other.sequence, @@ -363,7 +405,7 @@ impl From for ActorStateV2 { impl From<&ActorState> for ActorStateV2 { fn from(other: &ActorState) -> ActorStateV2 { - ActorStateV2 { + Self { code: other.code, state: other.state, sequence: other.sequence, @@ -373,6 +415,20 @@ impl From<&ActorState> for ActorStateV2 { } impl From for ActorStateV3 { + fn from(other: ActorState) -> Self { + Self { + code: other.code, + state: other.state, + sequence: other.sequence, + balance: TokenAmount::from(&other.balance).into(), + delegated_address: other + .delegated_address + .map(|addr| Address::from(addr).into()), + } + } +} + +impl From for ActorStateV4 { fn from(other: ActorState) -> Self { other.0 } diff --git a/src/shim/state_tree_v0.rs b/src/shim/state_tree_v0.rs index 5f2fefe17c9..085d5083939 100644 --- a/src/shim/state_tree_v0.rs +++ b/src/shim/state_tree_v0.rs @@ -72,7 +72,7 @@ where /// Get an ID address from any Address pub fn lookup_id(&self, addr: &Address) -> anyhow::Result> { - if addr.protocol() == fvm_shared3::address::Protocol::ID { + if addr.protocol() == fvm_shared4::address::Protocol::ID { return Ok(Some(*addr)); } anyhow::bail!("StateTreeV0::lookup_id is only defined for ID addresses") diff --git a/src/shim/version.rs b/src/shim/version.rs index 1fb4ab21de2..a34fe08e7e0 100644 --- a/src/shim/version.rs +++ b/src/shim/version.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0, MIT use std::ops::{Deref, DerefMut}; +use super::fvm_shared_latest::version::NetworkVersion as NetworkVersion_latest; pub use fvm_shared2::version::NetworkVersion as NetworkVersion_v2; use fvm_shared3::version::NetworkVersion as NetworkVersion_v3; use fvm_shared4::version::NetworkVersion as NetworkVersion_v4; @@ -23,35 +24,35 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd, Serialize, Deserialize)] #[repr(transparent)] #[serde(transparent)] -pub struct NetworkVersion(pub NetworkVersion_v4); +pub struct NetworkVersion(pub NetworkVersion_latest); impl NetworkVersion { - pub const V0: Self = Self(NetworkVersion_v4::new(0)); - pub const V1: Self = Self(NetworkVersion_v4::new(1)); - pub const V2: Self = Self(NetworkVersion_v4::new(2)); - pub const V3: Self = Self(NetworkVersion_v4::new(3)); - pub const V4: Self = Self(NetworkVersion_v4::new(4)); - pub const V5: Self = Self(NetworkVersion_v4::new(5)); - pub const V6: Self = Self(NetworkVersion_v4::new(6)); - pub const V7: Self = Self(NetworkVersion_v4::new(7)); - pub const V8: Self = Self(NetworkVersion_v4::new(8)); - pub const V9: Self = Self(NetworkVersion_v4::new(9)); - pub const V10: Self = Self(NetworkVersion_v4::new(10)); - pub const V11: Self = Self(NetworkVersion_v4::new(11)); - pub const V12: Self = Self(NetworkVersion_v4::new(12)); - pub const V13: Self = Self(NetworkVersion_v4::new(13)); - pub const V14: Self = Self(NetworkVersion_v4::new(14)); - pub const V15: Self = Self(NetworkVersion_v4::new(15)); - pub const V16: Self = Self(NetworkVersion_v4::new(16)); - pub const V17: Self = Self(NetworkVersion_v4::new(17)); - pub const V18: Self = Self(NetworkVersion_v4::new(18)); - pub const V19: Self = Self(NetworkVersion_v4::new(19)); - pub const V20: Self = Self(NetworkVersion_v4::new(20)); - pub const V21: Self = Self(NetworkVersion_v4::new(21)); + pub const V0: Self = Self(NetworkVersion_latest::new(0)); + pub const V1: Self = Self(NetworkVersion_latest::new(1)); + pub const V2: Self = Self(NetworkVersion_latest::new(2)); + pub const V3: Self = Self(NetworkVersion_latest::new(3)); + pub const V4: Self = Self(NetworkVersion_latest::new(4)); + pub const V5: Self = Self(NetworkVersion_latest::new(5)); + pub const V6: Self = Self(NetworkVersion_latest::new(6)); + pub const V7: Self = Self(NetworkVersion_latest::new(7)); + pub const V8: Self = Self(NetworkVersion_latest::new(8)); + pub const V9: Self = Self(NetworkVersion_latest::new(9)); + pub const V10: Self = Self(NetworkVersion_latest::new(10)); + pub const V11: Self = Self(NetworkVersion_latest::new(11)); + pub const V12: Self = Self(NetworkVersion_latest::new(12)); + pub const V13: Self = Self(NetworkVersion_latest::new(13)); + pub const V14: Self = Self(NetworkVersion_latest::new(14)); + pub const V15: Self = Self(NetworkVersion_latest::new(15)); + pub const V16: Self = Self(NetworkVersion_latest::new(16)); + pub const V17: Self = Self(NetworkVersion_latest::new(17)); + pub const V18: Self = Self(NetworkVersion_latest::new(18)); + pub const V19: Self = Self(NetworkVersion_latest::new(19)); + pub const V20: Self = Self(NetworkVersion_latest::new(20)); + pub const V21: Self = Self(NetworkVersion_latest::new(21)); } impl Deref for NetworkVersion { - type Target = NetworkVersion_v4; + type Target = NetworkVersion_latest; fn deref(&self) -> &Self::Target { &self.0 } diff --git a/src/statediff/mod.rs b/src/statediff/mod.rs index e1e60ffaa3f..908c5c2d1e2 100644 --- a/src/statediff/mod.rs +++ b/src/statediff/mod.rs @@ -274,7 +274,7 @@ mod tests { let db = MemoryDB::default(); let account_state = AccountState { - address: *Address::new_id(0xdeadbeef), + address: Address::new_id(0xdeadbeef).into(), }; let mut state = mk_account_v10(&db, &account_state); state.code = Cid::default(); // Use an unknown actor CID to force parsing to fail. diff --git a/src/tool/subcommands/snapshot_cmd.rs b/src/tool/subcommands/snapshot_cmd.rs index 678ebdfd5e6..db1a1bc9c04 100644 --- a/src/tool/subcommands/snapshot_cmd.rs +++ b/src/tool/subcommands/snapshot_cmd.rs @@ -24,7 +24,7 @@ use clap::Subcommand; use dialoguer::{theme::ColorfulTheme, Confirm}; use futures::TryStreamExt; use fvm_ipld_blockstore::Blockstore; -use fvm_shared3::address::Network; +use fvm_shared4::address::Network; use indicatif::{ProgressBar, ProgressStyle}; use std::path::PathBuf; use std::sync::Arc; From d6affd88f754057400171bcb01bdc6b6fa31bf64 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 27 Sep 2023 19:38:35 +0800 Subject: [PATCH 05/11] fix: state migration tests --- src/shim/state_tree.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/shim/state_tree.rs b/src/shim/state_tree.rs index bdeddc7b351..e4f21c25829 100644 --- a/src/shim/state_tree.rs +++ b/src/shim/state_tree.rs @@ -133,7 +133,9 @@ pub enum StateTree { FvmV2(StateTreeV2>), // fvm-3 support state tree versions 5. FvmV3(StateTreeV3>), - // fvm-3 support state tree versions *. + // FIXME: https://github.com/ChainSafe/forest/issues/3523 + // fvm-4 support state tree versions *. + #[allow(dead_code)] FvmV4(StateTreeV4>), } @@ -143,9 +145,7 @@ where { /// Constructor for a HAMT state tree given an IPLD store pub fn new(store: Arc, version: StateTreeVersion) -> anyhow::Result { - if let Ok(st) = StateTreeV4::new(store.clone(), version.try_into()?) { - Ok(StateTree::FvmV4(st)) - } else if let Ok(st) = StateTreeV3::new(store.clone(), version.try_into()?) { + if let Ok(st) = StateTreeV3::new(store.clone(), version.try_into()?) { Ok(StateTree::FvmV3(st)) } else if let Ok(st) = StateTreeV2::new(store, version.try_into()?) { Ok(StateTree::FvmV2(st)) @@ -155,9 +155,7 @@ where } pub fn new_from_root(store: Arc, c: &Cid) -> anyhow::Result { - if let Ok(st) = StateTreeV4::new_from_root(store.clone(), c) { - Ok(StateTree::FvmV4(st)) - } else if let Ok(st) = StateTreeV3::new_from_root(store.clone(), c) { + if let Ok(st) = StateTreeV3::new_from_root(store.clone(), c) { Ok(StateTree::FvmV3(st)) } else if let Ok(st) = StateTreeV2::new_from_root(store.clone(), c) { Ok(StateTree::FvmV2(st)) From d1c8557d846ed2d4294adf3bc104615bbd7ab3c9 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 27 Sep 2023 19:54:17 +0800 Subject: [PATCH 06/11] update issue link --- src/shim/gas.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shim/gas.rs b/src/shim/gas.rs index 6ad894fb4b7..14b68a0aabd 100644 --- a/src/shim/gas.rs +++ b/src/shim/gas.rs @@ -129,7 +129,7 @@ impl From for GasCharge { name: value.name, compute_gas: GasV3::from_milligas(value.compute_gas.as_milligas()), other_gas: GasV3::from_milligas(value.other_gas.as_milligas()), - // FIXME: https://github.com/filecoin-project/ref-fvm/pull/1897 + // FIXME: https://github.com/ChainSafe/forest/issues/3524 // elapsed: value.elapsed.into(), elapsed: Default::default(), } @@ -159,7 +159,7 @@ impl From for GasChargeV4 { name: value.0.name, compute_gas: GasV4::from_milligas(value.0.compute_gas.as_milligas() as _), other_gas: GasV4::from_milligas(value.0.other_gas.as_milligas() as _), - // FIXME: https://github.com/filecoin-project/ref-fvm/pull/1897 + // FIXME: https://github.com/ChainSafe/forest/issues/3524 // elapsed: value.elapsed.into(), elapsed: Default::default(), } From f61691111c7e1df4a8c6e2704c441a9a178cfba1 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 27 Sep 2023 20:21:18 +0800 Subject: [PATCH 07/11] fix-3523 --- src/shim/state_tree.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/shim/state_tree.rs b/src/shim/state_tree.rs index e4f21c25829..f7a6a05927d 100644 --- a/src/shim/state_tree.rs +++ b/src/shim/state_tree.rs @@ -133,9 +133,7 @@ pub enum StateTree { FvmV2(StateTreeV2>), // fvm-3 support state tree versions 5. FvmV3(StateTreeV3>), - // FIXME: https://github.com/ChainSafe/forest/issues/3523 // fvm-4 support state tree versions *. - #[allow(dead_code)] FvmV4(StateTreeV4>), } @@ -145,7 +143,9 @@ where { /// Constructor for a HAMT state tree given an IPLD store pub fn new(store: Arc, version: StateTreeVersion) -> anyhow::Result { - if let Ok(st) = StateTreeV3::new(store.clone(), version.try_into()?) { + if let Ok(st) = StateTreeV4::new(store.clone(), version.try_into()?) { + Ok(StateTree::FvmV4(st)) + } else if let Ok(st) = StateTreeV3::new(store.clone(), version.try_into()?) { Ok(StateTree::FvmV3(st)) } else if let Ok(st) = StateTreeV2::new(store, version.try_into()?) { Ok(StateTree::FvmV2(st)) @@ -155,7 +155,9 @@ where } pub fn new_from_root(store: Arc, c: &Cid) -> anyhow::Result { - if let Ok(st) = StateTreeV3::new_from_root(store.clone(), c) { + if let Ok(st) = StateTreeV4::new_from_root(store.clone(), c) { + Ok(StateTree::FvmV4(st)) + } else if let Ok(st) = StateTreeV3::new_from_root(store.clone(), c) { Ok(StateTree::FvmV3(st)) } else if let Ok(st) = StateTreeV2::new_from_root(store.clone(), c) { Ok(StateTree::FvmV2(st)) @@ -224,7 +226,8 @@ where match self { StateTree::FvmV2(st) => st.lookup_id(&addr.into()).map_err(|e| anyhow!("{e}")), StateTree::FvmV3(st) => Ok(st.lookup_id(&addr.into())?), - _ => bail!("StateTree::lookup_id not supported on old state trees"), + StateTree::FvmV4(st) => Ok(st.lookup_id(&addr.into())?), + StateTree::V0(_) => bail!("StateTree::lookup_id not supported on old state trees"), } } @@ -245,7 +248,13 @@ where }; st.for_each(inner) } - _ => bail!("StateTree::for_each not supported on old state trees"), + StateTree::FvmV4(st) => { + let inner = |address: fvm_shared4::address::Address, actor_state: &ActorStateV4| { + f(address.into(), &actor_state.into()) + }; + st.for_each(inner) + } + StateTree::V0(_) => bail!("StateTree::for_each not supported on old state trees"), } } @@ -254,7 +263,8 @@ where match self { StateTree::FvmV2(st) => st.flush().map_err(|e| anyhow!("{e}")), StateTree::FvmV3(st) => Ok(st.flush()?), - _ => bail!("StateTree::flush not supported on old state trees"), + StateTree::FvmV4(st) => Ok(st.flush()?), + StateTree::V0(_) => bail!("StateTree::flush not supported on old state trees"), } } @@ -271,7 +281,14 @@ where st.set_actor(id, actor.into()); Ok(()) } - _ => bail!("StateTree::set_actor not supported on old state trees"), + StateTree::FvmV4(st) => { + let id = st + .lookup_id(&addr.into())? + .context("couldn't find actor id")?; + st.set_actor(id, actor.into()); + Ok(()) + } + StateTree::V0(_) => bail!("StateTree::set_actor not supported on old state trees"), } } } From 4b71ceffda4196821ec53c3aae6a5a1e3c7fe8f2 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Thu, 28 Sep 2023 12:19:38 +0800 Subject: [PATCH 08/11] Bump fvm* versions to alpha3 --- Cargo.lock | 65 +++++++++++++++++++++++++++---------------------- Cargo.toml | 4 +-- src/shim/gas.rs | 60 +++++++++++++++++++++++++-------------------- 3 files changed, 71 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ebf309f3d3..0352eadb017 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,7 +798,7 @@ dependencies = [ "bitflags 2.4.0", "boa_interner", "boa_macros", - "indexmap 2.0.0", + "indexmap 2.0.1", "num-bigint", "rustc-hash", ] @@ -821,7 +821,7 @@ dependencies = [ "dashmap", "fast-float", "icu_normalizer", - "indexmap 2.0.0", + "indexmap 2.0.1", "itertools 0.11.0", "num-bigint", "num-integer", @@ -877,7 +877,7 @@ dependencies = [ "boa_gc", "boa_macros", "hashbrown 0.14.0", - "indexmap 2.0.0", + "indexmap 2.0.1", "once_cell", "phf", "rustc-hash", @@ -2136,6 +2136,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + [[package]] name = "encode_unicode" version = "0.3.6" @@ -2544,7 +2550,7 @@ dependencies = [ "fvm_ipld_hamt", "fvm_shared 2.6.0", "fvm_shared 3.6.0", - "indexmap 2.0.0", + "indexmap 2.0.1", "integer-encoding", "num-derive", "num-traits", @@ -2831,19 +2837,19 @@ dependencies = [ "futures", "fvm 2.7.0", "fvm 3.8.0", - "fvm 4.0.0-alpha.2", + "fvm 4.0.0-alpha.3", "fvm_ipld_blockstore", "fvm_ipld_encoding", "fvm_shared 2.6.0", "fvm_shared 3.6.0", - "fvm_shared 4.0.0-alpha.2", + "fvm_shared 4.0.0-alpha.3", "gethostname", "git-version", "hex", "http", "human-repr", "humantime", - "indexmap 2.0.0", + "indexmap 2.0.1", "indicatif", "integer-encoding", "is-terminal", @@ -3222,9 +3228,9 @@ dependencies = [ [[package]] name = "fvm" -version = "4.0.0-alpha.2" +version = "4.0.0-alpha.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6cb612a56eb37b1e36aa69de550b41f47cb7c482344af13a9aa7d54e1eb325f" +checksum = "bda3c868d334b0615c62595e1260dc9481b82535eb6427d14434063d27eed965" dependencies = [ "anyhow", "arbitrary", @@ -3238,7 +3244,7 @@ dependencies = [ "fvm_ipld_blockstore", "fvm_ipld_encoding", "fvm_ipld_hamt", - "fvm_shared 4.0.0-alpha.2", + "fvm_shared 4.0.0-alpha.3", "lazy_static", "log", "minstant", @@ -3421,9 +3427,9 @@ dependencies = [ [[package]] name = "fvm_shared" -version = "4.0.0-alpha.2" +version = "4.0.0-alpha.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dc4430d91e5269db5f32f8bb747810546e95485644c2d3ee432b7f6b15c7323" +checksum = "e04c88cf61b42fd2492509f87d92e6757f60430cd7ee692f5fc0b9a94a14ae45" dependencies = [ "anyhow", "arbitrary", @@ -4025,9 +4031,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5477fe2230a79769d8dc68e0eabf5437907c0457a5614a9e8dddb67f65eb65d" +checksum = "ad227c3af19d4914570ad36d30409928b75967c298feb9ea1969db3a610bb14e" dependencies = [ "equivalent", "hashbrown 0.14.0", @@ -5703,7 +5709,7 @@ checksum = "7b7d06c8716de428def400ae9e5cd92463cd458d98169ed835b2da5d08cf4698" dependencies = [ "deprecate-until", "fixedbitset", - "indexmap 2.0.0", + "indexmap 2.0.1", "integer-sqrt", "num-traits", "rustc-hash", @@ -5743,7 +5749,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" dependencies = [ "fixedbitset", - "indexmap 2.0.0", + "indexmap 2.0.1", ] [[package]] @@ -5956,11 +5962,12 @@ dependencies = [ [[package]] name = "postcard" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d534c6e61df1c7166e636ca612d9820d486fe96ddad37f7abc671517b297488e" +checksum = "a55c51ee6c0db07e68448e336cf8ea4131a620edefebf9893e759b2d793420f8" dependencies = [ "cobs", + "embedded-io", "serde", ] @@ -7012,7 +7019,7 @@ dependencies = [ "chrono", "hex", "indexmap 1.9.3", - "indexmap 2.0.0", + "indexmap 2.0.1", "serde", "serde_json", "serde_with_macros", @@ -7037,7 +7044,7 @@ version = "0.9.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a49e178e4452f45cb61d0cd8cebc1b0fafd3e41929e996cef79aa3aca91f574" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.1", "itoa", "ryu", "serde", @@ -7116,9 +7123,9 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.1.4" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" dependencies = [ "lazy_static", ] @@ -7917,7 +7924,7 @@ version = "0.19.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.1", "toml_datetime", "winnow", ] @@ -7928,7 +7935,7 @@ version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca676d9ba1a322c1b64eb8045a5ec5c0cfb0c9d08e15e9ff622589ad5221c8fe" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.1", "serde", "serde_spanned", "toml_datetime", @@ -8522,7 +8529,7 @@ version = "0.110.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dfcdb72d96f01e6c85b6bf20102e7423bdbaad5c337301bab2bbf253d26413c" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.1", "semver", ] @@ -8532,7 +8539,7 @@ version = "0.113.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0d44fab0bd78404e352f3399324eef76516a4580b52bc9031c60f064e98f3" dependencies = [ - "indexmap 2.0.0", + "indexmap 2.0.1", "semver", ] @@ -8557,7 +8564,7 @@ dependencies = [ "bumpalo", "cfg-if", "fxprof-processed-profile", - "indexmap 2.0.0", + "indexmap 2.0.1", "libc", "log", "object 0.31.1", @@ -8635,7 +8642,7 @@ dependencies = [ "anyhow", "cranelift-entity", "gimli 0.27.3", - "indexmap 2.0.0", + "indexmap 2.0.1", "log", "object 0.31.1", "serde", @@ -8699,7 +8706,7 @@ dependencies = [ "anyhow", "cc", "cfg-if", - "indexmap 2.0.0", + "indexmap 2.0.1", "libc", "log", "mach", diff --git a/Cargo.toml b/Cargo.toml index f91d8437afa..94cd3db4184 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,12 +68,12 @@ fs_extra = "1.2" futures = "0.3" fvm2 = { package = "fvm", version = "~2.7", default-features = false } fvm3 = { package = "fvm", default-features = false, version = "~3.8", features = ["arb"] } -fvm4 = { package = "fvm", default-features = false, version = "~4.0.0-alpha.2", features = ["arb"] } +fvm4 = { package = "fvm", default-features = false, version = "~4.0.0-alpha.3", features = ["arb"] } fvm_ipld_blockstore = "0.2" fvm_ipld_encoding = "0.4" fvm_shared2 = { package = "fvm_shared", version = "~2.6" } fvm_shared3 = { package = "fvm_shared", version = "~3.6", features = ["testing", "proofs"] } -fvm_shared4 = { package = "fvm_shared", version = "~4.0.0-alpha.2", features = ["testing", "proofs"] } +fvm_shared4 = { package = "fvm_shared", version = "~4.0.0-alpha.3", features = ["testing", "proofs"] } gethostname = "0.4" git-version = "0.3" diff --git a/src/shim/gas.rs b/src/shim/gas.rs index 14b68a0aabd..d91483a0025 100644 --- a/src/shim/gas.rs +++ b/src/shim/gas.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0, MIT use std::fmt::{Debug, Display}; +pub use super::fvm_latest::gas::{Gas as Gas_latest, GasCharge as GasCharge_latest}; use fvm2::gas::{ price_list_by_network_version as price_list_by_network_version_v2, Gas as GasV2, GasCharge as GasChargeV2, PriceList as PriceListV2, @@ -12,12 +13,14 @@ use fvm3::gas::{ }; pub use fvm3::gas::{GasCharge as GasChargeV3, GasTracker, PriceList as PriceListV3}; use fvm4::gas::price_list_by_network_version as price_list_by_network_version_v4; -pub use fvm4::gas::{Gas as GasV4, GasCharge as GasChargeV4, PriceList as PriceListV4}; +pub use fvm4::gas::{ + Gas as GasV4, GasCharge as GasChargeV4, GasDuration as GasDurationV4, PriceList as PriceListV4, +}; use crate::shim::version::NetworkVersion; #[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default)] -pub struct Gas(GasV3); +pub struct Gas(Gas_latest); impl Debug for Gas { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -47,7 +50,7 @@ impl Display for Gas { impl Gas { pub fn new(gas: u64) -> Self { - Self(GasV3::new(gas)) + Self(Gas_latest::new(gas)) } pub fn round_up(&self) -> u64 { @@ -57,13 +60,13 @@ impl Gas { impl From for Gas { fn from(value: GasV2) -> Self { - Gas(GasV3::from_milligas(value.as_milligas() as u64)) + Gas(Gas_latest::from_milligas(value.as_milligas() as _)) } } impl From for GasV2 { fn from(value: Gas) -> Self { - GasV2::from_milligas(value.0.as_milligas() as i64) + GasV2::from_milligas(value.0.as_milligas() as _) } } @@ -73,20 +76,26 @@ impl From for GasV3 { } } +impl From for Gas { + fn from(value: GasV3) -> Self { + Gas(Gas_latest::from_milligas(value.as_milligas() as _)) + } +} + impl From for GasV4 { fn from(value: Gas) -> Self { GasV4::from_milligas(value.0.as_milligas()) } } -impl From for Gas { - fn from(value: GasV3) -> Self { +impl From for Gas { + fn from(value: GasV4) -> Self { Gas(value) } } #[derive(Debug, Clone)] -pub struct GasCharge(GasChargeV3); +pub struct GasCharge(GasCharge_latest); impl GasCharge { /// Calculates total gas charge (in `milligas`) by summing compute and @@ -119,21 +128,19 @@ impl From for GasCharge { impl From for GasCharge { fn from(value: GasChargeV3) -> Self { - GasCharge(value) + GasChargeV4 { + name: value.name, + compute_gas: GasV4::from_milligas(value.compute_gas.as_milligas()), + other_gas: GasV4::from_milligas(value.other_gas.as_milligas()), + elapsed: value.elapsed.get().map(|&d| d.into()).unwrap_or_default(), + } + .into() } } impl From for GasCharge { fn from(value: GasChargeV4) -> Self { - GasChargeV3 { - name: value.name, - compute_gas: GasV3::from_milligas(value.compute_gas.as_milligas()), - other_gas: GasV3::from_milligas(value.other_gas.as_milligas()), - // FIXME: https://github.com/ChainSafe/forest/issues/3524 - // elapsed: value.elapsed.into(), - elapsed: Default::default(), - } - .into() + GasCharge(value) } } @@ -148,24 +155,23 @@ impl From for GasChargeV2 { } impl From for GasChargeV3 { - fn from(value: GasCharge) -> Self { - value.0 - } -} - -impl From for GasChargeV4 { fn from(value: GasCharge) -> Self { Self { name: value.0.name, - compute_gas: GasV4::from_milligas(value.0.compute_gas.as_milligas() as _), - other_gas: GasV4::from_milligas(value.0.other_gas.as_milligas() as _), + compute_gas: GasV3::from_milligas(value.0.compute_gas.as_milligas() as _), + other_gas: GasV3::from_milligas(value.0.other_gas.as_milligas() as _), // FIXME: https://github.com/ChainSafe/forest/issues/3524 - // elapsed: value.elapsed.into(), elapsed: Default::default(), } } } +impl From for GasChargeV4 { + fn from(value: GasCharge) -> Self { + value.0 + } +} + pub enum PriceList { V2(&'static PriceListV2), V3(&'static PriceListV3), From 6c233ab84b5ba435fbd10bde3b2e664610e4f546 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Thu, 28 Sep 2023 12:41:39 +0800 Subject: [PATCH 09/11] fix doctest --- src/shim/randomness.rs | 6 +++++- src/shim/state_tree.rs | 7 ++++++- src/shim/version.rs | 7 +++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/shim/randomness.rs b/src/shim/randomness.rs index bb9c8226e66..efc0c2ade29 100644 --- a/src/shim/randomness.rs +++ b/src/shim/randomness.rs @@ -22,9 +22,13 @@ use serde::{Deserialize, Serialize}; /// // Create a correspndoning FVM3 Randomness /// let fvm3_rand = fvm_shared3::randomness::Randomness(vec![]); /// +/// // Create a correspndoning FVM4 Randomness +/// let fvm4_rand = fvm_shared4::randomness::Randomness(vec![]); +/// /// // Create a shim Randomness, ensure conversions are correct /// let rand_shim = Randomness::new(vec![]); -/// assert_eq!(fvm3_rand, *rand_shim); +/// assert_eq!(fvm4_rand, *rand_shim); +/// assert_eq!(fvm3_rand, rand_shim.clone().into()); /// assert_eq!(fvm2_rand, rand_shim.into()); /// ``` #[derive(PartialEq, Eq, Default, Clone, Debug, Deserialize, Serialize)] diff --git a/src/shim/state_tree.rs b/src/shim/state_tree.rs index f7a6a05927d..c2b585c244c 100644 --- a/src/shim/state_tree.rs +++ b/src/shim/state_tree.rs @@ -308,9 +308,14 @@ where /// let fvm3_actor_state = fvm3::state_tree::ActorState::new(Cid::default(), Cid::default(), /// fvm_shared3::econ::TokenAmount::from_atto(42), 0, None); /// +/// // Create a correspndoning FVM4 ActorState +/// let fvm4_actor_state = fvm4::state_tree::ActorState::new(Cid::default(), Cid::default(), +/// fvm_shared4::econ::TokenAmount::from_atto(42), 0, None); +/// /// // Create a shim out of fvm2 state, ensure conversions are correct /// let state_shim = ActorState::from(fvm2_actor_state.clone()); -/// assert_eq!(fvm3_actor_state, *state_shim); +/// assert_eq!(fvm4_actor_state, *state_shim); +/// assert_eq!(fvm3_actor_state, state_shim.clone().into()); /// assert_eq!(fvm2_actor_state, state_shim.into()); /// ``` #[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)] diff --git a/src/shim/version.rs b/src/shim/version.rs index a34fe08e7e0..ab7e25addfd 100644 --- a/src/shim/version.rs +++ b/src/shim/version.rs @@ -15,8 +15,11 @@ use serde::{Deserialize, Serialize}; /// # use forest_filecoin::doctest_private::NetworkVersion; /// let v0 = NetworkVersion::V0; /// -/// // dereference to convert to FVM3 -/// assert_eq!(fvm_shared3::version::NetworkVersion::V0, *v0); +/// // dereference to convert to FVM4 +/// assert_eq!(fvm_shared4::version::NetworkVersion::V0, *v0); +/// +/// // use `.into()` when FVM3 has to be specified. +/// assert_eq!(fvm_shared3::version::NetworkVersion::V0, v0.into()); /// /// // use `.into()` when FVM2 has to be specified. /// assert_eq!(fvm_shared2::version::NetworkVersion::V0, v0.into()); From 24abab1c62910628c4264bda0bd5a3d48dae637b Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Thu, 28 Sep 2023 22:52:29 +0800 Subject: [PATCH 10/11] resolve comments --- src/shim/crypto.rs | 4 +++- src/shim/mod.rs | 4 ++-- src/tool/subcommands/snapshot_cmd.rs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/shim/crypto.rs b/src/shim/crypto.rs index 66455f9466f..5fccb1ed7b0 100644 --- a/src/shim/crypto.rs +++ b/src/shim/crypto.rs @@ -88,7 +88,9 @@ impl Signature { /// Checks if a signature is valid given data and address. pub fn verify(&self, data: &[u8], addr: &crate::shim::address::Address) -> Result<(), String> { - use fvm_shared4::crypto::signature::ops::{verify_bls_sig, verify_secp256k1_sig}; + use super::fvm_shared_latest::crypto::signature::ops::{ + verify_bls_sig, verify_secp256k1_sig, + }; match self.sig_type { SignatureType::Bls => verify_bls_sig(&self.bytes, data, addr), SignatureType::Secp256k1 => verify_secp256k1_sig(&self.bytes, data, addr), diff --git a/src/shim/mod.rs b/src/shim/mod.rs index a10694ea85c..745f6fb3a97 100644 --- a/src/shim/mod.rs +++ b/src/shim/mod.rs @@ -22,9 +22,9 @@ pub mod state_tree_v0; pub mod trace; pub mod version; -mod fvm_shared_latest { +pub mod fvm_shared_latest { pub use fvm_shared4::*; } -mod fvm_latest { +pub mod fvm_latest { pub use fvm4::*; } diff --git a/src/tool/subcommands/snapshot_cmd.rs b/src/tool/subcommands/snapshot_cmd.rs index db1a1bc9c04..fe89564990f 100644 --- a/src/tool/subcommands/snapshot_cmd.rs +++ b/src/tool/subcommands/snapshot_cmd.rs @@ -14,6 +14,7 @@ use crate::ipld::recurse_links_hash; use crate::networks::{calibnet, mainnet, ChainConfig, NetworkChain}; use crate::shim::address::CurrentNetwork; use crate::shim::clock::ChainEpoch; +use crate::shim::fvm_shared_latest::address::Network; use crate::shim::machine::MultiEngine; use crate::state_manager::apply_block_messages; use crate::utils::db::car_stream::CarStream; @@ -24,7 +25,6 @@ use clap::Subcommand; use dialoguer::{theme::ColorfulTheme, Confirm}; use futures::TryStreamExt; use fvm_ipld_blockstore::Blockstore; -use fvm_shared4::address::Network; use indicatif::{ProgressBar, ProgressStyle}; use std::path::PathBuf; use std::sync::Arc; From a2934afe5a857d1c5c0c4dd2b310e3658c9935cd Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Fri, 29 Sep 2023 08:39:41 +0200 Subject: [PATCH 11/11] re-export EPOCH_DURATION_SECONDS --- src/shim/clock.rs | 29 +---------------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/src/shim/clock.rs b/src/shim/clock.rs index 9cf4c5266e4..a39dc449136 100644 --- a/src/shim/clock.rs +++ b/src/shim/clock.rs @@ -1,38 +1,11 @@ // Copyright 2019-2023 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +pub use fvm_shared4::clock::EPOCH_DURATION_SECONDS; pub use fvm_shared4::ALLOWABLE_CLOCK_DRIFT; pub use fvm_shared4::BLOCKS_PER_EPOCH; pub const SECONDS_IN_DAY: i64 = 86400; pub const EPOCHS_IN_DAY: i64 = SECONDS_IN_DAY / EPOCH_DURATION_SECONDS; -pub const EPOCH_DURATION_SECONDS: i64 = 30; pub type ChainEpoch = i64; - -#[cfg(test)] -mod tests { - #[test] - fn fvm_shim_of_const_epoch_duration_seconds() { - assert_eq!( - super::EPOCH_DURATION_SECONDS, - fvm_shared2::clock::EPOCH_DURATION_SECONDS - ) - } - - #[test] - fn fvm3_shim_of_const_epoch_duration_seconds() { - assert_eq!( - super::EPOCH_DURATION_SECONDS, - fvm_shared3::clock::EPOCH_DURATION_SECONDS - ) - } - - #[test] - fn fvm4_shim_of_const_epoch_duration_seconds() { - assert_eq!( - super::EPOCH_DURATION_SECONDS, - fvm_shared4::clock::EPOCH_DURATION_SECONDS - ) - } -}