Skip to content

Commit

Permalink
fix: make delegation simpler by introducing a prelude module
Browse files Browse the repository at this point in the history
Users can now glob-import the kernel's "prelude" module instead of, e.g.,
glob-importing the entire `kernel` and the top-level `fvm` crate.
Ideally ambassador would handle importing all of its internal types, but
it doesn't.
  • Loading branch information
Stebalien committed Dec 6, 2023
1 parent f7f4230 commit f0f7e9a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 67 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fvm/src/executor/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
};

struct MachineExecRet {
result: crate::kernel::error::Result<InvocationResult>,
result: crate::kernel::Result<InvocationResult>,
gas_used: u64,
backtrace: Backtrace,
exec_trace: ExecutionTrace,
Expand Down
4 changes: 1 addition & 3 deletions fvm/src/kernel/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::IPLD_RAW;
use fvm_shared::address::Payload;
use fvm_shared::crypto::signature;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ErrorNumber;
use fvm_shared::event::{ActorEvent, Entry, Flags};
use fvm_shared::sys::out::vm::ContextFlags;
use fvm_shared::upgrade::UpgradeInfo;
use fvm_shared::ActorID;
use multihash::MultihashDigest;

use super::blocks::{Block, BlockRegistry};
Expand Down Expand Up @@ -606,7 +604,7 @@ where
)
}

