Skip to content

Commit

Permalink
pass the current depth as an argument to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jun 28, 2024
1 parent 31e827e commit 50ab8d2
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 173 deletions.
24 changes: 19 additions & 5 deletions radix-engine-monkey-tests/tests/fuzz_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl KernelCallbackObject for TestCallbackObject {
Ok(StableReferenceType::Global)
}

fn on_pin_node(&mut self, _node_id: &NodeId) -> Result<(), RuntimeError> {
fn on_pin_node(&mut self, _depth: usize, _node_id: &NodeId) -> Result<(), RuntimeError> {
Ok(())
}

Expand Down Expand Up @@ -168,24 +168,37 @@ impl KernelCallbackObject for TestCallbackObject {
Ok(())
}

fn on_set_substate(&mut self, _event: SetSubstateEvent) -> Result<(), RuntimeError> {
fn on_set_substate(
&mut self,
_depth: usize,
_event: SetSubstateEvent,
) -> Result<(), RuntimeError> {
Ok(())
}

fn on_remove_substate(&mut self, _event: RemoveSubstateEvent) -> Result<(), RuntimeError> {
fn on_remove_substate(
&mut self,
_depth: usize,
_event: RemoveSubstateEvent,
) -> Result<(), RuntimeError> {
Ok(())
}

fn on_scan_keys(&mut self, _event: ScanKeysEvent) -> Result<(), RuntimeError> {
fn on_scan_keys(&mut self, _depth: usize, _event: ScanKeysEvent) -> Result<(), RuntimeError> {
Ok(())
}

fn on_drain_substates(&mut self, _event: DrainSubstatesEvent) -> Result<(), RuntimeError> {
fn on_drain_substates(
&mut self,
_depth: usize,
_event: DrainSubstatesEvent,
) -> Result<(), RuntimeError> {
Ok(())
}

fn on_scan_sorted_substates(
&mut self,
_depth: usize,
_event: ScanSortedSubstatesEvent,
) -> Result<(), RuntimeError> {
Ok(())
Expand Down Expand Up @@ -248,6 +261,7 @@ impl KernelCallbackObject for TestCallbackObject {

fn on_mark_substate_as_transient(
&mut self,
_depth: usize,
_node_id: &NodeId,
_partition_number: &PartitionNumber,
_substate_key: &SubstateKey,
Expand Down
63 changes: 50 additions & 13 deletions radix-engine-tests/tests/kernel/kernel.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use radix_common::prelude::*;
use radix_engine::errors::{BootloadingError, CallFrameError, KernelError, RejectionReason, RuntimeError, TransactionExecutionError};
use radix_engine::kernel::call_frame::{CallFrameMessage, CloseSubstateError, CreateFrameError, CreateNodeError, MovePartitionError, PassMessageError, ProcessSubstateError, StableReferenceType, TakeNodeError, WriteSubstateError};
use radix_engine::errors::{
BootloadingError, CallFrameError, KernelError, RejectionReason, RuntimeError,
TransactionExecutionError,
};
use radix_engine::kernel::call_frame::{
CallFrameMessage, CloseSubstateError, CreateFrameError, CreateNodeError, MovePartitionError,
PassMessageError, ProcessSubstateError, StableReferenceType, TakeNodeError, WriteSubstateError,
};
use radix_engine::kernel::id_allocator::IdAllocator;
use radix_engine::kernel::kernel::Kernel;
use radix_engine::kernel::kernel_api::{
KernelApi, KernelInternalApi, KernelInvocation, KernelInvokeApi, KernelNodeApi,
KernelSubstateApi,
};
use radix_engine::kernel::kernel_callback_api::{CallFrameReferences, CloseSubstateEvent, CreateNodeEvent, DrainSubstatesEvent, DropNodeEvent, ExecutionReceipt, KernelCallbackObject, MoveModuleEvent, OpenSubstateEvent, ReadSubstateEvent, RemoveSubstateEvent, ScanKeysEvent, ScanSortedSubstatesEvent, SetSubstateEvent, WriteSubstateEvent};
use radix_engine::kernel::kernel_callback_api::{
CallFrameReferences, CloseSubstateEvent, CreateNodeEvent, DrainSubstatesEvent, DropNodeEvent,
ExecutionReceipt, KernelCallbackObject, MoveModuleEvent, OpenSubstateEvent, ReadSubstateEvent,
RemoveSubstateEvent, ScanKeysEvent, ScanSortedSubstatesEvent, SetSubstateEvent,
WriteSubstateEvent,
};
use radix_engine::track::{BootStore, CommitableSubstateStore, StoreCommitInfo, Track};
use radix_engine::transaction::ResourcesUsage;
use radix_engine_interface::prelude::*;
Expand Down Expand Up @@ -47,8 +58,7 @@ impl ExecutionReceipt for TestReceipt {
Self
}

fn set_resource_usage(&mut self, _resources_usage: ResourcesUsage) {
}
fn set_resource_usage(&mut self, _resources_usage: ResourcesUsage) {}
}

struct TestCallbackObject;
Expand All @@ -59,11 +69,19 @@ impl KernelCallbackObject for TestCallbackObject {
type ExecutionOutput = ();
type Receipt = TestReceipt;

fn init<S: BootStore + CommitableSubstateStore>(_store: &mut S, _executable: &Executable, _init_input: Self::Init) -> Result<Self, RejectionReason> {
fn init<S: BootStore + CommitableSubstateStore>(
_store: &mut S,
_executable: &Executable,
_init_input: Self::Init,
) -> Result<Self, RejectionReason> {
Ok(Self)
}

fn verify_boot_ref_value(&mut self, _node_id: &NodeId, _value: &IndexedScryptoValue) -> Result<StableReferenceType, BootloadingError> {
fn verify_boot_ref_value(
&mut self,
_node_id: &NodeId,
_value: &IndexedScryptoValue,
) -> Result<StableReferenceType, BootloadingError> {
Ok(StableReferenceType::Global)
}

Expand All @@ -84,11 +102,16 @@ impl KernelCallbackObject for TestCallbackObject {
Ok(())
}

fn create_receipt<S: SubstateDatabase>(self, _track: Track<S, SpreadPrefixKeyMapper>, _executable: &Executable, _result: Result<(), TransactionExecutionError>) -> TestReceipt {
fn create_receipt<S: SubstateDatabase>(
self,
_track: Track<S, SpreadPrefixKeyMapper>,
_executable: &Executable,
_result: Result<(), TransactionExecutionError>,
) -> TestReceipt {
TestReceipt
}

fn on_pin_node(&mut self, _node_id: &NodeId) -> Result<(), RuntimeError> {
fn on_pin_node(&mut self, _depth: usize, _node_id: &NodeId) -> Result<(), RuntimeError> {
Ok(())
}

Expand Down Expand Up @@ -141,24 +164,37 @@ impl KernelCallbackObject for TestCallbackObject {
Ok(())
}

fn on_set_substate(&mut self, _event: SetSubstateEvent) -> Result<(), RuntimeError> {
fn on_set_substate(
&mut self,
_depth: usize,
_event: SetSubstateEvent,
) -> Result<(), RuntimeError> {
Ok(())
}

fn on_remove_substate(&mut self, _event: RemoveSubstateEvent) -> Result<(), RuntimeError> {
fn on_remove_substate(
&mut self,
_depth: usize,
_event: RemoveSubstateEvent,
) -> Result<(), RuntimeError> {
Ok(())
}

fn on_scan_keys(&mut self, _event: ScanKeysEvent) -> Result<(), RuntimeError> {
fn on_scan_keys(&mut self, _depth: usize, _event: ScanKeysEvent) -> Result<(), RuntimeError> {
Ok(())
}

fn on_drain_substates(&mut self, _event: DrainSubstatesEvent) -> Result<(), RuntimeError> {
fn on_drain_substates(
&mut self,
_depth: usize,
_event: DrainSubstatesEvent,
) -> Result<(), RuntimeError> {
Ok(())
}

fn on_scan_sorted_substates(
&mut self,
_depth: usize,
_event: ScanSortedSubstatesEvent,
) -> Result<(), RuntimeError> {
Ok(())
Expand Down Expand Up @@ -221,6 +257,7 @@ impl KernelCallbackObject for TestCallbackObject {

fn on_mark_substate_as_transient(
&mut self,
_depth: usize,
_node_id: &NodeId,
_partition_number: &PartitionNumber,
_substate_key: &SubstateKey,
Expand Down
1 change: 0 additions & 1 deletion radix-engine-tests/tests/kernel/kernel_open_substate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pub fn test_open_substate_of_invisible_package_address() {
AuthModule::new(executable.auth_zone_params().clone()),
LimitsModule::babylon_genesis(),
CostingModule {
current_depth: 0,
fee_reserve: SystemLoanFeeReserve::default(),
fee_table: FeeTable::new(),
tx_payload_len: executable.payload_size(),
Expand Down
2 changes: 0 additions & 2 deletions radix-engine-tests/tests/vm/native_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ fn panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
}),
LimitsModule::babylon_genesis(),
CostingModule {
current_depth: 0,
fee_reserve: SystemLoanFeeReserve::default(),
fee_table: FeeTable::new(),
tx_payload_len: 0,
Expand Down Expand Up @@ -172,7 +171,6 @@ fn any_panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
}),
LimitsModule::babylon_genesis(),
CostingModule {
current_depth: 0,
fee_reserve: SystemLoanFeeReserve::default(),
fee_table: FeeTable::new(),
tx_payload_len: 0,
Expand Down
47 changes: 25 additions & 22 deletions radix-engine/src/kernel/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ where
{
#[trace_resources]
fn kernel_pin_node(&mut self, node_id: NodeId) -> Result<(), RuntimeError> {
self.callback.on_pin_node(&node_id)?;
let depth = self.kernel_get_current_depth();
self.callback.on_pin_node(depth, &node_id)?;

self.current_frame
.pin_node(&mut self.substate_io, node_id)
Expand Down Expand Up @@ -754,8 +755,9 @@ where
partition_num: PartitionNumber,
key: SubstateKey,
) -> Result<(), RuntimeError> {
let depth = self.kernel_get_current_depth();
self.callback
.on_mark_substate_as_transient(&node_id, &partition_num, &key)?;
.on_mark_substate_as_transient(depth, &node_id, &partition_num, &key)?;

self.current_frame
.mark_substate_as_transient(&mut self.substate_io, node_id, partition_num, key)
Expand Down Expand Up @@ -975,19 +977,18 @@ where
substate_key: SubstateKey,
value: IndexedScryptoValue,
) -> Result<(), RuntimeError> {
self.callback.on_set_substate(SetSubstateEvent::Start(
node_id,
&partition_num,
&substate_key,
&value,
))?;
let depth = self.kernel_get_current_depth();
self.callback.on_set_substate(
depth,
SetSubstateEvent::Start(node_id, &partition_num, &substate_key, &value),
)?;

let mut handler = KernelHandler {
callback: self.callback,
prev_frame: self.prev_frame_stack.last(),
on_io_access: |api, io_access| {
api.callback
.on_set_substate(SetSubstateEvent::IOAccess(&io_access))
.on_set_substate(depth, SetSubstateEvent::IOAccess(&io_access))
},
};

Expand Down Expand Up @@ -1017,19 +1018,18 @@ where
partition_num: PartitionNumber,
substate_key: &SubstateKey,
) -> Result<Option<IndexedScryptoValue>, RuntimeError> {
self.callback
.on_remove_substate(RemoveSubstateEvent::Start(
node_id,
&partition_num,
substate_key,
))?;
let depth = self.kernel_get_current_depth();
self.callback.on_remove_substate(
depth,
RemoveSubstateEvent::Start(node_id, &partition_num, substate_key),
)?;

let mut handler = KernelHandler {
callback: self.callback,
prev_frame: self.prev_frame_stack.last(),
on_io_access: |api, io_access| {
api.callback
.on_remove_substate(RemoveSubstateEvent::IOAccess(&io_access))
.on_remove_substate(depth, RemoveSubstateEvent::IOAccess(&io_access))
},
};

Expand Down Expand Up @@ -1059,14 +1059,15 @@ where
partition_num: PartitionNumber,
limit: u32,
) -> Result<Vec<SubstateKey>, RuntimeError> {
self.callback.on_scan_keys(ScanKeysEvent::Start)?;
let depth = self.kernel_get_current_depth();
self.callback.on_scan_keys(depth, ScanKeysEvent::Start)?;

let mut handler = KernelHandler {
callback: self.callback,
prev_frame: self.prev_frame_stack.last(),
on_io_access: |api, io_access| {
api.callback
.on_scan_keys(ScanKeysEvent::IOAccess(&io_access))
.on_scan_keys(depth, ScanKeysEvent::IOAccess(&io_access))
},
};

Expand Down Expand Up @@ -1096,15 +1097,16 @@ where
partition_num: PartitionNumber,
limit: u32,
) -> Result<Vec<(SubstateKey, IndexedScryptoValue)>, RuntimeError> {
let depth = self.kernel_get_current_depth();
self.callback
.on_drain_substates(DrainSubstatesEvent::Start(limit))?;
.on_drain_substates(depth, DrainSubstatesEvent::Start(limit))?;

let mut handler = KernelHandler {
callback: self.callback,
prev_frame: self.prev_frame_stack.last(),
on_io_access: |api, io_access| {
api.callback
.on_drain_substates(DrainSubstatesEvent::IOAccess(&io_access))
.on_drain_substates(depth, DrainSubstatesEvent::IOAccess(&io_access))
},
};

Expand Down Expand Up @@ -1134,15 +1136,16 @@ where
partition_num: PartitionNumber,
limit: u32,
) -> Result<Vec<(SortedKey, IndexedScryptoValue)>, RuntimeError> {
let depth = self.kernel_get_current_depth();
self.callback
.on_scan_sorted_substates(ScanSortedSubstatesEvent::Start)?;
.on_scan_sorted_substates(depth, ScanSortedSubstatesEvent::Start)?;

let mut handler = KernelHandler {
callback: self.callback,
prev_frame: self.prev_frame_stack.last(),
on_io_access: |api, io_access| {
api.callback
.on_scan_sorted_substates(ScanSortedSubstatesEvent::IOAccess(&io_access))
.on_scan_sorted_substates(depth, ScanSortedSubstatesEvent::IOAccess(&io_access))
},
};

Expand Down
24 changes: 19 additions & 5 deletions radix-engine/src/kernel/kernel_callback_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub trait KernelCallbackObject: Sized {
) -> Self::Receipt;

/// Callback before a node is pinned to it's device
fn on_pin_node(&mut self, node_id: &NodeId) -> Result<(), RuntimeError>;
fn on_pin_node(&mut self, depth: usize, node_id: &NodeId) -> Result<(), RuntimeError>;

/// Callbacks before/on-io-access/after a new node is created
fn on_create_node<Y>(api: &mut Y, event: CreateNodeEvent) -> Result<(), RuntimeError>
Expand Down Expand Up @@ -231,20 +231,33 @@ pub trait KernelCallbackObject: Sized {
Y: KernelInternalApi<Self>;

/// Callback before/on-io-access a substate is set
fn on_set_substate(&mut self, event: SetSubstateEvent) -> Result<(), RuntimeError>;
fn on_set_substate(
&mut self,
depth: usize,
event: SetSubstateEvent,
) -> Result<(), RuntimeError>;

/// Callback before/on-io-access a substate is removed
fn on_remove_substate(&mut self, event: RemoveSubstateEvent) -> Result<(), RuntimeError>;
fn on_remove_substate(
&mut self,
depth: usize,
event: RemoveSubstateEvent,
) -> Result<(), RuntimeError>;

/// Callback before/on-io-access a key scan occurs
fn on_scan_keys(&mut self, event: ScanKeysEvent) -> Result<(), RuntimeError>;
fn on_scan_keys(&mut self, depth: usize, event: ScanKeysEvent) -> Result<(), RuntimeError>;

/// Callback before/on-io-access a substate drain occurs
fn on_drain_substates(&mut self, event: DrainSubstatesEvent) -> Result<(), RuntimeError>;
fn on_drain_substates(
&mut self,
depth: usize,
event: DrainSubstatesEvent,
) -> Result<(), RuntimeError>;

/// Callback before/on-io-access a sorted substate scan occurs
fn on_scan_sorted_substates(
&mut self,
depth: usize,
event: ScanSortedSubstatesEvent,
) -> Result<(), RuntimeError>;

Expand Down Expand Up @@ -293,6 +306,7 @@ pub trait KernelCallbackObject: Sized {
/// Callback before a substate is marked as transient
fn on_mark_substate_as_transient(
&mut self,
depth: usize,
node_id: &NodeId,
partition_number: &PartitionNumber,
substate_key: &SubstateKey,
Expand Down
Loading

0 comments on commit 50ab8d2

Please sign in to comment.