Skip to content

Commit

Permalink
refactor: remove most ops from the base kernel trait
Browse files Browse the repository at this point in the history
This makes the kernel more customizable as it no longer "assumes" that
all of these operations will be available.

Also, remove the `LimiterOps` trait entirely, this isn't actually an
"op" (i.e., not a user-invokable operation). The gas ops will get moved
around in a future commit as many of them aren't really "ops" either.
  • Loading branch information
Stebalien committed Dec 8, 2023
1 parent 9f49b17 commit 645c625
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 49 deletions.
16 changes: 5 additions & 11 deletions fvm/src/kernel/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ where
C: CallManager,
{
type CallManager = C;
type Limiter = <<C as CallManager>::Machine as Machine>::Limiter;

fn into_inner(self) -> (Self::CallManager, BlockRegistry)
where
Expand Down Expand Up @@ -264,6 +265,10 @@ where
Err(err) => Err(err),
}
}

fn limiter_mut(&mut self) -> &mut Self::Limiter {
self.call_manager.limiter_mut()
}
}

impl<C> DefaultKernel<C>
Expand Down Expand Up @@ -949,17 +954,6 @@ where
}
}

impl<C> LimiterOps for DefaultKernel<C>
where
C: CallManager,
{
type Limiter = <<C as CallManager>::Machine as Machine>::Limiter;

fn limiter_mut(&mut self) -> &mut Self::Limiter {
self.call_manager.limiter_mut()
}
}

impl<C> EventOps for DefaultKernel<C>
where
C: CallManager,
Expand Down
6 changes: 5 additions & 1 deletion fvm/src/kernel/filecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ pub trait FilecoinKernel: Kernel {
#[delegate(NetworkOps)]
#[delegate(RandomnessOps)]
#[delegate(SelfOps)]
#[delegate(LimiterOps)]
pub struct DefaultFilecoinKernel<K>(pub K)
where
K: Kernel;
Expand Down Expand Up @@ -251,6 +250,7 @@ where
C: CallManager,
{
type CallManager = C;
type Limiter = <DefaultKernel<C> as Kernel>::Limiter;

fn into_inner(self) -> (Self::CallManager, BlockRegistry)
where
Expand Down Expand Up @@ -303,6 +303,10 @@ where
read_only,
))
}

fn limiter_mut(&mut self) -> &mut Self::Limiter {
self.0.call_manager.limiter_mut()
}
}