fn hash(&self, code: u64, data: &[u8]) -> Result<MultihashGeneric<64>> {
fn hash(&self, code: u64, data: &[u8]) -> Result<Multihash> {
let hasher = SupportedHashes::try_from(code).map_err(|e| {
if let multihash::Error::UnsupportedCode(code) = e {
syscall_error!(IllegalArgument; "unsupported hash code {}", code)
Expand Down
25 changes: 15 additions & 10 deletions fvm/src/kernel/filecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@ use std::collections::BTreeMap;
use std::convert::TryInto;
use std::panic::{self, UnwindSafe};

use ambassador::Delegate;
use filecoin_proofs_api::{self as proofs, ProverId, PublicReplicaInfo, SectorId};

use fvm_ipld_encoding::bytes_32;
use fvm_shared::econ::TokenAmount;
use fvm_shared::piece::{zero_piece_commitment, PaddedPieceSize};
use fvm_shared::sector::{RegisteredPoStProof, SectorInfo};
use fvm_shared::{commcid, ActorID};
use fvm_shared::commcid;
use fvm_shared::consensus::ConsensusFault;
use fvm_shared::piece::{zero_piece_commitment, PaddedPieceSize, PieceInfo};
use fvm_shared::randomness::Randomness;
use fvm_shared::sector::{
AggregateSealVerifyProofAndInfos, RegisteredPoStProof, RegisteredSealProof, ReplicaUpdateInfo,
SealVerifyInfo, SectorInfo, WindowPoStVerifyInfo,
};
use lazy_static::lazy_static;
use rayon::iter::{
IndexedParallelIterator, IntoParallelRefIterator, ParallelDrainRange, ParallelIterator,
};

use super::blocks::BlockRegistry;
use super::error::Result;
use super::*;
use super::Result;
use super::{ClassifyResult, Context};
use crate::call_manager::CallManager;
use crate::externs::Consensus;
use crate::*;
use crate::machine::Machine;
use crate::{syscall_error, DefaultKernel, Kernel};

use super::prelude::*;

lazy_static! {
static ref NUM_CPUS: usize = num_cpus::get();
Expand All @@ -35,7 +40,7 @@ pub trait FilecoinKernel: Kernel {
&self,
proof_type: RegisteredSealProof,
pieces: &[PieceInfo],
) -> Result<Cid>;
) -> Result<cid::Cid>;

/// Verifies a window proof of spacetime.
fn verify_post(&self, verify_info: &WindowPoStVerifyInfo) -> Result<bool>;
Expand Down
89 changes: 56 additions & 33 deletions fvm/src/kernel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
pub use blocks::{Block, BlockId, BlockRegistry, BlockStat};
use cid::Cid;
use fvm_shared::address::Address;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::consensus::ConsensusFault;
use fvm_shared::crypto::signature::{
SignatureType, SECP_PUB_LEN, SECP_SIG_LEN, SECP_SIG_MESSAGE_HASH_SIZE,
};
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::piece::PieceInfo;
use fvm_shared::randomness::{Randomness, RANDOMNESS_LENGTH};
use fvm_shared::sector::{
AggregateSealVerifyProofAndInfos, RegisteredSealProof, ReplicaUpdateInfo, SealVerifyInfo,
WindowPoStVerifyInfo,
};
use fvm_shared::sys::out::network::NetworkContext;
use fvm_shared::sys::out::vm::MessageContext;
use fvm_shared::sys::SendFlags;
use fvm_shared::{ActorID, MethodNum};
use ambassador::delegatable_trait;
use fvm_shared::event::StampedEvent;
use wasmtime::Linker;

use crate::call_manager::CallManager;
use crate::machine::limiter::MemoryLimiter;
use crate::machine::Machine;
use crate::syscalls::InvocationData;

mod blocks;
mod error;
mod hash;

pub mod default;
pub mod filecoin;

pub(crate) mod error;

use ambassador::delegatable_trait;
pub use blocks::{Block, BlockId, BlockRegistry, BlockStat};
pub use error::{ClassifyResult, Context, ExecutionError, Result, SyscallError};
use fvm_shared::event::StampedEvent;
pub use hash::SupportedHashes;
use multihash::MultihashGeneric;
use wasmtime::Linker;

use crate::call_manager::CallManager;
use crate::gas::{Gas, GasTimer, PriceList};
use crate::machine::limiter::MemoryLimiter;
use crate::machine::Machine;
use crate::syscalls::InvocationData;

pub struct CallResult {
pub block_id: BlockId,
Expand Down Expand Up @@ -280,7 +258,7 @@ pub trait CryptoOps {
/// `digest_out`, returning the size of the digest written to `digest_out`. If `digest_out` is
/// to small to fit the entire digest, it will be truncated. If too large, the leftover space
/// will not be overwritten.
fn hash(&self, code: u64, data: &[u8]) -> Result<MultihashGeneric<64>>;
fn hash(&self, code: u64, data: &[u8]) -> Result<Multihash>;
}

/// Randomness queries.
Expand Down Expand Up @@ -338,3 +316,48 @@ pub trait EventOps {
raw_val: &[u8],
) -> Result<()>;
}

// Unfortunately, I need to do this to make it possible to name these macros by path. I'm hiding
// this because I really don't want users to glob import the `kernel` module.
#[doc(hidden)]
pub use {
ambassador_impl_CryptoOps, ambassador_impl_DebugOps, ambassador_impl_EventOps,
ambassador_impl_GasOps, ambassador_impl_IpldBlockOps, ambassador_impl_LimiterOps,
ambassador_impl_MessageOps, ambassador_impl_NetworkOps, ambassador_impl_RandomnessOps,
ambassador_impl_SelfOps,
};

/// Import this module (with a glob) if you're implementing a kernel, _especially_ if you want to
/// use ambassador to delegate the implementation.
pub mod prelude {
pub use super::{
ActorOps, CryptoOps, DebugOps, EventOps, GasOps, IpldBlockOps, LimiterOps, MessageOps,
NetworkOps, RandomnessOps, SelfOps,
};
pub use super::{Block, BlockId, BlockRegistry, BlockStat, CallResult, Kernel, SyscallHandler};
pub use crate::gas::{Gas, GasTimer, PriceList};
pub use ambassador::Delegate;
pub use cid::Cid;
pub use fvm_shared::address::Address;
pub use fvm_shared::clock::ChainEpoch;
pub use fvm_shared::crypto::signature::{
SignatureType, SECP_PUB_LEN, SECP_SIG_LEN, SECP_SIG_MESSAGE_HASH_SIZE,
};
pub use fvm_shared::econ::TokenAmount;
pub use fvm_shared::error::ExitCode;
pub use fvm_shared::randomness::RANDOMNESS_LENGTH;
pub use fvm_shared::sys::out::network::NetworkContext;
pub use fvm_shared::sys::out::vm::MessageContext;
pub use fvm_shared::sys::SendFlags;
pub use fvm_shared::version::NetworkVersion;
pub use fvm_shared::{ActorID, MethodNum};
pub use multihash::Multihash;
pub use {
ambassador_impl_ActorOps, ambassador_impl_CryptoOps, ambassador_impl_DebugOps,
ambassador_impl_EventOps, ambassador_impl_GasOps, ambassador_impl_IpldBlockOps,
ambassador_impl_LimiterOps, ambassador_impl_MessageOps, ambassador_impl_NetworkOps,
ambassador_impl_RandomnessOps, ambassador_impl_SelfOps,
};
}

pub use prelude::*;
1 change: 0 additions & 1 deletion testing/conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ ittapi-rs = { version = "0.3.0", optional = true }
libipld-core = { version = "0.16.0", features = ["serde-codec"] }
tar = { version = "0.4.38", default-features = false }
zstd = { version = "0.12.3", default-features = false }
ambassador = "0.3.5"

[dependencies.fvm]
version = "4.0.0"
Expand Down
22 changes: 4 additions & 18 deletions testing/conformance/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,27 @@
use std::convert::TryFrom;
use std::sync::{Arc, Mutex};

use ambassador::Delegate;
use anyhow::anyhow;
use cid::Cid;
use fvm::kernel::filecoin::{DefaultFilecoinKernel, FilecoinKernel};
use fvm::syscalls::InvocationData;
use multihash::MultihashGeneric;

use fvm::call_manager::{CallManager, DefaultCallManager};
use fvm::gas::{price_list_by_network_version, Gas, GasTimer, PriceList};
use fvm::gas::price_list_by_network_version;
use fvm::machine::limiter::MemoryLimiter;
use fvm::machine::{DefaultMachine, Machine, MachineContext, Manifest, NetworkConfig};
use fvm::state_tree::StateTree;
use fvm_ipld_blockstore::MemoryBlockstore;
use fvm_shared::address::Address;
use fvm_shared::clock::ChainEpoch;
use fvm_shared::consensus::ConsensusFault;
use fvm_shared::crypto::signature::{
SignatureType, SECP_PUB_LEN, SECP_SIG_LEN, SECP_SIG_MESSAGE_HASH_SIZE,
};
use fvm_shared::econ::TokenAmount;
use fvm_shared::piece::PieceInfo;
use fvm_shared::randomness::RANDOMNESS_LENGTH;
use fvm_shared::sector::{
AggregateSealVerifyProofAndInfos, RegisteredSealProof, ReplicaUpdateInfo, SealVerifyInfo,
};
use fvm_shared::sys::out::network::NetworkContext;
use fvm_shared::sys::out::vm::MessageContext;
use fvm_shared::sys::SendFlags;
use fvm_shared::version::NetworkVersion;
use fvm_shared::{ActorID, MethodNum};
use wasmtime::Linker;

// We have glob imports here because delegation doesn't work well without it.
use fvm::*;
use kernel::*;
use fvm::kernel::prelude::*;
use fvm::kernel::Result;
use fvm::DefaultKernel;

use crate::externs::TestExterns;
use crate::vector::{MessageVector, Variant};
Expand Down

0 comments on commit f0f7e9a

Please sign in to comment.