Skip to content

Commit

Permalink
build(fee): sn_resources depend on resource bounds signature
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 15, 2024
1 parent 13ae058 commit 855c361
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 54 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ regex = "1.10.4"
replace_with = "0.1.7"
reqwest = { version = "0.11", features = ["blocking", "json"] }
rstest = "0.17.0"
rstest_reuse = "0.7.0"
rustc-hex = "2.1.0"
schemars = "0.8.12"
serde = "1.0.197"
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ paste.workspace = true
phf.workspace = true
rand = { workspace = true, optional = true }
rstest = { workspace = true, optional = true }
rstest_reuse.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["arbitrary_precision"] }
sha2.workspace = true
Expand Down
46 changes: 26 additions & 20 deletions crates/blockifier/src/fee/actual_cost_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn versioned_constants() -> &'static VersionedConstants {
VersionedConstants::latest_constants()
}

#[rstest_reuse::template]
#[rstest]
// TODO(Nimrod): Add true as a case.
fn include_l2_gas(#[values(false)] has_l2_gas: bool) {}

/// This test goes over seven cases. In each case, we calculate the gas usage given the parameters.
/// We then perform the same calculation manually, each time using only the relevant parameters.
/// The seven cases are:
Expand All @@ -48,7 +53,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
let versioned_constants = VersionedConstants::default();
let empty_tx_starknet_resources = StarknetResources::default();
let empty_tx_gas_usage_vector =
empty_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
empty_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);
assert_eq!(empty_tx_gas_usage_vector, GasVector::default());

// Declare.
Expand All @@ -72,7 +77,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
let manual_gas_vector =
GasVector { l1_gas: code_gas_cost.to_integer(), ..Default::default() };
let declare_gas_usage_vector =
declare_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
declare_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);
assert_eq!(manual_gas_vector, declare_gas_usage_vector);
}

Expand Down Expand Up @@ -104,7 +109,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
+ deploy_account_tx_starknet_resources.get_state_changes_cost(use_kzg_da);

let deploy_account_gas_usage_vector =
deploy_account_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
deploy_account_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);
assert_eq!(manual_gas_vector, deploy_account_gas_usage_vector);

// L1 handler.
Expand All @@ -119,7 +124,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
std::iter::empty(),
);
let l1_handler_gas_usage_vector =
l1_handler_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
l1_handler_tx_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);

// Manual calculation.
let message_segment_length = get_message_segment_length(&[], Some(l1_handler_payload_size));
Expand Down Expand Up @@ -183,7 +188,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
);

let l2_to_l1_messages_gas_usage_vector =
l2_to_l1_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
l2_to_l1_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);

// Manual calculation.
let message_segment_length = get_message_segment_length(&l2_to_l1_payload_lengths, None);
Expand Down Expand Up @@ -226,7 +231,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
);

let storage_writings_gas_usage_vector =
storage_writes_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
storage_writes_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);

// Manual calculation.
let manual_gas_computation =
Expand All @@ -252,7 +257,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
);

let gas_usage_vector =
combined_cases_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da);
combined_cases_starknet_resources.to_gas_vector(&versioned_constants, use_kzg_da, false);

// Manual calculation.
let fee_balance_discount = match use_kzg_da {
Expand Down Expand Up @@ -284,10 +289,11 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
// TODO(Aner, 21/01/24) modify for 4844 (taking blob_gas into account).
// TODO(Nimrod, 1/5/2024): Test regression w.r.t. all resources (including VM). (Only starknet
// resources are taken into account).
#[rstest]
#[rstest_reuse::apply(include_l2_gas)]
fn test_calculate_tx_gas_usage(
max_resource_bounds: ResourceBoundsMapping,
#[values(false, true)] use_kzg_da: bool,
has_l2_gas: bool,
) {
let account_cairo_version = CairoVersion::Cairo0;
let test_contract_cairo_version = CairoVersion::Cairo0;
Expand Down Expand Up @@ -327,12 +333,12 @@ fn test_calculate_tx_gas_usage(
);

assert_eq!(
starknet_resources.to_gas_vector(versioned_constants, use_kzg_da),
tx_execution_info
.receipt
.resources
.starknet_resources
.to_gas_vector(versioned_constants, use_kzg_da)
starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, has_l2_gas),
tx_execution_info.receipt.resources.starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
has_l2_gas
)
);

