-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(blockifier): update gas and vm resources computations #2153
feat(blockifier): update gas and vm resources computations #2153
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Artifacts upload triggered. View details here |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2153 +/- ##
===========================================
+ Coverage 40.10% 68.81% +28.71%
===========================================
Files 26 109 +83
Lines 1895 13890 +11995
Branches 1895 13890 +11995
===========================================
+ Hits 760 9559 +8799
- Misses 1100 3921 +2821
- Partials 35 410 +375 ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
690f44a
to
0ac452c
Compare
Artifacts upload triggered. View details here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 6 of 8 files at r1, 2 of 2 files at r2, all commit messages.
Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @TzahiTaub and @Yoni-Starkware)
crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs
line 0 at r2 (raw file):
these changes lgtm but can you add a reviewer from noa's team?
crates/blockifier/src/execution/entry_point_test.rs
line 517 at r2 (raw file):
#[test] fn test_old_cairo1_entry_point_segment_arena() { let test_contract = FeatureContract::CairoStepsTestContract;
no segment arena in cairo-native?
Code quote:
fn test_old_cairo1_entry_point_segment_arena() {
let test_contract = FeatureContract::CairoStepsTestContract;
crates/blockifier/src/execution/entry_point_execution_test.rs
line 107 at r2 (raw file):
let chain_info = &ChainInfo::create_for_testing(); let contracts = CompilerBasedVersion::iter().map(|version| (version.get_test_contract(), 1u16)); let mut state = test_state(chain_info, BALANCE, &contracts.collect::<Vec<_>>());
I think this is better - contracts
shouldn't be an iterator of tuples, + I don't think the collect
should need a type hint, test_state
argument type should be hint enough.
non-blocking
Suggestion:
let contracts = CompilerBasedVersion::iter().map(|version| version.get_test_contract());
let mut state = test_state(chain_info, BALANCE, &contracts.map(|contract| (contract, 1)).collect());
crates/blockifier/src/execution/entry_point_execution_test.rs
line 112 at r2 (raw file):
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1), CompilerBasedVersion::random_version_for_vm(), CompilerBasedVersion::random_version_for_vm(),
there are 4=O(1) combinations possible here; please explicitly parametrize instead of making a random test
Code quote:
CompilerBasedVersion::random_version_for_vm(),
CompilerBasedVersion::random_version_for_vm(),
crates/blockifier/src/execution/entry_point_execution_test.rs
line 116 at r2 (raw file):
let first_calldata = build_recurse_calldata(&call_versions); let second_calldata = build_recurse_calldata(&[CompilerBasedVersion::random_version_for_vm()]);
8 cases is still O(1) :)
Code quote:
ompilerBasedVersion::random_version_for_vm()
crates/blockifier/src/transaction/account_transactions_test.rs
line 183 at r2 (raw file):
24 ); }
what about the other case? can you verify it?
Suggestion:
match tx_execution_info.validate_call_info.unwrap().tracked_resource {
TrackedResource::CairoSteps => assert_eq!(
tx_execution_info.receipt.resources.computation.vm_resources.builtin_instance_counter
[&BuiltinName::range_check96],
24
),
TrackedResource::SierraGas => ???
}
crates/blockifier/src/transaction/account_transactions_test.rs
line 884 at r2 (raw file):
if result.validate_call_info.unwrap().tracked_resource == TrackedResource::SierraGas { return; }
blocking until a PR is open that does this TODO (otherwise we are losing test coverage here)
Code quote:
// TODO(Tzahi): adjust the steps in the test to gas for the SierraGas run (after a validate
// run gas limit is introduced to the code).
if result.validate_call_info.unwrap().tracked_resource == TrackedResource::SierraGas {
return;
}
crates/blockifier/src/execution/entry_point_execution.rs
line 444 at r2 (raw file):
let full_call_resources = &*syscall_handler.resources - &previous_resources; let charged_resources = ChargedResources { vm_resources: full_call_resources.filter_unused_builtins(),
if TrackedResource is SierraGas, why do you add these VM resources in the charged resources?
if the if
above is skipped, will full_call_resources
be empty?
if so, I prefer a to_vm_resources_for_fee
function with a similar API to to_gas_for_fee
, so it's clearer.
if not, what VM resources are you counting from this call?
Code quote:
vm_resources: full_call_resources.filter_unused_builtins(),
crates/blockifier/src/test_utils.rs
line 138 at r2 (raw file):
]; VERSIONS.iter() }
this is not forward-compatible, the FeatureContracts
enum uses EnumIter
for this.
PTAL here
Code quote:
/// Returns an iterator over all of the enum variants.
pub fn iter() -> Iter<'static, Self> {
static VERSIONS: [CompilerBasedVersion; 3] = [
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1,
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo1),
];
VERSIONS.iter()
}
crates/blockifier/src/test_utils.rs
line 142 at r2 (raw file):
/// Return a uniform random choice of Cairo0 version or old Cairo1 version. pub fn random_version_for_vm() -> CompilerBasedVersion { let is_cairo0: bool = rand::random();
please inject the random object. I think papyrus / committer crates have examples of random tests
Code quote:
let is_cairo0: bool = rand::random();
crates/blockifier/src/transaction/transactions_test.rs
line 255 at r2 (raw file):
charged_resources = ChargedResources::from_execution_resources(resources); } };
Suggestion:
let charged_resources = match tracked_resource {
TrackedResource::SierraGas => {
ChargedResources {
vm_resources: ExecutionResources::default(),
gas_for_fee: GasAmount(gas_consumed),
}
}
TrackedResource::CairoSteps => {
let n_range_checks = match cairo_version {
CairoVersion::Cairo0 => {
usize::from(entry_point_selector_name == constants::VALIDATE_ENTRY_POINT_NAME)
}
CairoVersion::Cairo1 => {
if entry_point_selector_name == constants::VALIDATE_ENTRY_POINT_NAME {
7
} else {
2
}
}
#[cfg(feature = "cairo_native")]
CairoVersion::Native => {
if entry_point_selector_name == constants::VALIDATE_ENTRY_POINT_NAME {
7
} else {
2
}
}
};
let n_steps = match (entry_point_selector_name, cairo_version) {
(constants::VALIDATE_DEPLOY_ENTRY_POINT_NAME, CairoVersion::Cairo0) => 13_usize,
(constants::VALIDATE_DEPLOY_ENTRY_POINT_NAME, CairoVersion::Cairo1) => 32_usize,
(constants::VALIDATE_DECLARE_ENTRY_POINT_NAME, CairoVersion::Cairo0) => 12_usize,
(constants::VALIDATE_DECLARE_ENTRY_POINT_NAME, CairoVersion::Cairo1) => 28_usize,
(constants::VALIDATE_ENTRY_POINT_NAME, CairoVersion::Cairo0) => 21_usize,
(constants::VALIDATE_ENTRY_POINT_NAME, CairoVersion::Cairo1) => 100_usize,
(selector, _) => panic!("Selector {selector} is not a known validate selector."),
};
let resources = ExecutionResources {
n_steps,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(
BuiltinName::range_check,
n_range_checks,
)]),
}
.filter_unused_builtins();
ChargedResources::from_execution_resources(resources)
}
};
crates/blockifier/src/transaction/transactions_test.rs
line 546 at r2 (raw file):
expected_inner_call_charged_resources.vm_resources.clone() } }
Suggestion:
let (expected_validate_gas_for_fee, expected_execute_gas_for_fee) = match tracked_resource {
TrackedResource::CairoSteps => {
// In CairoSteps mode, the initial gas is set to the default once before the validate
// call.
inner_call_initial_gas -=
expected_arguments.validate_gas_consumed + expected_arguments.execute_gas_consumed;
(GasAmount::default(), GasAmount::default())
}
TrackedResource::SierraGas => {
expected_arguments.resources =
expected_inner_call_charged_resources.vm_resources.clone();
(expected_arguments.validate_gas_consumed.into(), expected_arguments.execute_gas_consumed.into())
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 8 files at r1, all commit messages.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on @TzahiTaub)
crates/blockifier/src/execution/entry_point_execution.rs
line 371 at r2 (raw file):
/// Calculates the total gas for fee in the current call + subtree. fn to_gas_for_fee(
You should share this code with native
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on @dorimedini-starkware and @TzahiTaub)
crates/blockifier/src/execution/entry_point_execution.rs
line 444 at r2 (raw file):
Previously, dorimedini-starkware wrote…
if TrackedResource is SierraGas, why do you add these VM resources in the charged resources?
if theif
above is skipped, willfull_call_resources
be empty?
if so, I prefer ato_vm_resources_for_fee
function with a similar API toto_gas_for_fee
, so it's clearer.
if not, what VM resources are you counting from this call?
Your children can be cairoSteps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @dorimedini-starkware and @TzahiTaub)
crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs
line at r2 (raw file):
Previously, dorimedini-starkware wrote…
these changes lgtm but can you add a reviewer from noa's team?
The VM resources for native are irrelevant, and I think we can drop them for the VM as well since we can derive the gas cost from the VM resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @TzahiTaub)
crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs
line 176 at r2 (raw file):
}; let storage_entry_point_gas = GasAmount(if_native(&test_contract)(26990, 16990));
@Yoni-Starkware is this gas difference related to the 10k bug you talked about?
- same Q for the below
if_native
usage, but the other one does not have a 10k diff...
Code quote:
if_native(&test_contract)(26990, 16990)
0ac452c
to
50a48ff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 1 of 9 files reviewed, 11 unresolved discussions (waiting on @dorimedini-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)
crates/blockifier/src/test_utils.rs
line 138 at r2 (raw file):
Previously, dorimedini-starkware wrote…
this is not forward-compatible, the
FeatureContracts
enum usesEnumIter
for this.
PTAL here
Copying that seems like an overkill for something that simple that is not expected to change. How about a count assert (I remind you this is a test_util)? If we'll add more variants it will fail, and if we'll make a static variant into a dynamic we'll have to change this code in any case.
crates/blockifier/src/test_utils.rs
line 142 at r2 (raw file):
Previously, dorimedini-starkware wrote…
please inject the random object. I think papyrus / committer crates have examples of random tests
Removed as I don't need anymore
crates/blockifier/src/execution/entry_point_execution.rs
line 371 at r2 (raw file):
Previously, Yoni-Starkware (Yoni) wrote…
You should share this code with native
Done. Had to change this function due to your change as well.
crates/blockifier/src/execution/entry_point_execution_test.rs
line 107 at r2 (raw file):
Previously, dorimedini-starkware wrote…
I think this is better -
contracts
shouldn't be an iterator of tuples, + I don't think thecollect
should need a type hint,test_state
argument type should be hint enough.
non-blocking
Still needs the vec hint.
crates/blockifier/src/execution/entry_point_execution_test.rs
line 112 at r2 (raw file):
Previously, dorimedini-starkware wrote…
there are 4=O(1) combinations possible here; please explicitly parametrize instead of making a random test
Done.
crates/blockifier/src/execution/entry_point_execution_test.rs
line 116 at r2 (raw file):
Previously, dorimedini-starkware wrote…
8 cases is still O(1) :)
Done.
crates/blockifier/src/execution/entry_point_test.rs
line 517 at r2 (raw file):
Previously, dorimedini-starkware wrote…
no segment arena in cairo-native?
I don't know, but I changed the test not because of the builtin existence but because of the vm_resources verification in the end that is now 0. Don't know exactly what we want to test here, but for native it should be somehow counted in gas.
crates/blockifier/src/transaction/account_transactions_test.rs
line 183 at r2 (raw file):
Previously, dorimedini-starkware wrote…
what about the other case? can you verify it?
@ilyalesokhin-starkware said no, just that the run was successful.
crates/blockifier/src/execution/entry_point_execution_test.rs
line 119 at r3 (raw file):
CompilerBasedVersion::OldCairo1 )] second_branch_contract_version: CompilerBasedVersion,
Didn't find a way to make this value
macro work dynamically
Code quote:
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1
)]
third_contract_version: CompilerBasedVersion,
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1
)]
fourth_contract_version: CompilerBasedVersion,
#[values(
CompilerBasedVersion::CairoVersion(CairoVersion::Cairo0),
CompilerBasedVersion::OldCairo1
)]
second_branch_contract_version: CompilerBasedVersion,
crates/blockifier/src/transaction/transactions_test.rs
line 255 at r2 (raw file):
charged_resources = ChargedResources::from_execution_resources(resources); } };
:)
crates/blockifier/src/transaction/transactions_test.rs
line 546 at r2 (raw file):
expected_inner_call_charged_resources.vm_resources.clone() } }
:) :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: 1 of 9 files reviewed, 11 unresolved discussions (waiting on @dorimedini-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)
crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs
line 176 at r2 (raw file):
Previously, dorimedini-starkware wrote…
@Yoni-Starkware is this gas difference related to the 10k bug you talked about?
- same Q for the below
if_native
usage, but the other one does not have a 10k diff...
It seems the changes disappeared after a rebase.
b0fe421
to
4537e30
Compare
Artifacts upload triggered. View details here |
4537e30
to
0965952
Compare
Artifacts upload triggered. View details here |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 7 of 8 files at r3, 2 of 2 files at r4, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @TzahiTaub and @Yoni-Starkware)
crates/blockifier/src/execution/entry_point_execution.rs
line 364 at r3 (raw file):
/// Calculates the gas for fee in the current call. pub fn to_gas_for_fee(
can this be a method of CallInfo
?
all three parameters here seem to be part of this type
Code quote:
to_gas_for_fee(
crates/blockifier/src/execution/entry_point_execution.rs
line 379 at r3 (raw file):
TrackedResource::CairoSteps => 0, TrackedResource::SierraGas => gas_consumed .checked_sub(inner_calls.iter().map(|call| call.execution.gas_consumed).sum::<u64>())
ok, as I understand it, this function now does the following:
- If the tracked resource is CairoSteps, returns 0
- Otherwise: returns the gas consumed in this specific call, without the subtree
right?
so, this means that this value will not be the gas_consumed
of the call, it will be the gas_for_fee_charge
field?
please rewrite the documentation then... very confusing
Suggestion:
// The Sierra gas consumed in this specific call is `gas_consumed`
// (= total gas of self + subtree), minus the sum of all inner calls Sierra gas consumed.
// To compute the total Sierra gas to charge (of self, WITHOUT the subtree), if the tracked resource is
// Sierra gas, we subtract the subtrees consumed gas from the total.
// If the tracked resource is Cairo steps, this specific call incurs no Sierra gas cost for fee.
GasAmount(match tracked_resource {
TrackedResource::CairoSteps => 0,
TrackedResource::SierraGas => gas_consumed
.checked_sub(inner_calls.iter().map(|call| call.execution.gas_consumed).sum::<u64>())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @dorimedini-starkware and @Yoni-Starkware)
crates/blockifier/src/execution/entry_point_execution.rs
line 364 at r3 (raw file):
Previously, dorimedini-starkware wrote…
can this be a method of
CallInfo
?
all three parameters here seem to be part of this type
CallInfo needs the gas_for_fee in its initialization. We can make a create function for CallInfo to have this calculation inside - do you prefer that?
crates/blockifier/src/execution/entry_point_execution.rs
line 379 at r3 (raw file):
Previously, dorimedini-starkware wrote…
ok, as I understand it, this function now does the following:
- If the tracked resource is CairoSteps, returns 0
- Otherwise: returns the gas consumed in this specific call, without the subtree
right?
so, this means that this value will not be thegas_consumed
of the call, it will be thegas_for_fee_charge
field?please rewrite the documentation then... very confusing
This was more relevant in the old computations. After the change Yoni made, I think the current function is pretty self-explanatory. Changed the name to match the vm_resource var where this function is called.
0965952
to
a47b6d2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 2 of 2 files at r5, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TzahiTaub and @Yoni-Starkware)
crates/blockifier/src/execution/entry_point_execution.rs
line 364 at r3 (raw file):
Previously, TzahiTaub (Tzahi) wrote…
CallInfo needs the gas_for_fee in its initialization. We can make a create function for CallInfo to have this calculation inside - do you prefer that?
yes but in a separate PR... not need to continue refactoring this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 1 of 2 files at r5, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TzahiTaub)
crates/blockifier/src/execution/native/entry_point_execution.rs
line 90 at r5 (raw file):
let charged_resources_without_inner_calls = ChargedResources { vm_resources: ExecutionResources::default(), gas_for_fee: to_gas_for_fee(
Fix
Code quote:
to_gas_for_fee
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @TzahiTaub)
crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs
line 172 at r5 (raw file):
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 7)]), }; }
Cool, no VM resources for VM with new Cairo 1
Code quote:
if cairo_version == CairoVersion::Cairo1 {
first_storage_entry_point_resources.vm_resources = ExecutionResources {
n_steps: 244,
n_memory_holes: 0,
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 7)]),
};
}
a47b6d2
to
533098a
Compare
Previously, Yoni-Starkware (Yoni) wrote…
Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 7 of 7 files at r6, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TzahiTaub and @Yoni-Starkware)
533098a
to
56c2563
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 3 files at r7, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TzahiTaub and @Yoni-Starkware)
31ea81c
to
c85acf2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dismissed @Yoni-Starkware from a discussion.
Reviewable status: 8 of 15 files reviewed, 1 unresolved discussion (waiting on @dorimedini-starkware and @TzahiTaub)
crates/blockifier/src/transaction/account_transactions_test.rs
line 884 at r2 (raw file):
Previously, dorimedini-starkware wrote…
blocking until a PR is open that does this TODO (otherwise we are losing test coverage here)
The execute_max_sierra_gas
(and validate) is not used ATM in the code, so I'm keeping the todo and working on it. Please unblock so I won't get another huge rebase :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 7 of 7 files at r8, all commit messages.
Reviewable status: complete! all files reviewed, all discussions resolved (waiting on @TzahiTaub)
c85acf2
to
dcded19
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 4 of 4 files at r9, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TzahiTaub)
crates/blockifier/src/transaction/transactions_test.rs
line 556 at r9 (raw file):
let expected_execute_call = CallEntryPoint { entry_point_selector: selector_from_name(constants::EXECUTE_ENTRY_POINT_NAME), initial_gas: if account_cairo_version == CairoVersion::Cairo0 {
Suggestion:
match
crates/blockifier/src/transaction/transactions_test.rs
line 591 at r9 (raw file):
caller_address: sender_address, call_type: CallType::Call, initial_gas: versioned_constants.inifite_gas_for_vm_mode(),
oops
is this a typo here? or in the function name?
Code quote:
inifite
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @TzahiTaub)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @aner-starkware)
crates/blockifier/src/transaction/transactions_test.rs
line 591 at r9 (raw file):
Previously, dorimedini-starkware wrote…
oops
is this a typo here? or in the function name?
The function name, already told @aner-starkware about it.
dcded19
to
43545d9
Compare
Previously, TzahiTaub (Tzahi) wrote…
https://reviewable.io/reviews/starkware-libs/sequencer/2704#- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 4 files at r9, 1 of 1 files at r10, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @aner-starkware)
No description provided.