fn catch_and_log_panic<F: FnOnce() -> Result<R> + UnwindSafe, R>(context: &str, f: F) -> Result<R> {
Expand Down
46 changes: 12 additions & 34 deletions fvm/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,11 @@ pub struct CallResult {
///
/// Actors may call into the kernel via the syscalls defined in the [`syscalls`][crate::syscalls]
/// module.
pub trait Kernel:
SyscallHandler<Self>
+ ActorOps
+ IpldBlockOps
+ CryptoOps
+ DebugOps
+ EventOps
+ GasOps
+ MessageOps
+ NetworkOps
+ RandomnessOps
+ SelfOps
+ LimiterOps
+ 'static
{
pub trait Kernel: GasOps + SyscallHandler<Self> + 'static {
/// The [`Kernel`]'s [`CallManager`] is
type CallManager: CallManager;
/// The [`Kernel`]'s memory allocation tracker.
type Limiter: MemoryLimiter;

/// Consume the [`Kernel`] and return the underlying [`CallManager`] and [`BlockRegistry`].
fn into_inner(self) -> (Self::CallManager, BlockRegistry)
Expand Down Expand Up @@ -101,6 +89,9 @@ pub trait Kernel:
new_code_cid: Cid,
params_id: BlockId,
) -> Result<CallResult>;

/// Give access to the limiter of the underlying call manager.
fn limiter_mut(&mut self) -> &mut Self::Limiter;
}

pub trait SyscallHandler<K: Kernel>: Sized {
Expand Down Expand Up @@ -293,18 +284,6 @@ pub trait DebugOps {
fn store_artifact(&self, name: &str, data: &[u8]) -> Result<()>;
}

/// Track and limit memory expansion.
///
/// This interface is not one of the operations the kernel provides to actors.
/// It's only part of the kernel out of necessity to pass it through to the
/// call manager which tracks the limits across the whole execution stack.
#[delegatable_trait]
pub trait LimiterOps {
type Limiter: MemoryLimiter;
/// Give access to the limiter of the underlying call manager.
fn limiter_mut(&mut self) -> &mut Self::Limiter;
}

/// Eventing APIs.
#[delegatable_trait]
pub trait EventOps {
Expand All @@ -322,17 +301,16 @@ pub trait EventOps {
#[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,
ambassador_impl_GasOps, ambassador_impl_IpldBlockOps, 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,
ActorOps, CryptoOps, DebugOps, EventOps, GasOps, IpldBlockOps, MessageOps, NetworkOps,
RandomnessOps, SelfOps,
};
pub use super::{Block, BlockId, BlockRegistry, BlockStat, CallResult, Kernel, SyscallHandler};
pub use crate::gas::{Gas, GasTimer, PriceList};
Expand All @@ -355,8 +333,8 @@ pub mod prelude {
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,
ambassador_impl_MessageOps, ambassador_impl_NetworkOps, ambassador_impl_RandomnessOps,
ambassador_impl_SelfOps,
};
}

Expand Down
17 changes: 15 additions & 2 deletions fvm/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use wasmtime::{AsContextMut, ExternType, Global, Linker, Memory, Module, Val};
use crate::call_manager::{backtrace, CallManager};
use crate::gas::{Gas, GasInstant, GasTimer};
use crate::kernel::filecoin::DefaultFilecoinKernel;
use crate::kernel::{ExecutionError, SyscallHandler};
use crate::kernel::{
ActorOps, CryptoOps, DebugOps, EventOps, ExecutionError, GasOps, IpldBlockOps, MessageOps,
NetworkOps, RandomnessOps, SelfOps, SyscallHandler,
};

use crate::machine::limiter::MemoryLimiter;
use crate::{DefaultKernel, Kernel};
Expand Down Expand Up @@ -238,7 +241,17 @@ use self::bind::BindSyscall;

impl<K> SyscallHandler<K> for DefaultKernel<K::CallManager>
where
K: Kernel,
K: Kernel
+ ActorOps
+ IpldBlockOps
+ CryptoOps
+ DebugOps
+ EventOps
+ GasOps
+ MessageOps
+ NetworkOps
+ RandomnessOps
+ SelfOps,
{
fn bind_syscalls(
&self,
Expand Down
6 changes: 5 additions & 1 deletion testing/conformance/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ where
#[delegate(NetworkOps)]
#[delegate(RandomnessOps)]
#[delegate(SelfOps)]
#[delegate(LimiterOps)]
pub struct TestKernel<K = DefaultFilecoinKernel<DefaultKernel<DefaultCallManager<TestMachine>>>>(
pub K,
);
Expand All @@ -182,6 +181,7 @@ where
K: Kernel<CallManager = C>,
{
type CallManager = K::CallManager;
type Limiter = K::Limiter;

fn into_inner(self) -> (Self::CallManager, BlockRegistry)
where
Expand Down Expand Up @@ -237,6 +237,10 @@ where
fn upgrade_actor<KK>(&mut self, new_code_cid: Cid, params_id: BlockId) -> Result<CallResult> {
self.0.upgrade_actor::<Self>(new_code_cid, params_id)
}

fn limiter_mut(&mut self) -> &mut Self::Limiter {
self.0.limiter_mut()
}
}

impl<M, C, K> SyscallHandler<TestKernel<K>> for TestKernel<K>
Expand Down

0 comments on commit 645c625

Please sign in to comment.