Skip to content

Commit

Permalink
refactor: Streamline engine init
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Sep 23, 2024
1 parent 9d47e54 commit edc5781
Show file tree
Hide file tree
Showing 26 changed files with 630 additions and 569 deletions.
23 changes: 23 additions & 0 deletions radix-common/src/state/state_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub struct StateUpdates {
}

impl StateUpdates {
pub fn empty() -> Self {
Self {
by_node: Default::default(),
}
}

/// Starts a Node-level update.
pub fn of_node(&mut self, node_id: NodeId) -> &mut NodeStateUpdates {
self.by_node
Expand All @@ -25,6 +31,23 @@ impl StateUpdates {
})
}

pub fn set_substate<'a>(
mut self,
node_id: impl Into<NodeId>,
partition_num: PartitionNumber,
substate_key: impl ResolvableSubstateKey<'a>,
new_value: impl ScryptoEncode,
) -> Self {
let new_value = scrypto_encode(&new_value).expect("New substate value should be encodable");
self.of_node(node_id.into())
.of_partition(partition_num)
.update_substates([(
substate_key.into_substate_key(),
DatabaseUpdate::Set(new_value),
)]);
self
}

pub fn rebuild_without_empty_entries(&self) -> Self {
Self {
by_node: self
Expand Down
10 changes: 10 additions & 0 deletions radix-common/src/types/node_and_substate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ impl<'a> SubstateKeyOrRef<'a> {

pub trait ResolvableSubstateKey<'a>: Sized {
fn into_substate_key_or_ref(self) -> SubstateKeyOrRef<'a>;
fn into_substate_key(self) -> SubstateKey {
match self.into_substate_key_or_ref() {
SubstateKeyOrRef::Owned(key) => key,
SubstateKeyOrRef::Borrowed(SubstateKeyRef::Field(key)) => SubstateKey::Field(*key),
SubstateKeyOrRef::Borrowed(SubstateKeyRef::Map(key)) => SubstateKey::Map(key.clone()),
SubstateKeyOrRef::Borrowed(SubstateKeyRef::Sorted(key)) => {
SubstateKey::Sorted(key.clone())
}
}
}
}

impl<'a> ResolvableSubstateKey<'a> for &'a SubstateKey {
Expand Down
41 changes: 7 additions & 34 deletions radix-engine-tests/tests/kernel/kernel_open_substate.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,7 @@
use radix_common::prelude::*;
use radix_engine::errors::{CallFrameError, KernelError, RuntimeError};
use radix_engine::kernel::call_frame::OpenSubstateError;
use radix_engine::kernel::id_allocator::IdAllocator;
use radix_engine::kernel::kernel::Kernel;
use radix_engine::kernel::kernel_api::KernelSubstateApi;
use radix_engine::system::system_callback::{System, SystemLockData, VersionedSystemLogic};
use radix_engine::system::system_modules::auth::AuthModule;
use radix_engine::system::system_modules::costing::{
CostingModule, CostingModuleConfig, FeeTable, SystemLoanFeeReserve,
};
use radix_engine::system::system_modules::execution_trace::ExecutionTraceModule;
use radix_engine::system::system_modules::kernel_trace::KernelTraceModule;
use radix_engine::system::system_modules::limits::LimitsModule;
use radix_engine::system::system_modules::transaction_runtime::TransactionRuntimeModule;
use radix_engine::system::system_modules::{EnabledModules, SystemModuleMixer};
use radix_engine::track::Track;
use radix_engine::updates::ProtocolBuilder;
use radix_engine::vm::wasm::DefaultWasmEngine;
use radix_engine::vm::*;
use radix_engine_interface::api::LockFlags;
use radix_engine_interface::prelude::*;
use radix_substate_store_impls::memory_db::InMemorySubstateDatabase;
use radix_substate_store_queries::typed_substate_layout::{
BlueprintVersionKey, PACKAGE_AUTH_TEMPLATE_PARTITION_OFFSET,
};
use radix_transactions::prelude::*;
use scrypto_test::prelude::UniqueTransaction;
use scrypto_test::prelude::*;

#[test]
pub fn test_open_substate_of_invisible_package_address() {
Expand All @@ -46,17 +22,14 @@ pub fn test_open_substate_of_invisible_package_address() {
.commit_each_protocol_update(&mut database);

// Create kernel
let mut system = System {
versioned_system_logic: VersionedSystemLogic::V1,
blueprint_cache: NonIterMap::new(),
auth_cache: NonIterMap::new(),
schema_cache: NonIterMap::new(),
callback: Vm {
let mut system = System::new(
SystemVersion::latest(),
Vm {
scrypto_vm: &scrypto_vm,
native_vm,
vm_boot: VmBoot::latest(),
},
modules: SystemModuleMixer::new(
SystemModuleMixer::new(
EnabledModules::for_test_transaction(),
KernelTraceModule,
TransactionRuntimeModule::new(
Expand All @@ -78,8 +51,8 @@ pub fn test_open_substate_of_invisible_package_address() {
},
ExecutionTraceModule::new(MAX_EXECUTION_TRACE_DEPTH),
),
finalization: Default::default(),
};
SystemFinalization::no_nullifications(),
);
let mut track = Track::new(&database);
let mut id_allocator = IdAllocator::new(executable.unique_seed_for_id_allocator());
let mut kernel = Kernel::new_no_refs(&mut track, &mut id_allocator, &mut system);
Expand Down
33 changes: 16 additions & 17 deletions radix-engine-tests/tests/kernel/panics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use radix_common::prelude::*;
use radix_engine::errors::*;
use radix_engine::kernel::call_frame::*;
use radix_engine::kernel::kernel_api::*;
use radix_engine::system::actor::*;
#[cfg(not(feature = "alloc"))]
use radix_engine::system::system::SystemService;
use radix_engine::system::system_callback::*;
Expand All @@ -11,13 +10,13 @@ use radix_engine::vm::wasm::*;
use radix_engine::vm::*;
use radix_engine_interface::prelude::*;
use radix_substate_store_interface::db_key_mapper::*;
use scrypto_test::prelude::SystemCallbackObject;
use scrypto_test::prelude::*;

#[cfg(feature = "std")]
#[test]
fn panics_at_the_system_layer_or_below_can_be_caught() {
// Arrange
let mut kernel = MockKernel(PhantomData::<Vm<DefaultWasmEngine, NoExtension>>);
let mut kernel = MockKernel(PhantomData::<System<Vm<DefaultWasmEngine, NoExtension>>>);
let mut system_service = SystemService::new(&mut kernel);

// Act
Expand All @@ -36,14 +35,14 @@ macro_rules! panic1 {
};
}

pub struct MockKernel<M: SystemCallbackObject>(PhantomData<M>);
pub struct MockKernel<E: KernelTransactionExecutor>(PhantomData<E>);

impl<M: SystemCallbackObject> KernelApi for MockKernel<M> {
type CallbackObject = System<M>;
impl<E: KernelTransactionExecutor> KernelApi for MockKernel<E> {
type CallbackObject = E;
}

impl<M: SystemCallbackObject> KernelStackApi for MockKernel<M> {
type CallFrameData = Actor;
impl<E: KernelTransactionExecutor> KernelStackApi for MockKernel<E> {
type CallFrameData = E::CallFrameData;

fn kernel_get_stack_id(&self) -> usize {
panic1!()
Expand All @@ -53,7 +52,7 @@ impl<M: SystemCallbackObject> KernelStackApi for MockKernel<M> {
panic1!()
}

fn kernel_set_call_frame_data(&mut self, _data: Actor) -> Result<(), RuntimeError> {
fn kernel_set_call_frame_data(&mut self, _data: E::CallFrameData) -> Result<(), RuntimeError> {
panic1!()
}

Expand All @@ -62,7 +61,7 @@ impl<M: SystemCallbackObject> KernelStackApi for MockKernel<M> {
}
}

impl<M: SystemCallbackObject> KernelNodeApi for MockKernel<M> {
impl<E: KernelTransactionExecutor> KernelNodeApi for MockKernel<E> {
fn kernel_pin_node(&mut self, _: NodeId) -> Result<(), RuntimeError> {
panic1!()
}
Expand All @@ -88,7 +87,7 @@ impl<M: SystemCallbackObject> KernelNodeApi for MockKernel<M> {
}
}

impl<M: SystemCallbackObject> KernelSubstateApi<SystemLockData> for MockKernel<M> {
impl<E: KernelTransactionExecutor> KernelSubstateApi<E::LockData> for MockKernel<E> {
fn kernel_mark_substate_as_transient(
&mut self,
_: NodeId,
Expand All @@ -105,12 +104,12 @@ impl<M: SystemCallbackObject> KernelSubstateApi<SystemLockData> for MockKernel<M
_: &SubstateKey,
_: LockFlags,
_: Option<F>,
_: SystemLockData,
_: E::LockData,
) -> Result<SubstateHandle, RuntimeError> {
panic1!()
}

fn kernel_get_lock_data(&mut self, _: SubstateHandle) -> Result<SystemLockData, RuntimeError> {
fn kernel_get_lock_data(&mut self, _: SubstateHandle) -> Result<E::LockData, RuntimeError> {
panic1!()
}

Expand Down Expand Up @@ -180,17 +179,17 @@ impl<M: SystemCallbackObject> KernelSubstateApi<SystemLockData> for MockKernel<M
}
}

impl<M: SystemCallbackObject> KernelInvokeApi<Actor> for MockKernel<M> {
impl<E: KernelTransactionExecutor> KernelInvokeApi<E::CallFrameData> for MockKernel<E> {
fn kernel_invoke(
&mut self,
_: Box<KernelInvocation<Actor>>,
_: Box<KernelInvocation<E::CallFrameData>>,
) -> Result<IndexedScryptoValue, RuntimeError> {
panic1!()
}
}

impl<M: SystemCallbackObject> KernelInternalApi for MockKernel<M> {
type System = System<M>;
impl<E: KernelTransactionExecutor> KernelInternalApi for MockKernel<E> {
type System = E;

fn kernel_get_system_state(&mut self) -> SystemState<'_, Self::System> {
panic1!()
Expand Down
30 changes: 12 additions & 18 deletions radix-engine-tests/tests/vm/native_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,14 @@ fn panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
let native_vm = NativeVm::new_with_extension(Extension);

let intent_hash = Hash([0; 32]);
let mut system = System {
versioned_system_logic: VersionedSystemLogic::V1,
blueprint_cache: NonIterMap::new(),
auth_cache: NonIterMap::new(),
schema_cache: NonIterMap::new(),
callback: Vm {
let mut system = System::new(
SystemVersion::latest(),
Vm {
scrypto_vm: &scrypto_vm,
native_vm,
vm_boot: VmBoot::latest(),
},
modules: SystemModuleMixer::new(
SystemModuleMixer::new(
EnabledModules::for_notarized_transaction(),
KernelTraceModule,
TransactionRuntimeModule::new(NetworkDefinition::simulator(), intent_hash),
Expand All @@ -94,8 +91,8 @@ fn panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
},
ExecutionTraceModule::new(MAX_EXECUTION_TRACE_DEPTH),
),
finalization: Default::default(),
};
SystemFinalization::no_nullifications(),
);

let mut id_allocator = IdAllocator::new(intent_hash);
let mut kernel = Kernel::new_no_refs(&mut track, &mut id_allocator, &mut system);
Expand Down Expand Up @@ -136,17 +133,14 @@ fn any_panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
let native_vm = NativeVm::new_with_extension(NonStringPanicExtension);

let intent_hash = Hash([0; 32]);
let mut system = System {
versioned_system_logic: VersionedSystemLogic::V1,
blueprint_cache: NonIterMap::new(),
auth_cache: NonIterMap::new(),
schema_cache: NonIterMap::new(),
callback: Vm {
let mut system = System::new(
SystemVersion::latest(),
Vm {
scrypto_vm: &scrypto_vm,
native_vm,
vm_boot: VmBoot::latest(),
},
modules: SystemModuleMixer::new(
SystemModuleMixer::new(
EnabledModules::for_notarized_transaction(),
KernelTraceModule,
TransactionRuntimeModule::new(NetworkDefinition::simulator(), intent_hash),
Expand All @@ -165,8 +159,8 @@ fn any_panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
},
ExecutionTraceModule::new(MAX_EXECUTION_TRACE_DEPTH),
),
finalization: Default::default(),
};
SystemFinalization::no_nullifications(),
);

let mut id_allocator = IdAllocator::new(intent_hash);
let mut kernel = Kernel::new_no_refs(&mut track, &mut id_allocator, &mut system);
Expand Down
3 changes: 3 additions & 0 deletions radix-engine/src/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait InitializationParameters {
type For;
}
Loading

0 comments on commit edc5781

Please sign in to comment.