Skip to content

Commit

Permalink
refactor: use narrower trait requirements for syscalls
Browse files Browse the repository at this point in the history
This brings us one step closer to making the syscalls more configurable.
  • Loading branch information
Stebalien committed Dec 10, 2023
1 parent 6867921 commit f89bd0e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 49 deletions.
20 changes: 10 additions & 10 deletions fvm/src/syscalls/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use fvm_shared::{sys, ActorID};
use super::bind::ControlFlow;
use super::error::Abort;
use super::Context;
use crate::kernel::{CallResult, ClassifyResult, Result};
use crate::kernel::{ActorOps, CallResult, ClassifyResult, Result};
use crate::{syscall_error, Kernel};

pub fn resolve_address(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
addr_off: u32, // Address
addr_len: u32,
) -> Result<u64> {
Expand All @@ -20,7 +20,7 @@ pub fn resolve_address(
}

pub fn lookup_delegated_address(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
actor_id: ActorID,
obuf_off: u32,
obuf_len: u32,
Expand All @@ -41,7 +41,7 @@ pub fn lookup_delegated_address(
}

pub fn get_actor_code_cid(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
actor_id: u64,
obuf_off: u32, // Cid
obuf_len: u32,
Expand All @@ -59,7 +59,7 @@ pub fn get_actor_code_cid(
/// The output buffer must be at least 21 bytes long, which is the length of a class 2 address
/// (protocol-generated actor address).
pub fn next_actor_address(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
obuf_off: u32, // Address (out)
obuf_len: u32,
) -> Result<u32> {
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn next_actor_address(
}

pub fn create_actor(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
actor_id: u64, // ID
typ_off: u32, // Cid
delegated_addr_off: u32,
Expand Down Expand Up @@ -143,15 +143,15 @@ pub fn upgrade_actor<K: Kernel>(
}

pub fn get_builtin_actor_type(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
code_cid_off: u32, // Cid
) -> Result<i32> {
let cid = context.memory.read_cid(code_cid_off)?;
Ok(context.kernel.get_builtin_actor_type(&cid)? as i32)
}

pub fn get_code_cid_for_type(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
typ: i32,
obuf_off: u32, // Cid
obuf_len: u32,
Expand All @@ -163,14 +163,14 @@ pub fn get_code_cid_for_type(
}

pub fn install_actor(
context: Context<'_, impl Kernel>,
context: Context<'_, impl ActorOps>,
typ_off: u32, // Cid
) -> Result<()> {
let typ = context.memory.read_cid(typ_off)?;
context.kernel.install_actor(typ)
}

pub fn balance_of(context: Context<'_, impl Kernel>, actor_id: u64) -> Result<sys::TokenAmount> {
pub fn balance_of(context: Context<'_, impl ActorOps>, actor_id: u64) -> Result<sys::TokenAmount> {
let balance = context.kernel.balance_of(actor_id)?;
balance
.try_into()
Expand Down
9 changes: 4 additions & 5 deletions fvm/src/syscalls/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use fvm_shared::crypto::signature::{
use num_traits::FromPrimitive;

use super::Context;
use crate::kernel::{ClassifyResult, Result};
use crate::Kernel;
use crate::kernel::{ClassifyResult, CryptoOps, Result};

/// Verifies that a signature is valid for an address and plaintext.
///
Expand All @@ -19,7 +18,7 @@ use crate::Kernel;
/// - -1: verification failed.
#[allow(clippy::too_many_arguments)]
pub fn verify_signature(
context: Context<'_, impl Kernel>,
context: Context<'_, impl CryptoOps>,
sig_type: u32,
sig_off: u32,
sig_len: u32,
Expand All @@ -42,7 +41,7 @@ pub fn verify_signature(
}

pub fn recover_secp_public_key(
context: Context<'_, impl Kernel>,
context: Context<'_, impl CryptoOps>,
hash_off: u32,
sig_off: u32,
) -> Result<[u8; SECP_PUB_LEN]> {
Expand All @@ -66,7 +65,7 @@ pub fn recover_secp_public_key(
/// Hashes input data using the specified hash function, writing the digest into the provided
/// buffer.
pub fn hash(
context: Context<'_, impl Kernel>,
context: Context<'_, impl CryptoOps>,
hash_code: u64,
data_off: u32, // input
data_len: u32,
Expand Down
9 changes: 4 additions & 5 deletions fvm/src/syscalls/debug.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT
use crate::kernel::{ClassifyResult, Result};
use crate::kernel::{ClassifyResult, DebugOps, Result};
use crate::syscalls::context::Context;
use crate::Kernel;

pub fn log(context: Context<'_, impl Kernel>, msg_off: u32, msg_len: u32) -> Result<()> {
pub fn log(context: Context<'_, impl DebugOps>, msg_off: u32, msg_len: u32) -> Result<()> {
// No-op if disabled.
if !context.kernel.debug_enabled() {
return Ok(());
Expand All @@ -16,7 +15,7 @@ pub fn log(context: Context<'_, impl Kernel>, msg_off: u32, msg_len: u32) -> Res
Ok(())
}

pub fn enabled(context: Context<'_, impl Kernel>) -> Result<i32> {
pub fn enabled(context: Context<'_, impl DebugOps>) -> Result<i32> {
Ok(if context.kernel.debug_enabled() {
0
} else {
Expand All @@ -25,7 +24,7 @@ pub fn enabled(context: Context<'_, impl Kernel>) -> Result<i32> {
}

pub fn store_artifact(
context: Context<'_, impl Kernel>,
context: Context<'_, impl DebugOps>,
name_off: u32,
name_len: u32,
data_off: u32,
Expand Down
5 changes: 2 additions & 3 deletions fvm/src/syscalls/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use anyhow::Context as _;

use super::Context;
use crate::kernel::{ClassifyResult, Result};
use crate::Kernel;
use crate::kernel::{ClassifyResult, EventOps, Result};

/// Emits an actor event. The event is split into three raw byte buffers that have
/// been written to Wasm memory. This is done so that the FVM can accurately charge
Expand All @@ -25,7 +24,7 @@ use crate::Kernel;
/// Calling this syscall may immediately halt execution with an out of gas error,
/// if such condition arises.
pub fn emit_event(
context: Context<'_, impl Kernel>,
context: Context<'_, impl EventOps>,
event_off: u32,
event_len: u32,
key_off: u32,
Expand Down
7 changes: 3 additions & 4 deletions fvm/src/syscalls/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use std::str;

use super::Context;
use crate::gas::Gas;
use crate::kernel::{ClassifyResult, Result};
use crate::Kernel;
use crate::kernel::{ClassifyResult, GasOps, Result};

pub fn charge_gas(
context: Context<'_, impl Kernel>,
context: Context<'_, impl GasOps>,
name_off: u32,
name_len: u32,
compute: u64,
Expand All @@ -22,6 +21,6 @@ pub fn charge_gas(
.map(|_| ())
}

pub fn available(context: Context<'_, impl Kernel>) -> Result<u64> {
pub fn available(context: Context<'_, impl GasOps>) -> Result<u64> {
Ok(context.kernel.gas_available().round_down())
}
19 changes: 12 additions & 7 deletions fvm/src/syscalls/ipld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
use fvm_shared::sys;

use super::Context;
use crate::kernel::Result;
use crate::Kernel;
use crate::kernel::{IpldBlockOps, Result};

pub fn block_open(context: Context<'_, impl Kernel>, cid: u32) -> Result<sys::out::ipld::IpldOpen> {
pub fn block_open(
context: Context<'_, impl IpldBlockOps>,
cid: u32,
) -> Result<sys::out::ipld::IpldOpen> {
let cid = context.memory.read_cid(cid)?;
let (id, stat) = context.kernel.block_open(&cid)?;
Ok(sys::out::ipld::IpldOpen {
Expand All @@ -17,7 +19,7 @@ pub fn block_open(context: Context<'_, impl Kernel>, cid: u32) -> Result<sys::ou
}

pub fn block_create(
context: Context<'_, impl Kernel>,
context: Context<'_, impl IpldBlockOps>,
codec: u64,
data_off: u32,
data_len: u32,
Expand All @@ -27,7 +29,7 @@ pub fn block_create(
}

pub fn block_link(
context: Context<'_, impl Kernel>,
context: Context<'_, impl IpldBlockOps>,
id: u32,
hash_fun: u64,
hash_len: u32,
Expand All @@ -45,7 +47,7 @@ pub fn block_link(
}

pub fn block_read(
context: Context<'_, impl Kernel>,
context: Context<'_, impl IpldBlockOps>,
id: u32,
offset: u32,
obuf_off: u32,
Expand All @@ -55,7 +57,10 @@ pub fn block_read(
context.kernel.block_read(id, offset, data)
}

pub fn block_stat(context: Context<'_, impl Kernel>, id: u32) -> Result<sys::out::ipld::IpldStat> {
pub fn block_stat(
context: Context<'_, impl IpldBlockOps>,
id: u32,
) -> Result<sys::out::ipld::IpldStat> {
context
.kernel
.block_stat(id)
Expand Down
6 changes: 3 additions & 3 deletions fvm/src/syscalls/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use fvm_shared::sys::out::network::NetworkContext;

use super::Context;
use crate::kernel::{Kernel, Result};
use crate::kernel::{NetworkOps, Result};

pub fn context(context: Context<'_, impl Kernel>) -> crate::kernel::Result<NetworkContext> {
pub fn context(context: Context<'_, impl NetworkOps>) -> crate::kernel::Result<NetworkContext> {
context.kernel.network_context()
}

pub fn tipset_cid(
context: Context<'_, impl Kernel>,
context: Context<'_, impl NetworkOps>,
epoch: i64,
obuf_off: u32,
obuf_len: u32,
Expand Down
7 changes: 3 additions & 4 deletions fvm/src/syscalls/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
use fvm_shared::randomness::RANDOMNESS_LENGTH;

use super::Context;
use crate::kernel::Result;
use crate::Kernel;
use crate::kernel::{RandomnessOps, Result};

/// Gets 32 bytes of randomness from the ticket chain.
/// The supplied output buffer must have at least 32 bytes of capacity.
/// If this syscall succeeds, exactly 32 bytes will be written starting at the
/// supplied offset.
pub fn get_chain_randomness(
context: Context<'_, impl Kernel>,
context: Context<'_, impl RandomnessOps>,
round: i64, // ChainEpoch
) -> Result<[u8; RANDOMNESS_LENGTH]> {
context.kernel.get_randomness_from_tickets(round)
Expand All @@ -22,7 +21,7 @@ pub fn get_chain_randomness(
/// If this syscall succeeds, exactly 32 bytes will be written starting at the
/// supplied offset.
pub fn get_beacon_randomness(
context: Context<'_, impl Kernel>,
context: Context<'_, impl RandomnessOps>,
round: i64, // ChainEpoch
) -> Result<[u8; RANDOMNESS_LENGTH]> {
context.kernel.get_randomness_from_beacon(round)
Expand Down
10 changes: 5 additions & 5 deletions fvm/src/syscalls/sself.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ use anyhow::Context as _;
use fvm_shared::sys;

use super::Context;
use crate::kernel::{ClassifyResult, Kernel, Result};
use crate::kernel::{ClassifyResult, Result, SelfOps};

/// Returns the root CID of the actor's state by writing it in the specified buffer.
///
/// The returned u32 represents the _actual_ length of the CID. If the supplied
/// buffer is smaller, no value will have been written. The caller must retry
/// with a larger buffer.
pub fn root(context: Context<'_, impl Kernel>, obuf_off: u32, obuf_len: u32) -> Result<u32> {
pub fn root(context: Context<'_, impl SelfOps>, obuf_off: u32, obuf_len: u32) -> Result<u32> {
context.memory.check_bounds(obuf_off, obuf_len)?;

let root = context.kernel.root()?;

context.memory.write_cid(&root, obuf_off, obuf_len)
}

pub fn set_root(context: Context<'_, impl Kernel>, cid_off: u32) -> Result<()> {
pub fn set_root(context: Context<'_, impl SelfOps>, cid_off: u32) -> Result<()> {
let cid = context.memory.read_cid(cid_off)?;
context.kernel.set_root(cid)?;
Ok(())
}

pub fn current_balance(context: Context<'_, impl Kernel>) -> Result<sys::TokenAmount> {
pub fn current_balance(context: Context<'_, impl SelfOps>) -> Result<sys::TokenAmount> {
let balance = context.kernel.current_balance()?;
balance
.try_into()
.context("balance exceeds u128")
.or_fatal()
}

pub fn self_destruct(context: Context<'_, impl Kernel>, burn_unspent: u32) -> Result<()> {
pub fn self_destruct(context: Context<'_, impl SelfOps>, burn_unspent: u32) -> Result<()> {
context.kernel.self_destruct(burn_unspent > 0)?;
Ok(())
}
8 changes: 5 additions & 3 deletions fvm/src/syscalls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use fvm_shared::sys::out::vm::MessageContext;

use super::error::Abort;
use super::Context;
use crate::kernel::Kernel;
use crate::kernel::MessageOps;

/// The maximum message length included in the backtrace. Given 1024 levels, this gives us a total
/// maximum of around 1MiB for debugging.
const MAX_MESSAGE_LEN: usize = 1024;

// NOTE: this won't clobber the last syscall error because it directly returns a "trap".
pub fn exit(
context: Context<'_, impl Kernel>,
context: Context<'_, impl Sized>, // "impl Sized" means "any struct"
code: u32,
blk: u32,
message_off: u32,
Expand Down Expand Up @@ -52,6 +52,8 @@ pub fn exit(
Abort::Exit(code, message, blk)
}

pub fn message_context(context: Context<'_, impl Kernel>) -> crate::kernel::Result<MessageContext> {
pub fn message_context(
context: Context<'_, impl MessageOps>,
) -> crate::kernel::Result<MessageContext> {
context.kernel.msg_context()
}

0 comments on commit f89bd0e

Please sign in to comment.