Skip to content

Commit

Permalink
Remove VmData structure per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Feb 13, 2025
1 parent c731f33 commit 19c0b3c
Show file tree
Hide file tree
Showing 26 changed files with 248 additions and 287 deletions.
17 changes: 8 additions & 9 deletions script/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::syscalls::{

use crate::types::{
CoreMachineType, DataLocation, DataPieceId, DebugContext, Fd, FdArgs, FullSuspendedState,
Machine, Message, ReadState, RunMode, SgData, VmContext, VmData, VmId, VmState, WriteState,
Machine, Message, ReadState, RunMode, SgData, VmContext, VmId, VmState, WriteState,
FIRST_FD_SLOT, FIRST_VM_ID,
};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
Expand Down Expand Up @@ -884,21 +884,20 @@ where
// We will update max_cycles for each machine when it gets a chance to run
u64::MAX,
);
let vm_data = Arc::new(VmData {
sg_data: Arc::clone(&self.sg_data),
vm_id: *id,
});
let vm_context = VmContext {
base_cycles: Arc::clone(&self.total_cycles),
message_box: Arc::clone(&self.message_box),
snapshot2_context: Arc::new(Mutex::new(Snapshot2Context::new(Arc::clone(&vm_data)))),
snapshot2_context: Arc::new(Mutex::new(Snapshot2Context::new(Arc::clone(
&self.sg_data,
)))),
};

