Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Transactions support for simulation #703

Merged
merged 16 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 356 additions & 13 deletions src/lib.rs

Large diffs are not rendered by default.

49 changes: 41 additions & 8 deletions src/transaction/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ use num_traits::Zero;
use starknet_contract_class::EntryPointType;
use std::collections::HashMap;

use super::Transaction;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// Represents an internal transaction in the StarkNet network that is a declaration of a Cairo
/// contract class.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Declare {
pub class_hash: ClassHash,
pub sender_address: Address,
Expand All @@ -43,6 +45,9 @@ pub struct Declare {
pub nonce: Felt252,
pub hash_value: Felt252,
pub contract_class: ContractClass,
pub skip_validate: bool,
pub skip_execute: bool,
pub skip_fee_transfer: bool,
}

// ------------------------------------------------------------
Expand Down Expand Up @@ -88,6 +93,9 @@ impl Declare {
nonce,
hash_value,
contract_class,
skip_execute: false,
skip_validate: false,
skip_fee_transfer: false,
};

internal_declare.verify_version()?;
Expand Down Expand Up @@ -128,9 +136,11 @@ impl Declare {

// validate transaction
let mut resources_manager = ExecutionResourcesManager::default();
let validate_info =
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?;

let validate_info = if self.skip_validate {
None
} else {
self.run_validate_entrypoint(state, &mut resources_manager, block_context)?
};
let changes = state.count_actual_storage_changes();
let actual_resources = calculate_tx_resources(
resources_manager,
Expand Down Expand Up @@ -222,10 +232,17 @@ impl Declare {

let mut tx_execution_context =
self.get_execution_context(block_context.invoke_tx_max_n_steps);
let fee_transfer_info =
execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?;

Ok((Some(fee_transfer_info), actual_fee))
let fee_transfer_info = if self.skip_fee_transfer {
None
} else {
Some(execute_fee_transfer(
state,
block_context,
&mut tx_execution_context,
actual_fee,
)?)
};
Ok((fee_transfer_info, actual_fee))
}

fn handle_nonce<S: State + StateReader>(&self, state: &mut S) -> Result<(), TransactionError> {
Expand Down Expand Up @@ -280,6 +297,22 @@ impl Declare {
),
)
}

pub(crate) fn create_for_simulation(
SantiagoPittella marked this conversation as resolved.
Show resolved Hide resolved
&self,
skip_validate: bool,
skip_execute: bool,
skip_fee_transfer: bool,
) -> Transaction {
let tx = Declare {
skip_validate,
skip_execute,
skip_fee_transfer,
..self.clone()
};

Transaction::Declare(tx)
}
}

// ---------------
Expand Down
85 changes: 64 additions & 21 deletions src/transaction/declare_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use cairo_vm::felt::Felt252;
use num_traits::Zero;
use starknet_contract_class::EntryPointType;
use std::collections::HashMap;
#[derive(Debug)]

