From b32e9ad284df467502bcad3015a6c17e5d49f357 Mon Sep 17 00:00:00 2001 From: Joshua Primero Date: Thu, 2 May 2024 18:49:08 -0500 Subject: [PATCH] Some more cleanup --- .../tests/fuzz_kernel.rs | 19 +++++---- radix-engine-tests/tests/kernel/kernel.rs | 11 +++--- radix-engine/src/kernel/kernel.rs | 39 +++++-------------- .../src/kernel/kernel_callback_api.rs | 10 ++++- radix-engine/src/system/system_callback.rs | 36 ++++++++++++++++- .../ledger_simulator/inject_costing_err.rs | 16 ++++++-- 6 files changed, 81 insertions(+), 50 deletions(-) diff --git a/radix-engine-monkey-tests/tests/fuzz_kernel.rs b/radix-engine-monkey-tests/tests/fuzz_kernel.rs index c275a188050..0fa9525f137 100644 --- a/radix-engine-monkey-tests/tests/fuzz_kernel.rs +++ b/radix-engine-monkey-tests/tests/fuzz_kernel.rs @@ -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::{ @@ -279,15 +279,20 @@ impl KernelCallbackObject for TestCallbackObject { Ok(()) } - fn on_ref_check( - _api: &mut Y, + fn on_boot_ref_check( + &mut self, _event: scrypto_test::prelude::RefCheckEvent, - ) -> Result<(), BootloadingError> - where - Y: KernelApi, - { + ) -> Result<(), BootloadingError> { Ok(()) } + + fn check_ref( + &mut self, + _node_id: &NodeId, + _value: &IndexedScryptoValue, + ) -> Result { + Ok(StableReferenceType::Global) + } } struct KernelFuzzer { diff --git a/radix-engine-tests/tests/kernel/kernel.rs b/radix-engine-tests/tests/kernel/kernel.rs index b62a89a9ec7..1ccd3d80b5f 100644 --- a/radix-engine-tests/tests/kernel/kernel.rs +++ b/radix-engine-tests/tests/kernel/kernel.rs @@ -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::{ @@ -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 { + Ok(StableReferenceType::Global) + } } enum MoveVariation { diff --git a/radix-engine/src/kernel/kernel.rs b/radix-engine/src/kernel/kernel.rs index 8595fb11208..5e50ad1e5d2 100644 --- a/radix-engine/src/kernel/kernel.rs +++ b/radix-engine/src/kernel/kernel.rs @@ -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; @@ -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, @@ -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())); } } @@ -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))?; } } diff --git a/radix-engine/src/kernel/kernel_callback_api.rs b/radix-engine/src/kernel/kernel_callback_api.rs index e7c56e233cb..a12cf59c6fe 100644 --- a/radix-engine/src/kernel/kernel_callback_api.rs +++ b/radix-engine/src/kernel/kernel_callback_api.rs @@ -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; @@ -179,7 +179,13 @@ pub trait KernelCallbackObject: Sized { result: Result, ) -> Self::Receipt; - fn on_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError>; + fn check_ref( + &mut self, + node_id: &NodeId, + value: &IndexedScryptoValue, + ) -> Result; + + fn on_boot_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError>; fn on_pin_node(&mut self, node_id: &NodeId) -> Result<(), RuntimeError>; diff --git a/radix-engine/src/system/system_callback.rs b/radix-engine/src/system/system_callback.rs index 5c395e81173..55ed2200803 100644 --- a/radix-engine/src/system/system_callback.rs +++ b/radix-engine/src/system/system_callback.rs @@ -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; @@ -888,7 +888,39 @@ impl KernelCallbackObject for System { } } - fn on_ref_check(&mut self, event: RefCheckEvent) -> Result<(), BootloadingError> { + fn check_ref( + &mut self, + node_id: &NodeId, + value: &IndexedScryptoValue, + ) -> Result { + 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 }) diff --git a/scrypto-test/src/ledger_simulator/inject_costing_err.rs b/scrypto-test/src/ledger_simulator/inject_costing_err.rs index a0bd481ec5b..2ee5aa34bd2 100644 --- a/scrypto-test/src/ledger_simulator/inject_costing_err.rs +++ b/scrypto-test/src/ledger_simulator/inject_costing_err.rs @@ -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, @@ -361,11 +361,19 @@ impl KernelCallbackObject for InjectCostingError { ) } - 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 { + self.system.check_ref(node_id, value) } }