Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Dec 1, 2023
1 parent c166dfe commit 86200d7
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 79 deletions.
9 changes: 4 additions & 5 deletions fvm/src/call_manager/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::engine::Engine;
use crate::gas::{Gas, GasTracker};
use crate::kernel::{
Block, BlockRegistry, ClassifyResult, ExecutionError, Kernel, Result, SyscallError,
SyscallHandler,
};
use crate::machine::limiter::MemoryLimiter;
use crate::machine::Machine;
Expand Down Expand Up @@ -181,7 +180,7 @@ where
read_only: bool,
) -> Result<InvocationResult>
where
K: Kernel<CallManager = Self> + SyscallHandler<K>,
K: Kernel<CallManager = Self>,
{
if self.machine.context().tracing {
self.trace(ExecutionEvent::Call {
Expand Down Expand Up @@ -549,7 +548,7 @@ where
/// 2. Initializes it by calling the constructor.
fn create_account_actor_from_send<K>(&mut self, addr: &Address) -> Result<ActorID>
where
K: Kernel<CallManager = Self> + SyscallHandler<K>,
K: Kernel<CallManager = Self>,
{
if addr.is_bls_zero_address() {
return Err(
Expand Down Expand Up @@ -608,7 +607,7 @@ where
read_only: bool,
) -> Result<InvocationResult>
where
K: Kernel<CallManager = Self> + SyscallHandler<K>,
K: Kernel<CallManager = Self>,
{
// Get the receiver; this will resolve the address.
let to = match self.resolve_address(&to)? {
Expand Down Expand Up @@ -654,7 +653,7 @@ where
read_only: bool,
) -> Result<InvocationResult>
where
K: Kernel<CallManager = Self> + SyscallHandler<K>,
K: Kernel<CallManager = Self>,
{
// Lookup the actor.
let state = self
Expand Down
4 changes: 2 additions & 2 deletions fvm/src/call_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use fvm_shared::{ActorID, MethodNum};

use crate::engine::Engine;
use crate::gas::{Gas, GasCharge, GasTimer, GasTracker, PriceList};
use crate::kernel::{self, BlockRegistry, ClassifyResult, Context, Result, SyscallHandler};
use crate::kernel::{self, BlockRegistry, ClassifyResult, Context, Result};
use crate::machine::{Machine, MachineContext};
use crate::state_tree::ActorState;
use crate::Kernel;
Expand Down Expand Up @@ -65,7 +65,7 @@ pub trait CallManager: 'static {
/// Calls an actor at the given address and entrypoint. The type parameter `K` specifies the the _kernel_ on top of which the target
/// actor should execute.
#[allow(clippy::too_many_arguments)]
fn call_actor<K: Kernel<CallManager = Self> + SyscallHandler<K>>(
fn call_actor<K: Kernel<CallManager = Self>>(
&mut self,
from: ActorID,
to: Address,
Expand Down
10 changes: 2 additions & 8 deletions fvm/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use wasmtime::{
};

use crate::gas::{Gas, GasTimer, WasmGasPrices};
use crate::kernel::SyscallHandler;
use crate::machine::limiter::MemoryLimiter;
use crate::machine::{Machine, NetworkConfig};
use crate::syscalls::error::Abort;
Expand Down Expand Up @@ -498,7 +497,7 @@ impl Engine {
/// linker, syscalls, etc.
///
/// This returns an `Abort` as it may need to execute initialization code, charge gas, etc.
pub fn instantiate<K: Kernel + SyscallHandler<K>>(
pub fn instantiate<K: Kernel>(
&self,
store: &mut wasmtime::Store<InvocationData<K>>,
k: &Cid,
Expand All @@ -516,12 +515,7 @@ impl Engine {
.insert({
let mut linker = Linker::new(&self.inner.engine);
linker.allow_shadowing(true);

store
.data()
.kernel
.bind_syscalls(&mut linker)
.map_err(Abort::Fatal)?;
K::bind_syscalls(&mut linker).map_err(Abort::Fatal)?;

Box::new(Cache { linker })
})
Expand Down
4 changes: 2 additions & 2 deletions fvm/src/executor/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::call_manager::{backtrace, Backtrace, CallManager, Entrypoint, Invocat
use crate::eam_actor::EAM_ACTOR_ID;
use crate::engine::EnginePool;
use crate::gas::{Gas, GasCharge, GasOutputs};
use crate::kernel::{Block, ClassifyResult, Context as _, ExecutionError, Kernel, SyscallHandler};
use crate::kernel::{Block, ClassifyResult, Context as _, ExecutionError, Kernel};
use crate::machine::{Machine, BURNT_FUNDS_ACTOR_ID, REWARD_ACTOR_ID};
use crate::trace::ExecutionTrace;

Expand Down Expand Up @@ -53,7 +53,7 @@ impl<K: Kernel> DerefMut for DefaultExecutor<K> {

impl<K> Executor for DefaultExecutor<K>
where
K: Kernel + SyscallHandler,
K: Kernel,
{
type Kernel = K;

Expand Down
12 changes: 3 additions & 9 deletions fvm/src/kernel/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ where

impl<K> CallOps<K> for DefaultKernel<K::CallManager>
where
K: Kernel + SyscallHandler<K>,
K: Kernel,
{
fn send(
&mut self,
Expand All @@ -437,10 +437,7 @@ where
value: &TokenAmount,
gas_limit: Option<Gas>,
flags: SendFlags,
) -> Result<CallResult>
where
K: SyscallHandler<K> + Kernel,
{
) -> Result<CallResult> {
let from = self.actor_id;
let read_only = self.read_only || flags.read_only();

Expand Down Expand Up @@ -506,10 +503,7 @@ where
})
}

fn upgrade_actor(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<CallResult>
where
K: SyscallHandler<K> + Kernel,
{
fn upgrade_actor(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<CallResult> {
if self.read_only {
return Err(
syscall_error!(ReadOnly, "upgrade_actor cannot be called while read-only").into(),
Expand Down
2 changes: 1 addition & 1 deletion fvm/src/kernel/filecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub trait FilecoinKernel: Kernel {
#[derive(Delegate)]
#[delegate(IpldBlockOps, where = "C: CallManager")]
#[delegate(ActorOps, where = "C: CallManager")]
#[delegate(CallOps<K>, generics = "K", where = "C: CallManager, K: SyscallHandler<K> + Kernel")]
#[delegate(CallOps<K>, generics = "K", where = "C: CallManager, K: Kernel")]
#[delegate(CryptoOps, where = "C: CallManager")]
#[delegate(DebugOps, where = "C: CallManager")]
#[delegate(SystemOps, where = "C: CallManager")]
Expand Down
15 changes: 11 additions & 4 deletions fvm/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ pub struct CallResult {
pub exit_code: ExitCode,
}

pub trait Kernel: 'static {
/// The [`Kernel`]'s [`CallManager`] is
/// The base [`Kernel`] trait implemented by all kernels.
pub trait Kernel: SyscallHandler + 'static {
/// The [`Kernel`]'s [`CallManager`].
type CallManager: CallManager;
/// The [`Kernel`]'s memory allocation tracker.
type Limiter: MemoryLimiter;

/// Construct a new [`Kernel`] from the given [`CallManager`].
Expand Down Expand Up @@ -83,21 +85,26 @@ pub trait Kernel: 'static {
fn into_inner(self) -> (Self::CallManager, BlockRegistry)
where
Self: Sized;

/// The kernel's underlying "machine".
fn machine(&self) -> &<Self::CallManager as CallManager>::Machine;

/// ChargeGas charges specified amount of `gas` for execution.
/// `name` provides information about gas charging point.
fn charge_gas(&self, name: &str, compute: Gas) -> Result<GasTimer>;

/// XXX Returns the remaining gas for the transaction.
// XXX: Move these somewhere else?

/// Returns the remaining gas for the transaction.
fn gas_available(&self) -> Gas;

/// XXX: Testing only!
fn gas_used(&self) -> Gas;
}

/// This trait links the kernel to a wasm module via wasm "host" calls (syscalls).
pub trait SyscallHandler<K = Self>: Sized {
fn bind_syscalls(&self, linker: &mut Linker<InvocationData<K>>) -> anyhow::Result<()>;
fn bind_syscalls(linker: &mut Linker<InvocationData<K>>) -> anyhow::Result<()>;
}

#[delegatable_trait]
Expand Down
6 changes: 2 additions & 4 deletions fvm/src/syscalls/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use fvm_shared::{sys, ActorID};
use super::bind::ControlFlow;
use super::error::Abort;
use super::Context;
use crate::kernel::{
ActorOps, CallOps, CallResult, ClassifyResult, Result, SyscallHandler, SystemOps,
};
use crate::kernel::{ActorOps, CallOps, CallResult, ClassifyResult, Result, SystemOps};
use crate::{syscall_error, Kernel};

pub fn resolve_address(
Expand Down Expand Up @@ -113,7 +111,7 @@ pub fn create_actor(
context.kernel.create_actor(typ, actor_id, addr)
}

pub fn upgrade_actor<K: CallOps + Kernel + SyscallHandler>(
pub fn upgrade_actor<K: CallOps + Kernel>(
context: Context<'_, K>,
new_code_cid_off: u32,
params_id: u32,
Expand Down
31 changes: 9 additions & 22 deletions fvm/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use anyhow::{anyhow, Context as _};
use num_traits::Zero;
use wasmtime::{AsContextMut, ExternType, Global, Linker, Memory, Module, Val};

use crate::call_manager::{backtrace, CallManager};
use crate::call_manager::backtrace;
use crate::gas::{Gas, GasInstant, GasTimer};
use crate::kernel::filecoin::DefaultFilecoinKernel;
use crate::kernel::filecoin::{DefaultFilecoinKernel, FilecoinKernel};
use crate::kernel::{
ActorOps, CallOps, ChainOps, CryptoOps, DebugOps, ExecutionError, IpldBlockOps, SyscallHandler,
SystemOps,
Expand Down Expand Up @@ -241,20 +241,9 @@ use self::error::Abort;

impl<K> SyscallHandler<K> for DefaultKernel<K::CallManager>
where
K: ChainOps
+ ActorOps
+ CryptoOps
+ SystemOps
+ IpldBlockOps
+ DebugOps
+ CallOps
+ SyscallHandler<K>
+ Kernel,
K: Kernel + ChainOps + ActorOps + CryptoOps + SystemOps + IpldBlockOps + DebugOps + CallOps,
{
fn bind_syscalls(
&self,
linker: &mut wasmtime::Linker<InvocationData<K>>,
) -> anyhow::Result<()> {
fn bind_syscalls(linker: &mut wasmtime::Linker<InvocationData<K>>) -> anyhow::Result<()> {
linker.bind("vm", "exit", vm::exit)?;
linker.bind("vm", "message_context", vm::message_context)?;

Expand Down Expand Up @@ -330,15 +319,13 @@ where
}
}

impl<C> SyscallHandler<DefaultFilecoinKernel<C>> for DefaultFilecoinKernel<C>
impl<K> SyscallHandler<K> for DefaultFilecoinKernel<K::CallManager>
where
C: CallManager,
K: FilecoinKernel,
DefaultKernel<K::CallManager>: SyscallHandler<K>,
{
fn bind_syscalls(
&self,
linker: &mut Linker<InvocationData<DefaultFilecoinKernel<C>>>,
) -> anyhow::Result<()> {
self.0.bind_syscalls(linker)?;
fn bind_syscalls(linker: &mut Linker<InvocationData<K>>) -> anyhow::Result<()> {
DefaultKernel::bind_syscalls(linker)?;

// Now bind the crypto syscalls.
linker.bind(
Expand Down
4 changes: 2 additions & 2 deletions fvm/src/syscalls/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use fvm_shared::sys::{self, SendFlags};

use super::Context;
use crate::gas::Gas;
use crate::kernel::{CallOps, CallResult, ClassifyResult, Result, SyscallHandler};
use crate::kernel::{CallOps, CallResult, ClassifyResult, Result};
use crate::Kernel;

/// Send a message to another actor. The result is placed as a CBOR-encoded
/// receipt in the block registry, and can be retrieved by the returned BlockId.
#[allow(clippy::too_many_arguments)]
pub fn send<K: CallOps + Kernel + SyscallHandler>(
pub fn send<K: CallOps + Kernel>(
context: Context<'_, K>,
recipient_off: u32,
recipient_len: u32,
Expand Down
3 changes: 1 addition & 2 deletions fvm/tests/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use fvm::call_manager::{Backtrace, CallManager, Entrypoint, FinishRet, Invocatio
use fvm::engine::Engine;
use fvm::externs::{Chain, Consensus, Externs, Rand};
use fvm::gas::{Gas, GasCharge, GasTimer, GasTracker};
use fvm::kernel::SyscallHandler;
use fvm::machine::limiter::MemoryLimiter;
use fvm::machine::{Machine, MachineContext, Manifest, NetworkConfig};
use fvm::state_tree::StateTree;
Expand Down Expand Up @@ -275,7 +274,7 @@ impl CallManager for DummyCallManager {
}
}

fn call_actor<K: Kernel<CallManager = Self> + SyscallHandler<K>>(
fn call_actor<K: Kernel<CallManager = Self>>(
&mut self,
_from: fvm_shared::ActorID,
_to: Address,
Expand Down
32 changes: 14 additions & 18 deletions testing/conformance/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,11 @@ impl<M, C, K> Kernel for TestKernel<K>
where
M: Machine,
C: CallManager<Machine = TestMachine<M>>,
K: Kernel<CallManager = C>,
K: Kernel<CallManager = C> + SyscallHandler<Self>,
{
type CallManager = K::CallManager;
type Limiter = K::Limiter;

fn into_inner(self) -> (Self::CallManager, BlockRegistry)
where
Self: Sized,
{
self.0.into_inner()
}

fn new(
mgr: C,
blocks: BlockRegistry,
Expand Down Expand Up @@ -236,6 +229,13 @@ where
)
}

fn into_inner(self) -> (Self::CallManager, BlockRegistry)
where
Self: Sized,
{
self.0.into_inner()
}

fn machine(&self) -> &<Self::CallManager as CallManager>::Machine {
self.0.machine()
}
Expand All @@ -261,12 +261,11 @@ where
}
}

impl<M, C, K, KK> CallOps<KK> for TestKernel<K>
impl<M, C, K> CallOps<Self> for TestKernel<K>
where
M: Machine,
C: CallManager<Machine = TestMachine<M>>,
K: Kernel<CallManager = C> + CallOps<Self>,
KK: SyscallHandler<KK> + Kernel,
{
/// Sends a message to another actor.
/// The method type parameter K is the type of the kernel to instantiate for
Expand All @@ -293,25 +292,22 @@ where
}
}

impl<M, C, K> SyscallHandler<TestKernel<K>> for TestKernel<K>
impl<M, C, K> SyscallHandler<Self> for TestKernel<K>
where
M: Machine,
C: CallManager<Machine = TestMachine<M>>,
K: Kernel<CallManager = C>,
K: Kernel<CallManager = C> + SyscallHandler<Self>,
{
fn bind_syscalls(
&self,
_linker: &mut Linker<InvocationData<TestKernel<K>>>,
) -> anyhow::Result<()> {
Ok(())
fn bind_syscalls(linker: &mut Linker<InvocationData<Self>>) -> anyhow::Result<()> {
K::bind_syscalls(linker)
}
}

impl<M, C, K> FilecoinKernel for TestKernel<K>
where
M: Machine,
C: CallManager<Machine = TestMachine<M>>,
K: FilecoinKernel<CallManager = C>,
K: FilecoinKernel<CallManager = C> + SyscallHandler<Self>,
{
fn compute_unsealed_sector_cid(
&self,
Expand Down

0 comments on commit 86200d7

Please sign in to comment.