|
1 | 1 | use crate::cost_model::transferred_byte_cycles;
|
2 | 2 | use crate::syscalls::{
|
3 | 3 | EXEC_LOAD_ELF_V2_CYCLES_BASE, INVALID_FD, MAX_FDS_CREATED, MAX_VMS_SPAWNED, OTHER_END_CLOSED,
|
4 |
| - SPAWN_EXTRA_CYCLES_BASE, SUCCESS, WAIT_FAILURE, generator::generate_ckb_syscalls, |
| 4 | + SPAWN_EXTRA_CYCLES_BASE, SUCCESS, WAIT_FAILURE, |
5 | 5 | };
|
6 | 6 |
|
7 | 7 | use crate::types::{
|
8 |
| - CoreMachineType, DataLocation, DataPieceId, DebugContext, FIRST_FD_SLOT, FIRST_VM_ID, Fd, |
9 |
| - FdArgs, FullSuspendedState, Machine, Message, ReadState, RunMode, SgData, VmArgs, VmContext, |
10 |
| - VmId, VmState, WriteState, |
| 8 | + CoreMachineType, DataLocation, DataPieceId, FIRST_FD_SLOT, FIRST_VM_ID, Fd, FdArgs, |
| 9 | + FullSuspendedState, Machine, Message, ReadState, RunMode, SgData, SyscallGenerator, VmArgs, |
| 10 | + VmContext, VmId, VmState, WriteState, |
11 | 11 | };
|
12 | 12 | use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
|
13 | 13 | use ckb_types::core::Cycle;
|
@@ -43,15 +43,17 @@ pub const MAX_FDS: u64 = 64;
|
43 | 43 | /// A scheduler holds & manipulates a core, the scheduler also holds
|
44 | 44 | /// all CKB-VM machines, each CKB-VM machine also gets a mutable reference
|
45 | 45 | /// of the core for IO operations.
|
46 |
| -pub struct Scheduler<DL> |
| 46 | +pub struct Scheduler<DL, V> |
47 | 47 | where
|
48 | 48 | DL: CellDataProvider,
|
49 | 49 | {
|
50 | 50 | /// Immutable context data for current running transaction & script.
|
51 | 51 | pub sg_data: SgData<DL>,
|
52 | 52 |
|
53 |
| - /// Mutable context data used by current scheduler |
54 |
| - pub debug_context: DebugContext, |
| 53 | + /// Syscall generator |
| 54 | + pub syscall_generator: SyscallGenerator<DL, V>, |
| 55 | + /// Syscall generator context |
| 56 | + pub syscall_context: V, |
55 | 57 |
|
56 | 58 | /// Total cycles. When a scheduler executes, there are 3 variables
|
57 | 59 | /// that might all contain charged cycles: +total_cycles+,
|
@@ -106,15 +108,21 @@ where
|
106 | 108 | pub message_box: Arc<Mutex<Vec<Message>>>,
|
107 | 109 | }
|
108 | 110 |
|
109 |
| -impl<DL> Scheduler<DL> |
| 111 | +impl<DL, V> Scheduler<DL, V> |
110 | 112 | where
|
111 | 113 | DL: CellDataProvider + HeaderProvider + ExtensionProvider + Send + Sync + Clone + 'static,
|
| 114 | + V: Send + Clone, |
112 | 115 | {
|
113 | 116 | /// Create a new scheduler from empty state
|
114 |
| - pub fn new(sg_data: SgData<DL>, debug_context: DebugContext) -> Self { |
| 117 | + pub fn new( |
| 118 | + sg_data: SgData<DL>, |
| 119 | + syscall_generator: SyscallGenerator<DL, V>, |
| 120 | + syscall_context: V, |
| 121 | + ) -> Self { |
115 | 122 | Self {
|
116 | 123 | sg_data,
|
117 |
| - debug_context, |
| 124 | + syscall_generator, |
| 125 | + syscall_context, |
118 | 126 | total_cycles: Arc::new(AtomicU64::new(0)),
|
119 | 127 | iteration_cycles: 0,
|
120 | 128 | next_vm_id: FIRST_VM_ID,
|
@@ -149,12 +157,14 @@ where
|
149 | 157 | /// Resume a previously suspended scheduler state
|
150 | 158 | pub fn resume(
|
151 | 159 | sg_data: SgData<DL>,
|
152 |
| - debug_context: DebugContext, |
| 160 | + syscall_generator: SyscallGenerator<DL, V>, |
| 161 | + syscall_context: V, |
153 | 162 | full: FullSuspendedState,
|
154 | 163 | ) -> Self {
|
155 | 164 | let mut scheduler = Self {
|
156 | 165 | sg_data,
|
157 |
| - debug_context, |
| 166 | + syscall_generator, |
| 167 | + syscall_context, |
158 | 168 | total_cycles: Arc::new(AtomicU64::new(full.total_cycles)),
|
159 | 169 | iteration_cycles: 0,
|
160 | 170 | next_vm_id: full.next_vm_id,
|
@@ -898,7 +908,7 @@ where
|
898 | 908 | let machine_builder = DefaultMachineBuilder::new(core_machine)
|
899 | 909 | .instruction_cycle_func(Box::new(estimate_cycles));
|
900 | 910 | let machine_builder =
|
901 |
| - generate_ckb_syscalls(id, &self.sg_data, &vm_context, &self.debug_context) |
| 911 | + (self.syscall_generator)(id, &self.sg_data, &vm_context, &self.syscall_context) |
902 | 912 | .into_iter()
|
903 | 913 | .fold(machine_builder, |builder, syscall| builder.syscall(syscall));
|
904 | 914 | let default_machine = machine_builder.build();
|
|
0 commit comments