Skip to content

Commit

Permalink
feat: Refactors to kernel init for new executable model
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Aug 21, 2024
1 parent d85ee4e commit be682b4
Show file tree
Hide file tree
Showing 53 changed files with 889 additions and 705 deletions.
4 changes: 3 additions & 1 deletion radix-clis/src/replay/cmd_alloc_dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ impl TxnAllocDump {
output,
"user,{},{},{},{},{}",
TransactionHashBech32Encoder::new(&network)
.encode(&IntentHash(tx.signed_intent.intent.summary.hash))
.encode(&TransactionIntentHash(
tx.signed_intent.intent.summary.hash
))
.unwrap(),
execution_cost_units.unwrap(),
heap_allocations_sum,
Expand Down
2 changes: 1 addition & 1 deletion radix-clis/src/replay/cmd_measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl TxnMeasure {
output,
"{},{},{},{}",
TransactionHashBech32Encoder::new(&network)
.encode(&IntentHash(tx.signed_intent.intent.summary.hash))
.encode(&TransactionIntentHash(tx.signed_intent.intent.summary.hash))
.unwrap(),
tx_processing_time.as_micros(),
execution_cost_units.unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions radix-clis/src/replay/cmd_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ pub enum TypedTransactionIdentifiers {
system_transaction_hash: SystemTransactionHash,
},
User {
intent_hash: IntentHash,
signed_intent_hash: SignedIntentHash,
intent_hash: TransactionIntentHash,
signed_intent_hash: SignedTransactionIntentHash,
notarized_transaction_hash: NotarizedTransactionHash,
},
RoundUpdateV1 {
Expand Down
4 changes: 4 additions & 0 deletions radix-common/src/address/hrpset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct HrpSet {
/* Transaction Parts */
pub transaction_intent: String,
pub signed_transaction_intent: String,
pub subintent: String,
pub signed_subintent: String,
pub notarized_transaction: String,
pub round_update_transaction: String,
pub system_transaction: String,
Expand Down Expand Up @@ -82,6 +84,8 @@ impl From<&NetworkDefinition> for HrpSet {
/* Transaction Parts */
transaction_intent: format!("txid_{}", suffix),
signed_transaction_intent: format!("signedintent_{}", suffix),
subintent: format!("subtxid_{}", suffix),
signed_subintent: format!("signedsubintent_{}", suffix),
notarized_transaction: format!("notarizedtransaction_{}", suffix),
round_update_transaction: format!("roundupdatetransaction_{}", suffix),
system_transaction: format!("systemtransaction_{}", suffix),
Expand Down
14 changes: 1 addition & 13 deletions radix-engine-monkey-tests/tests/fuzz_kernel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use radix_common::prelude::*;
use radix_engine::errors::{RejectionReason, RuntimeError};
use radix_engine::errors::RuntimeError;
use radix_engine::kernel::call_frame::CallFrameMessage;
use radix_engine::kernel::id_allocator::IdAllocator;
use radix_engine::kernel::kernel::Kernel;
Expand All @@ -10,12 +10,10 @@ use radix_engine::kernel::kernel_api::{
use radix_engine::kernel::kernel_callback_api::*;
use radix_engine::system::checkers::KernelDatabaseChecker;
use radix_engine::track::{to_state_updates, CommitableSubstateStore, Track};
use radix_engine::transaction::ResourcesUsage;
use radix_engine_interface::prelude::*;
use radix_substate_store_impls::memory_db::InMemorySubstateDatabase;
use radix_substate_store_interface::db_key_mapper::SpreadPrefixKeyMapper;
use radix_substate_store_interface::interface::CommittableSubstateDatabase;
use radix_transactions::model::ExecutableTransaction;
use rand::Rng;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha8Rng;
Expand Down Expand Up @@ -44,16 +42,6 @@ impl CallFrameReferences for TestCallFrameData {
}
}

struct TestReceipt;

impl ExecutionReceipt<ExecutableTransaction> for TestReceipt {
fn from_rejection(_executable: ExecutableTransaction, _reason: RejectionReason) -> Self {
TestReceipt
}

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

struct TestCallbackObject;
impl KernelCallbackObject for TestCallbackObject {
type LockData = ();
Expand Down
4 changes: 2 additions & 2 deletions radix-engine-tests/benches/costing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ fn bench_spin_loop(c: &mut Criterion) {
c.bench_function("costing::spin_loop", |b| {
b.iter(|| {
let fee_reserve = SystemLoanFeeReserve::new(
&CostingParameters::babylon_genesis(),
&TransactionCostingParameters {
CostingParameters::babylon_genesis(),
TransactionCostingParameters {
free_credit_in_xrd: Decimal::try_from(PREVIEW_CREDIT_IN_XRD).unwrap(),
tip_percentage: DEFAULT_TIP_PERCENTAGE,
abort_when_loan_repaid: false,
Expand Down
7 changes: 3 additions & 4 deletions radix-engine-tests/tests/application/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,13 @@ fn transaction_processor_produces_expected_error_for_undecodable_instructions()
references,
Rc::new(blobs),
ExecutionContext {
intent_hash: TransactionIntentHash::NotToCheck {
intent_hash: Hash([0; 32]),
},
unique_hash: Hash([0; 32]),
intent_hash_check: IntentHashCheck::None,
epoch_range: Default::default(),
pre_allocated_addresses: Default::default(),
payload_size: 4,
num_of_signature_validations: 0,
auth_zone_params: Default::default(),
auth_zone_init: Default::default(),
costing_parameters: Default::default(),
},
false,
Expand Down
22 changes: 3 additions & 19 deletions radix-engine-tests/tests/kernel/kernel.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use radix_common::prelude::*;
use radix_engine::errors::{CallFrameError, KernelError, RejectionReason, RuntimeError};
use radix_engine::kernel::call_frame::{CallFrameMessage, CloseSubstateError, CreateFrameError, CreateNodeError, MovePartitionError, PassMessageError, ProcessSubstateError, TakeNodeError, WriteSubstateError};
use radix_engine::errors::*;
use radix_engine::kernel::call_frame::*;
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_api::*;
use radix_engine::kernel::kernel_callback_api::*;
use radix_engine::track::Track;
use radix_engine::transaction::ResourcesUsage;
use radix_engine_interface::prelude::*;
use radix_substate_store_impls::memory_db::InMemorySubstateDatabase;
use radix_substate_store_interface::db_key_mapper::SpreadPrefixKeyMapper;
use radix_transactions::model::ExecutableTransaction;

#[derive(Default)]
struct TestCallFrameData;
Expand All @@ -36,17 +31,6 @@ impl CallFrameReferences for TestCallFrameData {
}
}

struct TestReceipt;

impl ExecutionReceipt<ExecutableTransaction> for TestReceipt {
fn from_rejection(_executable: ExecutableTransaction, _reason: RejectionReason) -> Self {
Self
}

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

struct TestCallbackObject;
impl KernelCallbackObject for TestCallbackObject {
type LockData = ();
Expand Down
12 changes: 8 additions & 4 deletions radix-engine-tests/tests/kernel/kernel_open_substate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use radix_substate_store_queries::typed_substate_layout::{
BlueprintVersionKey, PACKAGE_AUTH_TEMPLATE_PARTITION_OFFSET,
};
use radix_transactions::prelude::*;
use scrypto_test::prelude::{AuthZoneParams, UniqueTransaction};

#[test]
pub fn test_open_substate_of_invisible_package_address() {
Expand All @@ -42,6 +43,7 @@ pub fn test_open_substate_of_invisible_package_address() {
let native_vm = DefaultNativeVm::new();
ProtocolBuilder::for_simulator().from_bootstrap_to_latest().commit_each_protocol_update(&mut database);

let auth_zone_inits: Vec<_> = executable.intents().iter().map(|i| i.auth_zone_init().clone()).collect();
// Create kernel
let mut system = System {
executable: (),
Expand All @@ -58,16 +60,18 @@ pub fn test_open_substate_of_invisible_package_address() {
KernelTraceModule,
TransactionRuntimeModule::new(
NetworkDefinition::simulator(),
executable.intent_hash().to_hash(),
*executable.unique_hash(),
),
AuthModule::new(executable.auth_zone_params().clone()),
AuthModule::new(AuthZoneParams {
auth_zone_init_for_each_intent: auth_zone_inits,
}),
LimitsModule::babylon_genesis(),
CostingModule {
current_depth: 0,
fee_reserve: SystemLoanFeeReserve::default(),
fee_table: FeeTable::new(),
tx_payload_len: executable.payload_size(),
tx_num_of_signature_validations: executable.auth_zone_params().initial_proofs.len(),
tx_num_of_signature_validations: executable.num_of_signature_validations(),
config: CostingModuleConfig::babylon_genesis(),
cost_breakdown: None,
detailed_cost_breakdown: None,
Expand All @@ -77,7 +81,7 @@ pub fn test_open_substate_of_invisible_package_address() {
),
};
let mut track = Track::<InMemorySubstateDatabase, SpreadPrefixKeyMapper>::new(&database);
let mut id_allocator = IdAllocator::new(executable.intent_hash().to_hash());
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);

// Lock package substate
Expand Down
4 changes: 4 additions & 0 deletions radix-engine-tests/tests/kernel/panics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ impl<M: SystemCallbackObject> KernelInternalApi for MockKernel<M> {
panic1!()
}

fn kernel_get_intent_index(&self) -> usize {
panic1!()
}

fn kernel_get_current_depth(&self) -> usize {
panic1!()
}
Expand Down
11 changes: 3 additions & 8 deletions radix-engine-tests/tests/vm/native_vm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(feature = "std")]

use auth::AuthZoneParams;
use radix_common::prelude::*;
use radix_engine::errors::*;
use radix_engine::kernel::id_allocator::*;
Expand Down Expand Up @@ -78,10 +79,7 @@ fn panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
EnabledModules::for_notarized_transaction(),
KernelTraceModule,
TransactionRuntimeModule::new(NetworkDefinition::simulator(), intent_hash),
AuthModule::new(AuthZoneParams {
initial_proofs: Default::default(),
virtual_resources: Default::default(),
}),
AuthModule::new(AuthZoneParams::default()),
LimitsModule::babylon_genesis(),
CostingModule {
current_depth: 0,
Expand Down Expand Up @@ -149,10 +147,7 @@ fn any_panics_can_be_caught_in_the_native_vm_and_converted_into_results() {
EnabledModules::for_notarized_transaction(),
KernelTraceModule,
TransactionRuntimeModule::new(NetworkDefinition::simulator(), intent_hash),
AuthModule::new(AuthZoneParams {
initial_proofs: Default::default(),
virtual_resources: Default::default(),
}),
AuthModule::new(AuthZoneParams::default()),
LimitsModule::babylon_genesis(),
CostingModule {
current_depth: 0,
Expand Down
8 changes: 4 additions & 4 deletions radix-engine-tests/tests/vm/wasm_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ fn test_wasm_memory_grow_read_write() {
let mut instance = wasm_engine.instantiate(CodeHash(Hash([0u8; 32])), &code);

let fee_reserve = SystemLoanFeeReserve::new(
&CostingParameters::babylon_genesis(),
&TransactionCostingParameters::default(),
CostingParameters::babylon_genesis(),
TransactionCostingParameters::default(),
);
let mut wasm_execution_units_consumed = 0;
let mut runtime: Box<dyn WasmRuntime> = Box::new(NoOpWasmRuntime::new(
Expand Down Expand Up @@ -163,8 +163,8 @@ fn test_wasm_memory_is_clean() {
let mut instance = wasm_engine.instantiate(CodeHash(Hash([0u8; 32])), &code);

let fee_reserve = SystemLoanFeeReserve::new(
&CostingParameters::babylon_genesis(),
&TransactionCostingParameters::default(),
CostingParameters::babylon_genesis(),
TransactionCostingParameters::default(),
);
let mut wasm_execution_units_consumed = 0;
let mut runtime: Box<dyn WasmRuntime> = Box::new(NoOpWasmRuntime::new(
Expand Down
6 changes: 6 additions & 0 deletions radix-engine/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ pub enum RejectionReason {
SuccessButFeeLoanNotRepaid,
}

impl From<BootloadingError> for RejectionReason {
fn from(value: BootloadingError) -> Self {
RejectionReason::BootloadingError(value)
}
}

impl fmt::Display for RejectionReason {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
Expand Down
45 changes: 28 additions & 17 deletions radix-engine/src/kernel/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,20 @@ pub struct BootLoader<'h, M: KernelTransactionCallbackObject, S: SubstateDatabas

impl<'h, M: KernelTransactionCallbackObject, S: SubstateDatabase> BootLoader<'h, M, S> {
/// Executes a transaction
pub fn execute(self, executable: M::Executable) -> M::Receipt {
pub fn execute(self, executable: M::TransactionExecutable) -> M::Receipt {
// Start hardware resource usage tracker
#[cfg(all(target_os = "linux", feature = "std", feature = "cpu_ram_metrics"))]
let mut resources_tracker =
crate::kernel::resources_tracker::ResourcesTracker::start_measurement();

#[cfg(not(all(target_os = "linux", feature = "std", feature = "cpu_ram_metrics")))]
{
self.execute_internal(executable.clone())
.unwrap_or_else(|reason| M::Receipt::from_rejection(executable, reason))
self.execute_internal(executable)
}

#[cfg(all(target_os = "linux", feature = "std", feature = "cpu_ram_metrics"))]
{
let mut receipt = self
.execute_internal(executable.clone())
.unwrap_or_else(|reason| M::Receipt::from_rejection(executable, reason));
let mut receipt = self.execute_internal(executable);

// Stop hardware resource usage tracker
receipt.set_resource_usage(resources_tracker.end_measurement());
Expand All @@ -76,10 +73,7 @@ impl<'h, M: KernelTransactionCallbackObject, S: SubstateDatabase> BootLoader<'h,
}
}

fn execute_internal(
mut self,
executable: M::Executable,
) -> Result<M::Receipt, RejectionReason> {
fn execute_internal(mut self, executable: M::TransactionExecutable) -> M::Receipt {
#[cfg(feature = "resource_tracker")]
radix_engine_profiling::QEMU_PLUGIN_CALIBRATOR.with(|v| {
v.borrow_mut();
Expand All @@ -98,15 +92,24 @@ impl<'h, M: KernelTransactionCallbackObject, S: SubstateDatabase> BootLoader<'h,
.unwrap_or(KernelBoot::babylon());

// Upper Layer Initialization
let (mut callback, call_frame_init) =
M::init(&mut self.track, executable, self.init.clone())?;
let system_init_result = M::init(&mut self.track, executable, self.init.clone());

let (mut system, call_frame_inits) = match system_init_result {
Ok(success) => success,
Err(receipt) => return receipt,
};

// Kernel Initialization
let mut kernel = Kernel::new(
&mut self.track,
&mut self.id_allocator,
&mut callback,
call_frame_init,
&mut system,
// TODO: Fix to take call frame inits for each intent
{
let mut call_frame_inits = call_frame_inits;
let first_call_frame_init = call_frame_inits.drain(..).next().unwrap();
first_call_frame_init
},
);

// Execution
Expand All @@ -129,9 +132,7 @@ impl<'h, M: KernelTransactionCallbackObject, S: SubstateDatabase> BootLoader<'h,
.map_err(|e| TransactionExecutionError::RuntimeError(e));

// Create receipt representing the result of a transaction
let receipt = M::create_receipt(callback, self.track, result);

Ok(receipt)
system.create_receipt(self.track, result)
}
}

Expand Down Expand Up @@ -438,6 +439,11 @@ impl<'g, M: KernelCallbackObject, S: CommitableSubstateStore> KernelInternalApi
self.current_frame.get_node_visibility(node_id)
}

fn kernel_get_intent_index(&self) -> usize {
// TODO: Fix when intent threading is implemented
0
}

fn kernel_get_current_depth(&self) -> usize {
self.current_frame.depth()
}
Expand Down Expand Up @@ -486,6 +492,11 @@ impl<'g, M: KernelCallbackObject> KernelInternalApi for KernelReadOnly<'g, M> {
self.current_frame.get_node_visibility(node_id)
}

fn kernel_get_intent_index(&self) -> usize {
// TODO - fix when threading is implemented!
unimplemented!()
}

fn kernel_get_current_depth(&self) -> usize {
self.current_frame.depth()
}
Expand Down
3 changes: 3 additions & 0 deletions radix-engine/src/kernel/kernel_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ pub trait KernelInternalApi {

fn kernel_get_system_state(&mut self) -> SystemState<'_, Self::System>;

/// Gets the intent index which is currently running
fn kernel_get_intent_index(&self) -> usize;

/// Gets the number of call frames that are currently in the call frame stack
fn kernel_get_current_depth(&self) -> usize;

Expand Down
Loading

0 comments on commit be682b4

Please sign in to comment.