let machine_builder = DefaultMachineBuilder::new(core_machine)
.instruction_cycle_func(Box::new(estimate_cycles));
let machine_builder = generate_ckb_syscalls(&vm_data, &vm_context, &self.debug_context)
.into_iter()
.fold(machine_builder, |builder, syscall| builder.syscall(syscall));
let machine_builder =
generate_ckb_syscalls(id, &self.sg_data, &vm_context, &self.debug_context)
.into_iter()
.fold(machine_builder, |builder, syscall| builder.syscall(syscall));
let default_machine = machine_builder.build();
Ok((vm_context, Machine::new(default_machine)))
}
Expand Down
6 changes: 3 additions & 3 deletions script/src/syscalls/close.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::syscalls::{CLOSE, SPAWN_YIELD_CYCLES_BASE};
use crate::types::{Fd, Message, VmContext, VmData, VmId};
use crate::types::{Fd, Message, VmContext, VmId};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
use ckb_vm::{
registers::{A0, A7},
Expand All @@ -14,12 +14,12 @@ pub struct Close {
}

impl Close {
pub fn new<DL>(vm_data: &Arc<VmData<DL>>, vm_context: &VmContext<DL>) -> Self
pub fn new<DL>(vm_id: &VmId, vm_context: &VmContext<DL>) -> Self
where
DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + Clone + 'static,
{
Self {
id: vm_data.vm_id,
id: *vm_id,
message_box: Arc::clone(&vm_context.message_box),
}
}
Expand Down
10 changes: 5 additions & 5 deletions script/src/syscalls/debugger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{DebugContext, DebugPrinter, VmData};
use crate::types::{DebugContext, DebugPrinter, SgData};
use crate::{cost_model::transferred_byte_cycles, syscalls::DEBUG_PRINT_SYSCALL_NUMBER};
use ckb_vm::{
registers::{A0, A7},
Expand All @@ -7,14 +7,14 @@ use ckb_vm::{
use std::sync::Arc;

pub struct Debugger<DL> {
vm_data: Arc<VmData<DL>>,
sg_data: Arc<SgData<DL>>,
printer: DebugPrinter,
}

impl<DL> Debugger<DL> {
pub fn new(vm_data: &Arc<VmData<DL>>, debug_context: &DebugContext) -> Debugger<DL> {
pub fn new(sg_data: &Arc<SgData<DL>>, debug_context: &DebugContext) -> Debugger<DL> {
Debugger {
vm_data: Arc::clone(vm_data),
sg_data: Arc::clone(sg_data),
printer: Arc::clone(&debug_context.debug_printer),
}
}
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<Mac: SupportMachine, DL: Send + Sync> Syscalls<Mac> for Debugger<DL> {
machine.add_cycles_no_checking(transferred_byte_cycles(buffer.len() as u64))?;
let s = String::from_utf8(buffer)
.map_err(|e| VMError::External(format!("String from buffer {e:?}")))?;
(self.printer)(self.vm_data.current_script_hash(), s.as_str());
(self.printer)(self.sg_data.current_script_hash(), s.as_str());

Ok(true)
}
Expand Down
28 changes: 14 additions & 14 deletions script/src/syscalls/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::syscalls::{
Place, Source, SourceEntry, EXEC, INDEX_OUT_OF_BOUND, MAX_ARGV_LENGTH, SLICE_OUT_OF_BOUND,
WRONG_FORMAT,
};
use crate::types::VmData;
use crate::types::SgData;
use ckb_traits::CellDataProvider;
use ckb_types::core::cell::CellMeta;
use ckb_types::core::error::ARGV_TOO_LONG_TEXT;
Expand All @@ -19,46 +19,46 @@ use std::sync::Arc;

#[derive(Debug)]
pub struct Exec<DL> {
vm_data: Arc<VmData<DL>>,
sg_data: Arc<SgData<DL>>,
}

impl<DL: CellDataProvider> Exec<DL> {
pub fn new(vm_data: &Arc<VmData<DL>>) -> Exec<DL> {
pub fn new(sg_data: &Arc<SgData<DL>>) -> Exec<DL> {
Exec {
vm_data: Arc::clone(vm_data),
sg_data: Arc::clone(sg_data),
}
}

#[inline]
fn resolved_inputs(&self) -> &Vec<CellMeta> {
&self.vm_data.rtx().resolved_inputs
&self.sg_data.rtx().resolved_inputs
}

#[inline]
fn resolved_cell_deps(&self) -> &Vec<CellMeta> {
&self.vm_data.rtx().resolved_cell_deps
&self.sg_data.rtx().resolved_cell_deps
}

#[inline]
fn witnesses(&self) -> BytesVec {
self.vm_data.rtx().transaction.witnesses()
self.sg_data.rtx().transaction.witnesses()
}

fn fetch_cell(&self, source: Source, index: usize) -> Result<&CellMeta, u8> {
let cell_opt = match source {
Source::Transaction(SourceEntry::Input) => self.resolved_inputs().get(index),
Source::Transaction(SourceEntry::Output) => self.vm_data.outputs().get(index),
Source::Transaction(SourceEntry::Output) => self.sg_data.outputs().get(index),
Source::Transaction(SourceEntry::CellDep) => self.resolved_cell_deps().get(index),
Source::Group(SourceEntry::Input) => self
.vm_data
.sg_data
.group_inputs()
.get(index)
.and_then(|actual_index| self.resolved_inputs().get(*actual_index)),
Source::Group(SourceEntry::Output) => self
.vm_data
.sg_data
.group_outputs()
.get(index)
.and_then(|actual_index| self.vm_data.outputs().get(*actual_index)),
.and_then(|actual_index| self.sg_data.outputs().get(*actual_index)),
Source::Transaction(SourceEntry::HeaderDep)
| Source::Group(SourceEntry::CellDep)
| Source::Group(SourceEntry::HeaderDep) => {
Expand All @@ -72,12 +72,12 @@ impl<DL: CellDataProvider> Exec<DL> {
fn fetch_witness(&self, source: Source, index: usize) -> Result<PackedBytes, u8> {
let witness_opt = match source {
Source::Group(SourceEntry::Input) => self
.vm_data
.sg_data
.group_inputs()
.get(index)
.and_then(|actual_index| self.witnesses().get(*actual_index)),
Source::Group(SourceEntry::Output) => self
.vm_data
.sg_data
.group_outputs()
.get(index)
.and_then(|actual_index| self.witnesses().get(*actual_index)),
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<Mac: SupportMachine, DL: CellDataProvider + Send + Sync> Syscalls<Mac> for
return Ok(true);
}
let cell = cell.unwrap();
self.vm_data
self.sg_data
.data_loader()
.load_cell_data(cell)
.ok_or_else(|| {
Expand Down
9 changes: 3 additions & 6 deletions script/src/syscalls/exec_v2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::syscalls::{EXEC, INDEX_OUT_OF_BOUND};
use crate::types::{DataLocation, DataPieceId, ExecV2Args, Message, VmContext, VmData, VmId};
use crate::types::{DataLocation, DataPieceId, ExecV2Args, Message, VmContext, VmId};
use ckb_traits::CellDataProvider;
use ckb_vm::{
registers::{A0, A1, A2, A3, A4, A5, A7},
Expand All @@ -13,12 +13,9 @@ pub struct ExecV2 {
}

impl ExecV2 {
pub fn new<DL: CellDataProvider>(
vm_data: &Arc<VmData<DL>>,
vm_context: &VmContext<DL>,
) -> ExecV2 {
pub fn new<DL: CellDataProvider>(vm_id: &VmId, vm_context: &VmContext<DL>) -> ExecV2 {
ExecV2 {
id: vm_data.vm_id,
id: *vm_id,
message_box: Arc::clone(&vm_context.message_box),
}
}
Expand Down
45 changes: 23 additions & 22 deletions script/src/syscalls/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,55 @@ use crate::{
LoadCellData, LoadHeader, LoadInput, LoadScript, LoadScriptHash, LoadTx, LoadWitness, Pipe,
ProcessID, Read, Spawn, VMVersion, Wait, Write,
},
types::{CoreMachine, DebugContext, ScriptVersion, VmContext, VmData},
types::{CoreMachine, DebugContext, ScriptVersion, SgData, VmContext, VmId},
};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
use ckb_vm::Syscalls;
use std::sync::Arc;

/// Generate RISC-V syscalls in CKB environment
pub fn generate_ckb_syscalls<DL>(
vm_data: &Arc<VmData<DL>>,
vm_id: &VmId,
sg_data: &Arc<SgData<DL>>,
vm_context: &VmContext<DL>,
debug_context: &DebugContext,
) -> Vec<Box<(dyn Syscalls<CoreMachine>)>>
where
DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + Clone + 'static,
{
let mut syscalls: Vec<Box<(dyn Syscalls<CoreMachine>)>> = vec![
Box::new(LoadScriptHash::new(vm_data)),
Box::new(LoadTx::new(vm_data)),
Box::new(LoadCell::new(vm_data)),
Box::new(LoadInput::new(vm_data)),
Box::new(LoadHeader::new(vm_data)),
Box::new(LoadWitness::new(vm_data)),
Box::new(LoadScript::new(vm_data)),
Box::new(LoadScriptHash::new(sg_data)),
Box::new(LoadTx::new(sg_data)),
Box::new(LoadCell::new(sg_data)),
Box::new(LoadInput::new(sg_data)),
Box::new(LoadHeader::new(sg_data)),
Box::new(LoadWitness::new(sg_data)),
Box::new(LoadScript::new(sg_data)),
Box::new(LoadCellData::new(vm_context)),
Box::new(Debugger::new(vm_data, debug_context)),
Box::new(Debugger::new(sg_data, debug_context)),
];
let script_version = &vm_data.sg_data.script_version;
let script_version = &sg_data.script_version;
if script_version >= &ScriptVersion::V1 {
syscalls.append(&mut vec![
Box::new(VMVersion::new()),
Box::new(CurrentCycles::new(vm_context)),
]);
}
if script_version == &ScriptVersion::V1 {
syscalls.push(Box::new(Exec::new(vm_data)));
syscalls.push(Box::new(Exec::new(sg_data)));
}
if script_version >= &ScriptVersion::V2 {
syscalls.append(&mut vec![
Box::new(ExecV2::new(vm_data, vm_context)),
Box::new(LoadBlockExtension::new(vm_data)),
Box::new(Spawn::new(vm_data, vm_context)),
Box::new(ProcessID::new(vm_data)),
Box::new(Pipe::new(vm_data, vm_context)),
Box::new(Wait::new(vm_data, vm_context)),
Box::new(Write::new(vm_data, vm_context)),
Box::new(Read::new(vm_data, vm_context)),
Box::new(InheritedFd::new(vm_data, vm_context)),
Box::new(Close::new(vm_data, vm_context)),
Box::new(ExecV2::new(vm_id, vm_context)),
Box::new(LoadBlockExtension::new(sg_data)),
Box::new(Spawn::new(vm_id, vm_context)),
Box::new(ProcessID::new(vm_id)),
Box::new(Pipe::new(vm_id, vm_context)),
Box::new(Wait::new(vm_id, vm_context)),
Box::new(Write::new(vm_id, vm_context)),
Box::new(Read::new(vm_id, vm_context)),
Box::new(InheritedFd::new(vm_id, vm_context)),
Box::new(Close::new(vm_id, vm_context)),
]);
}
#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions script/src/syscalls/inherited_fd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::syscalls::{INHERITED_FD, SPAWN_YIELD_CYCLES_BASE};
use crate::types::{Fd, FdArgs, Message, VmContext, VmData, VmId};
use crate::types::{Fd, FdArgs, Message, VmContext, VmId};
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
use ckb_vm::{
registers::{A0, A1, A7},
Expand All @@ -14,12 +14,12 @@ pub struct InheritedFd {
}

impl InheritedFd {
pub fn new<DL>(vm_data: &Arc<VmData<DL>>, vm_context: &VmContext<DL>) -> Self
pub fn new<DL>(vm_id: &VmId, vm_context: &VmContext<DL>) -> Self
where
DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + Clone + 'static,
{
Self {
id: vm_data.vm_id,
id: *vm_id,
message_box: Arc::clone(&vm_context.message_box),
}
}
Expand Down
20 changes: 10 additions & 10 deletions script/src/syscalls/load_block_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
utils::store_data, Source, SourceEntry, INDEX_OUT_OF_BOUND, ITEM_MISSING,
LOAD_BLOCK_EXTENSION, SUCCESS,
},
types::VmData,
types::SgData,
};
use ckb_traits::ExtensionProvider;
use ckb_types::{
Expand All @@ -19,29 +19,29 @@ use std::sync::Arc;

#[derive(Debug)]
pub struct LoadBlockExtension<DL> {
vm_data: Arc<VmData<DL>>,
sg_data: Arc<SgData<DL>>,
}

impl<DL: ExtensionProvider> LoadBlockExtension<DL> {
pub fn new(vm_data: &Arc<VmData<DL>>) -> LoadBlockExtension<DL> {
pub fn new(sg_data: &Arc<SgData<DL>>) -> LoadBlockExtension<DL> {
LoadBlockExtension {
vm_data: Arc::clone(vm_data),
sg_data: Arc::clone(sg_data),
}
}

#[inline]
fn header_deps(&self) -> Byte32Vec {
self.vm_data.rtx().transaction.header_deps()
self.sg_data.rtx().transaction.header_deps()
}

#[inline]
fn resolved_inputs(&self) -> &Vec<CellMeta> {
&self.vm_data.rtx().resolved_inputs
&self.sg_data.rtx().resolved_inputs
}

#[inline]
fn resolved_cell_deps(&self) -> &Vec<CellMeta> {
&self.vm_data.rtx().resolved_cell_deps
&self.sg_data.rtx().resolved_cell_deps
}

fn load_block_extension(&self, cell_meta: &CellMeta) -> Option<packed::Bytes> {
Expand All @@ -55,7 +55,7 @@ impl<DL: ExtensionProvider> LoadBlockExtension<DL> {
.into_iter()
.any(|hash| &hash == block_hash)
{
self.vm_data.data_loader().get_block_extension(block_hash)
self.sg_data.data_loader().get_block_extension(block_hash)
} else {
None
}
Expand All @@ -79,13 +79,13 @@ impl<DL: ExtensionProvider> LoadBlockExtension<DL> {
.get(index)
.ok_or(INDEX_OUT_OF_BOUND)
.and_then(|block_hash| {
self.vm_data
self.sg_data
.data_loader()
.get_block_extension(&block_hash)
.ok_or(ITEM_MISSING)
}),
Source::Group(SourceEntry::Input) => self
.vm_data
.sg_data
.group_inputs()
.get(index)
.ok_or(INDEX_OUT_OF_BOUND)
Expand Down
Loading

0 comments on commit 19c0b3c

Please sign in to comment.