Skip to content

Commit

Permalink
Some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
talekhinezh committed May 2, 2024
1 parent d3cfc75 commit b32e9ad
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 50 deletions.
19 changes: 12 additions & 7 deletions radix-engine-monkey-tests/tests/fuzz_kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use radix_common::prelude::*;
use radix_engine::errors::{
BootloadingError, RejectionReason, RuntimeError, TransactionExecutionError,
};
use radix_engine::kernel::call_frame::CallFrameMessage;
use radix_engine::kernel::call_frame::{CallFrameMessage, StableReferenceType};
use radix_engine::kernel::id_allocator::IdAllocator;
use radix_engine::kernel::kernel::Kernel;
use radix_engine::kernel::kernel_api::{
Expand Down Expand Up @@ -279,15 +279,20 @@ impl KernelCallbackObject for TestCallbackObject {
Ok(())
}

fn on_ref_check<Y>(
_api: &mut Y,
fn on_boot_ref_check(
&mut self,
_event: scrypto_test::prelude::RefCheckEvent,
) -> Result<(), BootloadingError>
where
Y: KernelApi<Self>,
{
) -> Result<(), BootloadingError> {
Ok(())
}

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

struct KernelFuzzer {
Expand Down
11 changes: 6 additions & 5 deletions radix-engine-tests/tests/kernel/kernel.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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, TakeNodeError, WriteSubstateError,
};
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::{
Expand Down Expand Up @@ -259,12 +256,16 @@ impl KernelCallbackObject for TestCallbackObject {
Ok(())
}

fn on_ref_check(
fn on_boot_ref_check(
&mut self,
_event: scrypto_test::prelude::RefCheckEvent,
) -> Result<(), BootloadingError> {
Ok(())
}

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

enum MoveVariation {
Expand Down
39 changes: 9 additions & 30 deletions radix-engine/src/kernel/kernel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::call_frame::{CallFrame, NodeVisibility, OpenSubstateError};
use super::call_frame::{CallFrame, NodeVisibility, OpenSubstateError, StableReferenceType};
use super::heap::Heap;
use super::id_allocator::IdAllocator;
use super::kernel_callback_api::RefCheckEvent;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<'h, M: KernelCallbackObject, S: SubstateDatabase> BootLoader<'h, M, S> {
continue;
}

let substate_ref = self
let value_ref = self
.track
.read_substate(
node_id,
Expand All @@ -121,33 +121,12 @@ impl<'h, M: KernelCallbackObject, S: SubstateDatabase> BootLoader<'h, M, S> {
)
.ok_or_else(|| BootloadingError::ReferencedNodeDoesNotExist(*node_id))?;

let type_substate: TypeInfoSubstate = substate_ref.as_typed().unwrap();
match &type_substate {
TypeInfoSubstate::Object(
info @ ObjectInfo {
blueprint_info: BlueprintInfo { blueprint_id, .. },
..
},
) => {
if info.is_global() {
global_addresses
.insert(GlobalAddress::new_or_panic(node_id.clone().into()));
} else if blueprint_id.package_address.eq(&RESOURCE_PACKAGE)
&& (blueprint_id.blueprint_name.eq(FUNGIBLE_VAULT_BLUEPRINT)
|| blueprint_id.blueprint_name.eq(NON_FUNGIBLE_VAULT_BLUEPRINT))
{
direct_accesses
.insert(InternalAddress::new_or_panic(node_id.clone().into()));
} else {
return Err(BootloadingError::ReferencedNodeDoesNotAllowDirectAccess(
node_id.clone(),
));
}
match callback.check_ref(node_id, value_ref)? {
StableReferenceType::Global => {
global_addresses.insert(GlobalAddress::new_or_panic(node_id.clone().into()));
}
_ => {
return Err(BootloadingError::ReferencedNodeIsNotAnObject(
node_id.clone(),
));
StableReferenceType::DirectAccess => {
direct_accesses.insert(InternalAddress::new_or_panic(node_id.clone().into()));
}
}

Expand All @@ -158,9 +137,9 @@ impl<'h, M: KernelCallbackObject, S: SubstateDatabase> BootLoader<'h, M, S> {
partition_number: TYPE_INFO_FIELD_PARTITION,
substate_key: SubstateKey::Field(TypeInfoField::TypeInfo.field_index()),
},
substate_ref.len(),
value_ref.len(),
);
callback.on_ref_check(RefCheckEvent::IOAccess(&io_access))?;
callback.on_boot_ref_check(RefCheckEvent::IOAccess(&io_access))?;
}
}

Expand Down
10 changes: 8 additions & 2 deletions radix-engine/src/kernel/kernel_callback_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::call_frame::CallFrameMessage;
use super::call_frame::{CallFrameMessage, StableReferenceType};
use crate::errors::*;
use crate::internal_prelude::*;
use crate::kernel::kernel_api::KernelInvocation;
Expand Down Expand Up @@ -179,7 +179,13 @@ pub trait KernelCallbackObject: Sized {
result: Result<Self::ExecutionOutput, TransactionExecutionError>,
) -> Self::Receipt;

fn on_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError>;
fn check_ref(
&mut self,
node_id: &NodeId,
value: &IndexedScryptoValue,
) -> Result<StableReferenceType, BootloadingError>;

fn on_boot_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError>;

fn on_pin_node(&mut self, node_id: &NodeId) -> Result<(), RuntimeError>;

Expand Down
36 changes: 34 additions & 2 deletions radix-engine/src/system/system_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::blueprints::transaction_tracker::{
};
use crate::errors::*;
use crate::internal_prelude::*;
use crate::kernel::call_frame::CallFrameMessage;
use crate::kernel::call_frame::{CallFrameMessage, StableReferenceType};
use crate::kernel::kernel_api::{KernelApi, KernelInvocation};
use crate::kernel::kernel_api::{KernelInternalApi, KernelSubstateApi};
use crate::kernel::kernel_callback_api::RefCheckEvent;
Expand Down Expand Up @@ -888,7 +888,39 @@ impl<C: SystemCallbackObject> KernelCallbackObject for System<C> {
}
}

fn on_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError> {
fn check_ref(
&mut self,
node_id: &NodeId,
value: &IndexedScryptoValue,
) -> Result<StableReferenceType, BootloadingError> {
let type_substate: TypeInfoSubstate = value.as_typed().unwrap();
return match &type_substate {
TypeInfoSubstate::Object(
info @ ObjectInfo {
blueprint_info: BlueprintInfo { blueprint_id, .. },
..
},
) => {
if info.is_global() {
Ok(StableReferenceType::Global)
} else if blueprint_id.package_address.eq(&RESOURCE_PACKAGE)
&& (blueprint_id.blueprint_name.eq(FUNGIBLE_VAULT_BLUEPRINT)
|| blueprint_id.blueprint_name.eq(NON_FUNGIBLE_VAULT_BLUEPRINT))
{
Ok(StableReferenceType::DirectAccess)
} else {
Err(BootloadingError::ReferencedNodeDoesNotAllowDirectAccess(
node_id.clone(),
))
}
}
_ => Err(BootloadingError::ReferencedNodeIsNotAnObject(
node_id.clone(),
)),
};
}

fn on_boot_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError> {
if let Some(costing) = self.modules.costing_mut() {
costing
.apply_deferred_execution_cost(ExecutionCostingEntry::RefCheck { event: &event })
Expand Down
16 changes: 12 additions & 4 deletions scrypto-test/src/ledger_simulator/inject_costing_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use radix_common::prelude::*;
use radix_engine::errors::BootloadingError;
use radix_engine::errors::{RejectionReason, TransactionExecutionError};
use radix_engine::errors::{RuntimeError, SystemModuleError};
use radix_engine::kernel::call_frame::{CallFrameMessage, NodeVisibility};
use radix_engine::kernel::call_frame::{CallFrameMessage, NodeVisibility, StableReferenceType};
use radix_engine::kernel::kernel_api::{
DroppedNode, KernelApi, KernelInternalApi, KernelInvocation, KernelInvokeApi, KernelNodeApi,
KernelSubstateApi, SystemState,
Expand Down Expand Up @@ -361,11 +361,19 @@ impl<K: SystemCallbackObject> KernelCallbackObject for InjectCostingError<K> {
)
}

fn on_ref_check(
fn on_boot_ref_check(
&mut self,
_event: radix_engine::kernel::kernel_callback_api::RefCheckEvent,
event: radix_engine::kernel::kernel_callback_api::RefCheckEvent,
) -> Result<(), BootloadingError> {
Ok(())
self.system.on_boot_ref_check(event)
}

fn check_ref(
&mut self,
node_id: &NodeId,
value: &IndexedScryptoValue,
) -> Result<StableReferenceType, BootloadingError> {
self.system.check_ref(node_id, value)
}
}

Expand Down

0 comments on commit b32e9ad

Please sign in to comment.