Skip to content

Commit

Permalink
Add FinalizationCost::CommitIntentStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyulong committed Oct 8, 2024
1 parent 31bd7d9 commit ef4aa5e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
6 changes: 4 additions & 2 deletions radix-engine/src/kernel/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'h, S: SubstateDatabase> BootLoader<'h, S> {
kernel_boot.always_visible_global_nodes(),
);

let (mut system, call_frame_inits) = match system_init_result {
let (mut system, call_frame_inits, num_of_intent_statuses) = match system_init_result {
Ok(success) => success,
Err(receipt) => return receipt,
};
Expand Down Expand Up @@ -177,7 +177,9 @@ impl<'h, S: SubstateDatabase> BootLoader<'h, S> {

// Finalize state updates based on what has occurred
let commit_info = kernel.substate_io.store.get_commit_info();
kernel.callback.finalize(commit_info)?;
kernel
.callback
.finalize(commit_info, num_of_intent_statuses)?;

Ok(output)
}()
Expand Down
8 changes: 6 additions & 2 deletions radix-engine/src/kernel/kernel_callback_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub trait KernelTransactionExecutor: KernelCallbackObject {
executable: &Self::Executable,
init: Self::Init,
always_visible_global_nodes: &'static IndexSet<NodeId>,
) -> Result<(Self, Vec<CallFrameInit<Self::CallFrameData>>), Self::Receipt>;
) -> Result<(Self, Vec<CallFrameInit<Self::CallFrameData>>, usize), Self::Receipt>;

/// Start execution
fn execute<Y: KernelApi<CallbackObject = Self>>(
Expand All @@ -175,7 +175,11 @@ pub trait KernelTransactionExecutor: KernelCallbackObject {
) -> Result<Self::ExecutionOutput, RuntimeError>;

/// Finish execution
fn finalize(&mut self, store_commit_info: StoreCommitInfo) -> Result<(), RuntimeError>;
fn finalize(
&mut self,
store_commit_info: StoreCommitInfo,
num_of_intent_statuses: usize,
) -> Result<(), RuntimeError>;

/// Create final receipt
fn create_receipt<S: SubstateDatabase>(
Expand Down
18 changes: 15 additions & 3 deletions radix-engine/src/system/system_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ impl<V: SystemCallbackObject> KernelTransactionExecutor for System<V> {
executable: &ExecutableTransaction,
init_input: Self::Init,
always_visible_global_nodes: &'static IndexSet<NodeId>,
) -> Result<(Self, Vec<CallFrameInit<Actor>>), Self::Receipt> {
) -> Result<(Self, Vec<CallFrameInit<Actor>>, usize), Self::Receipt> {
// Dump executable
#[cfg(not(feature = "alloc"))]
if init_input.self_init.enable_kernel_trace {
Expand Down Expand Up @@ -1541,6 +1541,7 @@ impl<V: SystemCallbackObject> KernelTransactionExecutor for System<V> {
}
}

let mut num_of_intent_statuses = 0;
for hash_nullification in executable.intent_hash_nullifications() {
let intent_hash_validation_result = match hash_nullification {
IntentHashNullification::TransactionIntent {
Expand Down Expand Up @@ -1580,6 +1581,8 @@ impl<V: SystemCallbackObject> KernelTransactionExecutor for System<V> {
// Transaction intent nullification is historically not costed.
}
IntentHashNullification::Subintent { .. } => {
num_of_intent_statuses += 1;

if let Some(costing) = modules.costing_mut() {
return costing
.apply_deferred_execution_cost(
Expand Down Expand Up @@ -1626,7 +1629,7 @@ impl<V: SystemCallbackObject> KernelTransactionExecutor for System<V> {
},
);

Ok((system, call_frame_inits))
Ok((system, call_frame_inits, num_of_intent_statuses))
}

fn execute<Y: SystemBasedKernelApi>(
Expand Down Expand Up @@ -1658,7 +1661,11 @@ impl<V: SystemCallbackObject> KernelTransactionExecutor for System<V> {
Ok(output)
}

fn finalize(&mut self, info: StoreCommitInfo) -> Result<(), RuntimeError> {
fn finalize(
&mut self,
info: StoreCommitInfo,
num_of_intent_statuses: usize,
) -> Result<(), RuntimeError> {
self.modules.on_teardown()?;

// Note that if a transactions fails during this phase, the costing is
Expand All @@ -1680,6 +1687,11 @@ impl<V: SystemCallbackObject> KernelTransactionExecutor for System<V> {
logs: &self.modules.logs().clone(),
})
.map_err(|e| RuntimeError::FinalizationCostingError(e))?;
self.modules
.apply_finalization_cost(FinalizationCostingEntry::CommitIntentStatus {
num_of_intent_statuses,
})
.map_err(|e| RuntimeError::FinalizationCostingError(e))?;

/* state storage costs */
for store_commit in &info {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub enum FinalizationCostingEntry<'a> {
CommitStateUpdates { store_commit: &'a StoreCommit },
CommitEvents { events: &'a Vec<Event> },
CommitLogs { logs: &'a Vec<(Level, String)> },
CommitIntentStatus { num_of_intent_statuses: usize },
}

impl<'a> ExecutionCostingEntry<'a> {
Expand Down Expand Up @@ -222,6 +223,10 @@ impl<'a> FinalizationCostingEntry<'a> {
}
FinalizationCostingEntry::CommitEvents { events } => ft.commit_events_cost(events),
FinalizationCostingEntry::CommitLogs { logs } => ft.commit_logs_cost(logs),

FinalizationCostingEntry::CommitIntentStatus {
num_of_intent_statuses,
} => ft.commit_intent_status(*num_of_intent_statuses),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions radix-engine/src/system/system_modules/costing/fee_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,12 @@ impl FeeTable {
}
sum
}

#[inline]
pub fn commit_intent_status(&self, num_of_intent_statuses: usize) -> u32 {
// Equivalent to a substate insertion
mul(cast(num_of_intent_statuses), 100_000)
}
}

#[inline]
Expand Down
14 changes: 10 additions & 4 deletions scrypto-test/src/ledger_simulator/inject_costing_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl<
executable: &ExecutableTransaction,
init_input: Self::Init,
always_visible_global_nodes: &'static IndexSet<NodeId>,
) -> Result<(Self, Vec<CallFrameInit<Self::CallFrameData>>), Self::Receipt> {
let (mut system, call_frame_inits) = E::init(
) -> Result<(Self, Vec<CallFrameInit<Self::CallFrameData>>, usize), Self::Receipt> {
let (mut system, call_frame_inits, num_of_intent_statuses) = E::init(
store,
executable,
init_input.system_input,
Expand All @@ -91,6 +91,7 @@ impl<
wrapped: system,
},
call_frame_inits,
num_of_intent_statuses,
))
}

Expand All @@ -102,9 +103,14 @@ impl<
E::execute(&mut api, executable)
}

fn finalize(&mut self, store_commit_info: StoreCommitInfo) -> Result<(), RuntimeError> {
fn finalize(
&mut self,
store_commit_info: StoreCommitInfo,
num_of_intent_statuses: usize,
) -> Result<(), RuntimeError> {
self.maybe_err()?;
self.wrapped.finalize(store_commit_info)
self.wrapped
.finalize(store_commit_info, num_of_intent_statuses)
}

fn create_receipt<S: SubstateDatabase>(
Expand Down

0 comments on commit ef4aa5e

Please sign in to comment.