Skip to content

Commit

Permalink
Add max_vms_count
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Mar 8, 2024
1 parent 6be39c2 commit db050dd
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
21 changes: 16 additions & 5 deletions script/src/v2_scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::types::CoreMachine as ICoreMachine;
use crate::v2_syscalls::INDEX_OUT_OF_BOUND;
use crate::v2_syscalls::{INDEX_OUT_OF_BOUND, MAX_VMS_SPAWNED};
use crate::v2_types::PipeIoArgs;
use crate::verify::TransactionScriptsSyscallsGenerator;
use crate::ScriptVersion;
use crate::{
v2_syscalls::{
transferred_byte_cycles, MachineContext, INVALID_PIPE, OTHER_END_CLOSED, SUCCESS,
Expand All @@ -12,7 +12,6 @@ use crate::{
FIRST_PIPE_SLOT, FIRST_VM_ID,
},
};
use crate::{ScriptVersion, TransactionScriptsVerifier};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
use ckb_types::core::Cycle;
use ckb_vm::snapshot2::Snapshot2Context;
Expand All @@ -27,7 +26,7 @@ use ckb_vm::{
memory::Memory,
registers::A0,
snapshot2::{DataSource, Snapshot2},
Error, Register, Syscalls,
Error, Register,
};
use std::sync::{Arc, Mutex};
use std::{
Expand All @@ -36,6 +35,7 @@ use std::{
};

const ROOT_VM_ID: VmId = FIRST_VM_ID;
const MAX_VMS_COUNT: u64 = 16;
const MAX_INSTANTIATED_VMS: usize = 4;

/// A single Scheduler instance is used to verify a single script
Expand All @@ -56,6 +56,7 @@ where
script_version: ScriptVersion,
syscalls_generator: TransactionScriptsSyscallsGenerator<DL>,

max_vms_count: u64,
total_cycles: Cycle,
next_vm_id: VmId,
next_pipe_slot: u64,
Expand Down Expand Up @@ -86,6 +87,7 @@ where
tx_data,
script_version,
syscalls_generator,
max_vms_count: MAX_VMS_COUNT,
total_cycles: 0,
next_vm_id: FIRST_VM_ID,
next_pipe_slot: FIRST_PIPE_SLOT,
Expand Down Expand Up @@ -115,6 +117,7 @@ where
tx_data,
script_version,
syscalls_generator,
max_vms_count: full.max_vm_count,
total_cycles: full.total_cycles,
next_vm_id: full.next_vm_id,
next_pipe_slot: full.next_pipe_slot,
Expand All @@ -138,6 +141,7 @@ where

/// Suspend current scheduler into a serializable full state
pub fn suspend(mut self) -> Result<FullSuspendedState, Error> {
assert!(self.message_box.lock().expect("lock").is_empty());
let mut vms = Vec::with_capacity(self.states.len());
let instantiated_ids: Vec<_> = self.instantiated.keys().cloned().collect();
for id in instantiated_ids {
Expand All @@ -148,6 +152,7 @@ where
vms.push((id, state, snapshot));
}
Ok(FullSuspendedState {
max_vm_count: self.max_vms_count,
total_cycles: self.total_cycles,
next_vm_id: self.next_vm_id,
next_pipe_slot: self.next_pipe_slot,
Expand Down Expand Up @@ -316,7 +321,13 @@ where
if !pipes_valid {
continue;
}
// TODO: spawn limits
if self.suspended.len() + self.instantiated.len() > self.max_vms_count as usize
{
self.ensure_vms_instantiated(&[vm_id])?;
let (_, machine) = self.instantiated.get_mut(&vm_id).unwrap();
machine.machine.set_register(A0, MAX_VMS_SPAWNED as u64);
continue;
}
let spawned_vm_id =
self.boot_vm(&args.data_piece_id, args.offset, args.length, &args.argv)?;
// Move passed pipes from spawner to spawnee
Expand Down
1 change: 1 addition & 0 deletions script/src/v2_syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,4 @@ pub(crate) const SLICE_OUT_OF_BOUND: u8 = 3;
pub(crate) const WAIT_FAILURE: u8 = 5;
pub(crate) const INVALID_PIPE: u8 = 6;
pub(crate) const OTHER_END_CLOSED: u8 = 7;
pub(crate) const MAX_VMS_SPAWNED: u8 = 8;
1 change: 1 addition & 0 deletions script/src/v2_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl TryFrom<(u64, u64, u64)> for DataPieceId {
/// fully recover the running environment with the full transaction environment.
#[derive(Clone, Debug)]
pub struct FullSuspendedState {
pub max_vm_count: u64,
pub total_cycles: Cycle,
pub next_vm_id: VmId,
pub next_pipe_slot: u64,
Expand Down
8 changes: 7 additions & 1 deletion script/src/verify/tests/ckb_latest/features_since_v2023.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ fn check_spawn_read_then_close() {
assert_eq!(result.is_ok(), SCRIPT_VERSION == ScriptVersion::V2);
}

#[test]
fn check_spawn_max_vms_count() {
let result = simple_spawn_test("testdata/spawn_cases", &[10]);
assert_eq!(result.is_ok(), SCRIPT_VERSION == ScriptVersion::V2);
}

#[test]
fn check_vm_version() {
let script_version = SCRIPT_VERSION;
Expand Down Expand Up @@ -348,7 +354,7 @@ fn check_spawn_recursive() {
let result = verifier.verify(script_version, &rtx, 70_000_000);
if script_version >= ScriptVersion::V2 {
let msg = result.unwrap_err().to_string();
assert!(msg.contains("ExceededMaximumCycles"))
assert!(msg.contains("error code 8"))
} else {
assert!(result.is_err())
}
Expand Down
Binary file modified script/testdata/spawn_cases
Binary file not shown.
19 changes: 19 additions & 0 deletions script/testdata/spawn_cases.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ int child_read_then_close() {
return err;
}

int parent_max_vms_count() {
const char* argv[2] = {"", 0};
return simple_spawn_args(0, 1, argv);
}

int child_max_vms_count() {
const char* argv[2] = {"", 0};
int err = simple_spawn_args(0, 1, argv);
CHECK2(err == 0 || err == CKB_MAX_VMS_SPAWNED, -2);
err = 0;
exit:
return err;
}

int parent_entry(int case_id) {
int err = 0;
uint64_t pid = 0;
Expand All @@ -341,6 +355,9 @@ int parent_entry(int case_id) {
err = parent_inherited_fds_without_owner(&pid);
} else if (case_id == 9) {
err = parent_read_then_close(&pid);
} else if (case_id == 10) {
err = parent_max_vms_count(&pid);
return err;
} else {
CHECK2(false, -2);
}
Expand Down Expand Up @@ -373,6 +390,8 @@ int child_entry(int case_id) {
return 0;
} else if (case_id == 9) {
return child_read_then_close();
} else if (case_id == 10) {
return child_max_vms_count();
} else {
return -1;
}
Expand Down

0 comments on commit db050dd

Please sign in to comment.