// A tx that changes the account and some other balance in execute.
Expand Down Expand Up @@ -378,11 +384,11 @@ fn test_calculate_tx_gas_usage(
);

assert_eq!(
starknet_resources.to_gas_vector(versioned_constants, use_kzg_da),
tx_execution_info
.receipt
.resources
.starknet_resources
.to_gas_vector(versioned_constants, use_kzg_da)
starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, has_l2_gas),
tx_execution_info.receipt.resources.starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
has_l2_gas
)
);
}
9 changes: 6 additions & 3 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use starknet_types_core::felt::Felt;
use crate::abi::constants;
use crate::context::BlockContext;
use crate::execution::call_info::{CallExecution, CallInfo, OrderedEvent};
use crate::fee::actual_cost::test::include_l2_gas;
use crate::fee::eth_gas_constants;
use crate::fee::fee_utils::get_fee_by_gas_vector;
use crate::fee::gas_usage::{
Expand All @@ -27,10 +28,11 @@ fn versioned_constants() -> &'static VersionedConstants {
VersionedConstants::latest_constants()
}

#[rstest]
#[rstest_reuse::apply(include_l2_gas)]
fn test_get_event_gas_cost(
versioned_constants: &VersionedConstants,
#[values(false, true)] use_kzg_da: bool,
has_l2_gas: bool,
) {
let l2_resource_gas_costs = &versioned_constants.l2_resource_gas_costs;
let (event_key_factor, data_word_cost) =
Expand All @@ -41,7 +43,7 @@ fn test_get_event_gas_cost(
StarknetResources::new(0, 0, 0, StateChangesCount::default(), None, call_infos_iter);
assert_eq!(
GasVector::default(),
starknet_resources.to_gas_vector(versioned_constants, use_kzg_da)
starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, has_l2_gas)
);

let create_event = |keys_size: usize, data_size: usize| OrderedEvent {
Expand Down Expand Up @@ -75,13 +77,14 @@ fn test_get_event_gas_cost(
};
let call_infos = vec![call_info_1, call_info_2, call_info_3];
let call_infos_iter = call_infos.iter();
// TODO(Nimrod): Modify the expected gas vector l2-gas events cost is implemented.
let expected = GasVector::from_l1_gas(
// 8 keys and 11 data words overall.
(data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer(),
);
let starknet_resources =
StarknetResources::new(0, 0, 0, StateChangesCount::default(), None, call_infos_iter);
let gas_vector = starknet_resources.to_gas_vector(versioned_constants, use_kzg_da);
let gas_vector = starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, has_l2_gas);
assert_eq!(expected, gas_vector);
assert_ne!(GasVector::default(), gas_vector)
}
Expand Down
74 changes: 48 additions & 26 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,13 @@ impl StarknetResources {
&self,
versioned_constants: &VersionedConstants,
use_kzg_da: bool,
include_l2_gas: bool,
) -> GasVector {
self.get_calldata_and_signature_cost(versioned_constants)
+ self.get_code_cost(versioned_constants)
self.get_calldata_and_signature_cost(versioned_constants, include_l2_gas)
+ self.get_code_cost(versioned_constants, include_l2_gas)
+ self.get_state_changes_cost(use_kzg_da)
+ self.get_messages_cost()
+ self.get_events_cost(versioned_constants)
+ self.get_events_cost(versioned_constants, include_l2_gas)
}

// Returns the gas cost for transaction calldata and transaction signature. Each felt costs a
Expand All @@ -324,13 +325,18 @@ impl StarknetResources {
pub fn get_calldata_and_signature_cost(
&self,
versioned_constants: &VersionedConstants,
include_l2_gas: bool,
) -> GasVector {
// TODO(Avi, 20/2/2024): Calculate the number of bytes instead of the number of felts.
let total_data_size = u128_from_usize(self.calldata_length + self.signature_length);
let l1_gas = (versioned_constants.l2_resource_gas_costs.gas_per_data_felt
* total_data_size)
.to_integer();
GasVector::from_l1_gas(l1_gas)
if include_l2_gas {
todo!()
} else {
// TODO(Avi, 20/2/2024): Calculate the number of bytes instead of the number of felts.
let total_data_size = u128_from_usize(self.calldata_length + self.signature_length);
let l1_gas = (versioned_constants.l2_resource_gas_costs.gas_per_data_felt
* total_data_size)
.to_integer();
GasVector::from_l1_gas(l1_gas)
}
}

/// Returns an estimation of the gas usage for processing L1<>L2 messages on L1. Accounts for
Expand Down Expand Up @@ -379,12 +385,20 @@ impl StarknetResources {
}

/// Returns the gas cost of declared class codes.
pub fn get_code_cost(&self, versioned_constants: &VersionedConstants) -> GasVector {
GasVector::from_l1_gas(
(versioned_constants.l2_resource_gas_costs.gas_per_code_byte
* u128_from_usize(self.code_size))
.to_integer(),
)
pub fn get_code_cost(
&self,
versioned_constants: &VersionedConstants,
include_l2_gas: bool,
) -> GasVector {
if include_l2_gas {
todo!()
} else {
GasVector::from_l1_gas(
(versioned_constants.l2_resource_gas_costs.gas_per_code_byte
* u128_from_usize(self.code_size))
.to_integer(),
)
}
}

/// Returns the gas cost of the transaction's state changes.
Expand All @@ -394,15 +408,23 @@ impl StarknetResources {
}

/// Returns the gas cost of the transaction's emmited events.
pub fn get_events_cost(&self, versioned_constants: &VersionedConstants) -> GasVector {
let l2_resource_gas_costs = &versioned_constants.l2_resource_gas_costs;
let (event_key_factor, data_word_cost) =
(l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt);
let l1_gas: u128 = (data_word_cost
* (event_key_factor * self.total_event_keys + self.total_event_data_size))
.to_integer();

GasVector::from_l1_gas(l1_gas)
pub fn get_events_cost(
&self,
versioned_constants: &VersionedConstants,
include_l2_gas: bool,
) -> GasVector {
if include_l2_gas {
todo!()
} else {
let l2_resource_gas_costs = &versioned_constants.l2_resource_gas_costs;
let (event_key_factor, data_word_cost) =
(l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt);
let l1_gas: u128 = (data_word_cost
* (event_key_factor * self.total_event_keys + self.total_event_data_size))
.to_integer();

GasVector::from_l1_gas(l1_gas)
}
}

pub fn get_onchain_data_segment_length(&self) -> usize {
Expand Down Expand Up @@ -449,7 +471,7 @@ impl TransactionResources {
versioned_constants: &VersionedConstants,
use_kzg_da: bool,
) -> TransactionFeeResult<GasVector> {
Ok(self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da)
Ok(self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, false)
+ calculate_l1_gas_by_vm_usage(
versioned_constants,
&self.vm_resources,
Expand All @@ -464,7 +486,7 @@ impl TransactionResources {
with_reverted_steps: bool,
) -> ResourcesMapping {
let GasVector { l1_gas, l1_data_gas, .. } =
self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da);
self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, false);
let mut resources = self.vm_resources.to_resources_mapping();
resources.0.extend(HashMap::from([
(
Expand Down
10 changes: 5 additions & 5 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,11 +1899,11 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
};
assert_eq!(
expected_gas,
actual_execution_info
.receipt
.resources
.starknet_resources
.to_gas_vector(versioned_constants, use_kzg_da)
actual_execution_info.receipt.resources.starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
false
)
);

let total_gas = expected_tx_resources
Expand Down

0 comments on commit 855c361

Please sign in to comment.