use super::Transaction;
#[derive(Debug, Clone)]
pub struct DeclareV2 {
pub sender_address: Address,
pub tx_type: TransactionType,
Expand All @@ -37,6 +39,9 @@ pub struct DeclareV2 {
pub sierra_contract_class: SierraContractClass,
pub hash_value: Felt252,
pub casm_class: once_cell::unsync::OnceCell<CasmContractClass>,
pub skip_validate: bool,
pub skip_execute: bool,
pub skip_fee_transfer: bool,
}

impl DeclareV2 {
Expand Down Expand Up @@ -79,6 +84,9 @@ impl DeclareV2 {
compiled_class_hash,
hash_value,
casm_class: Default::default(),
skip_execute: false,
skip_validate: false,
skip_fee_transfer: false,
};

internal_declare.verify_version()?;
Expand Down Expand Up @@ -147,10 +155,18 @@ impl DeclareV2 {

let mut tx_execution_context =
self.get_execution_context(block_context.invoke_tx_max_n_steps);
let fee_transfer_info =
execute_fee_transfer(state, block_context, &mut tx_execution_context, actual_fee)?;
let fee_transfer_info = if self.skip_fee_transfer {
None
} else {
Some(execute_fee_transfer(
state,
block_context,
&mut tx_execution_context,
actual_fee,
)?)
};

Ok((Some(fee_transfer_info), actual_fee))
Ok((fee_transfer_info, actual_fee))
}

// TODO: delete once used
Expand Down Expand Up @@ -185,17 +201,22 @@ impl DeclareV2 {

let mut resources_manager = ExecutionResourcesManager::default();

let (validate_info, _remaining_gas) = self.run_validate_entrypoint(
initial_gas,
state,
&mut resources_manager,
block_context,
)?;
let (validate_info, _remaining_gas) = if self.skip_validate {
(None, 0)
} else {
let (info, gas) = self.run_validate_entrypoint(
initial_gas,
state,
&mut resources_manager,
block_context,
)?;
(Some(info), gas)
};

let storage_changes = state.count_actual_storage_changes();
let actual_resources = calculate_tx_resources(
resources_manager,
&[Some(validate_info.clone())],
&[validate_info.clone()],
self.tx_type,
storage_changes,
None,
Expand All @@ -207,7 +228,7 @@ impl DeclareV2 {
self.charge_fee(state, &actual_resources, block_context)?;

let concurrent_exec_info = TransactionExecutionInfo::create_concurrent_stage_execution_info(
Some(validate_info),
validate_info,
None,
actual_resources,
Some(self.tx_type),
Expand Down Expand Up @@ -263,19 +284,41 @@ impl DeclareV2 {
let mut tx_execution_context =
self.get_execution_context(block_context.validate_max_n_steps);

let call_info = entry_point.execute(
state,
block_context,
resources_manager,
&mut tx_execution_context,
false,
)?;

let call_info = if self.skip_execute {
None
} else {
Some(entry_point.execute(
state,
block_context,
resources_manager,
&mut tx_execution_context,
false,
)?)
};
let call_info = verify_no_calls_to_other_contracts(&call_info)?;
remaining_gas -= call_info.gas_consumed;
verify_no_calls_to_other_contracts(&call_info)?;

Ok((call_info, remaining_gas))
}

// ---------------
// Simulation
// ---------------
pub(crate) fn create_for_simulation(
&self,
skip_validate: bool,
skip_execute: bool,
skip_fee_transfer: bool,
) -> Transaction {
let tx = DeclareV2 {
skip_validate,
skip_execute,
skip_fee_transfer,
..self.clone()
};

Transaction::DeclareV2(Box::new(tx))
}
}

#[cfg(test)]
Expand Down
29 changes: 28 additions & 1 deletion src/transaction/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use cairo_vm::felt::Felt252;
use num_traits::Zero;
use starknet_contract_class::EntryPointType;

#[derive(Debug)]
use super::Transaction;

#[derive(Debug, Clone)]
pub struct Deploy {
pub hash_value: Felt252,
pub version: Felt252,
Expand All @@ -37,6 +39,9 @@ pub struct Deploy {
pub contract_hash: ClassHash,
pub constructor_calldata: Vec<Felt252>,
pub tx_type: TransactionType,
pub skip_validate: bool,
pub skip_execute: bool,
pub skip_fee_transfer: bool,
}

impl Deploy {
Expand Down Expand Up @@ -77,6 +82,9 @@ impl Deploy {
contract_hash,
constructor_calldata,
tx_type: TransactionType::Deploy,
skip_validate: false,
skip_execute: false,
skip_fee_transfer: false,
})
}

Expand Down Expand Up @@ -223,6 +231,25 @@ impl Deploy {
),
)
}

// ---------------
// Simulation
// ---------------
pub(crate) fn create_for_simulation(
&self,
skip_validate: bool,
skip_execute: bool,
skip_fee_transfer: bool,
) -> Transaction {
let tx = Deploy {
skip_validate,
skip_execute,
skip_fee_transfer,
..self.clone()
};

Transaction::Deploy(tx)
}
}

#[cfg(test)]
Expand Down
Loading